How a Bitcoin Transaction Works – This article explains what a Bitcoin transaction is, its purpose and outcome. The explanation made below is suitable for both novice and intermediate Bitcoin users.
As a cryptocurrency user you need to be familiar with transaction rudiments – for the sake of your own confidence with this evolving innovation, and as a foundation for understanding emerging multi-signature transactions and contracts, both of which will be explored later in the series. This is not a technical article and explanation will focus on what you need to know about standard bitcoin transactions – the spend transactions we commonly make – and we’ll gloss over what you can safely ignore.
An infographic at the bottom of the article provides a comprehensive illustration of the entire Bitcoin transaction process from wallet to blockchain.
Note: Even the Core developers acknowledge that some of the language being used to describe transactions and their components can lead one to a mistaken concept of what is really happening. These misconceptions are avoided in the explanation below. So, while trying to keep things as simple as possible, and with the aid of a few diagrams, let’s dive right in.
Definition of Terms and Abbreviations
Bitcoin with a capital “B” refers to the protocol – the code, the nodes, the network and their peer-to-peer interaction.
bitcoin with a lowercase ‘b’ refers to the currency – the cryptocurrency we send and receive, via the Bitcoin network.
tx – wherever it is used in the text – is an abbreviation for ‘Bitcoin transaction‘
txid is an abbreviation for ‘transaction id’ – this is a hash that is used by both humans and the protocol to reference transactions.
Script is the name of the Bitcoin protocol’s scripting system that processes and validates transactions. Script is a clever, stack-based instruction engine, and it makes all transactions from simple payments to complex oracle overseen contracts possible.
UTXO is an abbreviation for Unspent Transaction Output, also referred to as an “output”.
satoshi – 1 BTC = 100,000,000 satoshi
What is a Bitcoin Transaction and Why?
A Bitcoin transaction is a signed piece of data that is broadcast to the network and, if valid, ends up in a block in the blockchain.
The purpose of a Bitcoin transaction is to transfer ownership of an amount of Bitcoin to a Bitcoin address.
When you send Bitcoin, a single data structure, namely a Bitcoin transaction, is created by your wallet client and then broadcast to the network. Bitcoin nodes on the network will relay and rebroadcast the transaction, and if the transaction is valid, nodes will include it in the block they are mining. Usually, within 10-20mins, the transaction will be included, along with other transactions, in a block in the blockchain. At this point the receiver is able to see the transaction amount in their wallet.
Here is an example transaction that was included in the blockchain earlier this year:
The main components of this standard transaction are color-coded:
- Transaction ID (highlighted in yellow)
- Descriptors and meta-data (blue curly brace elaborated upon to the right)
- Inputs (pink area)
- Outputs (green area)
Bitcoin Transaction Inputs and Outputs
Firstly, four axiomatic truths about transactions:
- Any Bitcoin amount that we send is always sent to an address.
- Any Bitcoin amount we receive is locked to the receiving address – which is (usually) associated with our wallet.
- Any time we spend Bitcoin, the amount we spend will always come from funds previously received and currently present in our wallet.
- Addresses receive Bitcoin, but they do not send Bitcoin – Bitcoin is sent from a wallet.
The amounts that go into our wallet are not jumbled like the coins in a physical wallet. The received amounts don’t mix but remain separate and distinct as the exact amounts received by the wallet. Here’s an illustration:
You create a brand new wallet and, in time, it receives three amounts of 0.01, 0.2 and 3 BTC as follows: you send 3 BTC to an address associated with the wallet and two payments are made to another address by Alice.
The wallet reports a balance of 3.21 BTC, yet if you were to virtually peek inside the wallet, you would see – not 321,000,000 satoshi (321 mil satoshi) – but three distinct amounts still grouped together by their originating transactions: 0.01, 0.2 and 3 BTC.
The received bitcoin amounts don’t mix but remain separated as the exact amounts sent to the wallet. The three amounts in the example above are called the outputs of their originating transactions.
Bitcoin wallets always keep outputs separate and distinct.
An output is an amount that was sent (via a standard transaction) to a Bitcoin address, along with a set of rules to unlock the output amount. In Bitcoin parlance an output is called an “unspent transaction output”, or UTXO.
A standard transaction output can be unlocked with the private key associated with the receiving address. Addresses and their associated public/private key pairs will be covered later in the series. For now, we are concerned with the output amount only.
Let’s consider an example by following the money in a scenario where you send 0.15 BTC to Bob.
As we have seen, your wallet does not select 15 mil satoshi (0.15 BTC) from an undifferentiated pool of 321 mil satoshi making up the wallet balance. Instead, the wallet selects a spend candidate from amongst the three existing “outputs” contained in the wallet. So, it chooses (for various reasons that are not important now) the 0.2 BTC output. The wallet will unlock the 0.2 BTC output and use the whole amount of 0.2 BTC as an input to your new 0.15 BTC transaction. The 0.2 BTC output is “spent” in the process. –Read this paragraph a second time.
The spend transaction your wallet creates will send 0.15 BTC to Bob’s address – where it will reside in his wallet as an output – waiting eventually to be spent.
The 0.05 BTC difference (0.2 BTC input minus 0.15 BTC output) is called “change” and the transaction will send this back to your wallet via a newly created address. The 0.05 BTC change amount will reside in your wallet as a new output – waiting eventually to be spent. So, now, a virtual peek inside your wallet reveals the following:
Each of the three outputs that are “waiting to be spent”, is locked to its receiving addresses until such time as one or more of them are selected as input(s) to a new spend transaction.
Behind the scenes, different wallet clients apply different logic rules when selecting UTXOs as inputs to new transactions. A sane wallet policy is to use older UTXOs first, wherever possible, but implementations differ. The manner in which UTXOs are selected is not of concern to us right now, since the objective has been emphasis of the point that amounts received to our wallets remain separate and distinct.
Summary of How a Bitcoin Transaction Works
Various received amounts don’t mix as they do in a physical wallet. Instead, received amounts (UTXOs) are used individually (or in combination) at the moment we spend Bitcoin. When creating the spend transaction our wallet selects UTXOs (of sufficient value to satisfy the amount we want to send) and typically creates two new outputs: one for the receiver and one for the change we receive back to our wallet. The change becomes a brand new UTXO in our wallet, and the amount we send becomes a UTXO locked to the recipient address – which may or may not be associated with a wallet, e.g. cold storage. The original UTXO used as input to the spend transaction is “spent” and destroyed forever.
This has been an introduction to how outputs (UTXOs) are handled by wallet software. Once a UTXO is selected for expenditure, it requires the private key associated with the address that received it. This private key redeems the UTXO and allows it to become an input in a new spend transaction. The mechanism whereby previous transaction outputs are reused as the inputs to new transactions is central to the Bitcoin protocol’s function – and exactly as per Satoshi’s design.