Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Galti opened this issue Apr 28, 2018 · 8 comments
Closed

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

Galti opened this issue Apr 28, 2018 · 8 comments
Assignees
Labels
2.x 2.0 related issues Feature Request Stale Has not received enough activity
Projects

Comments

@Galti
Copy link

Galti commented Apr 28, 2018

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.

@Galti Galti changed the title Restore private key from seed Restore private key from it's seed or mnemonic phrase Apr 28, 2018
@pau1m
Copy link

pau1m commented May 13, 2018

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 nivida self-assigned this Aug 9, 2018
@nivida
Copy link
Contributor

nivida commented Aug 9, 2018

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

@nivida nivida added this to To do in 1.0 Nov 28, 2018
@nivida nivida added the 2.x 2.0 related issues label Jun 20, 2019
@mickeymond
Copy link

mickeymond commented Sep 10, 2019

@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
Copy link

@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
Copy link

github-actions bot commented Jul 4, 2020

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

@github-actions github-actions bot added the Stale Has not received enough activity label Jul 4, 2020
@joticajulian
Copy link

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
Copy link

Is there a browser solution for this?

@italoHonoratoSA
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.x 2.0 related issues Feature Request Stale Has not received enough activity
Projects
No open projects
1.0
  
Backlog
Development

No branches or pull requests

8 participants