-
Notifications
You must be signed in to change notification settings - Fork 162
feat: add validator for trx addresses #384
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
3 commits
Select commit
Hold shift + click to select a range
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
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
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
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
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,62 @@ | ||
"""TRX Address.""" | ||
|
||
# standard | ||
import hashlib | ||
import re | ||
|
||
# local | ||
from validators.utils import validator | ||
|
||
|
||
def _base58_decode(addr: str) -> bytes: | ||
"""Decode a base58 encoded address.""" | ||
alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" | ||
num = 0 | ||
for char in addr: | ||
num = num * 58 + alphabet.index(char) | ||
return num.to_bytes(25, byteorder="big") | ||
|
||
|
||
def _validate_trx_checksum_address(addr: str) -> bool: | ||
"""Validate TRX type checksum address.""" | ||
if len(addr) != 34: | ||
return False | ||
|
||
try: | ||
address = _base58_decode(addr) | ||
except ValueError: | ||
return False | ||
|
||
if len(address) != 25 or address[0] != 0x41: | ||
return False | ||
|
||
check_sum = hashlib.sha256(hashlib.sha256(address[:-4]).digest()).digest()[:4] | ||
return address[-4:] == check_sum | ||
|
||
|
||
@validator | ||
def trx_address(value: str, /): | ||
"""Return whether or not given value is a valid tron address. | ||
|
||
Full validation is implemented for TRC20 tron addresses. | ||
|
||
Examples: | ||
>>> trx_address('TLjfbTbpZYDQ4EoA4N5CLNgGjfbF8ZWz38') | ||
# Output: True | ||
>>> trx_address('TR2G7Rm4vFqF8EpY4U5xdLdQ7XgJ2U8Vd') | ||
# Output: ValidationError(func=trx_address, args=...) | ||
|
||
Args: | ||
value: | ||
Tron address string to validate. | ||
|
||
Returns: | ||
(Literal[True]): If `value` is a valid tron address. | ||
(ValidationError): If `value` is an invalid tron address. | ||
""" | ||
if not value: | ||
return False | ||
|
||
return re.compile(r"^[T][a-km-zA-HJ-NP-Z1-9]{33}$").match( | ||
value | ||
) and _validate_trx_checksum_address(value) |
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,54 @@ | ||
"""Test TRX address.""" | ||
|
||
# external | ||
import pytest | ||
|
||
# local | ||
from validators import ValidationError, trx_address | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"value", | ||
[ | ||
"TLjfbTbpZYDQ4EoA4N5CLNgGjfbF8ZWz38", | ||
"TDQ6C92wuNqvMWE967sMptCFaXq77uj1PF", | ||
"TFuGbxCQGSL4oLnJzVsen844LDwFbrUY4e", | ||
"TFAPKADDRhkSe3v27CsR8TZSjN8eJ8ycDK", | ||
"TSJHywLNva2MNjCD5iYfn5QAKD9Rk5Ncit", | ||
"TEi1qhi5LuTicg1u9oAstyXCSf5uibSyqo", | ||
"TAGvx5An6VBeHTu91cQwdABNcAYMRPcP4n", | ||
"TXbE5tXTejqT3Q47sYKCDb9NJDm3xrFpab", | ||
"TMTxQWNuWHXvHcYXc5D1wQhFmZFJijAxcG", | ||
"TPHgw9E8QYM3esNWih5KVnUVpUHwLTPfpA", | ||
"TFFLtBTi9jdaGwV3hznjCmPYaJme5AeqwU", | ||
"TC74QG8tbtixG5Raa4fEifywgjrFs45fNz", | ||
], | ||
) | ||
def test_returns_true_on_valid_trx_address(value: str): | ||
"""Test returns true on valid trx address.""" | ||
assert trx_address(value) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"value", | ||
[ | ||
"T12345678901234567890123456789012345", | ||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ12345678", | ||
"TR2G7Rm4vFqF8EpY4U5xdLdQ7XgJ2U8Vd", | ||
"TP6ah2v5mdsj8Z3hGz1yDMvDq7BzEbK8o", | ||
"TQmmhp6uz2Xre8yL3FsPYZyo4mhtw4vg4XX", | ||
"TQNy2C6VHJPk4P32bsEX3QSGx2Qqm4J2k9", | ||
"TP6ah2v5mdsj8Z3hGz1yDMvDq7BzEbK8oN", | ||
"TSTVdfU1x4L7K3Bc3v5C28Gp2J1rPyeL3f", | ||
"THPByuCzvU5QER9j2NC2mUQ2JPyRCam4e7", | ||
"TW5eZqUZgdW4rxFKAKsc2ryJbfFA94WXvD", | ||
"TR2G7Rm4vFqF8EpY4U5xdLdQ7XgJ2U8Vdd", | ||
"tQmmhp6uz2Xre8yL3FsPYZyo4mhtw4vg4X", | ||
"TR2G7Rm4vFqF8EpY4U5xdLdQ7Xg", | ||
"TQmmhp6uz2Xre8yL3FsPYZyo4mhtw4vg4x", | ||
"my-trox-address.trx", | ||
], | ||
) | ||
def test_returns_failed_validation_on_invalid_trx_address(value: str): | ||
"""Test returns failed validation on invalid trx address.""" | ||
assert isinstance(trx_address(value), ValidationError) |
Oops, something went wrong.
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.