Serialization Flip-Flop
Serialization Flip-Flop
To master Bitcoin development, you must memorize which fields are flipped and which are not. Failing to reverse the bytes in the right place will result in invalid transactions or "Transaction Not Found" errors.
1. The "Flipped" List (Little Endian)
These values must be reversed when moving between a human-readable hex string and raw binary:
-
Transaction Version: (e.g.,
01 00 00 00for version 1). -
Previous TXID: The pointer to the coin you are spending.
-
Output Value: The amount in Satoshis.
-
Locktime: The time or block height restriction.
-
Block Hash: The identifier for a block.
2. The "Natural" List (Big Endian)
These values are copied "As-Is" and are not reversed:
-
Public Keys: (e.g.,
02...or03...). -
Signatures: (e.g., DER encoded
30...). -
Script OpCodes: (e.g.,
OP_DUPis76). -
P2P Magic Bytes: (e.g.,
F9 BE B4 D9).
3. The "Block ID" Trap
In Bitcoin, the "Block ID" is the Block Hash.
-
If you calculate the SHA-256 hash of a block header, you get a 32-byte result.
-
If you want to print that hash to the screen, you must reverse the bytes first.
-
If you don't reverse them, your "Block ID" will be backward, and no explorer will recognize it.
4. Scripts and Endianness
Bitcoin Script has its own rules.
-
When you put a number on the stack (like
OP_1), it is stored as a "Little Endian" signed integer. -
However, the
OP_CODEitself is a single byte, so endianness doesn't apply to it.
5. Identifying the Flip
A quick way to tell if a hex string is Little Endian:
-
Look at the trailing zeros.
-
If the number is small (like 100), and you see
64 00 00 00, it is Little Endian. -
If you see
00 00 00 64, it is Big Endian.
| Object | Part of Object | Endianness |
|---|---|---|
| Transaction | Input Sequence | Little |
| Transaction | Unlocking Script | Big (Natural) |
| Block Header | Merkle Root | Little |
In the final section, we will build a Python Endianness Auditor.
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: