Overview

4 Wallets

This chapter introduces a user-friendly mobile wallet that automates everyday tasks previously done manually, improving the experience of sending and receiving payments. The wallet generates new addresses for privacy, stores and safeguards private keys, scans payment details (e.g., via QR codes/URIs), prepares and signs payments, and tracks balances while only crediting funds after confirmation. The back-end process used by Lisa and the spreadsheet remains unchanged; the focus is on making things seamless for end users while enabling the use of many addresses without added complexity.

After highlighting the pitfalls of naïve backups (exporting keys to files or email) and the mixed results of password-encrypted backups (entropy, forgotten passwords, and changing attacker capabilities), the chapter introduces hierarchical deterministic (HD) wallets. In an HD wallet, all keys are derived from a single random seed and organized in a tree, enabling simple organization (e.g., “accounts”) and effectively infinite key generation. Extended private keys (xprv) and extended public keys (xpub) pair a key with a chain code to deterministically derive children. Backups become far simpler and safer: users write down the seed once, typically encoded as a 12–24 word mnemonic sentence with a checksum, and can later restore all keys from those words. Xpubs let systems such as web servers create fresh addresses without holding any private keys, keeping spending keys offline.

The chapter also addresses important security nuances. If an attacker obtains a child private key and the corresponding parent xpub, they may reconstruct the parent xprv; hardened derivation prevents this by making child derivation depend on private data, isolating branches and limiting blast radius. The text provides intuitive grounding for the underlying cryptography, including how public keys are derived from private keys using elliptic-curve “multiplication” and how xpub child derivation mirrors the private side with safe public-key operations. In sum, the wallet’s responsibilities—key management, address creation, payment initiation, fund tracking, and robust backup—are made practical and secure by HD wallets, mnemonic seeds, and careful use of xpubs and hardened paths.

   Bitcoin wallets
visual toc wallets.png
  John buys a cookie using the wallet app. The cafe generates a key and displays a QR code with payment details to John. John scans the payment details and taps "OK" to approve the payment. John’s wallet sends an email to Lisa.
wallet payment process.png
John has just clicked OK in his wallet to approve the payment. The wallet takes care of the rest for John. It selects a key with enough funds and signs a message to Lisa. The signed message is then automatically emailed to Lisa.
johns wallet payment process.png
  John’s and the cafe’s wallets check the spreadsheet every few seconds. If a new payment either incoming or outgoing is found, the wallet updates the balance of the concerned keys and notifies its user.
wallet update balance.png
5. John backs up his private keys. They are sent in a text file to his email address.
wallet backup private keys.png
  John backs up his private keys. They are sent in a file encrypted with a password that john enters into his phone.
wallet backup encrypted private keys.png
2ndcol technology advancements.png
2ndcol randomness is hard.png
  Create the first two of Rita’s three savings keys. A random seed is used to create a master extended private key (master xprv). This extended private key (xprv) is then used to create child extended private keys (child xprv).
hd wallet key tree overview.png
  An extended private key consists of a private key and a chain code.
xprv.png
   Deriving Rita’s master xprv. The seed is hashed with HMAC-SHA512. The resulting hash of 512 bits is split into left 256 bits that becomes the master private key and the right 256 bits that becomes the chain code.
hd wallet derive master xprv.png
  Deriving a child xprv from a parent xprv. The public key and chain code of the parent and the desired index are hashed together. The parent private key is added to the left half of the hash and the sum becomes the child private key. The right half becomes the child chain code.
hd wallet derive child xprv.png
2ndcol key tree.png
We are working on making a great wallet for users.
periscope wallets.png
backup seed.png
  Decoding a mnemonic sentence into the seed.
restore backup.png
  The cafe copies its "on-line sales" xprv to the web server.
cafe hd wallet.png
  An extended public key consists of a public key and a chain code
xpub.png
  Generating the tree of xpubs from the master xpub. The general pattern is the same as when generating xprvs, but the child derivation function differs.
hd wallet xpub tree.png
  Extended public key derivation. The private key addition from the xprv derivation is replaced by public key "multiplication".
hd wallet derive child xpub.png
  Plus on the private side have a corresponding "plus" on the public side. Parent private key plus some value is the child private key. Parent public key plus the public key derived from the same value is the child public key.
hd addition.png
  Mallory has stolen the private key m/1/1 from the cafe and the parent xpub from the web server. She can now steal all money in the on-line sales account.
steal xprv.png
2ndcol hd wallet derive child xprv.png
  The master xpub cannot be used to generate any child keys at all because m/0' and m/1' are hardened keys.
