Skip to content

Commit 75dcaad

Browse files
author
daveusa31
authored
Added validator btc addresses on format P2PKH (#166)
* Added validator btc addresses in P2PKH formate * Fix flake8 and isort * Added docs
1 parent 9c5e01e commit 75dcaad

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

docs/index.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ between
6969
.. autofunction:: between
7070

7171

72+
73+
btc_address
74+
------
75+
76+
.. module:: validators.btc_address
77+
78+
.. autofunction:: btc_address
79+
80+
81+
82+
7283
domain
7384
------
7485

tests/test_btc_address.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# -*- coding: utf-8 -*-
2+
import pytest
3+
4+
from validators import btc_address, ValidationFailure
5+
6+
7+
@pytest.mark.parametrize(('address',), [
8+
('17nuNm4QpgKuDvWy7Jh2AZ2nzZpMyKSKzT',),
9+
('3Cwgr2g7vsi1bXDUkpEnVoRLA9w4FZfC69',),
10+
])
11+
def test_returns_true_on_valid_mac_address(address):
12+
assert btc_address(address)
13+
14+
15+
@pytest.mark.parametrize(('address',), [
16+
('ff3Cwgr2g7vsi1bXDUkpEnVoRLA9w4FZfC69',),
17+
('b3Cgwgr2g7vsi1bXyjyDUkphEnVoRLA9w4FZfC69',),
18+
])
19+
def test_returns_failed_validation_on_invalid_mac_address(address):
20+
assert isinstance(btc_address(address), ValidationFailure)

validators/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from .between import between
2+
from .btc_address import btc_address
23
from .card import (
34
amex,
45
card_number,
@@ -29,6 +30,6 @@
2930
'ipv4_cidr', 'ipv6', 'ipv6_cidr', 'length', 'mac_address', 'slug',
3031
'truthy', 'url', 'ValidationFailure', 'validator', 'uuid',
3132
'card_number', 'visa', 'mastercard', 'amex', 'unionpay', 'diners',
32-
'jcb', 'discover')
33+
'jcb', 'discover', 'btc_address')
3334

3435
__version__ = '0.17.1'

validators/btc_address.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import re
2+
3+
from .utils import validator
4+
5+
pattern = re.compile(r"^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$")
6+
7+
8+
@validator
9+
def btc_address(value):
10+
"""
11+
Return whether or not given value is a valid bitcoin address.
12+
13+
If the value is valid bitcoin address this function returns ``True``,
14+
otherwise :class:`~validators.utils.ValidationFailure`.
15+
16+
Examples::
17+
18+
>>> btc_address('3Cwgr2g7vsi1bXDUkpEnVoRLA9w4FZfC69')
19+
True
20+
21+
:param value: Bitcoin address string to validate
22+
"""
23+
return pattern.match(value)

0 commit comments

Comments
 (0)