一、什么是ethers.js
ethers.js库旨在成为一个完整而紧凑的库,用于与以太坊区块链及其生态系统进行交互。它最初设计用于 ethers.io,后来扩展为更通用的库。官方链接:https://docs.ethers.io/ethers.js/html/getting-started.html
二、在 Node.js 中安装
(npm 安装) npm install -save ethers
(yarn 安装) yarn add ethers
三、创建 wallet.js 文件 生成钱包信息
//引入<a href="http://jinwei.fun/mdzztag/ethers-js" title="查看更多关于 ethers.js 的文章" target="_blank">ethers.js</a>
var ethers = require('ethers');
//拿到生成的<a href="http://jinwei.fun/mdzztag/%e9%92%b1%e5%8c%85" title="查看更多关于钱包的文章" target="_blank">钱包</a>信息 var wallet = ethers.Wallet.createRandom();
//获取助记词
var mnemonic = wallet.mnemonic;
console.log("<a href="http://jinwei.fun/mdzztag/%e9%92%b1%e5%8c%85" title="查看更多关于钱包的文章" target="_blank">钱包</a>助记词:",mnemonic)
//获取 path
var path = wallet.path;
console.log("<a href="http://jinwei.fun/mdzztag/%e9%92%b1%e5%8c%85" title="查看更多关于钱包的文章" target="_blank">钱包</a>path:",path)
//获取<a href="http://jinwei.fun/mdzztag/%e9%92%b1%e5%8c%85" title="查看更多关于钱包的文章" target="_blank">钱包</a>的私钥
var privateKey = wallet.privateKey;
console.log("<a href="http://jinwei.fun/mdzztag/%e9%92%b1%e5%8c%85" title="查看更多关于钱包的文章" target="_blank">钱包</a>私钥:",privateKey)
//获取钱包地址
var address = wallet.address;
console.log("钱包地址:",address)
在 node 中执行 node wallet.js 可以看到钱包的助记词、私钥、地址已经生成成功。
四、根据私钥找回钱包信息
//引入<a href="http://jinwei.fun/mdzztag/ethers-js" title="查看更多关于 ethers.js 的文章" target="_blank">ethers.js</a>
var ethers = require('ethers');
//根据助记词找回钱包信息
var monic= "peace mouse scrap chase order guess volume unit riot save reopen nation"
var mnemonic = ethers.Wallet.fromMnemonic(monic);
var privateKey = mnemonic.privateKey;
console.log("钱包私钥:",privateKey)
//根据私钥找回钱包地址 var wallet = new ethers.Wallet(privateKey);
//钱包地址
var address = wallet.address;
console.log("钱包地址:",address)
在 node 中执行 node wallet.js 可以看到钱包的私钥、地址已经成功恢复。
