Chapter 5. Transactions

Transactions are at the heart of Bitcoin. Transactions, simply put, are value transfers from one entity to another. We’ll see in Chapter 6 how “entities” in this case are really smart contracts—but we’re getting ahead of ourselves. Let’s first look at what transactions in Bitcoin are, what they look like, and how they are parsed.

Transaction Components

At a high level, a transaction really only has four components. They are:

  1. Version

  2. Inputs

  3. Outputs

  4. Locktime

A general overview of these fields might be helpful. The version indicates what additional features the transaction uses, inputs define what bitcoins are being spent, outputs define where the bitcoins are going, and locktime defines when this transaction starts being valid. We’ll go through each component in depth.

Figure 5-1 shows a hexadecimal dump of a typical transaction that shows which parts are which.

Transaction Version Inputs Outputs and Locktime
Figure 5-1. Transaction components: version, inputs, outputs, and locktime

The differently highlighted parts represent the version, inputs, outputs, and locktime, respectively.

With this in mind, we can start constructing the transaction class, which we’ll call Tx:

class Tx:

    def __init__(self, version, tx_ins, tx_outs, locktime, testnet=False):
        self.version = version
        self.tx_ins = tx_ins  
        self.tx_outs = tx_outs
        self.locktime = locktime
        self.testnet = testnet  

    def __repr__(self ...

Get Programming Bitcoin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.