Skip to content

Instantly share code, notes, and snippets.

@dancodery
Last active September 23, 2021 21:56
Show Gist options
  • Save dancodery/3eb951dd74634a9fd859e16059384114 to your computer and use it in GitHub Desktop.
Save dancodery/3eb951dd74634a9fd859e16059384114 to your computer and use it in GitHub Desktop.
This script validates Bitcoin addresses by calculating and comparing checksums as well as displaying the Bitcoin address type.
#!/usr/bin/env python
"""bitcoin-address-validator.py: This script validates Bitcoin addresses by
calculating and comparing checksums as well as displaying the Bitcoin address type."""
__author__ = "Daniel Gockel"
__website__ = "https://www.10xrecovery.org/"
from base58 import b58decode
from hashlib import sha256
bitcoin_address_to_validate = "1XRecoveryXXXXXXXXXXXXXXXXXXV47Rm7"
if __name__ == "__main__":
bitcoin_address_decoded = b58decode(bitcoin_address_to_validate)
version_plus_payload = bitcoin_address_decoded[:-4]
checksum_found = bitcoin_address_decoded[-4:]
# calculate real checksum
checksum_real = sha256(sha256(version_plus_payload).digest()).digest()[:4]
address_type = "undefined"
version_prefix = version_plus_payload.hex()[0:8]
if version_prefix[0:2] == "00":
address_type = "Bitcoin Address"
elif version_prefix[0:2] == "05":
address_type = "Pay-to-Script-Hash Address"
elif version_prefix[0:2] == "6F":
address_type = "Bitcoin Testnet Address"
elif version_prefix[0:2] == "80":
address_type = "Private Key WIF"
elif version_prefix[0:4] == "0142":
address_type = "BIP-38 Encrypted Private Key"
elif version_prefix[0:8] == "0488B21E":
address_type = "BIP-32 Extended Public Key"
if checksum_real == checksum_found:
print("Address with type '%s' is a valid Bitcoin address." % address_type)
else:
print("Address with type %s is not a valid Bitcoin address." % address_type)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment