A Bitcoin transaction is a structured set of data that moves value by unlocking existing bitcoin outputs and creating new, locked ones. This process forms the backbone of Bitcoin's transfer mechanism, enabling peer-to-peer value exchange without intermediaries.
What Constitutes a Bitcoin Transaction?
At its core, every Bitcoin transaction performs two fundamental actions:
- Selects existing outputs (often called "batches" of bitcoin) and unlocks them using cryptographic proof
- Creates new outputs and places cryptographic locks on them
Transactions can contain multiple inputs and outputs, making Bitcoin's system remarkably flexible for various transaction types. Think of transactions as links in a chain: one transaction creates an output, which then becomes the input for a future transaction when unlocked.
The locking and unlocking mechanism relies on public-key cryptography. Outputs typically contain a public key, and unlocking them requires the corresponding private key to create a valid digital signature. This system wasn't invented by Bitcoin but was brilliantly adapted to create a decentralized ownership and transfer system.
The Structure of a Raw Bitcoin Transaction
Raw Bitcoin transactions consist of specific fields, each containing bytes of data arranged in a precise structure. Understanding this structure reveals how Bitcoin transactions function at the protocol level.
Version Field
- Size: 4 bytes
- Format: Little-endian integer
- Purpose: Enables new transaction features
- Common versions: Version 1 (basic transactions) and Version 2 (enables relative locktime via BIP 68)
Marker and Flag Fields (Optional)
- Size: 2 bytes total
- Purpose: Indicates segregated witness (segwit) transactions
- Marker: Always
00for segwit transactions - Flag: Typically
01for current segwit transactions
Input Count
- Size: Variable
- Format: Compact Size integer
- Purpose: Indicates how many inputs the transaction contains
Inputs Structure
Each input selects and unlocks a specific output from a previous transaction:
- TXID: 32 bytes in natural byte order (reverse of what blockchain explorers show)
- VOUT: 4-byte little-endian integer indicating which output from the referenced transaction
- ScriptSig Size: Variable compact size indicating unlocking code length
- ScriptSig: The actual unlocking code (legacy transactions)
- Sequence: 4-byte field enabling locktime, replace-by-fee, or relative locktime features
Output Count
- Size: Variable
- Format: Compact Size integer
- Purpose: Indicates how many outputs the transaction creates
Outputs Structure
Each output creates new locked bitcoin:
- Amount: 8-byte little-endian integer representing satoshis (1 satoshi = 0.00000001 BTC)
- ScriptPubKey Size: Variable compact size indicating locking code length
- ScriptPubKey: The actual locking code that secures the output
Witness Field (Optional)
- Size: Variable
- Purpose: Contains unlocking code for segwit transactions
- Structure: Begins with stack items count, followed by size-item pairs for each witness component
Locktime
- Size: 4 bytes
- Format: Little-endian integer
- Purpose: Sets time or block height after which transaction can be mined
- Interpretation: Values ≤ 499,999,999 = block height; values ≥ 500,000,000 = Unix time
👉 Explore advanced transaction tools
Common Transaction Types and Examples
Legacy Transactions (P2PKH)
These traditional transactions use ScriptSig fields for unlocking and typically involve larger data sizes due to signature requirements. They follow the basic structure without marker, flag, or witness fields.
Segwit Transactions
Segregated Witness transactions move unlocking data to the witness field, reducing transaction size and enabling scalability improvements. These transactions include the marker and flag fields and utilize the witness section.
Mixed Transactions
Some transactions combine both legacy and segwit inputs, requiring both ScriptSig fields for legacy inputs and witness fields for segwit inputs. Each input must have a corresponding witness field, even if empty.
Transaction Identification and Metrics
TXID Calculation
The Transaction ID is generated by double-SHA256 hashing the version, inputs, outputs, and locktime fields. This creates a unique identifier for referencing transactions.
wTXID for Segwit Transactions
Witness transactions have an additional identifier (wTXID) that includes the marker, flag, and witness fields in the hash calculation. This ensures all transaction data is properly accounted for.
Transaction Size Measurements
- Bytes: Raw byte count of the transaction
- Weight: Bytes × 4, except witness data which receives a discount
- Virtual Bytes (vBytes): Weight ÷ 4, the standard measurement for block space calculation
Transaction Fees and Economics
Transaction fees represent the difference between input values and output values. Miners collect these fees as incentive for including transactions in blocks. Proper fee calculation is essential, as incorrectly sized outputs can result in accidentally large fees.
👉 Get real-time fee estimation tools
Frequently Asked Questions
What exactly is a Bitcoin transaction?
A Bitcoin transaction is a digitally signed instruction that transfers value from one owner to another. It works by referencing previous transactions' outputs as inputs and creating new outputs with different locking conditions. The transaction is broadcast to the network and included in blocks through the mining process.
How do transaction fees work?
Fees are automatically calculated as the difference between the total input value and total output value. Users effectively set fees by how much value they leave unallocated to outputs. Miners prioritize transactions with higher fee rates (satoshis per vByte) when including them in blocks.
What's the difference between legacy and segwit transactions?
Legacy transactions place unlocking code in the ScriptSig field, while segwit transactions move this data to a separate witness field. This change reduces transaction size, lowers fees, and enables future protocol improvements. Segwit transactions are recognizable by their marker and flag bytes.
Can transactions be reversed or cancelled?
Once confirmed in a block, Bitcoin transactions are essentially irreversible. However, unconfirmed transactions can sometimes be replaced using Replace-by-Fee (RBF) if the original transaction included sequence numbers that enable this feature. This requires broadcasting a new transaction with higher fees.
What is transaction locktime?
Locktime allows senders to specify when a transaction becomes valid. It can be set to a future block height or timestamp, preventing miners from including it in blocks until the specified condition is met. This feature is useful for post-dating transactions or creating time-based contracts.
How do I calculate the appropriate fee for my transaction?
Fee calculation depends on current network congestion, transaction size (in vBytes), and desired confirmation speed. Various tools and wallets provide fee estimation based on these factors. Generally, more complex transactions (with multiple inputs/outputs) require higher fees due to their larger size.
The Significance of Transaction Structure
Understanding Bitcoin's transaction structure reveals the elegance of its design. As Satoshi Nakamoto described in the whitepaper, Bitcoin creates "a chain of digital signatures" where each owner transfers coins to the next by signing previous transaction information and the next owner's public key.
This structure ensures that:
- Ownership transfers are cryptographically verifiable
- The history of each bitcoin can be traced through the chain of signatures
- The system operates without centralized authority
- Transactions are tamper-evident and secure
The introduction of segwit transactions in 2017 added complexity but also significant improvements to Bitcoin's scalability and functionality. Despite these advancements, the core transaction structure remains consistent, maintaining backward compatibility while enabling innovation.
Whether you're examining simple legacy transactions or complex segwit transactions with multiple inputs and outputs, the fundamental principles remain the same: transactions move value by unlocking previous outputs and creating new ones with updated ownership conditions. This elegant system continues to secure billions of dollars in value transfers daily across the global Bitcoin network.