Sub topic 4
How self-custody wallets work
This chapter assumes you know what Bitcoin or Ether is, that you have used online accounts and passwords, and that you have heard of public-key cryptography. The earlier chapters on keys and on custody define the terms used here. This chapter explains what a self-custody wallet does day to day, which parts you control, and where it breaks.
What the wallet actually is
A self-custody wallet is software or hardware that generates and protects private keys and uses them to sign transactions. It does not contain coins. Your balance is a set of unspent outputs, or an account state, recorded on the blockchain. The wallet reads that record and shows you a total.
The wallet does three jobs:
- Generate keys. It creates randomness, turns it into a seed, and derives many private keys from that seed.
- Watch the chain. It derives the matching public keys and addresses. It asks a node or an indexing service what those addresses hold.
- Sign. When you approve a payment, it builds a transaction and signs it with the right private key. It sends the signed result to the network.
In self-custody, no company can move your funds without your signature. A company can still run the app, the servers that report your balance, and the nodes that relay your transaction. Those parts affect privacy and availability, but they cannot spend your coins.
The signing flow
The private key never has to leave the signing component. Only the transaction goes in and only the signature comes out.
In a phone wallet, the app and the signer are the same program, so the key sits in the phone's secure storage. In a hardware wallet, the signer is a separate device. The second case shows the design more clearly, and the later chapter on hardware wallets covers it.
A working demonstration
The script below uses only the Python standard library. It implements ECDSA on secp256k1, the curve Bitcoin and Ethereum use, so you can see that "signing" is arithmetic on a secret number. It is for learning only. It is slow, it is not constant-time, and it skips details that real wallets require. Never use it to protect funds.
import hashlib
import secrets
# secp256k1 curve parameters
P = 2**256 - 2**32 - 977
N = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
G = (
0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798,
0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8,
)
def 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
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):
result = None
while k:
if k & 1:
result = add(result, point)
point = add(point, point)
k >>= 1
return result
def digest(message: bytes) -> int:
return int.from_bytes(hashlib.sha256(message).digest(), "big")
def sign(private_key: int, message: bytes):
z = digest(message)
while True:
k = secrets.randbelow(N - 1) + 1
r = mul(k, G)[0] % N
s = pow(k, -1, N) * (z + r * private_key) % N
if r and s:
return r, s
def verify(public_key, message: bytes, signature) -> bool:
r, s = signature
if not (0 < r < N and 0 < s < N):
return False
w = pow(s, -1, N)
point = add(mul(digest(message) * 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 # the secret the wallet protects
public_key = mul(private_key, G) # safe to share
fingerprint = hashlib.sha256(
public_key[0].to_bytes(32, "big") + public_key[1].to_bytes(32, "big")
).hexdigest()[:20]
print("address-like fingerprint:", fingerprint)
tx = b"send 0.01 to address X"
sig = sign(private_key, tx)
print("valid signature:", verify(public_key, tx, sig))
print("tampered message:", verify(public_key, b"send 10 to address Y", sig))The output shows True for the original transaction and False for the altered one. The network does the same check. It never sees private_key. It sees only the public key, the message and the signature. A real address is a hash of the public key with a version byte and a checksum. The fingerprint above is a simplified stand-in.
Where the keys live
Self-custody wallets differ mainly in where the signing key sits and what can reach it.
| Wallet type | Where the key lives | Main exposure | Typical use |
|---|---|---|---|
| Browser extension | Encrypted file in the browser profile | Malware, malicious sites, phishing prompts | Frequent use with DeFi and NFTs |
| Mobile app | Phone secure storage, unlocked by biometrics or PIN | Phone theft, malware, fake apps | Everyday payments |
| Desktop software | Encrypted file on the computer | Malware, keyloggers, clipboard hijackers | Power users with a hygienic machine |
| Hardware wallet | Dedicated chip, key never exported | Supply-chain tampering, blind signing, lost backup | Savings and larger amounts |
| Paper or metal seed backup | Words written down, no live key | Fire, theft, loss, sight by others | Backup only, not a spending tool |
The more convenient the wallet, the more often the key meets an internet-connected, general-purpose device. The next chapters on hot and cold storage and on hardware wallets build on this.
Seed, passphrase and derivation
Most wallets do not store one key. They store a seed, which is a large random number, usually written as 12 or 24 words. From the seed they derive a tree of keys by a fixed rule, called a hierarchical deterministic (HD) derivation. One backup recreates every address, in any compatible wallet.
Two consequences matter for operation:
- The seed is the wallet. Anyone who has the words can sign. The device is only a convenient container.
- An optional passphrase changes the whole tree. It is sometimes called the "25th word". The same words with a different passphrase produce a different, valid wallet. This gives you a hidden wallet and protects against someone who finds your words. It also means a typo or a forgotten passphrase leads to an empty wallet and no error message.
The earlier seed phrase chapter covers the word-to-bits mechanics.
Day-to-day operations
Receiving
Receiving needs only a public address. Many wallets generate a fresh address for each payment on Bitcoin, which improves privacy. All the addresses belong to the same seed. Before you give out an address, confirm it on the signing device's screen where one exists. Malware can swap what the computer displays.
Sending
Sending involves the choices below, and the wallet makes them for you or exposes them:
- The destination. A mistyped address cannot be reversed. Check the first and last characters at least, and send a small test amount for a large transfer.
- The fee. The fee is set by you, not by the wallet vendor. On Bitcoin it depends on transaction size in bytes and on network demand. On Ethereum it depends on gas used and the gas price.
- The network. The same address format can exist on several chains. Sending a token on the wrong network can leave it unreachable.
Verifying what you sign
A signature approves the exact data presented to the signer. If the wallet shows only "Confirm", you may be approving a token spending allowance instead of a payment. This is called blind signing. A malicious site can request such an approval. Prefer signers that decode and display the amount, recipient and contract action.
Failure modes you now own
A custodian carries these risks for its customers. In self-custody they fall on you:
- Lost backup plus lost device. The coins remain on the chain, but nobody can sign for them.
- Exposed backup. A photo in cloud storage, a note in an email, or a written copy found by someone else lets them spend without your knowledge.
- Fake software. Counterfeit apps and cloned extensions steal the seed on first use. Download only from the vendor's verified source and check signatures where offered.
- Bad approvals. Signing a malicious request drains funds in a transaction that is valid and final.
- Silent corruption. An unverified backup may contain a transcription error. Test recovery with a small balance before you trust it.
What you gain
Self-custody removes the need to trust a company with solvency, honesty or policy. Nobody can freeze your balance or delay your withdrawal. You can use any network and any application that accepts signatures. Those gains come with the full responsibility described above, so the later chapters on user-error risk and on choosing a strategy weigh them against the custodial model.
Exercise
A friend says their wallet app "holds" 2 BTC, and that if the app company shuts down, the BTC is gone. Assuming it is a true self-custody wallet and your friend has a verified seed backup, is the friend right? Explain what is on the chain, what is in the app, and what your friend should do.
Show answer
The friend is wrong. The 2 BTC is recorded on the Bitcoin blockchain as outputs locked to addresses derived from the friend's keys. The app only derives keys, reads the chain and signs transactions. If the company disappears, the friend installs any compatible wallet (or uses a hardware wallet), enters the seed and any passphrase, and the same addresses and balance reappear. The friend should confirm the backup restores correctly, ideally by restoring it in a second wallet and checking that the first address matches. If the wallet was actually custodial, the answer would be different, because the company would hold the keys.
Exercise
In the Python demonstration, which values would an attacker need to forge a valid signature for a new message, and which of those does the network see?
Show answer
The attacker needs the private key (private_key). The network sees only the public key, the message and the signature. Those three values let it verify a signature, but they do not reveal the private key. Without the private key, the attacker cannot produce a pair (r, s) that passes verification for a new message.