Sub topic 1
What a wallet actually holds: keys, addresses and signatures
Prerequisites
This chapter assumes you know three things:
- What a cryptocurrency and a blockchain transaction are, at a basic level.
- That public and private keys come in pairs, and that the private key must stay secret.
- How to send and receive crypto from a wallet app or exchange.
You don't need to know the mathematics of elliptic curves. The chapter gives only as much as you need to reason about where keys live and what that means for safety.
The wallet holds keys, not coins
A cryptocurrency wallet does not contain coins. The coins exist as entries on the blockchain. In Bitcoin, they are unspent transaction outputs, each locked by a spending condition. A wallet stores the secrets that can satisfy those conditions, and it tracks which outputs are yours.
This leads to the central idea of the whole tutorial. Whoever can produce a valid signature for an output can spend it. The network does not check your name, your device or your intentions. It checks the signature. A wallet is therefore a key manager and a signing tool, and "owning" crypto means controlling the private key.
The "hot versus cold" question is also a question about keys. A wallet is hot when its signing keys sit on a device that is connected to a network. It is cold when the keys are kept where no network can reach them, and they come out only to sign. The later chapters build on this distinction, so it is worth getting the key model right first.
Private keys, public keys and addresses
Three objects are easy to confuse.
| Object | What it is | Secret? | Role |
|---|---|---|---|
| Private key | A 256-bit number chosen at random | Yes | Creates signatures |
| Public key | A point on an elliptic curve computed from the private key | No | Lets anyone verify a signature |
| Address | An encoded form of the public key, or of a hash of it, or of a script | No | Where people send funds |
Bitcoin and Ethereum both use the secp256k1 curve for the main signature scheme. Each step from private key to address is a one-way function. You can compute forward cheaply, but nobody can work backward in any practical amount of time.
The address in Bitcoin is not the public key itself in most formats. A common legacy format hashes the public key with SHA-256 and then RIPEMD-160, and then adds a version byte and a checksum. Newer formats such as native SegWit and Taproot use different encodings, but the principle is the same. The address is derived from public data, so sharing it reveals nothing that lets someone spend.
Two practical consequences follow.
- Giving out an address is safe. Giving out the private key, or anything that can reconstruct it, is not.
- Because addresses can be computed from public keys, a wallet can hold only public keys and still generate addresses and watch balances. That is called a watch-only wallet. It is the basis of the offline-signing setup you will see in a later chapter.
What a signature proves
A transaction says, in effect, "spend these outputs and send the value to these new addresses." The sender attaches a digital signature. The signature is computed from two inputs: the private key, and a hash of the transaction data being authorized.
Anyone can check the signature using only the public key and the transaction. If it verifies, the signer knew the private key, and the signature covers exactly this transaction. If someone changes the destination address or the amount, the hash changes and the signature no longer verifies.
Two points matter for safe handling.
- Signing does not reveal the private key. You can sign on an isolated device and hand only the signature to a networked computer. This is the reason cold storage works at all. The keys can stay put while the signature travels.
- Signing authorizes only what the signer is shown. If malware on a computer swaps the destination address before it reaches the signing device, the device will sign the altered transaction. This is why hardware wallets display the address and amount on their own screen. Chapter 7 returns to this.
A runnable demonstration
The following Python program implements secp256k1 signing and verification from scratch. It makes the model concrete: a private key is a number, the public key is derived from it, and a signature verifies against the public key alone.
This is a teaching sketch. It is not constant-time, it uses SHA-256 once where Bitcoin uses a different message-hashing procedure, and Taproot uses Schnorr signatures rather than ECDSA. Never use it to protect real funds.
import hashlib
import secrets
# secp256k1 curve parameters
P = 2**256 - 2**32 - 977
N = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
G = (
0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798,
0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8,
)
def point_add(a, b):
"""Add two curve points. None represents the point at infinity."""
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
if a == b:
slope = 3 * x1 * x1 * pow(2 * y1 % P, -1, P) % P
else:
slope = (y2 - y1) * pow((x2 - x1) % P, -1, P) % P
x3 = (slope * slope - x1 - x2) % P
y3 = (slope * (x1 - x3) - y1) % P
return (x3, y3)
def point_mul(k, point):
"""Multiply a point by an integer using double-and-add."""
result = None
addend = point
while k:
if k & 1:
result = point_add(result, addend)
addend = point_add(addend, addend)
k >>= 1
return result
def message_hash(message: bytes) -> int:
return int.from_bytes(hashlib.sha256(message).digest(), "big")
def sign(private_key: int, message: bytes):
z = message_hash(message)
while True:
k = secrets.randbelow(N - 1) + 1 # fresh random nonce every time
r = point_mul(k, G)[0] % N
if r == 0:
continue
s = pow(k, -1, N) * (z + r * private_key) % N
if s == 0:
continue
return r, s
def verify(public_key, message: bytes, signature) -> bool:
r, s = signature
if not (1 <= r < N and 1 <= s < N):
return False
z = message_hash(message)
w = pow(s, -1, N)
point = point_add(point_mul(z * w % N, G), point_mul(r * w % N, public_key))
return point is not None and point[0] % N == r
private_key = secrets.randbelow(N - 1) + 1
public_key = point_mul(private_key, G)
tx = b"send 0.01 BTC to address A"
sig = sign(private_key, tx)
print("valid signature: ", verify(public_key, tx, sig))
print("tampered transaction: ", verify(public_key, b"send 0.01 BTC to address B", sig))The first line prints True. The second prints False, because changing the message changes its hash, and the signature no longer matches. Notice that verify receives only the public key, the message and the signature. It never sees the private key.
Where the nonce can go wrong
The sign function draws a fresh random number k for every signature. This value is called a nonce, and it must stay secret and must never repeat with the same private key. If two signatures reuse k, anyone can solve for the private key from the two signatures and their hashes. Real wallets avoid this by deriving k deterministically from the key and the message (RFC 6979), or by using hardware random number generators that are well tested.
This is a real failure mode, not a textbook worry. Weak random number generation in software wallets has led to stolen funds in the past. It is one reason to prefer well-reviewed wallet software over code you wrote yourself.
From one key to many: HD wallets
Modern wallets rarely hold a single independent key. They use a hierarchical deterministic (HD) wallet, defined in BIP32. One master secret generates a tree of key pairs in a repeatable way. A fresh address for each payment improves privacy, because it stops observers from linking all your payments to one address.
The master secret is in turn encoded as a seed phrase (BIP39), a list of 12 or 24 words. The seed phrase is therefore not a password for an account. It is the key material for everything the wallet will ever derive. Chapter 6 covers how to back it up. For now, remember the consequence: anyone who has the seed phrase can regenerate every private key in the wallet, on any device.
HD wallets also support extended public keys (xpubs). An xpub lets software derive all the public keys and addresses of a wallet without being able to sign. A watch-only wallet imported from an xpub shows your balance and creates unsigned transactions, and it holds nothing an attacker can spend. An attacker who gets an xpub can still see all your addresses and balances, so treat it as private information even though it cannot move funds.
What this means for hot and cold
With the key model in place, the hot and cold distinction becomes precise.
- A hot wallet keeps signing keys on a networked device, such as a phone app or browser extension. Signing is fast, and the same exposure that makes it convenient also lets malware read the key or alter transactions.
- A cold wallet keeps signing keys on a device or medium that never connects to a network. The signature crosses the gap, and the key does not.
- A watch-only wallet is neither. It holds public data only, and it is the online half of most cold setups.
One caution about the label "cold." A key that was generated on a networked computer, or typed into one once, has been exposed to that computer. Where a key was created and where it has been matter as much as where it sits today.
Common mistakes at this level
- Treating an address as an account with a balance stored inside it. The balance is computed from blockchain outputs. The address is just a lock description.
- Confusing the public key with the seed phrase or xpub. Only the private key and the seed phrase can spend. An xpub only reveals.
- Assuming a signature protects the content you didn't see. A signature covers whatever was presented to the signer. Verify the address and amount on a trusted screen.
- Reusing addresses. It is not a security failure for the key, but it hurts privacy and, for some older address types, exposes the public key earlier than necessary.
Summary of the model
- Coins live on the blockchain. The wallet holds keys that can authorize spending.
- The private key produces signatures. The public key verifies them. The address is derived from public data.
- A signature binds to one specific transaction, and it reveals nothing about the private key as long as nonces are handled correctly.
- HD wallets derive many keys from one seed. Whoever holds the seed controls everything.
- Hot and cold describe where signing keys live and when they touch a network. Watch-only wallets let the online side work without any secrets.
Exercise
A friend shows you their phone wallet, which holds an xpub for a savings wallet, and says "my coins are stored on my phone, but the keys are in a safe at home." Is the phone wallet hot or cold? Can the phone alone move funds? And what is one risk of the xpub being stolen?
Show answer
The phone wallet is watch-only, so it is neither hot nor cold in the sense of holding signing keys. It cannot move funds on its own, because it holds only public data and cannot create valid signatures. The coins are not "stored" on the phone either, because they exist on the blockchain. The keys in the safe are what make the savings cold. If the xpub is stolen, an attacker cannot spend, but they can derive every address and see all balances and transaction history, which is a privacy loss and may help them target the owner.
Exercise
In the Python demonstration, what would happen to security if sign used the same value of k for two different transactions signed with the same private key, and why does verify never need the private key?
Show answer
Two signatures made with the same nonce let an observer solve algebraically for the private key, using the two signature values and the two message hashes, so the key would be fully exposed. The verify function never needs the private key because verification uses only the public key, the message hash and the signature (r, s). It checks that a specific curve equation holds, which can only work out if the signature was made with the matching private key.