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:
-
Version
-
Inputs
-
Outputs
-
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.
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:
classTx:def__init__(self,version,tx_ins,tx_outs,locktime,testnet=False):self.version=versionself.tx_ins=tx_insself.tx_outs=tx_outsself.locktime=locktimeself.testnet=testnetdef__repr__(self ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access