In this article, we learn about the structure of a Bitcoin Transaction and the transaction life cycle from initiation to completion.
Table of contents.
- Structure of a Bitcoin Transaction.
- The Transaction Life Cycle.
The main purpose of the Bitcoin blockchain is to enable participants to be able to exchange value between themselves, other features are meant to make sure that transactions are validated, secure, and immutable among others. Programatically we can think of a transaction as a data structure that has the exchange of value between two parties encoded in them.
In this article, we will learn about the Bitcoin transaction's inputs and outputs. Specifically transaction outputs are the foundation of transactions on the bitcoin blockchain. They consist of indivisible chunks of bitcoins stored on the distributed ledger managed by full nodes that are recognized by all network nodes as valid and spendable. These spendable outputs are referred to as UTXOs(Unspent Transaction Outputs).
During a transaction when person A sends person B some bitcoins, he/she encodes person B's wallet address in the transaction. When B receives the funds, it means that his/her wallet address has detected UTXOs that can be spent using one of the keys controlled by the wallet. Remember the purpose of a bitcoin wallet is to store keys, not the actual coins. After the transaction, the recipient's balance is the total amount of UTXOs the user's wallet keys have access to and can spend.
For a wallet to be able to calculate the owner's balance, it scans the entire blockchain and aggregates the value of the UTXOs that are controlled by keys owned by the wallet. They do this by maintaining a database that acts as a quick reference for all UTXOs that can be spent using keys it owns.
Transaction outputs have arbitrary values denominated as multiple satoshis. A satoshi is the lowest denomination of bitcoins, it is named after the creator of bitcoin. A satoshi is equivalent to 100 millionth of a bitcoin.
Transaction outputs are discrete and indivisible units of value denominated using satoshis, they can have an arbitrary value and once created cannot be divided. This means that an unspent output is consumed whole. For example, if we buy goods worth 3 bitcoins using 10 bitcoins, a UTXO worth 10 bitcoin is generated, the transaction consumes the whole 10 bitcoins and produces two transaction outputs, the first pays the seller 3 bitcoins and the second pays 7 bitcoins back to the buyer's wallet. This is not the case for the Etherium blockchain as we shall come to learn.
A UTXO can be compared to hard cash, assuming we have a $100 note, we cannot split it into two. We will use the whole note to purchase goods and obtain two outputs, funds to the seller, and change if the good's price is less than $100.
A transaction input consists of previously recorded UTXOs, after the transaction new outputs are created that will be used as inputs for future transactions.
What about the first transaction, this is the first transaction in a block, it has no inputs. It is created by a successful miner who solved a puzzle specified by the network for an incentive. Mining is a way to create new bitcoins, this incentive can be spent by the miner.
Structure of a Bitcoin Transaction.
A transaction encodes the transfer of value between Bitcoin participants. The source is the input and the destination of funds is the output.
A standard Bitcoin transaction has the following fields;
Version - these are rules a transaction is required to follow, the version is usually 4 bytes.
Marker - this is a 1-byte field indicating that a transaction uses SegWit. If present and a transaction uses SegWit it will have the value 0x00, otherwise, it remains null.
Flag - this field performs the same function as the marker field. If it uses SegWit, its value will be 0x01, otherwise null.
Input counter - the number of inputs included in a transaction, it is usually between 2 - 9 bytes.
Inputs - transaction inputs.
Transaction inputs are further broken down into four other fields, namely;
Transaction ID(TxID) - This is the ID or the hash of the previous transaction.
Transaction Index(Tx-Index) - This field specifies the UTXO to be spent in the input.
Unlocking Script Length - Specifies the size of the unlocking script.
Unlocking Script - An unlocking script to fulfill conditions specified by the locking script.
Sequence - Can be used to change a transaction before it is confirmed in a block, for example replacing an unconfirmed transaction with another one that pays a higher fee.
Output counter - the number of outputs included in a transaction, this is usually between 2 - 9 bytes.
Outputs - transaction outputs.
Transaction outputs are also further broken down into three other fields, namely;
Value - The value in Satoshis.
Locking script length - The size of the locking script.
Locking script - The locking script specifies conditions to be met for the value to be spent by the recipient.
Witness - The serialization of all witness data for SegWit transactions is stored here.
Lock time - this is the current UNIX timestamp until a transaction is locked. Usually, it is set to zero, it's validated after a block is finalized.
The following image summarizes the structure of a bitcoin transaction;
The Transaction Life Cycle.
A bitcoin transaction consists of three main components, these are inputs, outputs, and the amount that is intended to be transferred between parties.
The cycle starts with party A creating a transaction, inputs, outputs, and the amount are embedded in a transaction. Similar to a banker's cheque referencing an account as the source of funds, bitcoin transactions reference previous transactions(inputs). The next step involves the use of digital signatures to sign the transaction, this means that A authorizes B to spend the funds to be transferred. At this point the transaction is fully formed, it holds all the needed information to initiate an exchange of funds between parties.
The transaction is then broadcasted to the entire network which might take some time since the network is very large. Note that all transactions are publicly accessible and can be seen by everyone at blockchain.com/explorer. Broadcasting transactions might be considered unsafe in the case of credit cards and banks but in bitcoin, no sensitive data is embedded in a transaction, furthermore, ways exist to ensure anonymity.
Broadcasting transactions ensures that all nodes in the network recognize the amount X has changed ownership from A to B and can be spent by B.
A single miner on the blockchain is responsible for validating the transaction and adding it to his/her block if valid, then propagating it to other network nodes. If valid the transaction is successful otherwise it is rejected. The block is stored on the distributed ledger to ensure security and immutability.
At this point, B now owns the amount X and can spend it in a new transaction in a new cycle of ownership.
The bitcoin network is resistant to attacks since every node in the network validates a transaction before sending it to other nodes. Furthermore, invalid transactions are not propagated beyond a single node.
The following image summarizes a transaction life cycle on the bitcoin blockchain network;
Bitcoins are split into smaller units referred to as satoshis to facilitate small transactions on the blockchains.
Satoshis are the smallest unit of bitcoin, named after the creator of bitcoin.
Transaction outputs are discrete and indivisible units of value denominated using satoshis.
Bitcoin transactions can be propagated using insecure networks such as Bluetooth, WiFi, and radio satellites among others. This is because a transaction has no sensitive data and therefore no use to hackers.
With this article at OpenGenus, you must have the complete idea of Bitcoin Transactions and Life Cycle.