8

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?

pmcoelho
  • 93
  • 1
  • 1
  • 6

4 Answers4

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:

5

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);
SirPhemmiey
  • 161
  • 1
  • 1
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);
      });
    }
  };
Etienne
  • 301
  • 2
  • 6
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

Ether Man
  • 56
  • 8