TeachMeBitcoin

Custom Python Script Analyzer

From TeachMeBitcoin, the free encyclopedia Reading time: 2 min

Custom Python ScriptPubKey Decoder

In this final simulation, we will write a script that identifies the type of a ScriptPubKey by looking at its "Opcode Fingerprint." This is how block explorers and wallets categorize the outputs they find in the blockchain.

The Python Script Analyzer

def analyze_script_pubkey(script_hex):
    # Convert hex string to raw bytes
    script = bytes.fromhex(script_hex)

    print(f"--- Analyzing ScriptPubKey Fingerprint ---")
    print(f"[*] Hex: {script_hex}")

    # 1. Check for P2PKH Pattern
    # Starts with OP_DUP (0x76) OP_HASH160 (0xa9) and ends with OP_EQUALVERIFY (0x88) OP_CHECKSIG (0xac)
    if script.startswith(b'\x76\xa9') and script.endswith(b'\x88\xac'):
        hash_bytes = script[3:-2]
        print(f"[TYPE] P2PKH (Legacy)")
        print(f"[*] Extracted Hash: {hash_bytes.hex()}")

    # 2. Check for P2SH Pattern
    # Starts with OP_HASH160 (0xa9) and ends with OP_EQUAL (0x87)
    elif script.startswith(b'\xa9') and script.endswith(b'\x87'):
        hash_bytes = script[2:-1]
        print(f"[TYPE] P2SH (Wrapped SegWit/Multi-sig)")
        print(f"[*] Extracted Hash: {hash_bytes.hex()}")

    # 3. Check for Native SegWit (P2WPKH)
    # Starts with 0x00 and length 0x14 (20 bytes)
    elif script.startswith(b'\x00\x14'):
        hash_bytes = script[2:]
        print(f"[TYPE] P2WPKH (Native SegWit)")
        print(f"[*] Extracted Hash: {hash_bytes.hex()}")

    # 4. Check for OP_RETURN
    elif script.startswith(b'\x6a'):
        data = script[1:]
        print(f"[TYPE] OP_RETURN (Unspendable Data)")
        print(f"[*] Embedded Data: {data.hex()}")

    else:
        print("[TYPE] Unknown / Custom Script")

# --- Simulation ---

# Pattern: OP_DUP OP_HASH160 <20-byte-hash> OP_EQUALVERIFY OP_CHECKSIG
p2pkh_sample = "76a91489abcde0123456789abcde0123456789abcde01288ac"
analyze_script_pubkey(p2pkh_sample)

# Pattern: 0x00 0x14 <20-byte-hash>
p2wpkh_sample = "001489abcde0123456789abcde0123456789abcde012"
print("\n")
analyze_script_pubkey(p2wpkh_sample)

# Pattern: OP_RETURN (0x6a) + Data
op_return_sample = "6a0548656c6c6f"
print("\n")
analyze_script_pubkey(op_return_sample)

How to Run the Analyzer

  1. Ensure you have Python 3 installed.

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

  3. Run it using python3 script_analyzer.py.

Technical Takeaways

  1. Pattern Matching: Notice how we look for specific "bookend" bytes (like 0x76a9 and 0x88ac). This is exactly how the isStandard() function in the Bitcoin Core source code works.

  2. Explicit vs. Implicit: In SegWit, the script is much shorter because the logic is "implied" by the protocol rather than explicitly listed in the script bytes.

  3. Data Extraction: Once you identify the pattern, you can reliably extract the underlying hash, which is the actual "Identity" of the coin owner.

Congratulations! You have completed the ScriptPubKey module. You now understand the locks that secure the global Bitcoin 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!