Custom Python Input Parser
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
-
Ensure you have Python 3 installed.
-
Copy the code into a file named
tx_input_parser.py. -
Run it using
python3 tx_input_parser.py.
Technical Takeaways
-
Byte Reversal: Notice the
[::-1]in the script. This is the crucial step to making raw protocol data human-readable. -
Deterministic Pointers: Every input in every transaction since 2009 has followed this exact binary format.
-
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.
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: