3

I have this code which generates a single wallet:

const ethers = require('ethers')
const wallet = ethers.Wallet.createRandom()console.log('address:', wallet.address)
console.log('mnemonic:', wallet.mnemonic.phrase)
console.log('privateKey:', wallet.privateKey)

What I need, however, is thousands of wallets and private keys to each one, generated out of this mnemonic. How to do it?

1 Answer 1

5

To generate multiple wallets from a mnemonic, you can use the HDNode class from the ethers library. Here is an example of how you could generate multiple wallets from a mnemonic:

const ethers = require('ethers')

// Create a random mnemonic to use as the seed for the HDNode
const mnemonic = ethers.utils.HDNode.entropyToMnemonic(ethers.utils.randomBytes(16))

// Use the mnemonic to create an HDNode instance
const node = ethers.utils.HDNode.fromMnemonic(mnemonic)

// Generate multiple wallets from the HDNode instance
const wallets = []
for (let i = 0; i < 1000; i++) {
  const path = "m/44'/60'/0'/0/" + i
  const wallet = node.derivePath(path).getWallet()
  wallets.push(wallet)
}

In this example, wallets is an array of 1000 Wallet instances, each with a different address and private key. You can access the address, mnemonic, and private key for each wallet using the address, mnemonic, and privateKey properties, respectively.

// Print the address, mnemonic, and private key for each wallet
for (const wallet of wallets) {
  console.log('address:', wallet.address)
  console.log('mnemonic:', wallet.mnemonic.phrase)
  console.log('privateKey:', wallet.privateKey)
}

Recovering wallet from mnemonic:

const ethers = require('ethers')

// The mnemonic phrase for the wallet
const mnemonic = 'candy maple cake sugar pudding cream honey rich smooth crumble sweet treat'

// The path of the wallet
const path = "m/44'/60'/0'/0/0"

// Create a root HDNode from the mnemonic phrase
const root = ethers.HDNode.fromMnemonic(mnemonic)

// Derive the wallet from the root node using the given path
const wallet = root.derivePath(path)

// Print the address and private key of the recreated wallet
console.log('address:', wallet.address)
console.log('privateKey:', wallet.privateKey)
7
  • What's the maximum that the counter i could be? Is there any limit?
    – Kum
    Commented Dec 3, 2022 at 0:38
  • If later I lose a private key of a some address, but keep the mnemonic, will it be possibly to deduce a private key of an address?
    – Kum
    Commented Dec 3, 2022 at 0:39
  • There is no inherent limit to the number of wallets that you can generate in this way. However, keep in mind that the more wallets you generate, the more memory and computational resources will be required to generate and store them. Updated my post to show you how you would recover a PK from a mnemonic
    – VX3
    Commented Dec 3, 2022 at 13:06
  • the more wallets you generate, the more memory and computational resources will be required to generate and store them --> Or do you mean "linearly more"? Otherwise, why will it require more of resources to store them, given that each key occupies the same space. The more of them generated, the more space get occupied, but it's the case for almost everything in life.
    – Kum
    Commented Dec 3, 2022 at 21:13
  • Correct - as in you could probably generate 1 billion keys but there will be a computing + storage cost as with anything :)
    – VX3
    Commented Dec 4, 2022 at 0:01

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.