hd wallet derive hardened child xprv.png
. Whole number solutions to the elliptic curve  y2=x3+7mod11. Each such solution is a public key.
curve solutions.png
  child public key is derived by adding the parent public key with the public key derived from the left 256 bits.
derive child xpub.png
  Xpub derivation and normal public key derivation. A normal public key is the start point G multiplied by a private key. A child public key is the parent public key "added" to the public key derived from left half hash.
derive pubkey and child xpub.png
  Each point on the curve has a symmetric point at the same x value
2ndcol point symmetry.png
  The cafe’s hierarchical deterministic wallet. They use key hardening to isolate different branches of the tree from each other.
summary hd wallet.png
exercise key derivation types.png
exercise counter sales tree.png

 Recap

In this chapter you learned that

  • You usually use a mobile app, called a wallet, to send and receive money, cookie tokens or bitcoins.
  • The wallet is responsible for creating and storing your keys, scan or show payment details, send payments, show your balance and backup your keys. You don’t have to do that manually.
  • Backup is hard to do right. Password protected backups suffer from problems with forgotten passwords, technology improvements, humans suck as random number generators.
  • With hierarchical deterministic wallets, you backup your random seed and store that seed in a safe place. Do it only once.
  • The seed can be encoded using a mnemonic sentence, which makes it easier for you to write down the seed.
  • HD wallets generate multiple private keys from its seed and organize them in a tree structure to improve your privacy.
  • The tree, or any branch of the tree, of public keys can be generated from an extended public key. This is very useful for insecure environments like web servers.
  • Hardened private key derivation keeps "accounts" compartmentalized. They confine an attacker to a single account.

FAQ

What is a “wallet” in this chapter, and what does it actually do?A wallet is an app that manages the keys required to spend funds. It creates new addresses, securely stores private keys, transfers payment details between users (for example by scanning QR codes), automates signing and sending payment messages, keeps track of balances, and provides a way to back up keys.
How are payment details transferred between payer and payee?Wallets use payment URIs (standardized as BIP21 in Bitcoin) embedded in QR codes. The payee’s wallet displays a QR with the address and amount; the payer scans it, reviews the details, and the wallet signs and sends the payment message automatically.
How does the wallet track my funds, and why might it show a payment as “pending”?The wallet periodically checks the authoritative record (the spreadsheet in the chapter’s model; the blockchain in Bitcoin) for activity affecting its keys. It only updates balances after confirmation to avoid showing money that may never finalize, but it can indicate that a payment is pending while waiting for confirmation.
Why do I need to back up private keys, and what’s wrong with simple file or email backups?If you lose your private keys, you lose access to your funds. Emailing or storing raw key files exposes them to theft, and making a new backup for every new address is error-prone and inconvenient; both issues make ad‑hoc backups risky.
What is a hierarchical deterministic (HD) wallet and how does it simplify backups?An HD wallet (BIP32) deterministically derives all keys from a single random seed. Backups become simple: protect the seed once and you can recreate the entire key tree later without re-backing up for every new address.
What is a mnemonic sentence (BIP39), and how is it used for backups?A mnemonic sentence encodes the seed as 12–24 human-readable words with a small checksum to catch mistakes. You write the words down and store them safely; restoring the wallet later is done by entering the words, which regenerate the seed and all keys.
What’s the difference between an extended private key (xprv) and an extended public key (xpub)?An xprv contains a private key plus a chain code and can derive both private and public descendants. An xpub contains a public key plus the same chain code and can derive public keys only, which lets you generate fresh receive addresses on untrusted systems without exposing any private keys.
What is hardened key derivation and why would I use it?Hardened derivation changes the child‑key computation so it requires the parent private key, preventing attacks that combine a leaked child private key with a parent xpub to climb back up the tree. The trade‑off is that hardened children cannot be derived from xpubs, so use hardened branches to isolate accounts and non‑hardened leaves where watch‑only address generation is needed.
What exactly must I keep to be able to restore my wallet?You must securely store your seed (typically as BIP39 words). If your wallet follows standard derivation paths (such as BIP44), the path structure is implied, allowing full restoration of accounts and addresses from the seed alone.
Why is public key multiplication important here, and is it reversible?Public key multiplication is the one‑way process that derives a public key from a private key and underpins xpub derivation. It’s easy to go from private to public, but there’s no known practical way to reverse it to recover the private key, which keeps xpub‑based address generation safe from spending.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Grokking Bitcoin ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Grokking Bitcoin ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Grokking Bitcoin ebook for free