|
1 |
| -import re |
| 1 | +"""BTC Address.""" |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +# standard |
2 | 5 | from hashlib import sha256
|
| 6 | +import re |
3 | 7 |
|
| 8 | +# local |
4 | 9 | from .utils import validator
|
5 | 10 |
|
6 |
| -segwit_pattern = re.compile( |
7 |
| - r'^(bc|tc)[0-3][02-9ac-hj-np-z]{14,74}$') |
8 |
| - |
9 |
| - |
10 |
| -def validate_segwit_address(addr): |
11 |
| - return segwit_pattern.match(addr) |
| 11 | +_segwit_pattern = re.compile(r"^(bc|tc)[0-3][02-9ac-hj-np-z]{14,74}$") |
12 | 12 |
|
13 | 13 |
|
14 |
| -def decode_base58(addr): |
| 14 | +def _decode_base58(addr: str): |
| 15 | + """Decode base58.""" |
15 | 16 | alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
|
16 |
| - return sum([ |
17 |
| - (58 ** e) * alphabet.index(i) |
18 |
| - for e, i in enumerate(addr[::-1]) |
19 |
| - ]) |
| 17 | + return sum((58**enm) * alphabet.index(idx) for enm, idx in enumerate(addr[::-1])) |
20 | 18 |
|
21 | 19 |
|
22 |
| -def validate_old_btc_address(addr): |
23 |
| - "Validate P2PKH and P2SH type address" |
24 |
| - if not len(addr) in range(25, 35): |
| 20 | +def _validate_old_btc_address(addr: str): |
| 21 | + """Validate P2PKH and P2SH type address.""" |
| 22 | + if len(addr) not in range(25, 35): |
25 | 23 | return False
|
26 |
| - decoded_bytes = decode_base58(addr).to_bytes(25, "big") |
27 |
| - header = decoded_bytes[:-4] |
28 |
| - checksum = decoded_bytes[-4:] |
| 24 | + decoded_bytes = _decode_base58(addr).to_bytes(25, "big") |
| 25 | + header, checksum = decoded_bytes[:-4], decoded_bytes[-4:] |
29 | 26 | return checksum == sha256(sha256(header).digest()).digest()[:4]
|
30 | 27 |
|
31 | 28 |
|
32 | 29 | @validator
|
33 |
| -def btc_address(value): |
34 |
| - """ |
35 |
| - Return whether or not given value is a valid bitcoin address. |
36 |
| -
|
37 |
| - If the value is valid bitcoin address this function returns ``True``, |
38 |
| - otherwise :class:`~validators.utils.ValidationFailure`. |
| 30 | +def btc_address(value: str, /): |
| 31 | + """Return whether or not given value is a valid bitcoin address. |
39 | 32 |
|
40 | 33 | Full validation is implemented for P2PKH and P2SH addresses.
|
41 |
| - For segwit addresses a regexp is used to provide a reasonable estimate |
42 |
| - on whether the address is valid. |
| 34 | + For segwit addresses a regexp is used to provide a reasonable |
| 35 | + estimate on whether the address is valid. |
43 | 36 |
|
44 | 37 | Examples::
|
45 | 38 |
|
46 | 39 | >>> btc_address('3Cwgr2g7vsi1bXDUkpEnVoRLA9w4FZfC69')
|
47 |
| - True |
| 40 | + # Output: True |
| 41 | + >>> btc_address('1BvBMsEYstWetqTFn5Au4m4GFg7xJaNVN2') |
| 42 | + # Output: ValidationFailure(func=btc_address, args=...) |
| 43 | +
|
| 44 | + Args: |
| 45 | + `value`: |
| 46 | + [Required] Bitcoin address string to validate. |
| 47 | +
|
| 48 | + Returns: |
| 49 | + `True`: |
| 50 | + If the value is valid bitcoin address. |
| 51 | + `ValidationFailure`: |
| 52 | + If the value is an invalid bitcoin address. |
48 | 53 |
|
49 |
| - :param value: Bitcoin address string to validate |
50 | 54 | """
|
51 |
| - if not value or not isinstance(value, str): |
52 |
| - return False |
53 |
| - if value[:2] in ("bc", "tb"): |
54 |
| - return validate_segwit_address(value) |
55 |
| - return validate_old_btc_address(value) |
| 55 | + if value and type(value) is str: |
| 56 | + return ( |
| 57 | + _segwit_pattern.match(value) |
| 58 | + if value[:2] in ("bc", "tb") |
| 59 | + else _validate_old_btc_address(value) |
| 60 | + ) |
| 61 | + return False |
0 commit comments