-
Notifications
You must be signed in to change notification settings - Fork 169
Improvements in address code #311
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
Open
yabirgb
wants to merge
1
commit into
ethereum:main
Choose a base branch
from
yabirgb:improvements
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,6 @@ | |
| keccak, | ||
| ) | ||
| from .hexadecimal import ( | ||
| add_0x_prefix, | ||
| decode_hex, | ||
| encode_hex, | ||
| remove_0x_prefix, | ||
|
|
@@ -38,31 +37,21 @@ def is_hex_address(value: Any) -> bool: | |
| """ | ||
| Checks if the given string of text type is an address in hexadecimal encoded form. | ||
| """ | ||
| if not is_text(value): | ||
| return False | ||
| return _HEX_ADDRESS_REGEXP.fullmatch(value) is not None | ||
| return is_text(value) and _HEX_ADDRESS_REGEXP.fullmatch(value) is not None | ||
|
|
||
|
|
||
| def is_binary_address(value: Any) -> bool: | ||
| """ | ||
| Checks if the given string is an address in raw bytes form. | ||
| """ | ||
| if not is_bytes(value): | ||
| return False | ||
| elif len(value) != 20: | ||
| return False | ||
| else: | ||
| return True | ||
| return is_bytes(value) and len(value) == 20 | ||
|
|
||
|
|
||
| def is_address(value: Any) -> bool: | ||
| """ | ||
| Is the given string an address in any of the known formats? | ||
| """ | ||
| if is_hex_address(value) or is_binary_address(value): | ||
| return True | ||
|
|
||
| return False | ||
| return is_hex_address(value) or is_binary_address(value) | ||
|
|
||
|
|
||
| def to_normalized_address(value: Union[AnyAddress, str, bytes]) -> HexAddress: | ||
|
|
@@ -88,9 +77,8 @@ def is_normalized_address(value: Any) -> bool: | |
| """ | ||
| if not is_address(value): | ||
| return False | ||
| else: | ||
| is_equal = value == to_normalized_address(value) | ||
| return cast(bool, is_equal) | ||
|
|
||
| return cast(bool, value == to_normalized_address(value)) | ||
|
|
||
|
|
||
| def to_canonical_address(address: Union[AnyAddress, str, bytes]) -> Address: | ||
|
|
@@ -106,8 +94,8 @@ def is_canonical_address(address: Any) -> bool: | |
| """ | ||
| if not is_bytes(address) or len(address) != 20: | ||
| return False | ||
| is_equal = address == to_canonical_address(address) | ||
| return cast(bool, is_equal) | ||
|
|
||
| return cast(bool, address == to_canonical_address(address)) | ||
|
|
||
|
|
||
| def is_same_address( | ||
|
|
@@ -118,40 +106,32 @@ def is_same_address( | |
| """ | ||
| if not is_address(left) or not is_address(right): | ||
| raise ValueError("Both values must be valid addresses") | ||
| else: | ||
| return bool(to_normalized_address(left) == to_normalized_address(right)) | ||
|
|
||
| return to_normalized_address(left) == to_normalized_address(right) | ||
|
|
||
|
|
||
| def to_checksum_address(value: Union[AnyAddress, str, bytes]) -> ChecksumAddress: | ||
| """ | ||
| Makes a checksum address given a supported format. | ||
| """ | ||
| norm_address = to_normalized_address(value) | ||
| address_hash = encode_hex(keccak(text=remove_0x_prefix(HexStr(norm_address)))) | ||
|
|
||
| checksum_address = add_0x_prefix( | ||
| HexStr( | ||
| "".join( | ||
| ( | ||
| norm_address[i].upper() | ||
| if int(address_hash[i], 16) > 7 | ||
| else norm_address[i] | ||
| address_part = to_normalized_address(value)[2:42] | ||
| hash_hex = encode_hex(keccak(text=address_part))[2:] | ||
| return ChecksumAddress( | ||
| HexAddress( | ||
| HexStr( | ||
| "0x" | ||
| + "".join( | ||
| addr_char.upper() if int(hash_char, 16) > 7 else addr_char | ||
| for hash_char, addr_char in zip(hash_hex, address_part) | ||
| ) | ||
| for i in range(2, 42) | ||
| ) | ||
| ) | ||
| ) | ||
| return ChecksumAddress(HexAddress(checksum_address)) | ||
|
|
||
|
|
||
| def is_checksum_address(value: Any) -> bool: | ||
| if not is_text(value): | ||
| return False | ||
|
|
||
| if not is_hex_address(value): | ||
| return False | ||
| is_equal = value == to_checksum_address(value) | ||
| return cast(bool, is_equal) | ||
| # is_hex_address already checks if it is_text | ||
| return is_hex_address(value) and bool(value == to_checksum_address(value)) | ||
|
Comment on lines
+133
to
+134
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here it removes a redundant function call |
||
|
|
||
|
|
||
| def _is_checksum_formatted(value: Any) -> bool: | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the cast is not needed here. Only when the argument has type Any