This article is not intended for beginners in the crypto space. It is an in-depth analysis on the structure and usage of an HD Wallet for developers.

Hierarchical Deterministic Wallets (or HD wallets for short) were introduced by BIP 32 and later improved by BIP 44. BIPs (if you’re not familiar with them) stand for Bitcoin Improvement Proposals. While HD wallets were introduced by the Bitcoin community, it is a wallet structure that supports many coins. HD wallets can allow for an entire suite of crypto-wallets to be generated from a single seed phrase, although not a commonly used feature.

<aside> 📘 HD Keys 本质上是一种面向非对称密钥的 KDF(key derivation function)。它可以从一个 root private key 为根节点,通过添加一些 key material 生成新的 child key。

$$ childKey = KDF(rootKey, material) $$

KDF 是一个确定性(deterministic)的函数,也就是相同的输出,始终能得出一样的结果。

用户只需要妥善保管根密钥,key materials(一般是随机生成的) 可以公开。在需要使用 child key 的场合,根据 key materials 可以随时派生出子密钥来。

根密钥和派生的字密钥可以构成一棵树状结构,key materials 有时候也被称为 derivation path,可以通过前缀词来体现树状关系(如 /root, /root/a, /root/b)。

</aside>

But what is an HD wallet? Put simply, an HD wallet is a public/private key tree all starting from a root node (master node). Here’s a great image for visualization:

Credit: BIP 32

Credit: BIP 32

An HD wallet tree is represented by derivation paths to the first address node. For example the default for Ethereum is m/44'/60'/0'/0. Each number in that path represents a certain level in the tree above.

m / purpose' / coin_type' / account' / chain / address_index

Path m/44'/60'/0'/0 deconstruction

For the most standard list of coin type codes look at this satoshilabs repo

Technically speaking, Hierarchical Deterministic Wallets are a tree structure where each node has an extended private and public key. Any node can have any number of children. Meaning a master key could regenerate 10 accounts in 10 different currencies each with a very high number of addresses. I chose 10 as an arbitrary number, the number could be as large as you’d like.

For example, a tree that holds the wallet keys for two networks (we can say Bitcoin and Blackcoin) will have 2 children under the ‘purpose’ node which we’ll keep at 44. Such a tree might look like this:

It is important to note that the master private key’s sole purpose is wallet tree regeneration. It is unable to sign transactions. The HD wallet is designed to be able to generate many public/private key pairs from a single seed or mnemonic.

Tree level Breakdown

You can hold multiple unique currencies within the same HD Wallet by utilizing many coinType nodes instead of just one. The coinType codes are available in this satoshilabs repo.

The account node is used to hold multiple wallets for the same coin in a single master wallet. The account node would hold all the children addresses that belonged to that ‘account’. This is useful for situations where you may want to store money like you would in a bank, with a savings account or checking account or perhaps an account for your child or spouse and etc and etc.