Understanding HD(Hierarchical deterministic) wallet

djibrilm

djibril mugisho

Posted on August 15, 2024

Understanding HD(Hierarchical deterministic) wallet

An HD (Hierarchical deterministic) wallet is a type of wallet that generates child keys from a single seed, which is a binary representation of the mnemonic phrase, the mnemonic phrase, a mechanism introduced by the Bitcoin improvement proposal also known as BIP-39 or BIP-0039 is the single source of truth in all HD wallets, whoever gains access to your mnemonic phrase gain access to all your funds. 

so how does this scary yet important mnemonic phrase get created and how does it result in private and public keys? 

Some clarifications first 😅, I won’t cover the cryptographic aspects that are involved in the creation of mnemonic phrases but will leave some important materials that cover all the cryptographic details of the process here is a link to a full guide on how things work deeply under the hood, back to our subject, the first process consists of generating the entropy a sequence of random bits that form the foundation for generating cryptographic keys, it’s from that entropy that the mnemonic phrase is derived. 

After creating the mnemonic phrase the second process consists of deriving the seed for that mnemonic phrase. 

To demonstrate this process I will use the BIP-39 library for javascript in a node.js environment, so make sure to initialize your project with npm init -y and install all the following dependencies bip39, you can either run your project with node your-file.js or specify a start command in your package.json

 

const bip39 = require("bip39");

const createMnemonic = async () => {
  const mnemonic = bip39.generateMnemonic();

  return {
    mnemonic, //ex: "simple courage strong..."
    words: mnemonic.split(" "), // Returns an array of all the words in an array
  };
};
Enter fullscreen mode Exit fullscreen mode

As I mentioned at the beginning we do need to derive the seed from the given mnemonic phrase, so here is how we can derive the seed phrase.

const generateSeed = (mnemonic) => {
  const seed = bip39.generateSeed(mnemonic);// generates the binary format of the seed;
  return seed; //The seed is useaully returned in bynary so may use something like seed.toString('hex') to make it more readabale
}
Enter fullscreen mode Exit fullscreen mode

Master private key and key derivation path 

The master private key is the root of an HD wallet's hierarchical structure. It's a 256-bit number derived from the seed using a cryptographic hash function (typically HMAC-SHA512). This key is crucial as it generates all subsequent child keys within the wallet.

For us to generate unique private and public keys we need some sort of path or in simple words some unique ID for each chain, which brings us to key derivation, a key derivation path is a road map that follows the BIP-32 standards used to find the location of private and public key in the master private key based on the given path. And that’s where the word deterministic comes from, with the exact mnemonic and the exact derivation path you get the same public and private keys, and things will be like that forever😅 since a deterministic system means with the provided input we get the same output forever 

You may be curious about how to derive those child keys from the master private key, here is an example with the HDK library for javascript and please make sure to run this in a node.js environment since this library relies on the node.js crypto module. 

Please note that this is not the only way to derive child keys, there are several approaches you can use, with this one because it is straightforward and doesn't abstract the process of deriving the derivation path.

Install the dependency 

npm i hdkey
Enter fullscreen mode Exit fullscreen mode
const createWallet = async (seed) => {
  const derivationPath = "m/44'/60'/0'/0"; 
  var hdkey = HDKey.fromMasterSeed(Buffer.from(seed, "hex")); //Convert the seed in its binary format if passed as string otherwise leave it as it is.
  var childkey = hdkey.derive(derivationPath);// key derivation path for Ethereum

  //You can use tools like Ether.js to derive the user's address from the private key.
  console.log(childkey.privateExtendedKey);  
  console.log(childkey.publicExtendedKey); 
};

Enter fullscreen mode Exit fullscreen mode

Benefits of HD(Hierarchical deterministic)

HD wallets offer a lot of flexibility and a good user experience, with HD wallets you only need to remember your mnemonic phrase, and since it consists of human-readable words instead of some crazy hash string, backing the wallet becomes a lot simpler, however that comes with some security implications, being the single source of truth keeping it in insecure location can lead to loss of funds and probably a big financial disaster to the owners.

I hope you had fun reading, thanks for passing by don’t forget to share your thoughts in the comment and if you find this resource helpful don’t hesitate to share it with others, peace 😇😇

💖 💪 🙅 🚩
djibrilm
djibril mugisho

Posted on August 15, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related