HEX
Server: LiteSpeed
System: Linux br-asc-web1845.main-hosting.eu 5.14.0-611.42.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Mar 24 05:30:20 EDT 2026 x86_64
User: u790421558 (790421558)
PHP: 8.2.30
Disabled: system, exec, shell_exec, passthru, mysql_list_dbs, ini_alter, dl, symlink, link, chgrp, leak, popen, apache_child_terminate, virtual, mb_send_mail
Upload Files
File: //opt/alt/python311/lib/python3.11/site-packages/validators/crypto_addresses/eth_address.py
"""ETH Address."""

# standard
import re

# local
from validators.utils import validator

_keccak_flag = True
try:
    # external
    from eth_hash.auto import keccak
except ImportError:
    _keccak_flag = False


def _validate_eth_checksum_address(addr: str):
    """Validate ETH type checksum address."""
    addr = addr.replace("0x", "")
    addr_hash = keccak.new(addr.lower().encode("ascii")).digest().hex()  # type: ignore

    if len(addr) != 40:
        return False

    for i in range(0, 40):
        if (int(addr_hash[i], 16) > 7 and addr[i].upper() != addr[i]) or (
            int(addr_hash[i], 16) <= 7 and addr[i].lower() != addr[i]
        ):
            return False
    return True


@validator
def eth_address(value: str, /):
    """Return whether or not given value is a valid ethereum address.

    Full validation is implemented for ERC20 addresses.

    Examples:
        >>> eth_address('0x9cc14ba4f9f68ca159ea4ebf2c292a808aaeb598')
        # Output: True
        >>> eth_address('0x8Ba1f109551bD432803012645Ac136ddd64DBa72')
        # Output: ValidationError(func=eth_address, args=...)

    Args:
        value:
            Ethereum address string to validate.

    Returns:
        (Literal[True]): If `value` is a valid ethereum address.
        (ValidationError): If `value` is an invalid ethereum address.
    """
    if not _keccak_flag:
        raise ImportError(
            "Do `pip install validators[crypto-eth-addresses]` to perform `eth_address` validation."
        )

    if not value:
        return False

    return re.compile(r"^0x[0-9a-f]{40}$|^0x[0-9A-F]{40}$").match(
        value
    ) or _validate_eth_checksum_address(value)