My purpose is to get private keys of accounts in hardhat generated from mnemonic specified. Nevertheless, countless answers suggest it is impossible to get private keys of those accounts in hardhat.
That being said, I have a script, which accepts a mnemonic, creates an HDWallet from it and then derives other HDWallets, which should have the same addresses hardhat generates from that same mnemonic. Although they does not.
Am I missing something? Why does HDWallets generate different addresses from the same mnemonic than hardhat does?
Here is the script:
async function main() {
const mnemonic = process.env.MNEMONIC!;
console.log('mnemonic:', mnemonic);
const HDNode = ethers.utils.HDNode.fromMnemonic(mnemonic);
for (let i = 0; i < 20; i++) {
const derivedNode = HDNode.derivePath(`m/44'/60'/0'/${i}`);
console.log(`child ${i} address:`, derivedNode.address);
}
}
As it turns out, I specified derive path incorrectly.
Instead of
const derivedNode = HDNode.derivePath(`m/44'/60'/0'/${i}`);
I should have used
const derivedNode = HDNode.derivePath(`m/44'/60'/0'/0/${i}`);