CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-validators

Python Data Validation for Humans™ - comprehensive validation library for various data types without schema definitions

Overview
Eval results
Files

crypto-validation.mddocs/

Cryptocurrency Address Validation

Validators for blockchain addresses across major cryptocurrency networks. These validators implement network-specific address formats, checksums, and encoding schemes to ensure valid cryptocurrency addresses.

Capabilities

Bitcoin Address Validation

Validates Bitcoin addresses including legacy, SegWit, and Bech32 formats.

def btc_address(value: str, /) -> Union[Literal[True], ValidationError]:
    """
    Validate Bitcoin addresses across all supported formats.
    
    Parameters:
    - value: Bitcoin address string to validate
    
    Returns:
    True if valid Bitcoin address, ValidationError otherwise
    
    Supported formats:
    - P2PKH (Pay-to-Public-Key-Hash): starts with '1'
    - P2SH (Pay-to-Script-Hash): starts with '3'  
    - Bech32 (SegWit): starts with 'bc1'
    
    Validates:
    - Address format and length
    - Base58 encoding (for legacy addresses)
    - Bech32 encoding (for SegWit addresses)
    - Checksum verification
    """

Ethereum Address Validation

Validates Ethereum addresses with ERC-55 mixed-case checksum support.

def eth_address(value: str, /) -> Union[Literal[True], ValidationError]:
    """
    Validate Ethereum addresses with optional ERC-55 checksum.
    
    Parameters:
    - value: Ethereum address string to validate
    
    Returns:
    True if valid Ethereum address, ValidationError otherwise
    
    Validates:
    - Length: exactly 42 characters (including 0x prefix)
    - Format: 0x followed by 40 hexadecimal characters
    - ERC-55 checksum: mixed-case checksum validation when present
    
    Accepts both checksummed and non-checksummed addresses.
    Requires 'validators[crypto-eth-addresses]' for advanced validation.
    """

Binance Smart Chain Address Validation

Validates Binance Smart Chain (BSC) addresses.

def bsc_address(value: str, /) -> Union[Literal[True], ValidationError]:
    """
    Validate Binance Smart Chain addresses.
    
    Parameters:
    - value: BSC address string to validate
    
    Returns:
    True if valid BSC address, ValidationError otherwise
    
    BSC addresses follow Ethereum address format:
    - Length: exactly 42 characters (including 0x prefix)
    - Format: 0x followed by 40 hexadecimal characters
    - Compatible with ERC-55 checksum validation
    
    BSC is EVM-compatible, so addresses use same format as Ethereum.
    """

Tron Address Validation

Validates Tron (TRX) addresses with base58 encoding and checksum verification.

def trx_address(value: str, /) -> Union[Literal[True], ValidationError]:
    """
    Validate Tron (TRX) addresses.
    
    Parameters:
    - value: Tron address string to validate
    
    Returns:
    True if valid Tron address, ValidationError otherwise
    
    Validates:
    - Format: starts with 'T' followed by 33 base58 characters
    - Length: exactly 34 characters total
    - Base58 encoding validation
    - Address checksum verification using double SHA-256
    
    Tron addresses use base58 encoding similar to Bitcoin but with different prefixes.
    """

Usage Examples

import validators

# Bitcoin address validation
validators.btc_address('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa')      # True (P2PKH)
validators.btc_address('3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy')      # True (P2SH)
validators.btc_address('bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4')  # True (Bech32)

# Invalid Bitcoin addresses
validators.btc_address('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNb')      # ValidationError (bad checksum)
validators.btc_address('not-a-bitcoin-address')                    # ValidationError

# Ethereum address validation
validators.eth_address('0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed')  # True (checksummed)
validators.eth_address('0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed')  # True (lowercase)
validators.eth_address('0x5AAEB6053F3E94C9B9A09F33669435E7EF1BEAED')  # True (uppercase)

