我是新使用ethers.js的
const { Wallet } = require('ethers');
const wallet = Wallet.fromMnemonic('one two three four ...');我想用这段代码从那个Mnemonic恢复所有帐户。也许这是我不太清楚的东西,但它只是检索第一个帐户的Mnemonic。
我该如何收回所有的帐户?
发布于 2020-06-29 09:45:25
Wallet.fromMnemonic函数有第二个参数来指定BIP-32派生路径。默认情况下,它将使用m/44'/60'/0'/0/0,但如果您想获得第二个帐户,则可以使用m/44'/60'/0'/0/1 (例如:
const { Wallet } = require('ethers');
const wallet = Wallet.fromMnemonic('one two three four ...', `m/44'/60'/0'/0/1`);或者,您可以使用HDNode类从助记符短语中快速派生子密钥。例如:
const { utils } = require('ethers');
const hdNode = utils.HDNode.fromMnemonic('one two three four ...');
const secondAccount = hdNode.derivePath(`m/44'/60'/0'/0/1`); // This returns a new HDNode
const thirdAccount = hdNode.derivePath(`m/44'/60'/0'/0/2`);HDNode也可以用来获取Wallet的一个实例。这也是Wallet.fromMnemonic所做的。
const wallet = new Wallet(hdNode);发布于 2023-04-26 12:33:34
在较新的以太版本中,这一点略有改变。
const { ethers } = require('ethers');
const mnemonic = "one two three four .....";
const hdNode = ethers.HDNodeWallet.fromPhrase(mnemonic);
const secondAccount = hdNode.derivePath(`m/44'/60'/0'/0/1`);
const thirdAccount = hdNode.derivePath(`m/44'/60'/0'/0/2`);
console.log(secondAccount.address);
console.log(thirdAccount.address);https://ethereum.stackexchange.com/questions/84615
复制相似问题