web3 / web3.js

Collection of comprehensive TypeScript libraries for Interaction with the Ethereum JSON RPC API and utility functions.
https://web3js.org/
Other
19.25k stars 4.93k forks source link

Restore private key from it's seed or mnemonic phrase #1594

Closed Galti closed 4 years ago

Galti commented 6 years ago

I investigated web3.js and, as I understood, the lib doesn't provide method for restoring a private key from it's seed or mnemonic phrase (e.g bip39 mnemonic/seed).

Is there another method to restore account from 12-word-phrase?

Example restoring method is web3j MnemonicUtills.java https://github.com/web3j/web3j/blob/master/crypto/src/main/java/org/web3j/crypto/MnemonicUtils.java

Thanks in advance for answering.

pau1m commented 6 years ago

You could try something like

function generateAddressesFromSeed(seed, count) {
    let bip39 = require("bip39");
    let hdkey = require('ethereumjs-wallet/hdkey');
    let hdwallet = hdkey.fromMasterSeed(bip39.mnemonicToSeed(seed));
    let wallet_hdpath = "m/44'/60'/0'/0/";

    let accounts = [];
    for (let i = 0; i < 10; i++) {

        let wallet = hdwallet.derivePath(wallet_hdpath + i).getWallet();
        let address = '0x' + wallet.getAddress().toString("hex");
        let privateKey = wallet.getPrivateKey().toString("hex");
        accounts.push({address: address, privateKey: privateKey});
    }

    return accounts;
}
nivida commented 6 years ago

I'll add the label "Feature Request" because I would like to support this.

mickeymond commented 5 years ago

@pau1m Your code did not work for me on the go I had to tweak it into something like this,

function generateAddressesFromSeed(mnemonic, count) { let bip39 = require("bip39"); let hdkey = require("ethereumjs-wallet/hdkey"); let seed = bip39.mnemonicToSeedSync(mnemonic); let hdwallet = hdkey.fromMasterSeed(seed); let wallet_hdpath = "m/44'/60'/0'/0/";

let accounts = []; for (let i = 0; i < count; i++) { let wallet = hdwallet.derivePath(wallet_hdpath + i).getWallet(); let address = "0x" + wallet.getAddress().toString("hex"); let privateKey = wallet.getPrivateKey().toString("hex"); accounts.push({ address: address, privateKey: privateKey }); }

return accounts; }

MarcelBlockchain commented 4 years ago

@pau1m Your code did not work for me on the go I had to tweak it into something like this,

function generateAddressesFromSeed(mnemonic, count) { let bip39 = require("bip39"); let hdkey = require("ethereumjs-wallet/hdkey"); let seed = bip39.mnemonicToSeedSync(mnemonic); let hdwallet = hdkey.fromMasterSeed(seed); let wallet_hdpath = "m/44'/60'/0'/0/";

let accounts = []; for (let i = 0; i < count; i++) { let wallet = hdwallet.derivePath(wallet_hdpath + i).getWallet(); let address = "0x" + wallet.getAddress().toString("hex"); let privateKey = wallet.getPrivateKey().toString("hex"); accounts.push({ address: address, privateKey: privateKey }); }

return accounts; }

Thanks for your reply. It helped me. 2 little things I needed to change to make it work on my machine: appending .toString('hex'): bip39.mnemonicToSeedSync(mnemonic).toString('hex');

remove the last / let wallet_hdpath = "m/44'/60'/0'/0";

github-actions[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions

joticajulian commented 3 years ago

The require("ethereumjs-wallet/hdkey"); didn't worked for me, I had to change it to const { hdkey } = require('ethereumjs-wallet');

Here is the result

const bip39 = require("bip39");
const { hdkey } = require('ethereumjs-wallet');

function generateAddressesFromSeed(mnemonic, count) {  
  let seed = bip39.mnemonicToSeedSync(mnemonic);
  let hdwallet = hdkey.fromMasterSeed(seed);
  let wallet_hdpath = "m/44'/60'/0'/0/";

  let accounts = [];
  for (let i = 0; i < count; i++) {
    let wallet = hdwallet.derivePath(wallet_hdpath + i).getWallet();
    let address = "0x" + wallet.getAddress().toString("hex");
    let privateKey = wallet.getPrivateKey().toString("hex");
    accounts.push({ address: address, privateKey: privateKey });
  }
  return accounts;
}
const mnemonic = "word1 word2 ..."
const count = 5;
const result = generateAddressesFromSeed(mnemonic, count);
console.log(result);
qudo-code commented 3 years ago

Is there a browser solution for this?

italoHonoratoSA commented 1 year ago

Hello friend, the web3.js doesn't have this tool installed natively. We use the HD Wallet Provider for this.

Check: https://www.npmjs.com/package/@truffle/hdwallet-provider

You solve this in Web3 as follows:

Install:

npm install @truffle/hdwallet-provider

Don't forget to install web3.js and truffle

I created the code below that will generate multiple accounts from Mnemonico in your web3.js. The code will also give you the account balance.

const HDWalletProvider = require("@truffle/hdwallet-provider");
const Web3 = require("web3");
const mnemonicPhrase = "mnemonicPhrase here"; 
const urlRPC = "https://bsc-dataseed.binance.org"; 

//Creating another instance to query balances
const Web3Instance = require('web3');
const web3instance = new Web3Instance(new Web3Instance.providers.HttpProvider(urlRPC));

//HD Wallet Provider loads by default only 10 accounts
//We change the limit to as many accounts as we want
//You cannot run the loop below for i greater than 10, because for
const limit = 50;

async function getAccount() {

  provider = new HDWalletProvider({
    mnemonic: mnemonicPhrase,
    numberOfAddresses: limit,
    providerOrUrl: urlRPC,
    addressIndex: 0,

    });

  //HDWalletProvider is compatible with Web3
  //Use it at Web3 constructor, just like any other Web3 Provider
  const web3 = new Web3(provider);

// I realized that iterating a loop to limit always gives an error when it reaches limit
//This happens in this HD Wallet Provider package
//limit - 1 seems to work best
  for (let i = 0; i < limit - 1; i ++) {

    var get = await web3.eth.getAccounts()
    var address = get[i];
    var balanceETH = await web3instance.eth.getBalance(address)

    //Checking the adress
    console.log(address);
    console.log(balanceETH);

  }

}

getAccount();

You can check about retrieving the private key here: https://ethereum.stackexchange.com/questions/102090/retrieving-the-private-public-key-with-web3js-for-a-specific-wallet-provided-by