THAMER
Sovereign Bitcoin Wallet

WALLET AI BITCOIN

Your Bitcoin Wallet. Your Keys. Your Engine.

Self-hosted Bitcoin software built for developers, applications, and AI agents.

CREATE WALLET

Bitcoin Transaction Serialization Explained: Inputs, Outputs, and Byte Order

permalink THAMER
#bitcoin-transaction-serialization#compactsize#little-endian#raw-transaction

A Bitcoin transaction is a structured sequence of bytes, not a JSON object. Wallets, nodes, and block explorers often display a decoded representation, but transaction identifiers and consensus validation operate on the underlying serialization.

Legacy transaction layout

The non-witness encoding consists of a four-byte version, a CompactSize input count, the inputs, a CompactSize output count, the outputs, and a four-byte nLockTime. Multi-byte integer fields such as version, output values, sequence, and locktime use little-endian encoding. The 32-byte previous-transaction hash in an outpoint is serialized in internal byte order, which often appears reversed relative to human-readable TXIDs.

Each input contains a 32-byte previous transaction hash, a four-byte previous output index, a CompactSize scriptSig length, the scriptSig bytes, and a four-byte sequence. A coinbase transaction has a special input outpoint and is not an ordinary spend of a previous UTXO.

Each output contains an eight-byte value in satoshis, a CompactSize scriptPubKey length, and the scriptPubKey bytes. The output index is determined by its position in the transaction, starting at zero.

CompactSize is not a normal fixed-width integer

Counts and script lengths use CompactSize: values below 253 occupy one byte; larger values use a prefix followed by a two-, four-, or eight-byte little-endian integer. It is distinct from the signed script-number encoding used by Bitcoin Script.

Witness serialization

For a transaction using witness serialization, a marker byte of 00 and a nonzero flag (currently 01 for the defined format) follow the version. Inputs and outputs follow, then witness fields for every input, and finally nLockTime. Each witness is a stack encoded with CompactSize counts and item lengths; an input with no witness still has an empty witness field.

Do not infer witness presence solely from the destination address type. The spending input and its serialization determine whether witness data exists.

Implementation checks

Parse with explicit bounds, reject malformed CompactSize encodings where required by the applicable rules, and avoid mixing display-order TXIDs with wire-order hashes. Never assume a decoded JSON field can be concatenated to reconstruct the exact serialized bytes.

Primary references

Bitcoin Developer Reference — Transactions: https://developer.bitcoin.org/reference/transactions.html
BIP141 — Segregated Witness: https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki
BIP144 — Segregated Witness Peer Services: https://github.com/bitcoin/bips/blob/master/bip-0144.mediawiki

Questions & Answers

Explore all questions →

Latest from the Lab

THAMER illustrated developer portrait and signature

ABOUT THE DEVELOPER

Independent Software Developer specializing in Bitcoin Script, Golang, and AI Agents.

Software engineering · Independent research · Open-source projects