Bitcoin Transaction Inputs and Outputs.
In this article, we learn about Bitcoin transaction inputs, outputs, and how transactions are serialized on the Bitcoin blockchain.
As mentioned in previous articles, Bitcoin's main purpose is the storage and exchange of value between its participants. In a standard transaction, we have transaction inputs and outputs. An exception is the coinbase transaction which does not have input but produces an output which is a payment to a miner for mining a block.
The most important piece of a transaction is the transaction output. These are indivisible pieces of Bitcoin recorded on the distributed ledger and recognized by the whole network as valid and therefore can be spent. A Bitcoin full node is a node that stores a local copy of the entire blockchain, about 200GB worth of disk space. This includes transaction outputs that can be spent referred to as unspent transaction outputs(UXTOs). A collection of these is referred to as a UTXO set. This set grows as new unspent outputs are added to it as a result of change and shrinks as the unspent outputs are spent during transactions.
Take a case where A sends B amount X of Bitcoins. Here, B's wallet will detect UTXOs on the blockchain that can be spent by keys controlled by B's wallet. B's wallet balance is the sum of all UTXOs he/she can control with keys owned by his/her wallet. Wallets cannot store the entire local copy of a blockchain, therefore, will store a copy of the entire blockchain and use APIs to aggregate all UTXOs that can be controlled by the wallet's keys to show a balance.
Transaction Outputs.
Bitcoin transactions produce outputs that are stored on a distributed ledger and used in subsequent transactions as inputs. Outputs create bitcoins that can be spent by a user, these outputs are broadcasted to the entire network of nodes so that everyone recognizes that they belong to the specified user.
During a transaction whereby A sends B bitcoins, UTXOs are created, these are registered to Bs wallet address and can be used by B. A UTXO set is a collection of UTXOs, these are tracked by a network of distributed full nodes. Now if B wants to spend the received amount, he/she will spend one or more UTXOs from the UTXO set/pool.
Structure of a transaction output Transaction outputs consist of three major components, the amount(8 bytes) we wish to spend, the locking script size(1-9 bytes) that specifies the size of the locking script, and the locking script(variable) itself that specifies the conditions to be met for the output to be spent. In later articles, we will learn about the transaction scripting language which is used to write locking and unlocking scripts.
The following image shows the structure of a standard Bitcoin transaction output;
The following code utilizes the blockchain.info API to fetch UTXOs of a specific wallet address.
import requests
import json
address = input('Paste Wallet Address: ')
response = requests.get(f'https://blockchain.info/unspent?active={address}')
# utxo list
utxo_list = json.loads(response.text)["unspent_outputs"]
total = 0
for utxo in utxo_list:
print(f"BTC: {utxo['tx_hash'], utxo['tx_output_n'], float(utxo['value']) / 100000000}")
total += utxo['value'] + 0
print(f"Total Spendable BTC: {float(total) / 100000000}")
The resulting output is a list of transaction hashes and a UTXO's index number separated by a colon. We then have the value of the UTXO in satoshis.
For a random Bitcoin wallet address, this link is helpful.
Transaction Inputs.
In a transaction, inputs consist of UTXOs that are to be sent to the recipient. The recipient also needs to provide proof that he/she owns a UTXO, this is done using an unlocking script that satisfies the spending conditions by the UTXO. A digital signature is used to prove ownership of funds. First, a wallet selects from a UTXO it has keys for, the UTXO should also have enough funds for the transaction, which includes the required transaction fee for transaction validation.
The following image shows the structure of a standard Bitcoin transaction input;
Components of a transaction input The first component is a reference pointer to a UTXO to the transaction hash and a sequence number whereby the UTXO is recorded on the distributed ledger.
The second component is an unlocking script built by the wallet to satisfy spending conditions, in this case, the unlocking script is a digital signature, which is also used to prove ownership of funds.
A final component is a sequence number where the UTXo will be recorded to the blockchain.
The following is an example of a transaction input;
"vin": [
{
"txid": "0d6cbc21a1b1a3e5df1eae4fc37aa849174b6b727e53f9f505a605f066b7f000",
"vout": 0,
"scriptSig": {
"asm": "304402203f198d9695a293c41d2124e790d473e68f4b35a09ece5517c7adfffc797f91760220304feb1cc2829d3c937665bcb4b7f9735acbcac50e9169bc53c9e689ce20b3bc[ALL] 025743bce4e775bb754e784dd2cfdc8dabc2023154eee539bb155612101a583e7e",
"hex": "47304402203f198d9695a293c41d2124e790d473e68f4b35a09ece5517c7adfffc797f91760220304feb1cc2829d3c937665bcb4b7f9735acbcac50e9169bc53c9e689ce20b3bc0121025743bce4e775bb754e784dd2cfdc8dabc2023154eee539bb155612101a583e7e"
},
"sequence": 4294967294
}
]
Above we have the following fields; txid - this is a unique identification number for a specific transaction. We can view this transaction by searching it using this link vout - this is the output index that identifies a UTXO from a transaction that is referenced. scriptSig(Sigscript) - it contains all needed digital signatures and a script used to unlock a UTXO for spending. Sequence number - it is a number for allowing unconfirmed time-locked transactions that are to be updated before being finalized.
Information such as the amount that is being transferred and the locking script for spending the amount is retrieved by retrieving the underlying transaction. Also, the input value is not explicitly stated therefore we will use the UTXO to calculate the transaction fees.
For network nodes to be able to validate this transaction, they all have to retrieve the referenced UTXO. This means that during the construction of software built on top of the Bitcoin blockchain, to validate transactions, calculate the transaction fees, or retrieve the unlocking script, we first need to retrieve the referenced UTXO. This allows for the creation of context around a transaction which as we saw is not there.
To obtain other transaction information such as the transaction fee, the scripts, and other data, we use the Bitcoin core commands such as getrawtransaction, decoderawtransaction to obtain the following output.
"vout": [
{
"value": 0.10000000,
"scriptPubKey": "OP_DUP OP_HASH160
7f9b1a7fb68d60c536c2fd8aeaa53a8f3cc025a8 OP_EQUALVERIFY OP_CHECKSIG"
}
]
Above, we have a UTXO value of 0.10000000, BTC, and a locking script with a hash.
Serializing Transactions.
For transactions to travel through the blockchain, inputs are encoded in a byte stream. The byte stream consists of the following fields;
Transaction hash(32 bytes) - a pointer to the transaction with the UTXO to be spent.
Output index(4 bytes) - index number of UTXO to be spent.
Unlocking script size(1-9 bytes) - the size of the unlocking script.
Unlocking script(variable) - the unlocking script for the UTXO locking script.
Sequence Number(4 bytes) - a disabled transaction replacement feature. (it overrides a transaction before the lock time expires.)
The following is summarized with the image below;
Summary
The most important piece of a transaction is the transaction output. Inputs to a transaction point to a UTXO. Transaction outputs consist of three major components, the amount(8 bytes) we wish to spend, the locking script size(1-9 bytes) and the locking script(variable) itself. On the other hand, transaction inputs consist of a reference pointer to a UTXO, an unlocking script, and a sequence number
References
Mastering Bitcoin Chapter 6 - Andreas M. Antonopoulos