Introduction
Since the inception of the blockchain (starting with bitcoin) in the early 2000s its fundamental functionality has predominantly been the transfer of economic value. Although other L1 blockchains have been developed, the fundamental use case has not changed much. Lately, the major focus of blockchain projects has been to find ways and build DApps that will encourage mass adoption of the technology by the larger population. One of the key challenges to this agender (as captured in the document Blockchain Enigma. Paradox. Opportunity issued by Deloitte UK) is awareness and understanding of blockchain technology. Although there has been a lot of hype on blockchain and cryptocurrency, a lot of people find a hard time onboarding due to the high learning curve of the technology even for the simplest things like creating a wallet.
Cryptocurrency Wallets
The cryptocurrency market is flooded with tons of wallets which offer/promise various packages and benefits to their users. Cryptocurrency wallets are categorized into various categories based on varying perspectives, use cases, and even environments. However, all these types of wallets generate accounts using a set of similar processes.
Non-deterministic
Wallets that use this approach generate a new (random) private key for every account.
Deterministic
Wallets that use this approach generate a master private key(called seed) that is used to generate the private keys for subsequent accounts.
Whiles both approaches generate accounts, deterministic wallets provide a good balance between privacy and convenience. Ideally, it is good practice to use a new key (account) for every transaction on Ethereum. But this can be inconvenient if you have to manage and backup multiple keys as in the case of non-deterministic wallets. With deterministic wallets, you back up only the master key and you can use that to recover all associated keys.
In this article, I will focus on one of the processes that have become a mainstay in the crypto world.
Hierarchical Deterministic (HD) Wallet
HD wallets come from the class of wallets called deterministic wallets. The main advantage of HD wallets over simple deterministic wallets is that HD wallets use a tree-like structure to generate accounts. In HD wallets a key of one account (parent) can be used to derive the key of a subsequent account (child) creating a parent-child hierarchy.
This makes it super easy to back up and recover all accounts from a single seed (or mnemonic) phrase. Also, this type of wallet is excellent for managed/shared wallets, where one person can generate receive only accounts or subaccounts without having the master key. This means that access to all the other accounts will be safe and obscured from the user.
Generating HD wallets involves a number of predefined steps that are defined by standards in the Bitcoin Improvement Protocol (BIP) - specifically BIP 32, BIP 39, and BIP 44.
BIP 32 and BIP 44
BIP 32 defines the standard for generating child key pairs from a master node using a 4-part nomenclature that specifies different properties of the key pair. The key pair is represented as m/a/O/k
where:
m
- specifies the master nodea
- defines the address derived from the master nodeO
- defines whether the address is for an internal or external keychain by setting it to 1 or 0 respectively.k
- defines the index of the key pair generated.
BIP 44 is an improvement of BIP 32 and it provides a 5-part path for defining key pairs which is defined as m/derivation_type/coin_type/category/internal_external/key_index
.
m
- defines the master nodederviation_type
- defines which method is used to derive the key pair. This is always set to 44 to indicate the BIP 44 standard.coint_type
- indicates the type of asset the account stores. This is a constant that represents various cryptocurrencies (like 0 for bitcoin and 60 for Ether) that are registered with the community.category
- defines a custom category for the account. This is entirely user-defined and can represent categories such as savings, expenses, etc.internal_external
- indicates whether it is an internal address (when set to 1) or an external address (when set to 0).key_index
- defines the index of the key pair.
BIP 39
This specification defines the standard for generating random 12-word or 24-word mnemonic phrases which are used to derive a 128-bit or 256-bit random seed. The 24-word mnemonic phrase is considered more secure.
Generate an HD wallet
- The first step is to generate a 12 or 24 mnemonic (or seed) phrase using the BIP 39 standard
- Generate 128 or 256 bits seed from the seed phrase.
- Generate the master node and public-private key pair from the bits seed phrase.
- Generate child key pairs from the master node using BIP32 and BIP44.