No history yet

Raw Transaction Anatomy

From Concept to Code

When you send Bitcoin, you aren't just telling the network to move digital money. You're creating a precise, structured data message. This message, before it gets its cryptographic signature, is known as a raw transaction. It’s a string of hexadecimal characters that contains all the essential instructions for the transfer.

Think of it as a highly specific order form. Every field must be filled out correctly and in the right sequence for the network to accept it. This process of converting the transaction details into this specific byte sequence is called serialization. The Bitcoin protocol demands this strict format so that every node on the network can interpret the transaction in exactly the same way, leaving no room for ambiguity.

Lesson image

Let's break down the components of this data structure. It's not a random jumble of letters and numbers; it's a carefully assembled package of information. Each part has a specific job.

The Core Components

Every raw Bitcoin transaction is built from a few key pieces of data. While it might look intimidating at first, the structure is quite logical. The main components are arranged in a specific order.

FieldSize (Bytes)Description
Version4Specifies the transaction version, indicating which rules to follow.
Marker & Flag2 (optional)Indicates a SegWit transaction if present.
Input Count1-9A variable-length integer specifying the number of inputs.
InputsVariableA list of all the transaction inputs.
Output Count1-9A variable-length integer specifying the number of outputs.
OutputsVariableA list of all the transaction outputs.
Locktime4A timestamp or block number specifying the earliest time the transaction can be mined.

One important detail is the byte order. Bitcoin uses [{]}, which means the least significant byte is stored first. For a 4-byte number like the version, 0x02000000 in raw data actually represents the number 2.

The raw format ensures that every node, anywhere in the world, can parse and validate the transaction without confusion.

Version and Locktime

The first field in a transaction is the version number. This 4-byte field tells nodes which set of consensus rules to apply. The original version was 1. Version 2, introduced later, enabled features like CheckSequenceVerify, which allows for more complex smart contracts.

Here's how version 2 looks in little-endian hex:

02000000

At the very end of the transaction, we find the locktime field, also 4 bytes. This is a powerful feature that acts as an embargo, telling miners not to include the transaction in a block until a certain condition is met. The network interprets this field differently depending on its value.

  • If the value is less than 500 million, it's treated as a block height. The transaction is invalid until the blockchain reaches that block number.
  • If the value is 500 million or greater, it's treated as a [{]}. The transaction is invalid until that point in time.

A locktime of 0 means the transaction can be included in the next block immediately. Most transactions use this default value.

Counting Inputs and Outputs

After the version field (and optional SegWit marker), the transaction lists its inputs and outputs. But first, it must declare how many of each there are. This is done using a special format called a compact size unsigned integer, or CompactSize.

This format is a variable-length integer that can use 1, 3, 5, or 9 bytes depending on the number it needs to represent. This saves space, as most transactions have few inputs and outputs and can use just a single byte for the counter.

ValueFirst ByteLength (Bytes)Format
0 to 2520x00 - 0xfc1The value itself
253 to 65,5350xfd30xfd followed by the number as a 2-byte little-endian integer
65,536 to 4,294,967,2950xfe50xfe followed by the number as a 4-byte little-endian integer
> 4,294,967,2950xff90xff followed by the number as an 8-byte little-endian integer

For example, a transaction with one input and two outputs would have an input count of 01 and an output count of 02. These counters are then followed by the actual input and output data structures, which have their own detailed anatomy we'll explore next.

This structured, compact format is what allows the Bitcoin network to process transactions efficiently and consistently, forming the foundation of its global ledger.

Ready to test your knowledge?

Quiz Questions 1/5

What is the primary purpose of serializing a Bitcoin transaction?

Quiz Questions 2/5

In a raw transaction, the version field is represented in little-endian format as 02000000. What does this mean?