TeachMeBitcoin

Custom Python P2PK Auditor

From TeachMeBitcoin, the free encyclopedia Reading time: 2 min

Custom Python P2PK Auditor

In this final guide, we will build a Python script that parses a raw P2PK ScriptPubKey. The script will identify the type of public key (compressed vs. uncompressed) and extract the mathematical coordinates of the key.

The P2PK Auditor

def audit_p2pk_script(script_hex):
    # 1. Check for the minimum length of a P2PK script
    # Standard P2PK: [PushLen][PubKey][OP_CHECKSIG]
    # ac is OP_CHECKSIG
    if not script_hex.endswith("ac"):
        print("[ERROR] Not a valid P2PK script (missing OP_CHECKSIG)")
        return

    # 2. Identify the Public Key length
    # The first byte is the push length
    push_len = int(script_hex[:2], 16)
    pubkey_hex = script_hex[2:-2] # Strip push byte and OP_CHECKSIG

    print(f"--- P2PK ScriptPubKey Audit ---")
    print(f"[*] Raw Script: {script_hex}")
    print(f"[*] Key Length: {push_len} bytes")

    # 3. Determine Key Type
    if push_len == 65:
        print("[*] Key Type:   UNCOMPRESSED (Legacy)")
        # Uncompressed key: 04 + X (32 bytes) + Y (32 bytes)
        x_coord = pubkey_hex[2:66]
        y_coord = pubkey_hex[66:]
        print(f"[*] X-Coordinate: {x_coord}")
        print(f"[*] Y-Coordinate: {y_coord}")
    elif push_len == 33:
        print("[*] Key Type:   COMPRESSED (Modern)")
        # Compressed key: 02/03 + X (32 bytes)
        prefix = pubkey_hex[:2]
        x_coord = pubkey_hex[2:]
        parity = "Even" if prefix == "02" else "Odd"
        print(f"[*] X-Coordinate: {x_coord}")
        print(f"[*] Y-Parity:     {parity}")
    else:
        print("[!] WARNING: Non-standard key length detected!")

# --- Simulation ---

# Case 1: Satoshi-era Uncompressed P2PK
satoshi_script = "4104ae6a192a7ef3f13c233159b4ad331009e25d070b43bb670b33a411440d9d20c3261626f6e5200000000000000000000000000000000000000000000000000ac"
audit_p2pk_script(satoshi_script)

# Case 2: Modern Compressed P2PK
compressed_script = "2102f9e61c56f7e841f77d337d45e4120f44e132e01b3d36b85994f31c28b5e28a95ac"
print("\n--- Next Audit ---")
audit_p2pk_script(compressed_script)

How to Run the Auditor

  1. Ensure you have Python 3 installed.

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

  3. Run it using python3 p2pk_auditor.py.

Technical Takeaways

  1. Direct Exposure: Notice how easy it is to extract the public key. This is why P2PK is less private and more vulnerable to future quantum computers than hashed addresses.

  2. Opcode Logic: The ac byte at the end is the critical instruction. Without it, the Bitcoin Virtual Machine would simply stop and the transaction would be invalid.

  3. Parsing Precision: In a real node, the software doesn't just look for ac; it must strictly follow the push-data rules to ensure no extra data is hidden in the script.

Congratulations! You have completed the P2PK (Pay-to-Public-Key) module. You now understand the foundation of Bitcoin scripting.

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