-
Notifications
You must be signed in to change notification settings - Fork 162
add validator ETH addresses (ERC20) #276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
504d092
add validator for eth addresses
msamsami cef562c
add test for eth address validator
msamsami 498166c
add pysha3 dependency
msamsami 02267c3
move eth and btc validators to a new submodule
msamsami 8a66692
move eth and btc validators' tests to a new submodule
msamsami 875dcd3
add init files for new submodules
msamsami ec1071f
add trx validator to init
msamsami 11ee0e1
fix bug in importing validator
msamsami a3b7367
revert moving btc validator and its tests to new submodules
msamsami a97c5d1
fix import
msamsami d0a1eac
remove faulty test value
msamsami e4128c9
use relative import
msamsami 1715ea3
remove trx_validator
msamsami 8be96ea
fix bugs in test values
msamsami 8f2e253
combine eth regex checks into one check
msamsami 04d9b8b
check length in checksum validation
msamsami 3fffb0d
use eth-hash instead of pysha3
msamsami b40cb12
use external tag
msamsami 802487f
fix import tags
msamsami c825de2
add eth-hash backends dependency
msamsami 21d51f3
reformat using black
msamsami File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
"""Test crypto addresses.""" | ||
# -*- coding: utf-8 -*- | ||
|
||
# isort: skip_file |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
"""Test ETH address.""" | ||
# -*- coding: utf-8 -*- | ||
|
||
# external | ||
import pytest | ||
|
||
# local | ||
from validators import eth_address, ValidationFailure | ||
|
||
|
||
@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), ValidationFailure) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
"""Crypto addresses.""" | ||
# -*- coding: utf-8 -*- | ||
|
||
# isort: skip_file | ||
|
||
# local | ||
from .eth_address import eth_address | ||
|
||
__all__ = ("eth_address",) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
"""ETH Address.""" | ||
# -*- coding: utf-8 -*- | ||
|
||
# standard | ||
import re | ||
|
||
yozachar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# 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: ValidationFailure(func=eth_address, args=...) | ||
|
||
Args: | ||
value: | ||
Ethereum address string to validate. | ||
|
||
Returns: | ||
(Literal[True]): | ||
If `value` is a valid ethereum address. | ||
(ValidationFailure): | ||
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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.