From cc921f70aec4398bca21b109264829fb5c4132f0 Mon Sep 17 00:00:00 2001 From: Mehdi Samsami Date: Tue, 25 Jun 2024 12:03:58 +0330 Subject: [PATCH] add a new feature to validate ethereum address --- pdm.lock | 59 ++++++++++++++++++- pyproject.toml | 4 +- src/validators/__init__.py | 3 +- src/validators/crypto_addresses/__init__.py | 6 ++ .../crypto_addresses/eth_address.py | 54 +++++++++++++++++ tests/crypto_addresses/test_eth_address.py | 44 ++++++++++++++ 6 files changed, 167 insertions(+), 3 deletions(-) create mode 100644 src/validators/crypto_addresses/eth_address.py create mode 100644 tests/crypto_addresses/test_eth_address.py diff --git a/pdm.lock b/pdm.lock index fd0bb463..6de6a4f2 100644 --- a/pdm.lock +++ b/pdm.lock @@ -5,7 +5,7 @@ groups = ["default", "docs-offline", "docs-online", "package", "runner", "sast", "tooling"] strategy = ["cross_platform", "inherit_metadata"] lock_version = "4.4.1" -content_hash = "sha256:f1143bde8b82a4e2bd7b47802b0d737bd856920f477462652751317cea175803" +content_hash = "sha256:c104f095424ff752e84f6bded48844feef50390a69f2d78eb298e35c4d546fd4" [[package]] name = "alabaster" @@ -317,6 +317,33 @@ files = [ {file = "docutils-0.20.1.tar.gz", hash = "sha256:f08a4e276c3a1583a86dce3e34aba3fe04d02bba2dd51ed16106244e8a923e3b"}, ] +[[package]] +name = "eth-hash" +version = "0.7.0" +requires_python = ">=3.8, <4" +summary = "eth-hash: The Ethereum hashing function, keccak256, sometimes (erroneously) called sha3" +groups = ["default"] +files = [ + {file = "eth-hash-0.7.0.tar.gz", hash = "sha256:bacdc705bfd85dadd055ecd35fd1b4f846b671add101427e089a4ca2e8db310a"}, + {file = "eth_hash-0.7.0-py3-none-any.whl", hash = "sha256:b8d5a230a2b251f4a291e3164a23a14057c4a6de4b0aa4a16fa4dc9161b57e2f"}, +] + +[[package]] +name = "eth-hash" +version = "0.7.0" +extras = ["pycryptodome"] +requires_python = ">=3.8, <4" +summary = "eth-hash: The Ethereum hashing function, keccak256, sometimes (erroneously) called sha3" +groups = ["default"] +dependencies = [ + "eth-hash==0.7.0", + "pycryptodome<4,>=3.6.6", +] +files = [ + {file = "eth-hash-0.7.0.tar.gz", hash = "sha256:bacdc705bfd85dadd055ecd35fd1b4f846b671add101427e089a4ca2e8db310a"}, + {file = "eth_hash-0.7.0-py3-none-any.whl", hash = "sha256:b8d5a230a2b251f4a291e3164a23a14057c4a6de4b0aa4a16fa4dc9161b57e2f"}, +] + [[package]] name = "exceptiongroup" version = "1.2.1" @@ -903,6 +930,36 @@ files = [ {file = "pluggy-1.5.0.tar.gz", hash = "sha256:2cffa88e94fdc978c4c574f15f9e59b7f4201d439195c3715ca9e2486f1d0cf1"}, ] +[[package]] +name = "pycryptodome" +version = "3.20.0" +requires_python = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" +summary = "Cryptographic library for Python" +groups = ["default"] +files = [ + {file = "pycryptodome-3.20.0-cp35-abi3-macosx_10_9_universal2.whl", hash = "sha256:ac1c7c0624a862f2e53438a15c9259d1655325fc2ec4392e66dc46cdae24d044"}, + {file = "pycryptodome-3.20.0-cp35-abi3-macosx_10_9_x86_64.whl", hash = "sha256:76658f0d942051d12a9bd08ca1b6b34fd762a8ee4240984f7c06ddfb55eaf15a"}, + {file = "pycryptodome-3.20.0-cp35-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:f35d6cee81fa145333137009d9c8ba90951d7d77b67c79cbe5f03c7eb74d8fe2"}, + {file = "pycryptodome-3.20.0-cp35-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:76cb39afede7055127e35a444c1c041d2e8d2f1f9c121ecef573757ba4cd2c3c"}, + {file = "pycryptodome-3.20.0-cp35-abi3-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:49a4c4dc60b78ec41d2afa392491d788c2e06edf48580fbfb0dd0f828af49d25"}, + {file = "pycryptodome-3.20.0-cp35-abi3-musllinux_1_1_aarch64.whl", hash = "sha256:fb3b87461fa35afa19c971b0a2b7456a7b1db7b4eba9a8424666104925b78128"}, + {file = "pycryptodome-3.20.0-cp35-abi3-musllinux_1_1_i686.whl", hash = "sha256:acc2614e2e5346a4a4eab6e199203034924313626f9620b7b4b38e9ad74b7e0c"}, + {file = "pycryptodome-3.20.0-cp35-abi3-musllinux_1_1_x86_64.whl", hash = "sha256:210ba1b647837bfc42dd5a813cdecb5b86193ae11a3f5d972b9a0ae2c7e9e4b4"}, + {file = "pycryptodome-3.20.0-cp35-abi3-win32.whl", hash = "sha256:8d6b98d0d83d21fb757a182d52940d028564efe8147baa9ce0f38d057104ae72"}, + {file = "pycryptodome-3.20.0-cp35-abi3-win_amd64.whl", hash = "sha256:9b3ae153c89a480a0ec402e23db8d8d84a3833b65fa4b15b81b83be9d637aab9"}, + {file = "pycryptodome-3.20.0-pp27-pypy_73-manylinux2010_x86_64.whl", hash = "sha256:4401564ebf37dfde45d096974c7a159b52eeabd9969135f0426907db367a652a"}, + {file = "pycryptodome-3.20.0-pp27-pypy_73-win32.whl", hash = "sha256:ec1f93feb3bb93380ab0ebf8b859e8e5678c0f010d2d78367cf6bc30bfeb148e"}, + {file = "pycryptodome-3.20.0-pp310-pypy310_pp73-macosx_10_9_x86_64.whl", hash = "sha256:acae12b9ede49f38eb0ef76fdec2df2e94aad85ae46ec85be3648a57f0a7db04"}, + {file = "pycryptodome-3.20.0-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:f47888542a0633baff535a04726948e876bf1ed880fddb7c10a736fa99146ab3"}, + {file = "pycryptodome-3.20.0-pp310-pypy310_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:6e0e4a987d38cfc2e71b4a1b591bae4891eeabe5fa0f56154f576e26287bfdea"}, + {file = "pycryptodome-3.20.0-pp310-pypy310_pp73-win_amd64.whl", hash = "sha256:c18b381553638414b38705f07d1ef0a7cf301bc78a5f9bc17a957eb19446834b"}, + {file = "pycryptodome-3.20.0-pp39-pypy39_pp73-macosx_10_9_x86_64.whl", hash = "sha256:a60fedd2b37b4cb11ccb5d0399efe26db9e0dd149016c1cc6c8161974ceac2d6"}, + {file = "pycryptodome-3.20.0-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:405002eafad114a2f9a930f5db65feef7b53c4784495dd8758069b89baf68eab"}, + {file = "pycryptodome-3.20.0-pp39-pypy39_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:2ab6ab0cb755154ad14e507d1df72de9897e99fd2d4922851a276ccc14f4f1a5"}, + {file = "pycryptodome-3.20.0-pp39-pypy39_pp73-win_amd64.whl", hash = "sha256:acf6e43fa75aca2d33e93409f2dafe386fe051818ee79ee8a3e21de9caa2ac9e"}, + {file = "pycryptodome-3.20.0.tar.gz", hash = "sha256:09609209ed7de61c2b560cc5c8c4fbf892f8b15b1faf7e4cbffac97db1fffda7"}, +] + [[package]] name = "pygments" version = "2.18.0" diff --git a/pyproject.toml b/pyproject.toml index 1cf7b901..847f87c8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,7 +35,9 @@ classifiers = [ ] requires-python = ">=3.8" dynamic = ["version"] -dependencies = [] +dependencies = [ + "eth-hash[pycryptodome]>=0.7.0", +] [project.urls] Homepage = "https://python-validators.github.io/validators" diff --git a/src/validators/__init__.py b/src/validators/__init__.py index 02e3ba95..23da82bd 100644 --- a/src/validators/__init__.py +++ b/src/validators/__init__.py @@ -5,7 +5,7 @@ from .card import amex, card_number, diners, discover, jcb, mastercard, unionpay, visa from .country import calling_code, country_code, currency from .cron import cron -from .crypto_addresses.btc_address import btc_address +from .crypto_addresses import btc_address, eth_address from .domain import domain from .email import email from .encoding import base58, base64 @@ -37,6 +37,7 @@ # ... "between", "btc_address", + "eth_address", # cards "amex", "card_number", diff --git a/src/validators/crypto_addresses/__init__.py b/src/validators/crypto_addresses/__init__.py index ba38e629..87c5e5c8 100644 --- a/src/validators/crypto_addresses/__init__.py +++ b/src/validators/crypto_addresses/__init__.py @@ -1 +1,7 @@ """Crypto addresses.""" + +# local +from .btc_address import btc_address +from .eth_address import eth_address + +__all__ = ("btc_address", "eth_address") diff --git a/src/validators/crypto_addresses/eth_address.py b/src/validators/crypto_addresses/eth_address.py new file mode 100644 index 00000000..0fb47b52 --- /dev/null +++ b/src/validators/crypto_addresses/eth_address.py @@ -0,0 +1,54 @@ +"""ETH Address.""" + +# standard +import re + +# external +from eth_hash.auto import keccak + +# local +from validators.utils import validator + + +def _validate_eth_checksum_address(addr: str): + """Validate ETH type checksum address.""" + addr = addr.replace("0x", "") + addr_hash = keccak.new(addr.lower().encode("ascii")).digest().hex() + + if len(addr) != 40: + return False + + for i in range(0, 40): + if (int(addr_hash[i], 16) > 7 and addr[i].upper() != addr[i]) or ( + int(addr_hash[i], 16) <= 7 and addr[i].lower() != addr[i] + ): + return False + return True + + +@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 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) diff --git a/tests/crypto_addresses/test_eth_address.py b/tests/crypto_addresses/test_eth_address.py new file mode 100644 index 00000000..2baaad5c --- /dev/null +++ b/tests/crypto_addresses/test_eth_address.py @@ -0,0 +1,44 @@ +"""Test ETH address.""" + +# external +import pytest + +# local +from validators import ValidationError, eth_address + + +@pytest.mark.parametrize( + "value", + [ + "0x8ba1f109551bd432803012645ac136ddd64dba72", + "0x9cc14ba4f9f68ca159ea4ebf2c292a808aaeb598", + "0x5AEDA56215b167893e80B4fE645BA6d5Bab767DE", + "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", + "0x742d35Cc6634C0532925a3b844Bc454e4438f44e", + "0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984", + "0x1234567890123456789012345678901234567890", + "0x57Ab1ec28D129707052df4dF418D58a2D46d5f51", + ], +) +def test_returns_true_on_valid_eth_address(value: str): + """Test returns true on valid eth address.""" + assert eth_address(value) + + +@pytest.mark.parametrize( + "value", + [ + "0x742d35Cc6634C0532925a3b844Bc454e4438f44g", + "0x742d35Cc6634C0532925a3b844Bc454e4438f44", + "0xAbcdefg1234567890Abcdefg1234567890Abcdefg", + "0x7c8EE9977c6f96b6b9774b3e8e4Cc9B93B12b2c72", + "0x80fBD7F8B3f81D0e1d6EACAb69AF104A6508AFB1", + "0x7c8EE9977c6f96b6b9774b3e8e4Cc9B93B12b2c7g", + "0x7c8EE9977c6f96b6b9774b3e8e4Cc9B93B12b2c", + "0x7Fb21a171205f3B8d8E4d88A2d2f8A56E45DdB5c", + "validators.eth", + ], +) +def test_returns_failed_validation_on_invalid_eth_address(value: str): + """Test returns failed validation on invalid eth address.""" + assert isinstance(eth_address(value), ValidationError)