9

I'm trying to create an ethereum account using web3-js by providing a mnemonic + passphrase (created with BIP39-js) but can't seem to find an implementation of web3.eth.accounts.create() that accepts both the mnemonic and the passphrase.

Is it not supported or am I missing something on how to implement it?

4 Answers 4

9

Do it with ethers:

const ethers = require('ethers');
const mnemonic = await ethers.HDNode.entropyToMnemonic(ethers.utils.randomBytes(16));
const wallet = ethers.Wallet.fromMnemonic(mnemonic);

P.S.

Here is a community you can find ask more questions about it:

2
  • 1
    In the meantime it is: ethers.utils.HDNode.entropyToMnemonic
    – heinob
    Commented Feb 17, 2019 at 5:41
  • OP mentioned mnemonic + passphrase: there's no passphrase in there. how is this the accepted answer ?
    – Syffys
    Commented Jan 12, 2022 at 13:49
6

Just in case someone comes here later, in the recent version of ethers, HDNODE has been moved to the utils method. So, in modification to @George's answer above;

const ethers = require('ethers');
const mnemonic = await ethers.utils.HDNode.entropyToMnemonic(ethers.utils.randomBytes(16));
const wallet = ethers.Wallet.fromMnemonic(mnemonic);
2

You can use ethers Lib: https://www.npmjs.com/package/ethers

import { ethers } from "ethers";

newWallet = async () => {
    let password = prompt("Password");

    if (password) {
      var randomSeed = ethers.Wallet.createRandom();

      console.log(randomSeed.mnemonic);
      console.log(randomSeed.address);

      function callback(progress) {
        console.log("Encrypting: " + parseInt(progress * 100) + "% complete");
      }

      let encryptPromise = randomSeed.encrypt(password, callback);

      encryptPromise.then(function (json) {
        console.log(json);
      });
    }
  };
0

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

The code below goes through the generation of all accounts of a mnemonic

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

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);

  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: Retrieving the private/ public key with web3js for a specific wallet provided by truffle/hdwallet-provider

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.