DEV Community

loading...

Ethereum Wallet Sample code

dongri profile image D ・1 min read
var Wallet = require('ethereumjs-wallet');
let hdkey = require('ethereumjs-wallet/hdkey');
var EthUtil = require('ethereumjs-util');
let bip39 = require("bip39");

// input secret
const privateKeyBuffer = EthUtil.toBuffer('0xb3daa3c32f6f6c71a3fc0c3af081e971c78706459f358b43451a1ba0d4ef3dba');
const wallet = Wallet.fromPrivateKey(privateKeyBuffer);

// public key from secret key
const publicKey = wallet.getPublicKeyString();                                                                                                                                                                                                                                                               
console.log('public key: ' + publicKey);

// address from secret key
const address = wallet.getAddressString()
console.log('addres   : ' + address);

const mnemonic = "soldier clump basket brain suit wire whisper equip aim neck kangaroo rely";

// hd wallet from mnemonic
let hdwallet = hdkey.fromMasterSeed(bip39.mnemonicToSeedSync(mnemonic));

// master key
console.log('master private key: ' + hdwallet.privateExtendedKey())
let wallet_hdpath = "m/44'/60'/0'/0/";

// generate wallets
for (let i = 0; i < 5; i++) {
  let wallet = hdwallet.derivePath(wallet_hdpath + i).getWallet();
  let address = wallet.getAddressString();
  console.log('address-' + i + ': ' + address);
}
public key: 0x12dd7df75e3a2b2b2d17358221f0811a6a19251c66cf502a6ca02a23205f3d27b7c416eca8e9e7ae45e47862ff7bf0ccc013f27a81e4fc9a0d513808db28d90b
addres   : 0x72a60325bbcff70e6278f03482b7ae44d175503b
master private key: xprv9s21ZrQH143K3oPizmavifFSRXSdVFTxVn3zdWqa3X2AqAidQnef4sxPVLsFM9PtZcASh14fr3e1Fwac7qdvKyqQgdg8tCKTDQ4LLgkvLmh
address-0: 0x72a60325bbcff70e6278f03482b7ae44d175503b
address-1: 0x2bd38b66259092248e9a1943acd63dffda479010
address-2: 0x48a0806dc7c143f4e31a68dd5590cb57b1b89cda
address-3: 0x54072de9b7c7d29dbf189fb0ed4670d53975a6a6
address-4: 0xf6b82083948be7ce8e36699a00041ddc1be2354d

Discussion (1)

pic
Editor guide
Collapse
skomra profile image
Aaron Skomra

TypeError: hdkey.fromMasterSeed is not a function