cryptonist
A printed circuit board photographed flat, its traces and mounted components in rows

Explainers

How bitcoin transactions work: inputs, outputs and change

A bitcoin transaction does not take an amount from one account and add it to another, because Bitcoin has no accounts. It consumes earlier outputs in full and creates new ones, each locked by a short script that says who may spend it next. The fee is whatever the inputs hold that the new outputs do not claim, and the balance a wallet shows is a figure the wallet works out for itself.

Outputs, not accounts

The unit of the ledger is the transaction output. Each output carries two things: an amount, counted in satoshis, 100,000,000 to the bitcoin, and a locking script that sets the conditions for spending it. Bitcoin’s developer documentation calls this the pubkey script; the code calls it scriptPubKey. Outputs are numbered by their position in the transaction that created them, starting from zero.

Until a later transaction spends it, an output is an unspent transaction output, or UTXO. Once spent, it is finished. The developer guide’s account of the chain calls any second reference to it “a forbidden double spend”, and says that for a payment to be valid it must use only unspent outputs as inputs. Full nodes keep a database of the unspent ones, which Bitcoin Core calls the unspent transaction output set, and each new transaction is checked against it.

That is all that holding bitcoin amounts to at this level. The developer guide’s page on transactions puts it in one sentence: a wallet showing a 10,000-satoshi balance means 10,000 satoshis waiting in one or more UTXOs. The whitepaper’s definition is older and plainer still. It defines an electronic coin as “a chain of digital signatures”, each owner passing it on by signing a hash of the previous transaction and the next owner’s public key.

Ethereum and Solana keep accounts with balances that go up and down instead, and that difference is one of the reasons a wallet works on one chain and not another.

Inputs point at earlier outputs

An input carries no amount. It names an earlier output by two coordinates, the identifier of the transaction that created it (the txid, a hash of that transaction) and the output’s index within it, and supplies the data that satisfies that output’s locking script. In older output types the data sits in the input’s signature script; in the SegWit types defined by BIP-141 it moves to a separate witness field. Each input also has a sequence number, a field that BIP-68 uses for relative time locks and BIP-125 used to signal that a transaction could be replaced; why transactions get stuck and what replacement does covers what has changed since.

Because the amount lives in the output being spent, a node has to look the output up to know what an input is worth. That detail returns below, where it matters for signatures and fees.

The first transaction on the chain that is not a block reward shows the whole shape. It sits in block 170, timestamped 12 January 2009, and no earlier block holds anything but its reward. It has one input, the 50-bitcoin reward output of block 9, and two outputs: 10 bitcoin to a different public key and 40 bitcoin back to the key that held the 50. Fifty in, fifty out, and no fee.

Change: outputs are spent whole

An output cannot be partly spent. A wallet that wants to pay less than an output holds spends all of it and sends the remainder back to itself in a second output, the change. The whitepaper sets out the pattern in its section on combining and splitting value: normally one input from a larger earlier transaction or several smaller ones combined, and “at most two outputs: one for the payment, and one returning the change, if any, back to the sender.”

An illustration, with invented numbers. A wallet holds three outputs, of 0.004, 0.012 and 0.05 BTC, and wants to pay 0.015 BTC. It can spend the 0.05 output and create two new ones, 0.015 to the payee and 0.0349 to an address of its own, leaving 0.0001 unclaimed as the fee. Or it can spend the two smaller outputs together, 0.016 in all, pay the 0.015, keep 0.0008 in change and leave 0.0002 as the fee, more than before because two inputs take more space than one. Which it does is called coin selection, and it is the wallet’s decision, not the network’s.

The choice has effects beyond the fee. The developer guide’s section on merge avoidance describes a crude strategy of paying with the smallest single output larger than the bill, because spending outputs together lets each earlier payer see the others. The whitepaper says as much in its section on privacy: multi-input transactions “necessarily reveal that their inputs were owned by the same owner”. How analysts build on that, and how they guess which output is the change, is set out in what the chain shows and what KYC adds.

Nothing in a transaction labels an output as change. The developer guide calls change outputs “regular outputs” and recommends sending them to a new address rather than back to the one spent from, which the block 170 transaction did not do. Wallets built on BIP-44 keep a separate internal chain of addresses that the standard says is “used for return transaction change”. On a block explorer the outputs simply sit side by side, which is why a payment of 0.015 can look like a movement of 0.05; how to read a block explorer walks through that view.

Scripts decide who can spend

The locking script is a small program, and a handful of standard templates cover most outputs. An address is an encoding of one of those templates with its data filled in, so sending to an address means creating an output with that script. Four matter in practice.

Pay to public key hash (P2PKH). The script holds a hash of a public key, and spending requires the full key and a signature from the matching private key. These addresses use Base58Check with a version byte of zero, which the address conversion procedure encodes as a leading 1.

Pay to script hash (P2SH). Introduced by BIP-16, the output holds a hash of a script, and the spender reveals the script and satisfies it. The point, in BIP-16’s words, was to move the responsibility for the redeeming conditions “from the sender of the funds to the redeemer”, so a sender can fund a script of any complexity with a 20-byte hash. BIP-13 chose the version byte so that these addresses begin with 3. Multi-signature arrangements are the usual case, and how multisig wallets work covers them.

