libcrypto.validation

Address validation and decoding. These helpers let you confirm an address is well-formed before sending funds — a critical safety check for wallets and exchanges.

Functions

validate_address(address, network=None) -> bool

Return True when address is well-formed. When network is provided, the address must also belong to that network (matching version byte, HRP, or the EVM family). Never raises.

get_address_info(address) -> AddressInfo

Return structured decoding information. Never raises; malformed input yields AddressInfo(valid=False, ...).

is_valid_ethereum_address(address, *, require_checksum=False) -> bool

Validate an EVM hex address. Mixed-case addresses always have their EIP-55 checksum verified. With require_checksum=True, all-lower/all-upper addresses are rejected because they carry no checksum protection.

to_checksum_address(address) -> str

Return the EIP-55 checksummed form of an EVM address. Raises InvalidFormatError for malformed input.

decode_base58check_address(address, *, alphabet=BASE58_ALPHABET) -> tuple[int, bytes]

Decode to (version_byte, payload) with checksum verification. Raises InvalidFormatError on failure.

decode_segwit_address(address) -> tuple[str, int, bytes]

Decode a SegWit address to (hrp, witness_version, program) with full Bech32/Bech32m and witness-program validation.

class AddressInfo

A NamedTuple with fields:

Field Description
valid bool.
address_format "base58check", "bech32", "bech32m", "evm-hex", or None.
address_type "p2pkh", "p2sh", "p2wpkh", "p2wsh", "p2tr", "account", "tron", "witness", or None.
networks Tuple of possible networks (may be ambiguous — see note).
witness_version Witness version for SegWit addresses, else None.
payload hash160 / witness program / account hash, else None.

Ambiguity note

Some networks share Base58 version bytes (for example Bitcoin and Bitcoin Cash legacy P2PKH both use 0x00; DigiByte and Dogecoin both use 0x1E). For these, networks lists all matching networks rather than guessing one. Pass a network argument to validate_address when you need a definitive answer.

Example

from libcrypto import validate_address, get_address_info, to_checksum_address

assert validate_address("1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH", "bitcoin")
assert not validate_address("1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMX")  # bad checksum

info = get_address_info("bc1pmfr3p9j00pfxjh0zmgp99y8zftmd3s5pmedqhyptwy6lm87hf5sspknck9")
print(info.address_format, info.address_type)  # bech32m p2tr

print(to_checksum_address("0x7e5f4552091a69125d5dfcb7b8c2659029395bdf"))
# 0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf