*

HD Wallet

A HD Wallet, or hierarchical deterministic wallet, is a wallet that generates all of its keys and addresses from a single source.

It is a practice of BIP-32 , BIP-39 , and BIP-44 standards. And it is widely used in the blockchain industry.

From Seed to Extended Key

HMAC-SHA512 the seed will generate a 64-byte value. Called the master extended key. The first 32 bytes are the private key, and the last 32 bytes are the chain code.

The Seed

In BIP-32 standard, which is the standard for HD wallets, it defines a way to derive a master key from a seed. The seed is between 128 and 512 bits.

In BIP-39 standard, which is the standard for mnemonic, it defines a way to convert a mnemonic to a seed. The seed is always 512 bits.

BIP-39 Mnemonic
abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about
BIP-39 PBKDF2
Seed
5EB00BBDDCF069084889A8AB9155568165F5C453CCB85E70811AAED6F6DA5FC19A5AC40B389CD370D086206DEC8AA6C43DAEA6690F20AD3D8D48B2D2CE9E38E4
HMAC-SHA512
Master Extended Key
1837C1BE8E2995EC11CDA2B066151BE2CFB48ADF9E47B151D46ADAB3A21CDF677923408DADD3C7B56EED15567707AE5E5DCA089DE972E07F3B860450E2A3B70E
Private Key
1837C1BE8E2995EC11CDA2B066151BE2CFB48ADF9E47B151D46ADAB3A21CDF67
Chain Code
7923408DADD3C7B56EED15567707AE5E5DCA089DE972E07F3B860450E2A3B70E

Branch and Tree

Every extended key has the ability to derive its own child keys. It's like a branch of a tree. And the master extended key is like the root of the tree.

You shared the an extended key with others, you shared the branch of the tree. You shared the master extended key with others, you shared the whole tree (HD Wallet) with them.

Share the Extended Key

The extended key, as a branch of the tree, can be import/export independently to/from a wallet. The serialized format of the extended key is as same as the WIF format of a private key. By hashing the extended key with SHA256 twice, and taking the first 4 bytes as a checksum, finally base58 encode the whole string. It is defined in the standard, but it is not widely used in practice. So we will not cover it here. You can find more information in the BIP-32 standard.

The WIF format is only a serialization. And the derivation process has nothing to do with it, we will focus on the extended key itself.

Chain Code

Why do we need a chain code? The chain code is used to prevent child keys from depending solely on the key itself. Otherwise if the key is leaked, all it's child keys will be leaked. With the chain code added, the child keys are still secure. Because the chain code comes from the key's parent extended key.

From Extended Key to Child Key

With the extended key, we can derive child keys.

A derivation is just one more HMAC-SHA512 operation with the data of the extended key.

To derive multiple child keys, we can add an index to the data of the extended key.

Index

By standard, the index is a 32-bit number. So with it we can derive 232 child keys.

Master Extended Key
Index
HMAC-SHA512
Child Exdened Key
Child Private Key
Child Chain Code

Same as the master extended key, the child extended key is also 64-byte. The first 32 bytes are the private key, and the last 32 bytes are the chain code.

With the 32-bit index, we can derive 232 child keys. And every single child key can derive another 232 child keys. The depth of the tree can be infinite. So the number of child keys is theoretically infinite.

The word Hierarchical in HD wallet also comes from this feature. It means that the keys can be organized in a tree structure.

If the index is less than 231, the child key is called a normal derivation.

Otherwise it is called a hardened derivation.

Hardened Derivation

The index can be hardened by adding a constant 231 to it.

The normal derivation uses public key derivation, and the hardened derivation uses private key derivation. The hardened derivation is more secure because it does not leak the private key.

Wallet Structure

BIP-44 defines a wallet structure that is organized in a tree structure. Checkout this interactive chart below to see how it works.