# Invalid Ethereum addresses
validators.eth_address('5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed')   # ValidationError (no 0x prefix)
validators.eth_address('0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeA')    # ValidationError (too short)

# BSC address validation (same format as Ethereum)
validators.bsc_address('0x10ED43C718714eb63d5aA57B78B54704E256024E')  # True
validators.bsc_address('0x10ed43c718714eb63d5aa57b78b54704e256024e')  # True

# Tron address validation
validators.trx_address('TLyqzVGLV1srkB7dToTAEqgDSfPtXRJZYH')        # True
validators.trx_address('TMuA6YqfCeX8EhbfYEg5y7S4DqzSJireY9')        # True

# Invalid Tron addresses
validators.trx_address('TLyqzVGLV1srkB7dToTAEqgDSfPtXRJZY')         # ValidationError (too short)
validators.trx_address('BLyqzVGLV1srkB7dToTAEqgDSfPtXRJZYH')        # ValidationError (wrong prefix)

Advanced Usage

Multi-Network Address Validation

import validators

def validate_crypto_address(address: str, network: str) -> bool:
    """Validate cryptocurrency address for specific network."""
    
    network_validators = {
        'bitcoin': validators.btc_address,
        'ethereum': validators.eth_address,
        'bsc': validators.bsc_address,
        'tron': validators.trx_address,
    }
    
    validator_func = network_validators.get(network.lower())
    if not validator_func:
        raise ValueError(f"Unsupported network: {network}")
    
    return bool(validator_func(address))

# Usage examples
validate_crypto_address('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', 'bitcoin')  # True
validate_crypto_address('0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed', 'ethereum')  # True

Address Format Detection

import validators

def detect_crypto_address_type(address: str) -> str:
    """Detect cryptocurrency address type."""
    
    if validators.btc_address(address):
        if address.startswith('1'):
            return 'Bitcoin P2PKH'
        elif address.startswith('3'):
            return 'Bitcoin P2SH'
        elif address.startswith('bc1'):
            return 'Bitcoin Bech32'
    elif validators.eth_address(address):
        return 'Ethereum'
    elif validators.bsc_address(address):
        return 'Binance Smart Chain'
    elif validators.trx_address(address):
        return 'Tron'
    
    return 'Unknown'

# Usage
addresses = [
    '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa',
    '0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed',
    'TLyqzVGLV1srkB7dToTAEqgDSfPtXRJZYH'
]

for addr in addresses:
    print(f"{addr}: {detect_crypto_address_type(addr)}")

Security Considerations

Checksum Validation

import validators

def validate_with_checksum_warning(address: str, network: str) -> tuple[bool, str]:
    """Validate address and provide checksum warnings."""
    
    if network == 'ethereum':
        # Check if address has mixed case (likely checksummed)
        has_mixed_case = any(c.isupper() for c in address) and any(c.islower() for c in address)
        
        if validators.eth_address(address):
            if not has_mixed_case and address != address.lower():
                return True, "Valid but consider using checksummed address for better security"
            return True, "Valid address"
        else:
            return False, "Invalid address format"
    
    # Add similar logic for other networks...
    return False, "Network not supported"

# Usage
result, message = validate_with_checksum_warning('0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed', 'ethereum')
print(f"Valid: {result}, Message: {message}")

Installation Requirements

Some cryptocurrency validators require additional dependencies:

# Basic installation
pip install validators

# With Ethereum address checksum validation
pip install validators[crypto-eth-addresses]

The crypto-eth-addresses extra provides enhanced Ethereum address validation including:

  • ERC-55 mixed-case checksum validation
  • Improved address format checking
  • Better error messages for invalid checksums

Install with Tessl CLI

npx tessl i tessl/pypi-validators

docs

basic-validation.md

country-validation.md

crypto-validation.md

encoding-validation.md

financial-validation.md

i18n-validation.md

index.md

network-validation.md

system-validation.md

tile.json