How to restore Wallets using Mnemonic Phrases in ethersjs with compatibility to MyCrypto?

Viewed 115

I'm curious how I can be compatible with MyCrypto in terms of Recovery Phrases using ethers.js.

Lets say, I have the following Mnemonic:

federal train gather tumble service amount address need tail crunch better baby

which leads to a public address: 0xd4E22d1ffe328d8b3F834fa5791f50C16E453a0d

Code:

const derivationPath = "m/44'/60'/0'/0";
var hdNode = ethers.utils.HDNode.fromMnemonic(mnemonic);
var childNode = hdNode.derivePath(derivationPath + '/' + index);

var w = new ethers.Wallet(childNode.privateKey);
console.log(w.address);

Importing this passphrase into MyCrypto will also return the same address, but NOT anymore once I have added a password.

Let's import this wallet into MyCrypto like this:

Mnemonic Phrase: federal train gather tumble service amount address need tail crunch better baby Password: "test"

The first generated address will be 0x3454Bd153214d900253812b4D47787eF23cD76e6 and not that one mentioned above any more. Only when I remove the password and leave it empty, the same wallet-address will be generated.

How can we produce the same thing with ethersjs?

Adding a 13th word to the mnemonics is will give error message "invalid mnemonic".

enter image description hereenter image description here

1 Answers

The Ethereum has legacy and modern BIP39 derivation paths. Unfortunately wallets sometimes use incompatible derivation paths.

Here are some common paths:

  • Ethereum — m’/44’/60’/0’/0

  • Ethereum Classic — m’/44’/61’/0’/0

  • Ethereum Testnet (Ropsten) — m’/44’/1’/0’/0

  • Ethereum (Ledger) — m’/44’/60’/0’

  • Ethereum Classic (Ledger) — m’/44’/60’/160720’/0

  • Ethereum Classic (Ledger, Vintage MEW) — m’/44’/60’/160720’/0’

  • Ethereum (Ledger Live) — m’/44’/60’

  • Ethereum Classic (Ledger Live) — m’/44’/61’

  • Ethereum (KeepKey) — m’/44’/60’

  • Ethereum Classic (KeepKey) — m’/44’/61’

See the source blog post for derivation paths.. For the authoritative source, please read the wallet source code and pick the derivation path from there.

Related