Skip to content

crypto_addresses

validators.crypto_addresses.bsc_address

BSC Address.

bsc_address(value)

Return whether or not given value is a valid binance smart chain address.

Full validation is implemented for BSC addresses.

Examples:

>>> bsc_address('0x4e5acf9684652BEa56F2f01b7101a225Ee33d23f')
# Output: True
>>> bsc_address('0x4g5acf9684652BEa56F2f01b7101a225Eh33d23z')
# Output: ValidationError(func=bsc_address, args=...)

Parameters:

Name Type Description Default
value str

BSC address string to validate.

required

Returns:

Type Description
Literal[True]

If value is a valid bsc address.

ValidationError

If value is an invalid bsc address.

Source code in src/validators/crypto_addresses/bsc_address.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
@validator
def bsc_address(value: str, /):
    """Return whether or not given value is a valid binance smart chain address.

    Full validation is implemented for BSC addresses.

    Examples:
        >>> bsc_address('0x4e5acf9684652BEa56F2f01b7101a225Ee33d23f')
        # Output: True
        >>> bsc_address('0x4g5acf9684652BEa56F2f01b7101a225Eh33d23z')
        # Output: ValidationError(func=bsc_address, args=...)

    Args:
        value:
            BSC address string to validate.

    Returns:
        (Literal[True]): If `value` is a valid bsc address.
        (ValidationError): If `value` is an invalid bsc address.
    """
    if not value:
        return False

    if not re.fullmatch(r"0x[a-fA-F0-9]{40}", value):
        return False

    return True

validators.crypto_addresses.btc_address

BTC Address.

btc_address(value)

Return whether or not given value is a valid bitcoin address.

Full validation is implemented for P2PKH and P2SH addresses. For segwit addresses a regexp is used to provide a reasonable estimate on whether the address is valid.

Examples:

>>> btc_address('3Cwgr2g7vsi1bXDUkpEnVoRLA9w4FZfC69')
# Output: True
>>> btc_address('1BvBMsEYstWetqTFn5Au4m4GFg7xJaNVN2')
# Output: ValidationError(func=btc_address, args=...)

Parameters:

Name Type Description Default
value str

Bitcoin address string to validate.

required

Returns:

Type Description
Literal[True]

If value is a valid bitcoin address.

ValidationError

If value is an invalid bitcoin address.

Source code in src/validators/crypto_addresses/btc_address.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
@validator
def btc_address(value: str, /):
    """Return whether or not given value is a valid bitcoin address.

    Full validation is implemented for P2PKH and P2SH addresses.
    For segwit addresses a regexp is used to provide a reasonable
    estimate on whether the address is valid.

    Examples:
        >>> btc_address('3Cwgr2g7vsi1bXDUkpEnVoRLA9w4FZfC69')
        # Output: True
        >>> btc_address('1BvBMsEYstWetqTFn5Au4m4GFg7xJaNVN2')
        # Output: ValidationError(func=btc_address, args=...)

    Args:
        value:
            Bitcoin address string to validate.

    Returns:
        (Literal[True]): If `value` is a valid bitcoin address.
        (ValidationError): If `value` is an invalid bitcoin address.
    """
    if not value:
        return False

    return (
        # segwit pattern
        re.compile(r"^(bc|tc)[0-3][02-9ac-hj-np-z]{14,74}$").match(value)
        if value[:2] in ("bc", "tb")
        else _validate_old_btc_address(value)
    )

validators.crypto_addresses.eth_address

ETH Address.

eth_address(value)

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=...)

Parameters:

Name Type Description Default
value str

Ethereum address string to validate.

required

Returns:

Type Description
Literal[True]

If value is a valid ethereum address.

ValidationError

If value is an invalid ethereum address.

Source code in src/validators/crypto_addresses/eth_address.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
@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)

validators.crypto_addresses.trx_address

TRX Address.

trx_address(value)

Return whether or not given value is a valid tron address.

Full validation is implemented for TRC20 tron addresses.

Examples:

>>> trx_address('TLjfbTbpZYDQ4EoA4N5CLNgGjfbF8ZWz38')
# Output: True
>>> trx_address('TR2G7Rm4vFqF8EpY4U5xdLdQ7XgJ2U8Vd')
# Output: ValidationError(func=trx_address, args=...)

Parameters:

Name Type Description Default
value str

Tron address string to validate.

required

Returns:

Type Description
Literal[True]

If value is a valid tron address.

ValidationError

If value is an invalid tron address.

Source code in src/validators/crypto_addresses/trx_address.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
@validator
def trx_address(value: str, /):
    """Return whether or not given value is a valid tron address.

    Full validation is implemented for TRC20 tron addresses.

    Examples:
        >>> trx_address('TLjfbTbpZYDQ4EoA4N5CLNgGjfbF8ZWz38')
        # Output: True
        >>> trx_address('TR2G7Rm4vFqF8EpY4U5xdLdQ7XgJ2U8Vd')
        # Output: ValidationError(func=trx_address, args=...)

    Args:
        value:
            Tron address string to validate.

    Returns:
        (Literal[True]): If `value` is a valid tron address.
        (ValidationError): If `value` is an invalid tron address.
    """
    if not value:
        return False

    return re.compile(r"^[T][a-km-zA-HJ-NP-Z1-9]{33}$").match(
        value
    ) and _validate_trx_checksum_address(value)