TeachMeBitcoin

Byte Serialization in Raw Transactions

From TeachMeBitcoin, the free encyclopedia Reading time: 2 min

Byte Serialization in Raw Transactions

A "Raw Transaction" is just a long string of hexadecimal characters. To a node, this string isn't just text; it's a sequence of tightly packed bytes that follow a strict schedule.

1. Fixed-Width Fields

Many fields in a Bitcoin transaction have a fixed size.

2. Variable-Width Fields (VarInt)

What about fields that change size, like a message or a list of inputs?

3. String Serialization

When Bitcoin stores a string (like a message or an address HRP), it doesn't use "Null Terminators" like the C programming language.

4. Padding

In certain cryptographic structures (like DER Signatures), padding is used to ensure that the data fits specific mathematical requirements.

5. Hex to Byte Converters

When you copy a hex string into a tool, the tool performs a "Decode":

  1. Verify the length is even (2 hex chars per byte).

  2. Map each pair to a decimal value (0–255).

  3. Write that byte to a buffer.

Field Size Type
nVersion 4 Bytes Fixed (LE)
Vin Count 1-9 Bytes VarInt
Output Value 8 Bytes Fixed (LE)

In the final section, we will build a Python Hex Auditor.

☕ Help support TeachMeBitcoin

TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation:

Ethereum: 0x578417C51783663D8A6A811B3544E1f779D39A85
Bitcoin: bc1q77k9e95rn669kpzyjr8ke9w95zhk7pa5s63qzz
Solana: 4ycT2ayqeMucixj3wS8Ay8Tq9NRDYRPKYbj3UGESyQ4J
Address copied to clipboard!