ethers.js是与web3.js一样是用于与以太坊节点交互的js库。这两者功能相近,相对来说,ethers.js更轻量级、更易用些。这次也上手体验了下,用它来创建帐户、管理帐户这些。 [手册](https://learnblockchain.cn/docs/ethers.js/) ```js cnpm install ethers --save //"ethers": "^5.4.3" const ethers = require('ethers'); // 创建钱包账号 //随机数生成钱包地址 let privateKey = ethers.utils.randomBytes(32) console.log(111, privateKey) console.log(112, Buffer.from(privateKey).toString('hex')) //转成16进制 let wallet = new ethers.Wallet(privateKey) console.log("账号地址: " + wallet.address, wallet) //生成随机助记词创建钱包 let mnemonic = ethers.utils.entropyToMnemonic(ethers.utils.randomBytes(16)) var path = "m/44'/60'/0'/0/0"; // 通过助记词创建钱包 let wallet = ethers.Wallet.fromMnemonic(mnemonic, path) console.log("账号地址: " + wallet.address, wallet) //创建随机地址 let wallet = ethers.Wallet.createRandom() console.log(111, wallet) //获取助记词 let mnemonic = wallet.mnemonic.phrase console.log("钱包助记词:",mnemonic) //获取path let path = wallet.mnemonic.path console.log("钱包path:",path) //获取钱包的私钥 let privateKey = wallet.privateKey console.log("钱包私钥:",privateKey) //获取钱包地址 let address = wallet.address console.log(116, address) /*输出 111 Wallet { _isSigner: true, _signingKey: [Function], address: '0x037567fd7A3e0EFdD62Dd36F76Eca325313Cf1c1', _mnemonic: [Function], provider: null } 钱包助记词: inquiry force clean xxxxxxxxxxxxx 钱包path: m/44'/60'/0'/0/0 钱包私钥: 0x0cfa7bd16f73xxXXXXXXXXXXXXXXXX 116 0x037567fd7A3e0EFdD62Dd36F76Eca325313Cf1c1 */ //根据助记词找回钱包信息 let monic= "peace mouse scrap chase order guess xxxxxxxxxxx" let mnemonic = ethers.Wallet.fromMnemonic(monic) let privateKey = mnemonic.privateKey console.log("钱包私钥:",privateKey) //根据私钥找回钱包地址 let wallet = new ethers.Wallet(privateKey) //钱包地址 let address = wallet.address console.log(156, address) //json文件找回钱包信息 const fs = require('fs') let readFile = function(path){ return new Promise(resolve => { fs.readFile(path, (error, data) => { resolve(data) }) }) } let main = async function (){ let res = await readFile('./UTC--2016-04-1xxxx') let password = "pwd" let wallet = await ethers.Wallet.fromEncryptedJson(res, password) console.log("Address: " + wallet.address, wallet, wallet.privateKey) } main() //生成json钱包文件 const ethers = require('ethers') const fs = require('fs') let privateKey = ethers.utils.randomBytes(32) //随机数 let wallet = new ethers.Wallet(privateKey) let d = new Date() let time = d.getTime() let writeFile = function(path, data){ return new Promise(resolve => { fs.writeFile(path, data, error => { if (error) { console.log('写入失败') } else { resolve(data) console.log('写入成功了') } }) }) } let main = async function (){ let password = "pwd" let res = await wallet.encrypt(password) let path = './keystore/' + time + '-' + wallet.address await writeFile(path, res) } main() ``` 用ethers.js实现了以太坊帐户管理中几个主流的方法,比如:随机生成私钥以生成帐号,用助记词生成帐号, 以及反向的方法等。总体是简单易用,上手方便,是个不错的工具。
author | lemooljiang |
---|---|
permlink | ethers-js-44 |
category | hive-105017 |
json_metadata | {"tags":["cn","blockchain","ethers","web3","account"],"links":["https://learnblockchain.cn/docs/ethers.js/"],"app":"hiveblog/0.1","format":"markdown","description":"用ethers.js实现了以太坊帐户管理中几个主流的方法","author":"lemooljiang"} |
created | 2021-08-05 11:27:00 |
last_update | 2021-08-05 11:27:00 |
depth | 0 |
children | 0 |
last_payout | 2021-08-12 11:27:00 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 10.770 HBD |
curator_payout_value | 10.758 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 3,009 |
author_reputation | 438,608,506,193,732 |
root_title | "ethers.js与帐户管理 / 学习智能合约#44" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 105,337,352 |
net_rshares | 31,247,389,944,209 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
abit | 0 | 11,573,168,899,370 | 50% | ||
mangou007 | 0 | 196,978,833 | 7.37% | ||
arcange | 0 | 111,265,702,788 | 2% | ||
raphaelle | 0 | 1,586,691,398 | 2% | ||
lemooljiang | 0 | 16,380,690,034 | 100% | ||
ace108 | 0 | 1,002,956,153,541 | 25% | ||
laoyao | 0 | 25,553,033,434 | 50% | ||
midnightoil | 0 | 91,070,198,892 | 50% | ||
xiaohui | 0 | 8,113,116,097 | 50% | ||
oflyhigh | 0 | 2,519,127,194,565 | 50% | ||
djennyfloro | 0 | 1,371,361,302 | 10% | ||
bert0 | 0 | 660,652,287 | 7.37% | ||
rivalhw | 0 | 1,366,851,144,923 | 100% | ||
helene | 0 | 682,427,648,022 | 50% | ||
ethansteem | 0 | 56,464,239,571 | 50% | ||
sweetsssj | 0 | 11,011,500,399,099 | 33% | ||
justyy | 0 | 434,261,020,680 | 100% | ||
dapeng | 0 | 197,827,579,022 | 100% | ||
sqube | 0 | 317,585,542 | 1% | ||
walterjay | 0 | 4,485,279,911 | 0.35% | ||
azazqwe | 0 | 11,397,183,567 | 100% | ||
blackbunny | 0 | 39,548,900,591 | 50% | ||
bxt | 0 | 163,414,478,328 | 50% | ||
lingfei | 0 | 48,070,913,685 | 50% | ||
laodr | 0 | 3,625,127,465 | 100% | ||
passion-fruit | 0 | 13,006,747,072 | 100% | ||
htliao | 0 | 4,987,918,178 | 35% | ||
fortune-master | 0 | 12,692,449,877 | 100% | ||
exec | 0 | 139,113,344,376 | 50% | ||
alphacore | 0 | 57,832,171,145 | 2.01% | ||
aaronli | 0 | 85,330,740,921 | 33% | ||
catwomanteresa | 0 | 246,008,390,127 | 50% | ||
liangfengyouren | 0 | 3,438,687,371 | 50% | ||
idx | 0 | 11,684,837,751 | 50% | ||
aafeng | 0 | 445,239,135,788 | 50% | ||
cn-reader | 0 | 14,878,617,910 | 50% | ||
tvb | 0 | 41,011,582,941 | 50% | ||
karja | 0 | 15,783,845,969 | 10% | ||
floatinglin | 0 | 7,692,553,960 | 100% | ||
kimzwarch | 0 | 11,946,451,666 | 4% | ||
yellowbird | 0 | 2,204,572,616 | 100% | ||
dancingapple | 0 | 12,132,311,723 | 50% | ||
blc | 0 | 4,018,541,503 | 100% | ||
fermionico | 0 | 134,381,380,236 | 25% | ||
kymio | 0 | 523,414,373 | 3.75% | ||
sbi2 | 0 | 34,181,013,515 | 1.37% | ||
tresor | 0 | 9,059,642,274 | 7.37% | ||
archisteem | 0 | 1,252,592,784 | 7.5% | ||
smartvote | 0 | 55,258,257,465 | 2.4% | ||
anttn | 0 | 7,591,904,236 | 30% | ||
julian2013 | 0 | 104,799,923,825 | 50% | ||
pboulet | 0 | 842,187,433 | 0.7% | ||
pet.society | 0 | 15,146,439,943 | 6% | ||
memeteca | 0 | 968,850,419 | 7.37% | ||
wherein | 0 | 61,195,914,354 | 50% | ||
zerofive | 0 | 549,711,968 | 25% | ||
tagalong | 0 | 713,628,086 | 100% | ||
cnstm | 0 | 91,895,004,241 | 50% | ||
ctime | 0 | 108,642,388,078 | 2.5% | ||
mia-cc | 0 | 12,901,645,435 | 100% | ||
fengchao | 0 | 6,277,058,672 | 2% | ||
laruche | 0 | 32,159,887,328 | 0.7% | ||
bnk | 0 | 9,979,509,535 | 7.37% | ||
hive-143869 | 0 | 1,182,737,559 | 0.7% | ||
portraits | 0 | 0 | 25% | ||
blogstats | 0 | 573,426,562 | 100% | ||
senseiphil | 0 | 1,318,126,798 | 1% | ||
philipmak | 0 | 1,471,103,805 | 50% | ||
emma-emma | 0 | 18,382,381,271 | 100% | ||
livegaming | 0 | 144,464,893 | 10% | ||
shenpozhi | 0 | 8,825,332,035 | 100% | ||
everlandd | 0 | 26,526,943,245 | 100% |