Created
September 11, 2019 12:51
-
-
Save Meshugah/2cc13b94e6b9478ef2138b8d372f9d6b to your computer and use it in GitHub Desktop.
Sample HD wallet for Ethereum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
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
yes, it should be.