Pay to witness public key hash (P2WPKH). BIP-141’s SegWit form of P2PKH: a version 0 witness program holding a 20-byte key hash, with the signature and public key moved out of the input’s script and into the witness. Its addresses use the bech32 format of BIP-173, whose mainnet examples begin bc1q.

Pay to Taproot (P2TR). BIP-341 defines a SegWit version 1 output whose program is a 32-byte public key. It can be spent with one signature for that key or by revealing one script from a tree committed to inside the key, and as long as the key is used, nobody learns whether a script path existed. The signatures are the 64-byte Schnorr signatures of BIP-340, and the addresses use the bech32m variant from BIP-350, whose examples begin bc1p.

The block 170 transaction uses none of the four. Its outputs use pay to public key, in which the script holds the public key itself, and mempool.space labels them that way.

What a signature commits to

The signature in each input is what makes a transfer the key holder’s act. In the common case, the developer guide says, “the entire transaction is signed except for any signature scripts”: which earlier outputs are being spent, and every new output with its amount and script. Alter any of that afterwards and the signature fails. Variants called signature hash types let a signer cover less, such as only their own input; the default, SIGHASH_ALL, covers all the inputs and outputs.

The original design left one gap, and it concerns the fee. Because an input does not state its amount, a legacy signature did not cover it. BIP-143, the signing rules for SegWit version 0, explains why that mattered: an offline signing device could not work out the amount being spent, or the fee, without also being given the full transaction it was spending from. BIP-143 added the spent amount to what is signed. Taproot’s rules by default commit to the amounts of every input, which, in BIP-341’s words, “eliminates the possibility to lie to offline signing devices about the fee of a transaction”. Those devices are the subject of how hardware wallets work.

The fee is what the outputs leave

There is no fee field. The whitepaper defines the fee by subtraction: “If the output value of a transaction is less than its input value, the difference is a transaction fee”. Outputs worth more than the inputs make a transaction invalid, rejected by Bitcoin Core as bad-txns-in-belowout. Inputs worth more than the outputs leave the difference for the miner who includes the transaction, collected in the block’s first transaction alongside the newly issued coins.

Two consequences follow. A wallet that forgets the change output pays the whole remainder to the miner, since, in the developer guide’s words, “the full value of the included UTXOs must be spent or given to a miner as a transaction fee”. And the fee is priced by space, not by the amount moved. BIP-141 sets a transaction’s weight at its size without witness data times three, plus its full size; a block may hold at most 4,000,000 weight units, and a transaction’s virtual size is its weight divided by four, so a witness byte counts for a quarter of any other. Bitcoin Core’s wallet takes fee rates in satoshis per virtual byte. A small payment assembled from ten small inputs can therefore cost more than a large payment from one.

The developer guide describes block assembly as filling space in order of fee per byte, so a transaction that paid too little for the moment waits. What a sender can do then, by replacing it or by spending its change with a higher fee, is covered in stuck crypto transactions.

Confirmations, and what they measure

A signed transaction is passed from node to node, and each one checks it and holds it in its memory pool, which the developer guide on the peer-to-peer network describes as the place unconfirmed transactions wait, since they have no permanent status. Each node keeps its own pool, and a transaction that is never mined does not wait for ever: nodes purge some transactions to make room for others, the guide says, and Bitcoin Core by default drops one after 336 hours, two weeks. Once a miner includes it in a block it has one confirmation, and each block built on top adds another.

A confirmation count measures how much work an attacker would have to redo to replace the transaction. The whitepaper’s section 11 models an attacker trying to catch up from behind. With 30% of the hashing power, its table gives about an 18% chance of ever catching up from five blocks back and about 4% from ten, and the attacker must be 24 blocks behind before the chance drops below 0.1%. The developer guide’s suggestion of six confirmations for high-value payments is, by its own description, “somewhat arbitrary”, and it is advice to the software receiving a payment, not a rule the network enforces. Newly issued coins wait by rule: a block reward’s output cannot be spent for 100 blocks, which the developer guide explains as protection against the block being replaced in a fork.

A balance is the wallet’s sum

Since the chain holds outputs and not balances, a wallet computes its balance. One built on a recovery phrase derives a sequence of addresses, looks on the chain for unspent outputs locked to any of them, and adds them up. BIP-44 tells it when to stop looking: after 20 unused addresses in a row, the software “expects there are no used addresses beyond this point”. Address types matter too. BIP-84 and BIP-86 define separate derivation schemes for P2WPKH and Taproot keys, so software that looks only for one type can restore a phrase and show nothing while the coins sit under the other. What a seed phrase controls, compared with a private key explains the derivation that sits under all of this.

Two practical points follow from the model. An explorer’s address page shows the outputs at one address, which is a fragment of a wallet and not the wallet. And the “sent” figure in a wallet’s history is the wallet’s own netting, the inputs it owned minus the change that came back. The transaction records only outputs consumed and outputs created; which of them was a payment, and to whom, is an interpretation laid over it.