Bitcoin

Anatomy of a transaction

We’ve watched blocks chain together into an unchangeable ledger. But what actually goes inside a block? Transactions. And a Bitcoin transaction doesn’t work the way a bank transfer does — there are no account balances at all. Let’s crack one open.

No balances — only coins

A bank keeps a single number for your account and edits it up and down. Bitcoin has no such number. Instead, every coin you own is really an unspent output from some earlier transaction — a discrete chunk of bitcoin locked to your key, like a physical coin in your pocket. Your “balance” is just the sum of all these chunks. They’re called UTXOs: unspent transaction outputs.

To pay someone, you don’t edit a balance — you spend whole coins as inputs and create new coins as outputs. Just like handing over a $20 note for a $12 coffee, you can’t tear the note in half: you hand over the whole thing and get change back. A Bitcoin transaction does exactly that — it consumes entire coins and mints fresh ones, one of which is usually change sent back to yourself.

Build a transaction

Here are three coins you’ve received. Tick which ones to spend (the inputs), choose how much to pay and what fee to attach — and watch the outputs and change compute themselves. Notice you can never send exactly the right amount, so the leftover comes back to you as change.

Inputs — coins you own

Total in0.4 BTC

Outputs — where it goes

➜ To recipient0.3 BTC
↩ Change to you0 BTC
⛏ Miner fee0.0001 BTC
Total out0.4 BTC

Under the hood — the raw transaction

  

The fee is the leftover

Here’s the elegant part: a transaction never states the fee explicitly. The fee is simply whatever’s left when you add up the inputs and subtract the outputs. If you feed in 0.40 BTC and your outputs (payment + change) only account for 0.3999, the missing 0.0001 is the fee — and any miner who includes your transaction gets to claim it. Set your change too high and you’d underpay the miner; set it too low and you’re tipping them your own coins. Wallets do this arithmetic for you, but that’s all it is: inputs minus outputs.

Under the hood: a transaction as raw bytes

Everything above is really just one string of bytes broadcast to the network. Here’s a real, published transaction serialised exactly as it travels the wire — colour-coded field by field. Edit the output amount and watch the eight little-endian value bytes change; the rest is the version, the input it spends (with the signature that unlocks it), the locking script, and the locktime.

Size:

    How a block bundles many transactions: the Merkle tree

    A block holds hundreds or thousands of transactions, but its header stores just one 32-byte fingerprint that commits to all of them: the Merkle root. Each transaction is hashed, then pairs of hashes are hashed together, and again, and again — halving each step until a single root remains. Edit any transaction below and watch every hash on its path up to the root change. That’s why the root pins down every transaction: change one, and the whole block’s fingerprint changes.

    This is also how a lightweight wallet can prove a payment is in a block without downloading the whole thing: it only needs the branch of hashes from that one transaction up to the root — a handful of hashes instead of the entire block.

    Why your wallet has so many addresses. Every time you spend, the change lands on a brand-new address your wallet controls. That’s not clutter — it’s privacy. If all your change piled back onto one address, anyone could watch your entire net worth in real time. Spreading coins across fresh addresses is exactly why you were told, back in the Addresses lesson, to use a new one each time.

    One more thing makes a transaction valid: a signature. Each input carries a cryptographic signature proving you own the coin being spent — the digital-signature machinery from the cryptography section, doing its real job. But building and signing a transaction is only half the story. What happens the moment you broadcast it — how it reaches a block and becomes irreversible — is a short journey of its own. That’s next.

    Key takeaways

    • Bitcoin has no account balances — you own discrete coins (UTXOs: unspent transaction outputs), and your balance is just their sum.
    • To pay, you spend whole coins as inputs and create new outputs; the leftover comes back to you as change, and the fee is simply inputs minus outputs.

    Check yourself

    How is a Bitcoin “balance” actually stored?

    In a transaction, how is the miner fee determined?

    Spotted an error or have feedback on this lesson? Suggest a correction ↗

    Comments

    Powered by Nostr — reply from any Nostr client, and zap the lesson over Lightning.