Bitcoin Learning Series
How does the Bitcoin Seed Phrase Work?
The seed phrase is the key to the bitcoin wallet.
Bitcoin doesn’t need any recognition today. It is the first decentralised project that has a 21 Million supply. It was started in 2009. The name behind bitcoin is Satoshi Nakamoto.
“Banks must be trusted to hold our money and transfer it electronically, but they lend it out in waves of credit bubbles with barely a fraction in reserve.” — Satoshi Nakamoto.
Hello Friends, When I heard the concept of bitcoin first time, I was curious to learn what was behind the reels, mostly about the seed phrase. Here I am sharing my experience so come join me and get ready to eat with a silver spoon.
* Indexes *
- What is Seed Phrase?
- How to generate Seed Phrase?
- What is the PBKDF2 function?
- How to validate Mnemonic?
- What are Master Extended Keys?
What is Seed Phrase?
- When we create a wallet in dApp then we get the random words in a series form that is called the seed phrase and also known as a mnemonic phrase.
- We can create infinite public/private key pairs using a single seed phrase. We don’t need to remember all the private keys because a wallet can regenerate the key pairs again and we can restore them.
- BIP-39 (Bitcoin Improvement Proposal) has a list of 2048 words. these words are used to create the combination of seed phrases.
- There are many combinations of seed phrases like 9 words, 12 words, 15-words… 24 words. Today most wallets use 24-word mnemonic because it uses 264-bit protection and prevents from hacking. see the below image the about seed phrase.
How to generate Seed Phrase?
- Encoding a random number into words and these words will use to create the seed. We will understand this process in three steps.
Step 1: Generate Entropy
- Entropy is a large random number that has a 0 and 1 value (Small Unit). It should be between 128 to 256 bits because the other person cannot generate the same.
- The entropy number must multiply by 32 bits because it can be split into chunks and converted to a word later.
Make sure that the source of creating a random number is secure. Don’t use any other site or use any other library for this.
Step 2: Entropy to Mnemonic
- We have to validate our entropy So we add a checksum (SHA256 hash of entropy) into it that makes the entropy unique.
- Now we shift 1 bit of every 32 bits of hash to the end.
-- take this 1 bit to end001101010110111011000110010010010111001001011001001010110001011...1101010110111011000110010010010111001001011001001010110001011...00
- We split them into a group of 11 bits and convert them into Decimal to get the corresponding words. We will make 11 bits of a group because it can hold a decimal number between 0–2047 and the BIP-39 word list is the same.
1101010110111011000110010010010111001001011001001010110001011...00
---- ---- ---- ---- ---- ---- ---- ....173 1839 934 394 900 ....better total input idea ----- This is the word based on corresponding indexes -------
Step 3: Mnemonic to Seed
- We have mnemonic words and we will use a
PBKDF2()
function (detail in next point) to generate a final seed. - This function produces a 64-byte (512-bit) result with a combination of hash and Optional passphrase(Password). It has a round series multiplied by 2048 like 2048, 4096, 8192 etc.
- A passphrase is optional it will change the hash completely.
- This 64 bytes result is your seed and is used to create the master extended key for an HD Wallet.
Don’t select the 12–24 words randomly from the wordlist because the last word contains the checksum of all the rest words and if it is not matched then it will show invalid when you import it to a wallet.
What is the PBKDF2 function?
- PBKDF2 means Password-Based Key Derivation Function 2.
- This function hashes the data multiple times before getting the result. We cannot decode the hashing to the original seed that we used.
- It has a functionality that we can add a passphrase for more security. It will completely change that seed. we can add this passphrase into a
salt
method.
salt (“passphrase”, “seed extension”)
How to validate Mnemonic?
- A mnemonic sentence is a combination of words and checksum. This checksum is used to check whether the mnemonic is valid or not.
- We will reverse the generate process. Follow the below steps
Step 1: Convert Mnemonic into bits.
Step 2: Find the mnemonic and checksum data.
Step 3: Match the checksum from the mnemonic with the entropy checksum.
What are Master Extended Keys?
- We can create a master extended key by simple pass a parameter into a function HMAC-SHA512. This function returns 64 bytes of data and we will split this data into two parts.
- The left half (32 bytes) will be a private key and the right half (32 bytes) is a chain code which is random data. The Chain code data is used to generate the child keys.
- Extended Private Key: It is the combination of a simple private key and chain code.
- Extended Public Key: It is a simple public key that is gotten from a private key and adds the same chain code into this.
- These keys are used to generate the child key as we see that we can generate an infinite number of keys from a single wallet.
Motivation Quote: If the ladder is not leaning against the right wall, every step we take just gets us to the wrong place faster. — Stephen Covey
Stay tuned with me and join me to learn the blockchain development series on every Monday morning at 9 AM. You can share your thought as well. I am happy to read your words respectfully.
Thank you for reading.
Signing off.