Skip to content

btc_address

validators.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.

New in version 0.18.0.

Source code in /opt/hostedtoolcache/Python/3.11.7/x64/lib/python3.11/site-packages/validators/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
57
58
59
60
@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.

    > *New in version 0.18.0*.
    """
    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)
    )