Skip to content

Instantly share code, notes, and snippets.

@alexanderattar
Forked from pedrouid/wallet.js
Last active December 17, 2021 23:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexanderattar/fb9b19be9d918ac963d74e42e5d42632 to your computer and use it in GitHub Desktop.
Save alexanderattar/fb9b19be9d918ac963d74e42e5d42632 to your computer and use it in GitHub Desktop.
Example with ethers.js (Ethereum Wallet)
const ethers = require('ethers')
const standardPath = "m/44'/60'/0'/0";
const activeIndex = 0;
function generatePath(index) {
const path = `${standardPath}/${index}`;
return path;
}
function generateMnemonic() {
const entropy = ethers.utils.randomBytes(16);
const mnemonic = ethers.utils.entropyToMnemonic(entropy);
return mnemonic;
}
function createWallet() {
const mnemonic = generateMnemonic();
const path = generatePath(activeIndex);
const account = ethers.Wallet.fromMnemonic(mnemonic, path);
return account;
}
function createWallets(numWallets) {
const accounts = [];
const mnemonic = generateMnemonic();
for (let i = activeIndex; i < numWallets; i++) {
const path = generatePath(i);
accounts.push(ethers.Wallet.fromMnemonic(mnemonic, path));
}
return accounts;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment