Bitcoin Transactions and their role in the bigger pictureBitcoin is comprised of a few major pieces: nodes and a blockchain. The role of a typical node is to maintain its own blockchain version and update it once it hears of a “better” (longer) version. Simply put, the blockchain has blocks, and blocks have transactions. With this simplified but accurate picture in mind, you might be wondering what exactly a transaction is made out of.
- How will understanding transactions help me to become a better blockchain developer?
- How do transactions allow me to transfer some bitcoin to a friend?
A deeper look into Bitcoin transactionsLet’s understand the mechanics of a real bitcoin transaction. We’ll use the image above as a reference. If you were to cut open a typical bitcoin transaction, you’d end up with three major pieces: the header, the input(s), and the output(s). Let’s briefly look at the fields available to us in these sections, as they’ll be important for discussion. Note that these are the fields that are in a so-called raw transaction. Raw transactions are broadcast between peers when a transaction is created. The Header
- hash: The hash over this entire transaction. Bitcoin generally uses hash values both a pointer and a means to check the integrity of a piece of data. We’ll look at this more in the next section.
- ver: The version number that should be used to verify this block. The latest version was introduced in a soft fork that became active in December 2015.
- vin_sz: The number of inputs to this transaction. Similarly, vout_sz counts the number of outputs.
- lock_time: We’ll look at this more in later articles, but this basically describes the earliest time at which a block can be added to the blockchain. It is either the block height or a unix timestamp.
- previous output hash: This is a hash pointer to a previously unspent transaction output (UTXO). Essentially, this is money that belongs to you that you are about to spend in this transaction.
- n: An index into the list of outputs of the previous transaction. This is the actual output that you are spending.
- scriptSig: This is a spending script that proves that the creator of this transaction has permission to spend the money referenced by 1. and 2.
- value: The amount of Satoshi being spent (1 BTC = 100,000,000 Satoshi).
- scriptPubKey: The second of two scripts provided in a bitcoin transaction, which points to a recipient’s hashed public key. More on this in the last section of this article.
Transaction verificationOne of the jobs of a bitcoin node is the verify that incoming transactions are correct (data hasn’t been tampered with, money isn’t being created, only intended recipients spend UTXOs, etc). A more exhaustive list can be found online, but I’ll list out a few of the important ones here:
- All outputs claimed by inputs of this transaction are in the UTXO pool. Unspent outputs can only ever be claimed once.
- The signatures on each input are valid. More precisely, we’re saying that the combined scripts return true after executing them one after the other. More on this in the last section.
- No UTXO is spent more than once by this transaction. Notice how this is different than the first item.
- All of the transaction’s output values are non-negative.
- The sum of this transaction’s input values is greater than the sum of its output values. Note that if the numbers are different, the difference is considered to be a transaction fee that can be claimed by the miner.