Understanding Bitcoin Transaction Structure: Legacy and Witness Versions

Posted on In Blockchain, Systems, Systems 101, Tutorial

Bitcoin transactions are the backbone of the Bitcoin network, facilitating the transfer of value. Understanding their structure is essential for anyone interested in blockchain technology. This article delves into the details of both legacy and witness (SegWit) transaction structures.

Basics of Bitcoin Transactions

A Bitcoin transaction comprises inputs and outputs. Inputs are sources of funds, and outputs are destinations. Each transaction consumes previous outputs (unspent transaction outputs or UTXOs) and creates new ones.

Key Components

  1. Transaction ID (TXID): A unique identifier for the transaction.
  2. Inputs: Reference previous outputs being spent.
  3. Outputs: Specify new outputs with associated values.
  4. Locktime: Determines when a transaction can be added to the blockchain.

Legacy Transaction Structure

Legacy transactions, also known as pre-SegWit transactions, follow the original Bitcoin protocol format.

Structure

  1. Version (4 bytes): Indicates the version of the transaction format.
  2. Input Count: Number of inputs.
  3. Inputs:
    • TXID (32 bytes): Reference to the previous transaction output.
    • Vout (4 bytes): The index of the output in the referenced transaction.
    • ScriptSig Length: Length of the script signature.
    • ScriptSig: Script that satisfies the referenced output’s conditions.
    • Sequence (4 bytes): Used for transaction replacement.
  4. Output Count: Number of outputs.
  5. Outputs:
    • Value (8 bytes): Amount in satoshis.
    • ScriptPubKey Length: Length of the script public key.
    • ScriptPubKey: Conditions to be met for spending.
  6. Locktime (4 bytes): Finalizes the transaction as per time or block height.

Example

Version: 02000000
Input Count: 01
Inputs:
  TXID: e3c0e1b8...
  Vout: 00000000
  ScriptSig Length: 6b
  ScriptSig: 4830450221...
  Sequence: ffffffff
Output Count: 01
Outputs:
  Value: 00e1f50500000000
  ScriptPubKey Length: 19
  ScriptPubKey: 76a91489abc...
Locktime: 00000000

SegWit (Witness) Transaction Structure

Introduced in 2017, Segregated Witness (SegWit) transactions separate the witness data from the transaction, increasing block capacity and fixing transaction malleability.

Structure

  1. Version (4 bytes): Transaction version.
  2. Flag (2 bytes): Always 0001 in SegWit transactions.
  3. Input Count: Number of inputs.
  4. Inputs: Similar to legacy but without a script signature.
  5. Output Count: Number of outputs.
  6. Outputs: Similar to legacy.
  7. Witnesses:
    • Witness Count: Number of witness items.
    • Witness Data: Contains signatures and public keys.
  8. Locktime (4 bytes): Same as legacy.

Example

Version: 02000000
Flag: 0001
Input Count: 01
Inputs:
  TXID: e3c0e1b8...
  Vout: 00000000
  Sequence: ffffffff
Output Count: 01
Outputs:
  Value: 00e1f50500000000
  ScriptPubKey Length: 19
  ScriptPubKey: 76a91489abc...
Witnesses:
  Witness Count: 02
  Witness Data: 0247304402...
Locktime: 00000000

Transaction Malleability

Transaction malleability is an issue where a transaction’s ID (TXID) could be changed without altering its contents, affecting transaction tracking and reliability.

How Malleability Occurs

In legacy transactions, the TXID is generated from the entire transaction, including the script signatures. Malicious actors could modify the signature slightly, which would change the TXID without affecting the transaction’s validity.

SegWit Solution

SegWit addresses this issue by separating the witness data (signatures) from the transaction data. This means:

  • TXID Integrity: The TXID is calculated without including the witness data, ensuring it remains constant even if the signature data changes.
  • Prevents Malleability: By isolating signatures, any attempts to alter them do not affect the TXID, maintaining transaction integrity.

Key Differences

  • Witness Data: SegWit transactions have a separate field for witness data, reducing transaction size on the blockchain.
  • Transaction Malleability: SegWit addresses malleability by separating signature data, preventing TXID changes after signing.
  • Block Capacity: SegWit increases effective block size, accommodating more transactions per block.

Conclusion

Understanding Bitcoin’s transaction structure, both legacy and SegWit, is crucial for blockchain developers and enthusiasts. SegWit transactions offer significant improvements over legacy transactions, enhancing scalability and security. As Bitcoin continues to evolve, these structures may adapt further, continuing to shape the future of digital currency.

Eric Ma

Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

Leave a Reply

Your email address will not be published. Required fields are marked *