TeachMeBitcoin

Custom Python Input Parser

From TeachMeBitcoin, the free encyclopedia Reading time: 2 min

Custom Python OutPoint Parser

In this final guide for the Input Structure module, we will build a script that parses the OutPoint (TXID and VOUT) from a raw Bitcoin transaction hex. This is the first step in understanding how a transaction "connects" to its history.

The Python Input Parser

import struct

def parse_tx_inputs(tx_hex):
    # Convert hex to bytes
    tx_bytes = bytes.fromhex(tx_hex)

    # Skip Version (4 bytes)
    cursor = 4

    # Read Input Count (simplified VarInt logic for this example)
    # Most standard TXs have 1-252 inputs (1 byte)
    input_count = tx_bytes[cursor]
    cursor += 1

    print(f"--- Parsing {input_count} Transaction Inputs ---")

    for i in range(input_count):
        # 1. Read TXID (32 bytes, stored in little-endian)
        raw_txid = tx_bytes[cursor:cursor+32]
        # Reverse to display as Big-Endian (Block Explorer format)
        display_txid = raw_txid[::-1].hex()
        cursor += 32

        # 2. Read VOUT (4 bytes, little-endian uint32)
        raw_vout = tx_bytes[cursor:cursor+4]
        vout = struct.unpack('<I', raw_vout)[0]
        cursor += 4

        # Print results
        print(f"\n[Input {i}]")
        print(f"[*] Previous TXID: {display_txid}")
        print(f"[*] Output Index:  {vout}")

        # Skip ScriptSig Length and ScriptSig for this simulation
        script_len = tx_bytes[cursor]
        cursor += 1 + script_len

        # Skip Sequence (4 bytes)
        cursor += 4

# --- Simulation ---
# A real Bitcoin transaction hex (truncated for example)
sample_tx = (
    "01000000" + # Version
    "01" +       # Input Count (1)
    "81cd6495d3550e553c3066d40026a090ef666993a46610000000000000000000" + # TXID
    "00000000" + # VOUT (0)
    "016a" +     # ScriptLen (1) and ScriptSig (0x6a)
    "ffffffff"   # Sequence
)

parse_tx_inputs(sample_tx)

How to Run the Parser

  1. Ensure you have Python 3 installed.

  2. Copy the code into a file named tx_input_parser.py.

  3. Run it using python3 tx_input_parser.py.

Technical Takeaways

  1. Byte Reversal: Notice the [::-1] in the script. This is the crucial step to making raw protocol data human-readable.

  2. Deterministic Pointers: Every input in every transaction since 2009 has followed this exact binary format.

  3. OutPoint Extraction: Extracting the TXID and VOUT is the only way to "follow the money" on the blockchain.

Congratulations! You have completed the Input Structure module. You now know exactly how Bitcoin points back to its past.

☕ Help support TeachMeBitcoin

TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation:

Ethereum: 0x578417C51783663D8A6A811B3544E1f779D39A85
Bitcoin: bc1q77k9e95rn669kpzyjr8ke9w95zhk7pa5s63qzz
Solana: 4ycT2ayqeMucixj3wS8Ay8Tq9NRDYRPKYbj3UGESyQ4J
Address copied to clipboard!