Sub topic 1
What a wallet really holds: keys, addresses and signatures
A crypto wallet does not hold coins. Coins exist only as entries on a blockchain, which is a shared ledger that many computers keep in sync. What a wallet holds is the secret material that lets you add new entries to that ledger on your own behalf. This chapter explains that material, because every later comparison between custodial and self-custody wallets depends on it.
Prerequisites
This chapter assumes you:
- know what a cryptocurrency such as Bitcoin or Ether is, at the level of "a digital asset recorded on a public ledger";
- are comfortable with online accounts and passwords;
- have heard of public-key cryptography. This is helpful but not required, and the next section covers the part you need.
To run the code example you need Python 3.8 or newer. It uses only the standard library.
The three objects: private key, public key, address
Public-key cryptography uses pairs of mathematically linked numbers. Bitcoin, Ethereum and many other chains use the same curve, called secp256k1. These are the three objects you meet:
| Object | What it is | Secret? | Can you derive it from the previous one? |
|---|---|---|---|
| Private key | A random 256-bit number | Yes | Not applicable, it is the starting point |
| Public key | A point on the curve, computed from the private key | No | Yes, by elliptic-curve multiplication |
| Address | A shortened, checksummed hash of the public key (or, on some chains, a script) | No | Yes, by hashing |
Each step is a one-way function. Going from the private key to the public key is cheap. Going backwards is believed to be infeasible with any known classical computer. The same holds for the step from public key to address. This is why you can hand out an address freely: it lets people pay you, and it does not let them spend.
What "holding coins" actually means
Suppose the ledger records that address X can spend 0.5 BTC. You "own" that balance in one sense only: you can produce a valid signature for address X, and nobody else can. The ledger does not know who you are. It knows only whether a signature checks out.
Two consequences follow:
- Possession of the key is the ownership. There is no account recovery on the network itself. If the key is lost, the coins stay on the ledger and nobody can move them. If the key is copied, anyone with the copy can move them.
- The wallet is a key manager and a transaction builder. Wallet software stores or accesses keys, reads the chain to show balances, builds transactions, asks the key to sign them, and broadcasts the result. The balance you see is the wallet's view of the public ledger, not a file on your device.
Two ledger models, briefly
How the ledger tracks "what address X can spend" differs by chain.
- UTXO model (Bitcoin). The ledger tracks unspent transaction outputs, which are discrete chunks of value locked to an address. Your balance is the sum of the chunks you can unlock. Spending consumes whole chunks and creates new ones, including a "change" output back to you. Wallets usually send change to a fresh address of yours.
- Account model (Ethereum). The ledger tracks a balance per account, and each transaction carries an incrementing number called a nonce so it cannot be replayed.
In both models the authorization step is the same: a valid signature from the controlling key.
Signatures: proving control without revealing the key
A digital signature is a number pair computed from the message and the private key. Anyone who has the public key can check it. The check does not reveal the private key. Signing a transaction commits to its exact contents (recipient, amount, fee), so changing any byte afterwards invalidates the signature.
The following program implements the elliptic-curve signature scheme ECDSA from scratch, so you can see that nothing else is involved. It is for learning only. It is not constant-time and must never protect real funds. The address step uses a simplified hash. Real Bitcoin addresses use RIPEMD-160 and Base58Check or Bech32 encoding, and Ethereum uses Keccak-256.
import hashlib
import secrets
# secp256k1 curve parameters
P = 2**256 - 2**32 - 977
N = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
G = (0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798,
0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8)
def add(a, b):
"""Add two curve points (None is 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:
m = 3 * x1 * x1 * pow(2 * y1, -1, P) % P
else:
m = (y2 - y1) * pow(x2 - x1, -1, P) % P
x3 = (m * m - x1 - x2) % P
return (x3, (m * (x1 - x3) - y1) % P)
def mul(k, point):
"""Multiply a point by a number (double-and-add)."""
result = None
while k:
if k & 1:
result = add(result, point)
point = add(point, point)
k >>= 1
return result
def sign(private_key, message):
z = int.from_bytes(hashlib.sha256(message).digest(), "big")
while True:
k = secrets.randbelow(N - 1) + 1 # fresh random nonce every time
r = mul(k, G)[0] % N
s = pow(k, -1, N) * (z + r * private_key) % N
if r != 0 and s != 0:
return r, s
def verify(public_key, message, signature):
r, s = signature
if not (0 < r < N and 0 < s < N):
return False
z = int.from_bytes(hashlib.sha256(message).digest(), "big")
w = pow(s, -1, N)
point = add(mul(z * w % N, G), 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 = mul(private_key, G)
# Toy address: NOT a real Bitcoin or Ethereum format.
pub_bytes = public_key[0].to_bytes(32, "big") + public_key[1].to_bytes(32, "big")
address = hashlib.sha256(pub_bytes).hexdigest()[:40]
print("address:", address)
tx = b"pay 0.1 to alice, fee 0.0001"
sig = sign(private_key, tx)
print("valid signature:", verify(public_key, tx, sig))
print("tampered message:", verify(public_key, b"pay 9.9 to mallory, fee 0.0001", sig))The output shows True for the original message and False for the tampered one. The verifier used only the public key. The private key never left the signing function.
Common errors and misconceptions
- "My coins are on my phone." The phone holds keys. If the phone is destroyed but the keys are backed up, the coins are unaffected. If the phone is intact but the keys were also copied by malware, the coins are at risk.
- "An address is an account with a password." An address has no password. Control comes only from the key, and there is no "forgot password" path.
- Reusing nonces. In ECDSA the per-signature random number
kmust never repeat or be predictable. Two signatures made with the sameklet anyone compute the private key. Real wallets use vetted libraries and deterministic nonce schemes (RFC 6979) so they never have to trust a weak random source. - Confusing public key and private key in backups. Exporting an "extended public key" (xpub) is safe for watching balances. Exporting a private key or seed is not.
- Sending to the wrong chain or format. An address valid on one chain can look valid on another and still lead to lost funds. The signature scheme is shared, but the ledgers are separate.
- Trusting a wallet display. The balance and the transaction preview come from software that can be wrong or compromised. This is why some devices show the recipient and amount on their own screen (see the later chapter on hardware wallets).
Why this matters for custody
Everything in the rest of this tutorial turns on one question: who can produce the signature?
- In a self-custody wallet, you hold the private key, or the seed that generates it, so you produce the signatures.
- In a custodial wallet, a company holds the keys. You hold a login to the company's database, which records what it owes you. The company signs on-chain transactions when you ask it to, and it can also decline.
The mathematics is the same in both cases. The difference is in who holds the secret and who can be pressured, hacked, or make a mistake.
Summary
- A wallet manages keys and signs transactions. The coins are entries on the ledger.
- A private key produces a public key, which produces an address. Each step is one-way.
- A valid signature is the only proof of control the network accepts. It binds to the exact transaction contents.
- Whoever can sign controls the funds. That is the basis for defining custody in the next chapter.
Exercise
A friend says, "I lost my phone, so my Bitcoin is gone." You know they wrote down a backup of their wallet's seed earlier. Is the statement necessarily true? Explain using the terms private key, address and ledger.
Show answer
No. The coins are entries on the ledger tied to addresses, not files on the phone. The phone only held the private keys (derived from the seed). If the written backup is intact and safe, the friend can restore the keys in new wallet software, which will regenerate the same addresses and show the same balance, and then sign transactions again. The funds would be gone only if the seed backup were also lost, or if someone else had copied the keys and spent the coins.
Exercise
In the Python example, the message is changed after signing and verify returns False. What does this show about what a signature protects, and why can the verifier work with only the public key?
Show answer
The signature commits to the exact bytes that were hashed and signed, so any change to the transaction (recipient, amount or fee) breaks verification. The verifier needs only the public key because the verification equation uses the public key, the message hash and the signature. The private key is needed only to create a valid signature. The one-way link between the two keys means the public key does not reveal the private key.