Sub topic 2
Keys, addresses, and what a wallet really holds
This chapter assumes you know what a hash function is and that public/private keys come in pairs. It also assumes you can read short Python. It explains how a wallet controls coins without storing them, and how Bitcoin and Ethereum differ in the way they turn keys into addresses.
The ledger holds balances, the wallet holds keys
Coins do not live inside a phone, a hardware device, or an app. The blockchain ledger records who may spend what. A wallet is software or hardware that does three things:
- It stores or derives private keys.
- It computes public keys and addresses from them.
- It signs transactions to prove authority to spend.
Whoever can produce a valid signature for an address can move its funds. Nothing else is checked. There is no account recovery, no identity check, and no way to reverse a transfer once it is confirmed. That is why this chapter matters for the custody discussion later in the tutorial.
The key chain: private key, public key, address
Both Bitcoin and Ethereum use the same elliptic curve, secp256k1, and the ECDSA signature scheme. The derivation runs in one direction only.
- The private key is a number between 1 and just under 2^256 (the curve order n). Any number in that range is valid, so a good wallet picks one with a cryptographically secure random generator.
- The public key is the point you get by adding the curve's generator point G to itself k times, where k is the private key. Going from k to the point is fast. Going back is the discrete logarithm problem, which is infeasible at this size.
- The address is a shorter, hashed form of the public key, with extra formatting.
The following script uses only the Python standard library. It implements the curve arithmetic and derives a compressed public key. It is for learning, not for generating real keys: it is not constant-time, and the example key is deliberately trivial.
import hashlib
P = 2**256 - 2**32 - 977
N = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
G = (0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798,
0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8)
def point_add(a, b):
if a is None:
return b
if b is None:
return a
x1, y1 = a
x2, y2 = b
if x1 == x2 and (y1 + y2) % P == 0:
return None # point at infinity
if a == b:
lam = 3 * x1 * x1 * pow(2 * y1, -1, P) % P
else:
lam = (y2 - y1) * pow(x2 - x1, -1, P) % P
x3 = (lam * lam - x1 - x2) % P
y3 = (lam * (x1 - x3) - y1) % P
return (x3, y3)
def point_mul(k, point):
result = None
while k:
if k & 1:
result = point_add(result, point)
point = point_add(point, point)
k >>= 1
return result
def compressed(pub):
x, y = pub
prefix = b"\x02" if y % 2 == 0 else b"\x03"
return prefix + x.to_bytes(32, "big")
private_key = 1 # never use a small or guessable key for real funds
assert 1 <= private_key < N
public_key = point_mul(private_key, G)
print(public_key == G)
print(compressed(public_key).hex())
print(hashlib.sha256(compressed(public_key)).hexdigest())With key 1 the public key is G itself, so the first line prints True. The compressed form stores only the x coordinate plus one byte saying whether y is even or odd. That is enough to recover y from the curve equation, and it halves the public key size to 33 bytes.
From public key to address
Bitcoin
Bitcoin addresses commit to a hash of the public key, not the key itself. The usual hash is HASH160, which is RIPEMD-160 applied to the SHA-256 of the key, giving 20 bytes. The formats differ in how those bytes are wrapped.
| Type | Prefix | Encoding | Notes |
|---|---|---|---|
| P2PKH (legacy) | `1` | Base58Check | Original format, largest transactions |
| P2SH | `3` | Base58Check | Pays to a script hash, used for multisig and wrapped SegWit |
| P2WPKH (native SegWit) | `bc1q` | Bech32 | Cheaper fees, case-insensitive |
| P2TR (Taproot) | `bc1p` | Bech32m | Schnorr signatures, more flexible scripts |
Each format includes a checksum, so a mistyped character is almost always rejected before any coins move. The checksum catches typos only. It cannot tell you whether the address belongs to the person you intend to pay.
Because the address hides the public key behind a hash, the key is revealed only when you spend. This is one reason reusing addresses is discouraged. Reuse also lets observers link all of your payments together.
Ethereum
Ethereum takes the 64-byte uncompressed public key (x and y, without the prefix byte), hashes it with Keccak-256, and keeps the last 20 bytes. The result is written as 0x plus 40 hex characters.
Keccak-256 is not the same as the standardized SHA3-256 in Python's hashlib, because the two use different padding. Ethereum libraries ship a separate Keccak function for this reason.
Ethereum addresses are not Base58 or Bech32. Their checksum, defined in EIP-55, is hidden in the capitalization of the hex letters. A wallet that supports it can detect typos, but an all-lowercase address is still valid and carries no checksum.
What the wallet does when you pay
To spend, the wallet builds a transaction, hashes it, and signs the hash with the private key. Nodes verify the signature against the public key. The private key never leaves the wallet and never appears on the network.
How the signature is bound to the funds depends on the model from the next chapters:
- In Bitcoin's UTXO model, each input proves the right to spend one specific unspent output, so a wallet's "balance" is the sum of the outputs its keys can spend.
- In Ethereum's account model, one key controls one account with a balance and a nonce (a counter of sent transactions). The nonce stops a signed transaction from being replayed.
ECDSA has one dangerous requirement. Each signature needs a unique, unpredictable random value. If a wallet reuses or leaks that value, an observer can compute the private key from two signatures. Modern wallets use deterministic nonces (RFC 6979) to avoid this. Trust audited libraries here and never write your own signing code for real funds.
Seed phrases and hierarchical wallets
Early wallets stored a pile of independent random keys, and each one needed its own backup. Modern wallets use a hierarchical deterministic (HD) design:
- BIP-39 turns random entropy into a list of 12 or 24 words, the seed phrase. The last word includes a checksum.
- The words are stretched through PBKDF2 into a 512-bit seed. An optional passphrase can be added, and a different passphrase yields an entirely different wallet.
- BIP-32 derives a tree of keys from the seed.
- BIP-44 and related standards assign paths. A path such as
m/84'/0'/0'/0/0names purpose, coin type, account, change flag, and index.
One backup therefore covers every address the wallet will ever generate, on every chain it supports, provided the same standard is used on restore.
An extended public key (xpub) lets software derive all of a wallet's public addresses without any private keys. This supports watch-only wallets, such as an accounting tool that shows balances but cannot spend. The trade-off is privacy: sharing an xpub reveals every address in that branch.
Hardware, software, and custodial wallets
| Kind | Where keys live | Main strength | Main risk |
|---|---|---|---|
| Software (hot) wallet | Phone or computer | Convenient | Malware can steal keys or alter the address you sign |
| Hardware wallet | Dedicated secure chip | Keys never exposed to the host computer | Supply-chain tampering, lost backup |
| Custodial account | Company's systems | Password reset, easy interface | Company failure, freezing, hacks |
A hardware wallet signs inside the device and shows the destination on its own screen. That screen is the point: check the address there, because malware on the computer can swap the one displayed on the monitor. A custodial exchange account is a database entry. You hold a claim against the company, not keys. The risks chapter returns to this.
Common errors
- Believing coins are "in" the wallet. Deleting the app destroys nothing. Without the seed phrase, you lose the ability to sign, and the funds stay on the ledger unspendable.
- Sending to the wrong network. The same 0x address format works on many Ethereum-compatible chains. A transfer sent on a different chain than the recipient expects may be unrecoverable by the sender.
- Treating a typo-checked address as verified. A checksum proves the string is well formed. It does not prove ownership. Clipboard-swapping malware replaces copied addresses with valid ones, so compare the first and last characters after pasting.
- Weak randomness. Brainwallets, human-chosen "random" numbers, and buggy generators have all led to drained wallets, because attackers scan small key spaces continuously.
- Mixing standards on restore. The same seed can produce different addresses under different derivation paths. A restored wallet that shows a zero balance may simply be looking at the wrong path.
- Sharing the wrong thing. Addresses and xpubs are shareable, though an xpub costs privacy. Private keys and seed phrases are never shareable.
Exercise
A friend says, "I lost my phone, but my coins are safe because they're on the blockchain, not on the phone." What is correct and what is missing? Also, what must be true for the friend to recover access?
Show answer
Correct: the balance is recorded on the ledger, not stored on the phone, so losing the phone does not erase it. Missing: the ability to spend depends on the private keys. If the friend has the seed phrase (and the passphrase, if one was set), they can restore the wallet on a new device with the same derivation standard and regain control. If the seed phrase existed only on the lost phone, the funds remain on the ledger but can no longer be spent by anyone.
Exercise
Why does a Bitcoin P2PKH address not reveal the public key before the coins are spent, while an Ethereum address can still be used to receive funds safely? Name the step that both address types share.
Show answer
Both derive the address by hashing the public key (HASH160 for Bitcoin, Keccak-256 for Ethereum, keeping the last 20 bytes). Hashing is one-way, so the address does not expose the public key until a signed spend reveals it. Recovering the key from the address would require inverting the hash.