11

I know the basics of blockchain and how to use geth. However, I'm curious to know how to build a wallet using nodejs. What I understand is that you can create a account using geth console (private network) to work on and when pass command personal.listWallets then you will this :Output of the personal.listWallets in geth console

What is HD wallet and how to create one using node.js?

2 Answers 2

6

You can also use the highly convenient HDWalletProvider library from the Truffle team

const HDWalletProvider = require("truffle-hdwallet-provider");
const Web3 = require('web3');

var mnemonic = "opinion destroy betray ..."; // 12 word mnemonic
var provider = new HDWalletProvider(mnemonic, "http://localhost:8545");

const web3 = new Web3(provider);

HD wallets can be convenient, since you can generate an infinite number of deterministic accounts, but never lose these 12 words or you will lose all access to use your signatures and ether

2
  • There is also a caveat around word selection. You should not pick arbitrary words, but words from a word bag that assures randomization. If I find a link, I'll post it here. Dec 17, 2018 at 23:55
  • Here's the BIP39 reference standard. You'll need a PRNG to generate the 128-256 bits of entropy, but I'd suggest not rolling your own if possible. github.com/bitcoin/bips/blob/master/… Jul 10, 2021 at 19:05
17

HD Wallets refer to Hierarchical Deterministic wallets. They are wallets that, from a root, can generate multiple "child" private keys in a deterministic way. You only need to remember the "path" of the child key.

For example hardware wallets use them - from a single root you can generate separate keys for Bitcoin (with path m/44'/0'/0'/0) and Ethereum (path m/44'/60'/0'/0).

A JavaScript library that support HD wallets ethereumjs-wallet.

var hdkey = require("ethereumjs-wallet/hdkey")
var bip39 = require("bip39");

// You can generate a mnemonic seed with bip39 library
var mnemonic = "seed sock milk ...";
var path = "m/44'/60'/0'/0/0";

var hdwallet = hdkey.fromMasterSeed(bip39.mnemonicToSeed(mnemonic));
var wallet = hdwallet.derivePath(path).getWallet();
var address = "0x" + wallet.getAddress().toString("hex");

console.log(address);
4
  • Is there any documentation of that? Like how to add more accounts, get private key/password of account, recover wallet from seed etc? Jan 2, 2018 at 5:59
  • @PrashantPrabhakarSingh Ethereumjs-wallet has a decent documentation and has unit test for hdkey wallets but otherwise the source code is good as documentation.
    – Ismael
    Jan 2, 2018 at 16:50
  • @Ismael how could we add this with Geth. Jan 18, 2018 at 20:44
  • @ShivamNema Once you have created the wallet you can export the private key and import it in geth. But it is better if you say what are you trying to accomplish in the end, ie why do you want to use a HD wallet? They are used when you need a large number of addresses, and geth has no support for the special properties of hd wallets.
    – Ismael
    Jan 20, 2018 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.