Skip to content

Instantly share code, notes, and snippets.

@Meshugah
Created September 11, 2019 12:51
Show Gist options
  • Save Meshugah/2cc13b94e6b9478ef2138b8d372f9d6b to your computer and use it in GitHub Desktop.
Save Meshugah/2cc13b94e6b9478ef2138b8d372f9d6b to your computer and use it in GitHub Desktop.
Sample HD wallet for Ethereum
const bip39 = require("bip39"); // mnemonic generator
const hdkey = require("hdkey"); // wallet lib
const ethUtil = require("ethereumjs-util"); // Used here for public address and public key generation
const mnemonic = bip39.generateMnemonic(); // generates string
const seed = bip39.mnemonicToSeedSync(mnemonic).toString('hex'); // creates seed for wallet
const root = hdkey.fromMasterSeed(seed);
const masterPrivateKey = root.privateKey.toString('hex');
// 1st Address
var addrNode = root.derive("m/44'/60'/0'/0/0"); // "m / purpose' / coin_type' / account' / change / address_index"
var pubKey = ethUtil.privateToPublic(addrNode._privateKey);
var addr = ethUtil.publicToAddress(pubKey).toString('hex');
var address = ethUtil.toChecksumAddress(addr);
// 2nd Address
var addrNode = root.derive("m/44'/60'/0'/0/1"); // "m / purpose' / coin_type' / account' / change / address_index"
var pubKey = ethUtil.privateToPublic(addrNode._privateKey);
var addr = ethUtil.publicToAddress(pubKey).toString('hex');
var address = ethUtil.toChecksumAddress(addr);
@Khanisic
Copy link

But I can't seem to link the seed phrase with the private key here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment