Chapter 6: Bitcoin Transactions - Mastering Bitcoin (2nd Edition)

·

6.1 Introduction

Bitcoin transactions are the most critical part of the Bitcoin system. According to Bitcoin's design principles, every other component exists to ensure transactions can be created, propagated, validated, and ultimately added to the global ledger (blockchain). At their core, Bitcoin transactions are data structures that encode value transfers between participants.

In this chapter, we will examine:

When we use the term "wallet" in this chapter, we refer to transaction-building software, not just a database of keys.

6.2 Transaction Details

6.2.1 Transaction - Behind the Scenes

What you see in block explorers is a simplified representation. The actual transaction structure differs significantly from what user interfaces display.

Example decoded transaction:

{
  "version": 1,
  "locktime": 0,
  "vin": [
    {
      "txid": "7957a35fe64f80d234d76d83a2a8f1a0d8149a41d81de548f0a65a8a999f6f18",
      "vout": 0,
      "scriptSig": "3045022100884d142d...",
      "sequence": 4294967295
    }
  ],
  "vout": [
    {
      "value": 0.01500000,
      "scriptPubKey": "OP_DUP OP_HASH160 ab680255... OP_EQUALVERIFY OP_CHECKSIG"
    },
    {
      "value": 0.08450000,
      "scriptPubKey": "OP_DUP OP_HASH160 7f9b1a7f... OP_EQUALVERIFY OP_CHECKSIG"
    }
  ]
}

Key observations:

6.3 Transaction Inputs and Outputs

6.3.1 Transaction Outputs (UTXOs)

Fundamental building blocks with two components:

  1. Amount in satoshis
  2. Cryptographic puzzle (locking script)

Example output:

{
  "value": 0.10000000,
  "scriptPubKey": "OP_DUP OP_HASH160 7f9b1a7fb68d60c536c2fd8aeaa53a8f3cc025a8 OP_EQUALVERIFY OP_CHECKSIG"
}

6.3.2 Transaction Inputs

Inputs reference UTXOs being spent and provide unlocking proofs:

Example input:

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

6.3.3 Transaction Fees

Calculated as:
Fee = Sum(Inputs) - Sum(Outputs)

Best practices:

👉 Calculate your exact fee requirements

6.4 Bitcoin Scripts

6.4.1 P2PKH (Pay-to-Public-Key-Hash)

Standard transaction type with:

Validation combines both scripts:

<signature> <publicKey> OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG

6.5 Digital Signatures (ECDSA)

6.5.1 Signature Creation

6.5.2 Signature Verification

6.5.3 SIGHASH Types

FlagDescription
ALLSigns all inputs/outputs
NONESigns inputs only
SINGLESigns corresponding input/output
ANYONECANPAYModifier for flexibility

6.6 Addresses and Balances

Key points:

👉 View live blockchain data

FAQ

Q: Where are Bitcoin addresses stored in transactions?

A: They aren't directly stored. Addresses are derived from the public key hashes in locking scripts.

Q: How do wallets calculate balances?

A: By scanning the blockchain and summing values from UTXOs that belong to the wallet's addresses.

Q: Why doesn't the transaction show my exact balance?

A: The blockchain only records individual transactions. Balance displays are wallet-side calculations.

Q: What happens if transaction fees are too low?

A: Low-fee transactions may experience delayed confirmations or require fee bumping.