10

I want to convert a 24 words length seed phrase into a private key using web3.

1 Answer 1

25

Here are two options:

Using ethers.js - the example below uses a mnemonic ethers wallet documentation

const ethers = require('ethers');
let mnemonic = "YOUR MNEMONIC";
let mnemonicWallet = ethers.Wallet.fromMnemonic(mnemonic);
console.log(mnemonicWallet.privateKey);

This doesn't seem to be included in web3, but has been added to the list of enhancements for 2.0. There are some options described in this thread.

Update Mar 31, 2022

As of [email protected], the api now uses fromPhrase instead of fromMnemonic:

const ethers = require("ethers");
const mnemonic = "YOUR MNEMONIC";
const mnemonicWallet = ethers.Wallet.fromPhrase(mnemonic);
console.log(mnemonicWallet.privateKey);
7
  • Code works but I'm not getting the expected result. FYI I'm using a MyEtherWallet recovery phrase as mnemonic.
    – viarnes
    Jan 24, 2020 at 21:56
  • 1
    Updated answer and tested. This seems to be working. I created a MyEthersWallet, used this code with the mnemonic from mew. Then I used the logged private key to add the account in metamask and I got the same address. Sorry for the initial answer.
    – Steven V
    Jan 24, 2020 at 23:48
  • 1
    this is great! was able to convert SafePal mnemonic and old MEW keystore into private keys enabling mirroring of accounts in Metamask and SafePal! needed an extra lib called 'keythereum' and everything worked smooth!
    – 4UmNinja
    Mar 29, 2021 at 8:30
  • 1
    As of [email protected] the api is now Wallet.fromPhrase(mnemonic)
    – 0xPingo
    Mar 31, 2023 at 4:03
  • 1
    How about with Foundry instead of Ethers? I there a way? Jun 17, 2023 at 0:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.