ethers.js与帐户管理 / 学习智能合约#44

in hive-180932 •  3 years ago 

ethers.js是与web3.js一样是用于与以太坊节点交互的js库。这两者功能相近,相对来说,ethers.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实现了以太坊帐户管理中几个主流的方法,比如:随机生成私钥以生成帐号,用助记词生成帐号, 以及反向的方法等。总体是简单易用,上手方便,是个不错的工具。

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!