Skip to content

Commit 5489605

Browse files
msamsamiyozachar
andauthored
feat: add validator for bsc addresses (#389)
- add a new validator for bsc addresses - minor fix in test cases - chore: updates CHANGES.md --------- Co-authored-by: Yozachar <[email protected]>
1 parent cd01b4b commit 5489605

File tree

8 files changed

+114
-4
lines changed

8 files changed

+114
-4
lines changed

CHANGES.md

+18
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@ Note to self: Breaking changes must increment either
99
1010
-->
1111

12+
## 0.33.0 (2024-07-15)
13+
14+
_**Breaking**_
15+
16+
> No breaking changes were introduced in this version.
17+
18+
_**Features**_
19+
20+
- feat: adds validator for `bsc` addresses by @msamsami in [#389](https://github.com/python-validators/validators/pull/389)
21+
22+
_**Maintenance**_
23+
24+
- chore: bump version by @msamsami in [#389](https://github.com/python-validators/validators/pull/389)
25+
26+
**Full Changelog**: [`0.32.0...0.33.0`](https://github.com/python-validators/validators/compare/0.32.0...0.33.0)
27+
28+
---
29+
1230
## 0.32.0 (2024-07-10)
1331

1432
_**Breaking**_

SECURITY.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
| Version | Supported |
66
| ---------- | ------------------ |
7-
| `>=0.32.0` | :white_check_mark: |
7+
| `>=0.33.0` | :white_check_mark: |
88

99
## Reporting a Vulnerability
1010

docs/api/crypto_addresses.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# crypto_addresses
22

3+
::: validators.crypto_addresses.bsc_address
34
::: validators.crypto_addresses.btc_address
45
::: validators.crypto_addresses.eth_address
56
::: validators.crypto_addresses.trx_address

docs/api/crypto_addresses.rst

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ crypto_addresses
22
----------------
33

44
.. module:: validators.crypto_addresses
5+
.. autofunction:: bsc_address
56
.. autofunction:: btc_address
67
.. autofunction:: eth_address
78
.. autofunction:: trx_address

src/validators/__init__.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from .card import amex, card_number, diners, discover, jcb, mastercard, unionpay, visa
66
from .country import calling_code, country_code, currency
77
from .cron import cron
8-
from .crypto_addresses import btc_address, eth_address, trx_address
8+
from .crypto_addresses import bsc_address, btc_address, eth_address, trx_address
99
from .domain import domain
1010
from .email import email
1111
from .encoding import base16, base32, base58, base64
@@ -37,6 +37,7 @@
3737
# ...
3838
"between",
3939
# crypto_addresses
40+
"bsc_address",
4041
"btc_address",
4142
"eth_address",
4243
"trx_address",
@@ -108,4 +109,4 @@
108109
"validator",
109110
)
110111

111-
__version__ = "0.32.0"
112+
__version__ = "0.33.0"
+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
"""Crypto addresses."""
22

33
# local
4+
from .bsc_address import bsc_address
45
from .btc_address import btc_address
56
from .eth_address import eth_address
67
from .trx_address import trx_address
78

8-
__all__ = ("btc_address", "eth_address", "trx_address")
9+
__all__ = ("bsc_address", "btc_address", "eth_address", "trx_address")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""BSC Address."""
2+
3+
# standard
4+
import re
5+
6+
# local
7+
from validators.utils import validator
8+
9+
10+
@validator
11+
def bsc_address(value: str, /):
12+
"""Return whether or not given value is a valid binance smart chain address.
13+
14+
Full validation is implemented for BSC addresses.
15+
16+
Examples:
17+
>>> bsc_address('0x4e5acf9684652BEa56F2f01b7101a225Ee33d23f')
18+
# Output: True
19+
>>> bsc_address('0x4g5acf9684652BEa56F2f01b7101a225Eh33d23z')
20+
# Output: ValidationError(func=bsc_address, args=...)
21+
22+
Args:
23+
value:
24+
BSC address string to validate.
25+
26+
Returns:
27+
(Literal[True]): If `value` is a valid bsc address.
28+
(ValidationError): If `value` is an invalid bsc address.
29+
"""
30+
if not value:
31+
return False
32+
33+
if not re.fullmatch(r"0x[a-fA-F0-9]{40}", value):
34+
return False
35+
36+
return True
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""Test BSC address."""
2+
3+
# external
4+
import pytest
5+
6+
# local
7+
from validators import ValidationError, bsc_address
8+
9+
10+
@pytest.mark.parametrize(
11+
"value",
12+
[
13+
"0x4e5acf9684652BEa56F2f01b7101a225Ee33d23f",
14+
"0x22B0f92af10FdC25659e4C3A590c2F0D0c809c27",
15+
"0xb61724F993E7942ef2d8e4A94fF7c9e1cc26995F",
16+
"0x9c3dF8a511Fec8076D4B8EFb4d5E733B9F953dD7",
17+
"0x4536337B91c0623a4FD098023E6065e4773117c5",
18+
"0xAC484e1CE274eD1d40A7C2AeAb0bEA863634286F",
19+
"0x1FDE521fBe3483Cbb5957E6275028225a74387e4",
20+
"0x1693c3D1bA787Ba2bf81ac8897614AAaee5cb800",
21+
"0xf4C3Fd476A40658aEd9e595DA49c37d8965D2fFE",
22+
"0xc053E3D4932640787D6Cf67FcA36021E7BE62653",
23+
"0xaFd563A5aED0bC363e802842aD93Af46c1168b8a",
24+
],
25+
)
26+
def test_returns_true_on_valid_bsc_address(value: str):
27+
"""Test returns true on valid bsc address."""
28+
assert bsc_address(value)
29+
30+
31+
@pytest.mark.parametrize(
32+
"value",
33+
[
34+
"1x32Be343B94f860124dC4fEe278FDCBD38C102D88",
35+
"0x32Be343B94f860124dC4fEe278FDCBD38C102D",
36+
"0x32Be343B94f860124dC4fEe278FDCBD38C102D88aabbcc",
37+
"0x4g5acf9684652BEa56F2f01b7101a225Eh33d23z",
38+
"0x",
39+
40+
"0x32Be343B94f860124dC4fEe278FDCBD38C102D__",
41+
"0x32Be343B94f860124dC4fEe278FDCBD38C102D88G",
42+
"0X32Be343B94f860124dC4fEe278FDCBD38C102D88",
43+
"0X32BE343B94F860124DCFEE278FDCBD38C102D88",
44+
"0x32Be 343B94f860124dC4fEe278FDCBD38C102D88",
45+
"0x32Be343B94f860124dC4fEe278FDCBD38C102D88!",
46+
"ox32Be343B94f860124dC4fEe278FDCBD38C102D88",
47+
"0x32Be343B94f860124dC4fEe278FDCBD38C102D88XYZ",
48+
],
49+
)
50+
def test_returns_failed_validation_on_invalid_bsc_address(value: str):
51+
"""Test returns failed validation on invalid bsc address."""
52+
assert isinstance(bsc_address(value), ValidationError)

0 commit comments

Comments
 (0)