Skip to content

Commit 4d6a955

Browse files
Byczongdjstrong
andauthored
feat: [sc-26096] Update web3.py dependency in NameGuard (#567)
* update web3; fix: use hex with 0x * use updated snake case method * update ENS * typo fix --------- Co-authored-by: djstrong <[email protected]>
1 parent e7ea18f commit 4d6a955

File tree

5 files changed

+186
-442
lines changed

5 files changed

+186
-442
lines changed

packages/nameguard-python/nameguard/nameguard.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def init_inspector():
9191
return Inspector(config)
9292

9393

94-
ens_contract_adresses = {
94+
ens_contract_addresses = {
9595
'0x57f1887a8bf19b14fc0df6fd9b2acc9af147ea85', # Base Registrar
9696
'0xd4416b13d2b3a9abae7acd5d6c2bbdbe25686401', # Name Wrapper
9797
}
@@ -512,7 +512,7 @@ async def fake_eth_name_check(self, network_name, contract_address, token_id) ->
512512

513513
token_type = res_json['id']['tokenMetadata']['tokenType']
514514

515-
if token_type not in ['ERC721', 'ERC1155'] and contract_address in ens_contract_adresses:
515+
if token_type not in ['ERC721', 'ERC1155'] and contract_address in ens_contract_addresses:
516516
return FakeEthNameCheckResult(
517517
status=FakeEthNameCheckStatus.UNKNOWN_NFT, nameguard_report=None, investigated_fields=None
518518
)
@@ -543,7 +543,7 @@ async def fake_eth_name_check(self, network_name, contract_address, token_id) ->
543543
except KeyError:
544544
pass
545545

546-
if contract_address in ens_contract_adresses:
546+
if contract_address in ens_contract_addresses:
547547
if ALCHEMY_UNKNOWN_NAME.match(title):
548548
unknown_name = f"[{res_json['id']['tokenId'][2:]}].eth"
549549
investigated_fields['title'] = unknown_name
@@ -555,7 +555,7 @@ async def fake_eth_name_check(self, network_name, contract_address, token_id) ->
555555
async def _fake_eth_name_check(
556556
self, network_name, contract_address, fields: dict[str, str]
557557
) -> FakeEthNameCheckResult:
558-
if contract_address in ens_contract_adresses:
558+
if contract_address in ens_contract_addresses:
559559
if 'title' not in fields:
560560
raise MissingTitle()
561561

packages/nameguard-python/nameguard/our_ens.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
from ens.constants import ENS_EXTENDED_RESOLVER_INTERFACE_ID, EMPTY_SHA3_BYTES
2828
from ens.ens import _resolver_supports_interface, ENS
29-
from ens.exceptions import ENSValidationError
29+
from ens.exceptions import ENSValidationError, ENSValueError
3030
from ens.utils import address_to_reverse_domain, is_empty_name, is_none_or_zero_address, Web3
3131

3232
# from ens.utils import normalize_name
@@ -39,17 +39,17 @@
3939
def label_to_hash(label: str) -> HexBytes:
4040
# label = normalize_name(label)
4141
if '.' in label:
42-
raise ValueError(f"Cannot generate hash for label {label!r} with a '.'")
42+
raise ENSValueError(f"Cannot generate hash for label {label!r} with a '.'")
4343
return Web3().keccak(text=label)
4444

4545

4646
def normal_name_to_hash(name: str) -> HexBytes:
4747
"""
48-
This method will not normalize the name. 'normal' name here means the name
49-
should already be normalized before calling this method.
48+
Hashes a pre-normalized name.
49+
The normalization of the name is a prerequisite and is not handled by this function.
5050
51-
:param name: the name to hash - should already be normalized
52-
:return: namehash the hash of the name
51+
:param str name: A normalized name string to be hashed.
52+
:return: namehash - the hash of the name
5353
:rtype: HexBytes
5454
"""
5555
node = EMPTY_SHA3_BYTES
@@ -71,7 +71,7 @@ def raw_name_to_hash(name: str) -> HexBytes:
7171
behind the scenes. For advanced usage, it is a helpful utility.
7272
7373
This normalizes the name with `nameprep
74-
<https://github.com/ethereum/EIPs/blob/master/EIPS/eip-137.md#name-syntax>`_
74+
<https://github.com/ethereum/EIPs/blob/master/EIPS/eip-137.md#name-syntax>`_ # blocklint: pragma # noqa: E501
7575
before hashing.
7676
7777
:param str name: ENS name to hash
@@ -85,7 +85,7 @@ def raw_name_to_hash(name: str) -> HexBytes:
8585

8686

8787
def ens_encode_name(name: str) -> bytes:
88-
"""
88+
r"""
8989
Encode a name according to DNS standards specified in section 3.1
9090
of RFC1035 with the following validations:
9191
@@ -135,9 +135,7 @@ def name(self, address: ChecksumAddress) -> Optional[str]:
135135

136136
# To be absolutely certain of the name, via reverse resolution,
137137
# the address must match in the forward resolution
138-
result = name if to_checksum_address(address) == self.address(name) else None
139-
140-
return result
138+
return name if to_checksum_address(address) == self.address(name) else None
141139

142140
def resolver(self, name: str) -> Optional['Contract']:
143141
"""
@@ -154,7 +152,7 @@ def resolver(self, name: str) -> Optional['Contract']:
154152
def namehash(name: str) -> HexBytes:
155153
return raw_name_to_hash(name)
156154

157-
def _resolve(self, name: str, fn_name: str = 'addr') -> Optional[Union[ChecksumAddress, str]]:
155+
def _resolve(self, name: str, fn_name: str = 'addr') -> Optional[Union[ChecksumAddress, str]]: # OK
158156
# normal_name = normalize_name(name)
159157
normal_name = name
160158
resolver, current_name = self._get_resolver(normal_name, fn_name)
@@ -167,9 +165,9 @@ def _resolve(self, name: str, fn_name: str = 'addr') -> Optional[Union[ChecksumA
167165
if _resolver_supports_interface(resolver, ENS_EXTENDED_RESOLVER_INTERFACE_ID):
168166
contract_func_with_args = (fn_name, [node])
169167

170-
calldata = resolver.encodeABI(*contract_func_with_args)
168+
calldata = resolver.encode_abi(*contract_func_with_args)
171169
contract_call_result = resolver.caller.resolve(
172-
ens_encode_name(normal_name), # TODO ens_encode_name
170+
ens_encode_name(normal_name),
173171
calldata,
174172
)
175173
result = self._decode_ensip10_resolve_data(contract_call_result, resolver, fn_name)

packages/nameguard-python/nameguard/utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def labelhash_in_name(x: str) -> bool:
4141

4242

4343
def hexbytes_to_int(hb: HexBytes) -> int:
44-
return int(hb.hex(), base=16)
44+
return int(hb.to_0x_hex(), base=16)
4545

4646

4747
def int_to_hexstr(n: int, hex_len=64) -> str:
@@ -69,7 +69,7 @@ def int_to_hexstr(n: int, hex_len=64) -> str:
6969

7070

7171
def labelhash_from_label(label: str) -> str:
72-
return Web3().keccak(text=label).hex()
72+
return Web3().keccak(text=label).to_0x_hex()
7373

7474

7575
def namehash_from_name(name: str) -> str:
@@ -86,13 +86,13 @@ def namehash_from_name(name: str) -> str:
8686
else:
8787
labelhash = Web3().keccak(text=label)
8888
node = Web3().keccak(node + labelhash)
89-
return node.hex()
89+
return node.to_0x_hex()
9090

9191

9292
def namehash_from_labelhash(labelhash_hexstr: str, parent_name='eth') -> str:
9393
parent_namehash_hexstr = namehash_from_name(parent_name)
9494
node = Web3().keccak(HexBytes(parent_namehash_hexstr) + HexBytes(labelhash_hexstr))
95-
return node.hex()
95+
return node.to_0x_hex()
9696

9797

9898
def validate_token_id(token_id: str) -> str:

0 commit comments

Comments
 (0)