The Wallet.fromMnemonic
function has a second argument to specify the BIP-32 derivation path. By default it will use m/44'/60'/0'/0/0
, but if you want to get the second account, you can use m/44'/60'/0'/0/1
for example:
const { Wallet } = require('ethers');
const wallet = Wallet.fromMnemonic('one two three four ...', `m/44'/60'/0'/0/1`);
Alternatively, you can use the HDNode
class to quickly derive child keys from a mnemonic phrase. For example:
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`);
The HDNode
can also be used to get an instance of Wallet
. This is also what Wallet.fromMnemonic
does.
const wallet = new Wallet(hdNode);