How Bitcoin Transactions Work: The Complete Guide

·

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:

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

Marker and Flag Fields (Optional)

Input Count

Inputs Structure

Each input selects and unlocks a specific output from a previous transaction:

Output Count

Outputs Structure

Each output creates new locked bitcoin:

Witness Field (Optional)

Locktime

👉 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

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:

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.