0

I have a HD private key which I have derived from my mnemonic using the ethers library. I would now like to derive child addresses and corresponding private keys from it.

Does anybody know how I can do this?

This is how I have obtained the HD private key:

const xKey = ethers.Wallet.fromMnemonic(...)

Thanks in advance!

wild_nothing
  • 129
  • 1
  • 7

2 Answers2

2
const derived = xKey.derivePath("m/44'/60'/0'/0/0");

You should use the derivePath function of the master key, specifying the path of the derived keys.

See more bip-32 and hdnode.

Arnaudov_St
  • 137
  • 1
  • 7
  • FYI to others, this is the typical way we generate derived keys. We can simply increase the index to get more keys. "m/44'/60'/0'/0/n" See https://github.com/ethereum/EIPs/issues/84 for more info. – valem May 26 '20 at 23:16
1

ethers.Wallet.fromMnemonic is used to create the master private key out of they mnemonic, but for creating new pairs of public and private keys I think you should use this method with different passphrase for each pair:

const HDNode  = await ethers.utils.HDNode.fromMnemonic(YOUR_MNEMONIC, null, YOUR_PASSPHRASE);

When you check the new generated HDNode inside of it you can see it contains the same mnenomic you used for the master private key.

Miroslav Nedelchev
  • 3,519
  • 2
  • 10
  • 12