m/44'/0'/0'/0/0
Seed
5EB00BBDDCF069084889A8AB9155568165F5C453CCB85E70811AAED6F6DA5FC19A5AC40B389CD370D086206DEC8AA6C43DAEA6690F20AD3D8D48B2D2CE9E38E4
HMAC-SHA512
1837C1BE8E2995EC11CDA2B066151BE2CFB48ADF9E47B151D46ADAB3A21CDF677923408DADD3C7B56EED15567707AE5E5DCA089DE972E07F3B860450E2A3B70E
Private Key
1837C1BE8E2995EC11CDA2B066151BE2CFB48ADF9E47B151D46ADAB3A21CDF67
Chain Code
7923408DADD3C7B56EED15567707AE5E5DCA089DE972E07F3B860450E2A3B70E
Parent Key
1837C1BE8E2995EC11CDA2B066151BE2CFB48ADF9E47B151D46ADAB3A21CDF67
Parent Chain Code
7923408DADD3C7B56EED15567707AE5E5DCA089DE972E07F3B860450E2A3B70E
Index
8000002C
HMAC-SHA512
B50E42E12A00B9E0ABC8D9A1CED0F570252AFCF07ED746E16D9D3A12DCC7D88345D3B0E8206DB10A08D555317C7E245C5BBD12254CE968F3C79A959D4E6AF98A
Child Tweak
B50E42E12A00B9E0ABC8D9A1CED0F570252AFCF07ED746E16D9D3A12DCC7D883
Child Private Key
CD46049FB82A4FCCBD967C5234E61152F4DF87D01D1EF833420814C67EE4B7EA
Chain Code
45D3B0E8206DB10A08D555317C7E245C5BBD12254CE968F3C79A959D4E6AF98A
Parent Key
CD46049FB82A4FCCBD967C5234E61152F4DF87D01D1EF833420814C67EE4B7EA
Parent Chain Code
45D3B0E8206DB10A08D555317C7E245C5BBD12254CE968F3C79A959D4E6AF98A
Index
80000000
HMAC-SHA512
DA201B04DFCC2AB09C817647B280248D333901BF4AC4700AAA75F70DBA60D14DAF0894DC5F2D5BED0DC85B2FD2053A98575765C144E4E64126EE1009B38860B2
Child Tweak
DA201B04DFCC2AB09C817647B280248D333901BF4AC4700AAA75F70DBA60D14D
Child Private Key
A7661FA497F67A7D5A17F299E76635E16D69ACA8B89AC8022CABAD47690F47F6
Chain Code
AF0894DC5F2D5BED0DC85B2FD2053A98575765C144E4E64126EE1009B38860B2
Parent Key
A7661FA497F67A7D5A17F299E76635E16D69ACA8B89AC8022CABAD47690F47F6
Parent Chain Code
AF0894DC5F2D5BED0DC85B2FD2053A98575765C144E4E64126EE1009B38860B2
Index
80000000
HMAC-SHA512
56FE8FDDC364FDD7F21BAFF13BA229DF158CE50B0E78041B21BB33EBC06E60843DA4BC190A2680111D31FADFDC905F2A7F6CE77C6F109919116F253D43445219
Child Tweak
56FE8FDDC364FDD7F21BAFF13BA229DF158CE50B0E78041B21BB33EBC06E6084
Child Private Key
FE64AF825B5B78554C33A28B23085FC082F691B3C712CC1D4E66E133297DA87A
Chain Code
3DA4BC190A2680111D31FADFDC905F2A7F6CE77C6F109919116F253D43445219
Parent Key
FE64AF825B5B78554C33A28B23085FC082F691B3C712CC1D4E66E133297DA87A
Parent Chain Code
3DA4BC190A2680111D31FADFDC905F2A7F6CE77C6F109919116F253D43445219
Index
00000000
HMAC-SHA512
8558F645527606A46F8E4DA86E88E025044CC5D47480085478DDCBDB033C877EBCE80DD580792CD18AF542790E56AA813178DC28644BB5F03DBD44C85F2D2E7A
Child Tweak
8558F645527606A46F8E4DA86E88E025044CC5D47480085478DDCBDB033C877E
Child Private Key
83BDA5C7ADD17EF9BBC1F03391913FE6CC947AA18C4A343607724E815C83EEB7
Chain Code
BCE80DD580792CD18AF542790E56AA813178DC28644BB5F03DBD44C85F2D2E7A
Parent Key
83BDA5C7ADD17EF9BBC1F03391913FE6CC947AA18C4A343607724E815C83EEB7
Parent Chain Code
BCE80DD580792CD18AF542790E56AA813178DC28644BB5F03DBD44C85F2D2E7A
Index
00000000
HMAC-SHA512
5EC66CD512C0A67FE973CBC140127270A67492873CBF87D9E601675EC5A8D4BB213909708058E0EC4A99C19D8E041C014AE6C7DC21D2A1FAC86772DF7CA357A6
Child Tweak
5EC66CD512C0A67FE973CBC140127270A67492873CBF87D9E601675EC5A8D4BB
Child Private Key
E284129CC0922579A535BBF4D1A3B25773090D28C909BC0FED73B5E0222CC372
Chain Code
213909708058E0EC4A99C19D8E041C014AE6C7DC21D2A1FAC86772DF7CA357A6