Sub topic 5
Seed phrases and key derivation
This chapter assumes you know that a wallet manages private keys and signs transactions, and that custody means controlling those keys. It also assumes basic public-key cryptography: a private key produces a public key, and the reverse is infeasible. Here we look at how a short list of words can stand in for every key a wallet will ever use.
Why wallets use seed phrases
Early wallets generated a fresh random private key for each address and stored them all in a file. Each new address required a new backup. A backup made last month missed every key created since.
Modern self-custody wallets are hierarchical deterministic (HD) wallets. A single random secret, the seed, is the input to a fixed algorithm that produces an unlimited, reproducible tree of keys. Back up the seed once and you can regenerate every key, on any compatible device, at any time.
The seed is usually presented as a seed phrase (also called a recovery phrase or mnemonic): 12 or 24 words from a fixed list. The words are an encoding that is easier for humans to write down than 256 bits of binary.
Whoever holds the words holds all the keys. The wallet application, the hardware device and the company that made them are all replaceable. The phrase is not.
From randomness to words (BIP-39)
BIP-39 is the standard that defines the encoding. The steps are:
- Generate random entropy: 128 to 256 bits, in multiples of 32.
- Compute SHA-256 of the entropy. Take the first
entropy_bits / 32bits as a checksum. - Append the checksum to the entropy.
- Split the result into 11-bit groups. Each group is a number from 0 to 2047, an index into a fixed list of 2048 words.
| Words | Entropy bits | Checksum bits | Total bits |
|---|---|---|---|
| 12 | 128 | 4 | 132 |
| 15 | 160 | 5 | 165 |
| 18 | 192 | 6 | 198 |
| 21 | 224 | 7 | 231 |
| 24 | 256 | 8 | 264 |
The code below performs steps 1 to 4 for a 12-word phrase. It stops at the indexes, because the 2048-word list is too long to print here.
import hashlib
import secrets
# BIP-39: 128 bits of entropy -> 12 words.
entropy = secrets.token_bytes(16)
checksum_len = 4 # entropy bits / 32
digest = hashlib.sha256(entropy).digest()
entropy_bits = bin(int.from_bytes(entropy, "big"))[2:].zfill(128)
checksum_bits = bin(digest[0])[2:].zfill(8)[:checksum_len]
all_bits = entropy_bits + checksum_bits # 132 bits
# Each 11-bit group is an index (0-2047) into the 2048-word BIP-39 wordlist.
indices = [int(all_bits[i:i + 11], 2) for i in range(0, len(all_bits), 11)]
print(len(indices)) # 12
print(all(0 <= i < 2048 for i in indices)) # TrueThree properties of this design matter in practice:
- The security is in the entropy, not the words. A 12-word phrase carries 128 bits of randomness. That is out of reach for brute force when it comes from a proper random source. It is weak if a person picks "random" words themselves, or if the generating software has a flawed random number generator.
- The checksum catches typos, not attacks. With 4 checksum bits, a random wrong 12-word phrase still passes about 1 time in 16. A wallet that accepts your phrase has not proven it is the right one.
- The wordlist is designed for transcription. The first four letters of every word are unique, so a partly legible or abbreviated word can still be identified.
From words to a master key
The words are not used as keys directly. BIP-39 stretches them into a 512-bit seed with PBKDF2, a function that repeats HMAC-SHA512 2048 times. The salt is the string "mnemonic" followed by an optional passphrase.
import hashlib
# A well-known test phrase. Never use it for real funds.
mnemonic = " ".join(["abandon"] * 11 + ["about"])
def to_seed(mnemonic: str, passphrase: str = "") -> bytes:
return hashlib.pbkdf2_hmac(
"sha512",
mnemonic.encode("utf-8"),
("mnemonic" + passphrase).encode("utf-8"),
2048,
)
seed_plain = to_seed(mnemonic)
seed_with_passphrase = to_seed(mnemonic, "correct horse")
print(len(seed_plain)) # 64 bytes = 512 bits
print(seed_plain == seed_with_passphrase) # FalseThe seed is then fed into HMAC-SHA512 (BIP-32) to produce a master private key and a chain code. Every child key is derived from a parent key, the parent's chain code and an index number. The same seed always gives the same tree, and nothing in the tree reveals the seed.
The optional passphrase
The passphrase, sometimes called the "25th word", changes the salt. The result is a completely different seed, and so a completely different wallet. There is no error for a wrong passphrase: the wallet opens normally and shows an empty balance.
This gives you two useful options and one hazard:
- A stolen word list alone does not reach the funds.
- You can keep a small decoy balance under the empty passphrase.
- If you forget the passphrase, the funds are gone. The words cannot recover it.
The derivation tree and paths
Keys are addressed by a derivation path such as m/84'/0'/0'/0/0. Each segment is a level in the tree (the standards are BIP-44 and its descendants). The apostrophe marks hardened derivation.
| Path | Typical use |
|---|---|
| `m/44'/0'/0'/0/0` | Bitcoin, legacy (P2PKH) addresses |
| `m/84'/0'/0'/0/0` | Bitcoin, native SegWit addresses |
| `m/86'/0'/0'/0/0` | Bitcoin, Taproot addresses |
| `m/44'/60'/0'/0/0` | Ethereum and EVM chains |
The levels are purpose, coin type, account, change (0 for receiving and 1 for change addresses on Bitcoin) and address index.
Hardened and non-hardened derivation
A non-hardened child can be computed from the parent public key alone. That is how a watch-only wallet generates fresh addresses from an extended public key (xpub) without ever seeing a private key.
The cost is a known weakness. If an attacker has the xpub and any one non-hardened child private key, they can compute the parent private key and then every sibling key. Hardened derivation (the apostrophe) needs the parent private key, which breaks that link. Treat an xpub as private-ish: it exposes your whole address history and balance, though not your ability to spend.
What this means for compatibility
Because derivation is deterministic, the same phrase restores the same keys in any compliant wallet. This is why you can replace a lost hardware device with a different brand.
Several things can still go wrong:
- Different default paths. A wallet that derives
m/44'/...shows different addresses than one usingm/84'/.... The funds are not lost. You are looking at the wrong branch of the tree. Check the path before assuming a restore failed. - Not every wallet uses BIP-39. Electrum uses its own seed format, and SLIP-39 splits a secret into shares. A phrase from one scheme does not necessarily load in another.
- The phrase does not contain the history. It restores keys. Wallet labels, contact lists and transaction notes live elsewhere and must be backed up separately.
Backing up a seed phrase safely
The threats are loss, damage, and theft. Defending against one often worsens another, so the backup is a trade-off.
- Write the words on paper or, better, stamp them into metal. Paper burns and floods. Ink fades.
- Never photograph the phrase, store it in a notes app or cloud drive, or email it to yourself. Those copies are online and often synced.
- Never type it into a website or give it to anyone who asks. No legitimate support team or wallet vendor needs it.
- Keep at least two copies in separate physical places, so one fire or burglary does not take everything.
- Generate the phrase on the device or wallet itself. A pre-printed phrase or a phrase supplied by someone else is not yours alone.
- Test the backup before relying on it. Restore into a second wallet with a small balance and confirm the first receiving address matches.
Splitting the words across places (for example, writing words 1 to 6 in one location and 7 to 12 in another) is tempting but dangerous. Each half reveals half the secret, and the loss of either half is permanent. Schemes such as SLIP-39 or multisig (covered in a later chapter) split the secret in a way that tolerates loss.
Compare with custodial wallets
With a custodial wallet you never see a seed phrase. The provider holds the keys, and you log in with credentials the provider can reset. That removes the backup burden and the risk of losing words. It also means your claim to the funds depends on the provider, a trade-off the next chapters examine under counterparty risk.
Exercise
You restore a 12-word phrase into a new wallet app. The wallet accepts the words with no checksum error, but it shows a zero balance and a different first address from your old wallet. Name two likely causes, and say whether the funds are lost.
Show answer
Likely causes: the new app uses a different default derivation path (for example m/44' instead of m/84'), or the old wallet used a BIP-39 passphrase that you did not enter. The checksum passing only shows the words are well formed, not that they are the right ones for this wallet. The funds are almost certainly not lost. Switch to the original derivation path, or enter the original passphrase, and the original addresses will reappear.
Exercise
Why does a 24-word phrase not protect you if you store a photo of it in cloud storage?
Show answer
The strength of the phrase lies in its secrecy, not its length. A cloud photo creates an online copy that can be reached by account takeover, malware or a provider breach. Anyone who gets the image has all the keys and can spend the funds irreversibly.