Custom Python Script Analyzer
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
-
Ensure you have Python 3 installed.
-
Copy the code into a file named
script_analyzer.py. -
Run it using
python3 script_analyzer.py.
Technical Takeaways
-
Pattern Matching: Notice how we look for specific "bookend" bytes (like
0x76a9and0x88ac). This is exactly how theisStandard()function in the Bitcoin Core source code works. -
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.
-
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.
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: