Common utility functions for python code that interacts with Ethereum
—
Comprehensive Ethereum address handling including validation, format conversion, and EIP-55 checksum processing. Supports all address formats used in Ethereum: binary (20 bytes), hex, normalized (lowercase), and checksummed.
Check if a value is a valid Ethereum address in any format.
def is_address(value) -> bool:
"""
Check if value is any valid Ethereum address format.
Args:
value: Value to check for address validity
Returns:
bool: True if value is a valid address format
"""from eth_utils import is_address
# Valid addresses return True
is_address("0xd3CdA913deB6f67967B99D67aCDFa1712C293601") # True
is_address("0xd3cda913deb6f67967b99d67acdfa1712c293601") # True
is_address(b'\xd3\xcd\xa9\x13\xde\xb6\xf6yg\xb9\x9dg\xac\xdf\xa1q,)6\x01') # True
# Invalid addresses return False
is_address("0x123") # False (too short)
is_address("not an address") # FalseCheck for specific address formats.
def is_binary_address(value) -> bool:
"""Check if value is 20-byte binary address."""
def is_hex_address(value) -> bool:
"""Check if value is hex-encoded address (40 hex chars with 0x prefix)."""
def is_canonical_address(address) -> bool:
"""Check if address is in canonical (20-byte) format."""
def is_normalized_address(value) -> bool:
"""Check if address is normalized (lowercase hex with 0x prefix)."""
def is_checksum_address(value) -> bool:
"""Validate EIP-55 checksum address format."""
def is_checksum_formatted_address(value) -> bool:
"""Check if address has checksum formatting (mixed case)."""Convert addresses between different formats.
def to_canonical_address(address) -> bytes:
"""
Convert address to canonical 20-byte format.
Args:
address: Address in any valid format
Returns:
bytes: 20-byte canonical address
Raises:
ValidationError: If address is invalid
"""
def to_checksum_address(value) -> str:
"""
Convert address to EIP-55 checksum format.
Args:
value: Address in any valid format
Returns:
str: EIP-55 checksummed address (0x-prefixed)
Raises:
ValidationError: If address is invalid
"""
def to_normalized_address(value) -> str:
"""
Convert address to normalized format (lowercase hex).
Args:
value: Address in any valid format
Returns:
str: Normalized address (0x-prefixed lowercase)
Raises:
ValidationError: If address is invalid
"""from eth_utils import to_checksum_address, to_canonical_address, to_normalized_address
address = "0xd3cda913deb6f67967b99d67acdfa1712c293601"
# Convert to checksum format (EIP-55)
checksum = to_checksum_address(address)
print(checksum) # 0xd3CdA913deB6f67967B99D67aCDFa1712C293601
# Convert to canonical (bytes) format
canonical = to_canonical_address(address)
print(canonical.hex()) # d3cda913deb6f67967b99d67acdfa1712c293601
# Convert to normalized (lowercase) format
normalized = to_normalized_address(address)
print(normalized) # 0xd3cda913deb6f67967b99d67acdfa1712c293601Compare addresses for equality regardless of format.
def is_same_address(left, right) -> bool:
"""
Compare two addresses for equality (format-agnostic).
Args:
left: First address in any valid format
right: Second address in any valid format
Returns:
bool: True if addresses represent the same Ethereum address
Raises:
ValidationError: If either address is invalid
"""from eth_utils import is_same_address
# These all represent the same address
addr1 = "0xd3CdA913deB6f67967B99D67aCDFa1712C293601" # Checksum
addr2 = "0xd3cda913deb6f67967b99d67acdfa1712c293601" # Lowercase
addr3 = b'\xd3\xcd\xa9\x13\xde\xb6\xf6yg\xb9\x9dg\xac\xdf\xa1q,)6\x01' # Binary
print(is_same_address(addr1, addr2)) # True
print(is_same_address(addr1, addr3)) # True
print(is_same_address(addr2, addr3)) # TrueEthereum addresses can be represented in multiple formats:
bytes objectfrom eth_utils import is_address, to_checksum_address
def process_address(addr_input):
"""Safely process an address input."""
if not is_address(addr_input):
raise ValueError(f"Invalid address: {addr_input}")
# Always work with checksum format for display
return to_checksum_address(addr_input)
# Usage
user_input = "0xd3cda913deb6f67967b99d67acdfa1712c293601"
safe_addr = process_address(user_input)
print(safe_addr) # 0xd3CdA913deB6f67967B99D67aCDFa1712C293601from eth_utils import to_canonical_address
def deduplicate_addresses(addresses):
"""Remove duplicate addresses regardless of format."""
canonical_set = set()
unique_addresses = []
for addr in addresses:
canonical = to_canonical_address(addr)
if canonical not in canonical_set:
canonical_set.add(canonical)
unique_addresses.append(addr)
return unique_addresses