Bitcoin Transactions: A Comprehensive Guide

·

Introduction to Bitcoin Transactions

Bitcoin transactions (often abbreviated as "Tx") are fundamental components of the Bitcoin network. Blocks serve as containers that bundle these transactions together, secured by immense computational power that protects both individual transactions and the entire blockchain.

A transaction can be conceptually understood as a transfer of value. Figure 1 illustrates a simple transaction scenario where Alice sends Bitcoin to Bob. In this transaction:

The inclusion of Alice in the outputs occurs because transaction inputs and outputs must balance (excluding transaction fees). For instance, if Alice has a 10 BTC input but only wants to send 3 BTC to Bob, the transaction will include:

Transaction Structure Deep Dive

Transaction Inputs (vin)

Each input identifies which UTXO (Unspent Transaction Output) is being spent and provides proof of ownership through an unlocking script. Inputs contain four key elements:

  1. Transaction ID: References the transaction containing the UTXO
  2. Output Index (vout): Specifies which UTXO from that transaction
  3. Unlocking Script (scriptSig): Satisfies the UTXO's spending conditions
  4. Sequence Number: Used for transaction replacement

Example input structure:

{
 "txid": "7957a35fe64f80d234d76d83a2a8f1a0d8149a41d81de548f0a65a8a999f6f18",
 "vout": 0,
 "scriptSig": "3045022100884d142d...",
 "sequence": 4294967295
}

Transaction Outputs (vout)

Outputs create new UTXOs and contain:

  1. Amount in satoshis (smallest Bitcoin unit)
  2. Locking Script (scriptPubKey): Cryptographic puzzle specifying spending conditions

Example output structure:

{
 "value": 0.01500000,
 "scriptPubKey": "OP_DUP OP_HASH160 ab68025513c3dbd2f7b92a94e0581f5d50f654e7 OP_EQUALVERIFY OP_CHECKSIG"
}

UTXO Model Explained

The UTXO (Unspent Transaction Output) model is Bitcoin's accounting mechanism:

UTXO Example Scenario

Consider this sequence:

  1. Zhang mines 12.5 BTC (new UTXO created)
  2. Zhang spends 2.5 BTC to Li (12.5 BTC UTXO consumed; two new UTXOs created: 2.5 BTC to Li and 10 BTC change back)
  3. Zhang and Li jointly spend 5 BTC to Wang (combining their 2.5 BTC UTXOs)

👉 Learn more about UTXO mechanics

Transaction Scripts: Locking and Unlocking

Bitcoin uses two types of scripts for transaction validation:

  1. Locking Script (scriptPubKey): Spending condition placed on output
  2. Unlocking Script (scriptSig): Solution satisfying the locking script's conditions

Standard P2PKH Script Example

Locking Script (Output):

OP_DUP OP_HASH160 [PublicKeyHash] OP_EQUALVERIFY OP_CHECKSIG

Unlocking Script (Input):

[Signature] [PublicKey]

The verification process executes both scripts sequentially (Figure 2). If the final stack result is TRUE, the input is valid.

Transaction Fees

Transaction fees:

👉 Calculate optimal transaction fees

Advanced Transaction Features

1. Multisignature (Multisig)

Multisig scripts require M signatures from N authorized keys. Example 2/3 multisig locking script:

2 [PubKey1] [PubKey2] [PubKey3] 3 CHECKMULTISIG

2. Pay-to-Script-Hash (P2SH)

P2SH allows complex redeem scripts to be represented by their hash:

3. Segregated Witness (SegWit)

SegWit (BIP 141) separates witness data (signatures) from transaction data, providing:

SegWit Transaction Example (P2WPKH)

Output Script:

0 [PublicKeyHash]

Input Contains:

Transaction Lifecycle

  1. Creation: Wallet constructs and signs transaction
  2. Propagation: Nodes relay transaction via flooding
  3. Validation: Miners verify transaction against consensus rules
  4. Confirmation: Inclusion in a mined block

Frequently Asked Questions

Q1: Why does my transaction have multiple inputs/outputs?

A: Wallets combine UTXOs to create sufficient funds (inputs) and generate change outputs to comply with Bitcoin's accounting model.

Q2: How long do transactions typically take?

A: Confirmations depend on network congestion and fee payment. First confirmation averages 10 minutes but can vary.

Q3: What's the difference between legacy and SegWit addresses?

A: SegWit (Bech32) addresses start with "bc1" and offer lower fees, while legacy addresses begin with "1" or "3".

Q4: Can transactions be cancelled?

A: Before confirmation, transactions can sometimes be replaced (with RBF) or double-spent. After confirmation, they're irreversible.

Q5: Why do some wallets estimate fees differently?

A: Fee estimation algorithms analyze recent block inclusion patterns differently. Wallets may use various strategies for optimal fee prediction.

Key Takeaways

For developers building Bitcoin applications, understanding these transaction mechanics is crucial for creating secure and efficient wallet software and services.

👉 Explore Bitcoin development resources