Custom Python Address Auditor
Custom Python Address Auditor
In this final guide, we will build a Python script that decodes a Base58Check Address (Legacy). This script will verify the mathematical checksum and extract the raw 20-byte Public Key Hash hidden inside the address.
The Base58Check Decoder
import hashlib
# Standard Base58 Alphabet
ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
def decode_base58(s):
"""Converts a Base58 string back into raw bytes."""
num = 0
for char in s:
num = num * 58 + ALPHABET.index(char)
# Convert to 25 bytes (1 version + 20 hash + 4 checksum)
return num.to_bytes(25, byteorder='big')
def audit_address(address_str):
print(f"--- Bitcoin Address (Base58) Audit ---")
print(f"[*] Address: {address_str}")
# 1. Decode to raw bytes
try:
decoded = decode_base58(address_str)
except Exception as e:
print("[ERROR] Invalid characters in address.")
return
# 2. Split components
version = decoded[0]
payload = decoded[1:21]
checksum_received = decoded[21:]
print(f"[*] Version Byte: {hex(version)}")
print(f"[*] PKH (20 bytes): {payload.hex()}")
print(f"[*] Checksum: {checksum_received.hex()}")
# 3. Verify Checksum (Double SHA256)
# Hash the (version + payload)
hash1 = hashlib.sha256(decoded[:21]).digest()
hash2 = hashlib.sha256(hash1).digest()
checksum_calculated = hash2[:4]
# 4. Final Comparison
if checksum_received == checksum_calculated:
print("[SUCCESS] Checksum is VALID.")
if version == 0x00:
print("[TYPE] Legacy P2PKH (starts with 1)")
elif version == 0x05:
print("[TYPE] Legacy P2SH (starts with 3)")
else:
print("[ERROR] INVALID CHECKSUM! This address has a typo.")
# --- Simulation ---
# Case: A valid legacy address
valid_addr = "1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2"
audit_address(valid_addr)
# Case: An address with a typo (changed the last '2' to '3')
print("\n--- Testing Typo ---")
typo_addr = "1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN3"
audit_address(typo_addr)
How to Run the Auditor
-
Ensure you have Python 3 installed.
-
Copy the code into a file named
address_auditor.py. -
Run it using
python3 address_auditor.py.
Technical Takeaways
-
Integrity: Notice how the typo detection works. By changing just one character, the entire checksum fails, preventing you from sending money to a non-existent wallet.
-
Versioning: The "Version Byte" is what makes addresses start with a specific number. This allows wallets to immediately know which ScriptPubKey they need to build.
-
Standardization: This same Base58Check process is used by many other cryptocurrencies (like Litecoin and Dogecoin), often just by changing the version byte.
Congratulations! You have completed the Address (Encoding Standards) module. You now understand the bridges that connect complex cryptographic hashes to human-readable strings.
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: