Constructing Transaction in Bitcoin
Transactions are a key feature of the Bitcoin blockchain network. In this article, we learn about transaction inputs, and outputs, and how they are added to the distributed ledger.
Table of contents.
- Introduction.
- Transaction Inputs.
- Transaction Outputs.
- Appending to the distributed ledger.
- Summary.
- References.
Prerequisites
Introduction
In Bitcoin, a wallet is an interface used by Bitcoiners to interact with the blockchain. Note that a wallet only stores keys, not actual funds. Keys are the ones that give access to funds. On Ethereum, accounts are a means to interact with the blockchain. These wallets and accounts have been programmed with logic that is used to choose the inputs and outputs of any transaction on the blockchain.
Note that, the main purpose of Bitcoin is the exchange of value. In this article, we will examine how transactions are constructed in Bitcoin. A transaction involves the exchange of value from person A to person B. In Bitcoin transactions are all recorded separately, this means when A sends B amount X, amount X will not be automatically added to B's amount, instead, it will be recorded as a separate transaction. For B to sum up all account digits, a transaction needs to be initiated, a different one. This means more work for miners who are responsible for processing the transaction. This also means more energy is needed to join the two transactions into one. This is both good and bad. Good because we can track every and all transaction that ever happened since Bitcoin's conception in '09 and bad since it consumes more energy.
A transaction in Bitcoin operates with inputs and produces outputs which are inputs to the next transaction. This mechanism as mentioned creates an immutable chain. Hashing and cryptography are implemented to secure the chain.
The following image summarizes this concept;
UTXOs
UTXO stands for Unspent Transaction Outputs, this is an amount of Bitcoins that have been authorized for use by the owner that is yet to be unlocked by an input. Consider the case where sender A sends B 50 Bitcoins as payment for goods worth 20 Bitcoins(plus transaction fee). In this case, the remaining UTXOs are 30 Bitcoins. They are returned to the sender as the change from the transaction. After this transaction, the UTXOs are placed back into the blockchain and can be used for later transactions by A.
Note that a UTXO can be considered as the change from a transaction, however, in this case, it is not of a lower denomination instead it is a transaction output stored in the distributed database that is generated by the blockchain to give room for non-exact change transactions and is used as input for subsequent transactions.
Transaction inputs.
Now, A wants to send amount X to B. For this transaction to execute, it finds inputs, these inputs are what determine what A can send to B. Inputs consist of an account balance that needs to be greater than amount X plus the transaction fees that should meet the set threshold. Inputs come from previously executed transactions between A and another party C. A wallet can come up with transaction inputs and at the same time verify incoming transactions to make sure they have the correct inputs.
What about the event when a wallet does not have a copy of UTXOs? In this case, since the records are distributed to all nodes of the network, the wallet queries this information from the network with the use of an API(Application Programming Interface). This query asks the full node for the information. This is because a wallet is just an app, it cannot be able to store the whole copy of a blockchain without the need for significant computing resources such as persistent storage and RAM. A full node usually maintains a full copy of the blockchain.
Components of a Bitcoin transaction input.
- A pointer to a UTXO that references the transaction hash and output index that identifies the UTXO in the transaction.
- An unlocking script(digital signature) constructed by the wallet to satisfy spending conditions specified in the UTXO.
- A sequence number.
Transaction outputs.
All transactions on the bitcoin network generate outputs that are later recorded in the distributed immutable ledger.
All outputs create spendable bitcoins referred to as UTXOs that are recognized by all network participants meaning the recipient can spend the bitcoins. UTXOs are stored by all bitcoin nodes.
A transaction output is created in the form of a script(locking script) that makes sure that only another script(unlocking script) can redeem the locked value. Meaning that the amount sent can only be spent by a person with a digital signature from a key that corresponds to the recipient's public key.
Components of a Bitcoin transaction output
- The amount in bitcoin - this is the amount intended to be transacted.
- Cryptographic puzzle specified by the network - miners find a nonce that solves the puzzle using powerful computational resources. Miners process, validate, and verify all Bitcoin transactions.
Appending to the distributed ledger.
The transaction has all details that can be used to confirm ownership. How is it then added to the distributed ledger?
A block in the bitcoin blockchain is used to store all transactions in a database that is consistent with all other databases in a node. At this point, the transaction is propagated across the whole network whereby every peer/node validates it. If validation fails at the first node, the transaction is not propagated thereby saving network bandwidth and time.
In the case of the Ethereum blockchain that goes beyond value exchange, the smart contract code is propagated and executed by all miners on their nodes.
Bitcoin uses a technique referred to as flooding - this is whereby a transaction is propagated to all nodes of the network. When the transaction arrives at a network node, the node broadcasts it to all its neighboring peer nodes. Once a node sees a transaction, the transaction can no longer be validated again, all subsequent transactions similar to the validated one are blocked and the node that propagates them can no longer participate in the blockchain. This situation is referred to as double-spending whereby bitcoins are spent more than once, in this case, two transactions referencing the same digital asset.
Summary
When participating in a bitcoin blockchain network, all transactions ever made are stored on the distributed ledger, when a participant wants to initiate a transaction, his/her previously executed transactions are used as inputs to the impending transaction.
The payer is provided with transaction outputs that reference his/her bitcoins.
For a transaction to be generated the wallet chooses from a UTXO it controls.
Inputs reference previous transaction's outputs. However, the output from the previous transaction has to be an unspent bitcoin - UTXO.
Transaction confirmations ensure that a transaction has been validated by network nodes. With this article at OpenGenus, you must have the complete idea of Constructing Transaction in Bitcoin.