TeachMeBitcoin

Custom Python VOUT Analyzer

From TeachMeBitcoin, the free encyclopedia Reading time: 2 min

Custom Python VOUT Analyzer

In this final simulation, we will write a script that analyzes the Outputs of a transaction to determine their VOUT indexes. This will help you visualize exactly how a transaction creates the "OutPoints" that future transactions will spend.

The Python Output Mapper

import struct

def map_transaction_outputs(tx_hex):
    # Convert hex string to raw bytes
    tx_bytes = bytes.fromhex(tx_hex)

    # In a real parser, we would skip the Version and Inputs
    # For this simulation, we will jump directly to the Output section
    # Mock data: 1 byte for output count, followed by outputs
    output_count = tx_bytes[0]
    cursor = 1

    print(f"--- Mapping {output_count} Transaction Outputs ---")

    for vout in range(output_count):
        # 1. Read Value (8 bytes, Little-Endian Satoshis)
        raw_value = tx_bytes[cursor:cursor+8]
        satoshis = struct.unpack('<q', raw_value)[0]
        cursor += 8

        # 2. Read Script Length (simplified 1-byte VarInt)
        script_len = tx_bytes[cursor]
        cursor += 1

        # 3. Read Script (for display)
        script = tx_bytes[cursor:cursor+script_len].hex()
        cursor += script_len

        # Print the mapping
        print(f"\n[VOUT {vout}]")
        print(f"[*] Amount: {satoshis / 100_000_000:.8f} BTC")
        print(f"[*] Script: {script}")

# --- Simulation ---
# A mock output section hex:
# [02] -> Count (2)
# [00e1f50500000000] -> 100,000,000 sats (1 BTC)
# [02] -> Script Len (2)
# [6a6a] -> Script (OP_RETURN OP_RETURN)
# [80f0fa0200000000] -> 50,000,000 sats (0.5 BTC)
# [01] -> Script Len (1)
# [6a] -> Script (OP_RETURN)

mock_output_data = "0200e1f50500000000026a6a80f0fa0200000000016a"
map_transaction_outputs(mock_output_data)

How to Run the Analyzer

  1. Ensure you have Python 3 installed.

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

  3. Run it using python3 vout_analyzer.py.

Technical Takeaways

  1. Zero-Based Indexing: Notice how the range(output_count) naturally assigns 0 to the first output. This is exactly how Bitcoin Core handles it.

  2. Implicit Indexing: The VOUT is never actually written into the output section of a transaction. It is implied by the order in which the outputs appear.

  3. OutPoint Genesis: By knowing the order of these outputs, you can predict the exact OutPoint that will be used in the future to spend them.

Congratulations! You have completed the VOUT & Output Indexing module. You now understand the coordinate system of the world's most secure ledger.

☕ 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!