TeachMeBitcoin

Custom Python Extended Key Auditor

From TeachMeBitcoin, the free encyclopedia Reading time: 2 min

Custom Python Extended Key Auditor

In this final guide, we will build a Python script that parses an xpub string. We will decode the Base58Check wrapper and extract the hidden metadata: version bytes, depth, parent fingerprint, child index, and the all-important Chain Code.

The Extended Key Auditor

# The 58 characters for decoding
ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"

def decode_base58(s):
    """Simple Base58 decoder."""
    num = 0
    for char in s:
        num = num * 58 + ALPHABET.index(char)
    combined = num.to_bytes(82, 'big') # 78 payload + 4 checksum
    return combined[:-4] # Remove checksum

def audit_extended_key(xpub_string):
    print(f"--- Extended Key (BIP32) Audit ---")
    print(f"[*] Input: {xpub_string}")

    try:
        # 1. Decode payload
        payload = decode_base58(xpub_string)

        # 2. Extract Components
        version = payload[0:4]
        depth = payload[4]
        fingerprint = payload[5:9]
        child_index = int.from_bytes(payload[9:13], 'big')
        chain_code = payload[13:45]
        key_data = payload[45:78]

        # 3. Print Results
        print(f"[*] Version Bytes: {version.hex()}")
        print(f"[*] Depth:         {depth}")
        print(f"[*] Parent Fingerprint: {fingerprint.hex()}")
        print(f"[*] Child Index:   {child_index}")
        print(f"[*] Chain Code:    {chain_code.hex()}")
        print(f"[*] Key Data:      {key_data.hex()}")

        # Verification of type
        if version.hex() == "0488b21e":
            print("[INFO] This is a Mainnet XPUB.")
        elif version.hex() == "04b24746":
            print("[INFO] This is a Mainnet ZPUB (SegWit).")

    except Exception as e:
        print(f"[ERROR] Audit failed: {e}")

# --- Simulation ---

# A standard BIP32 Master xpub
test_xpub = "xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqeseD26wldDA486XzB72sHAnNmd4X"
audit_extended_key(test_xpub)

How to Run the Auditor

  1. Ensure you have Python 3 installed.

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

  3. Run it using python3 xpub_auditor.py.

Technical Takeaways

  1. Chain Code Access: Notice how easy it is to extract the 32-byte chain code. This is why you should keep your xpub private if you are concerned about address privacy.

  2. Depth: If the depth is 0, you are looking at a master key. If it is 1 or higher, this key was derived from a parent.

  3. Self-Contained: The reason an xpub is so long is that it must carry its own "Lineage" (fingerprint and index) so that a wallet can place it correctly in the HD tree.

Congratulations! You have completed the Extended Keys (xpub / xprv master keys) module. You now understand the hierarchical architecture that powers modern Bitcoin self-custody.

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