Skip to content

Commit 606ee3a

Browse files
ankitchiplunkarcarver
authored andcommitted
Only try to resolve ENS names on mainnet
1 parent 38b0303 commit 606ee3a

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

docs/middleware.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ AttributeDict
4040
address that the name points to. For example :meth:`~web3.Eth.sendTransaction` will
4141
accept .eth names in the 'from' and 'to' fields.
4242

43+
.. note::
44+
This middleware only converts ENS names if invoked with the mainnet
45+
(where the ENS contract is deployed), for all other cases will result in an
46+
``InvalidAddress`` error
47+
4348
Pythonic
4449
~~~~~~~~~~~~
4550

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import pytest
2+
3+
from web3 import Web3
4+
from web3.exceptions import (
5+
InvalidAddress,
6+
)
7+
from web3.middleware import ( # noqa: F401
8+
construct_fixture_middleware,
9+
name_to_address_middleware,
10+
)
11+
from web3.providers.base import (
12+
BaseProvider,
13+
)
14+
15+
NAME = "dump.eth"
16+
ADDRESS = "0x0000000000000000000000000000000000000000"
17+
BALANCE = 0
18+
19+
20+
class TempENS():
21+
def __init__(self, name_addr_pairs):
22+
self.registry = dict(name_addr_pairs)
23+
24+
def address(self, name, guess_tld=True):
25+
# no automated web3 usages should be guessing the TLD
26+
assert not guess_tld
27+
return self.registry.get(name, None)
28+
29+
30+
@pytest.fixture
31+
def w3():
32+
w3 = Web3(providers=[BaseProvider()], middlewares=[])
33+
w3.ens = TempENS({NAME: ADDRESS})
34+
w3.middleware_stack.add(name_to_address_middleware(w3))
35+
return w3
36+
37+
38+
def test_pass_name_resolver(w3):
39+
return_chain_on_mainnet = construct_fixture_middleware({
40+
'net_version': '1',
41+
})
42+
return_balance = construct_fixture_middleware({
43+
'eth_getBalance': BALANCE
44+
})
45+
w3.middleware_stack.inject(return_chain_on_mainnet, layer=0)
46+
w3.middleware_stack.inject(return_balance, layer=0)
47+
assert w3.eth.getBalance(NAME) == BALANCE
48+
49+
50+
def test_fail_name_resolver(w3):
51+
return_chain_on_mainnet = construct_fixture_middleware({
52+
'net_version': '2',
53+
})
54+
w3.middleware_stack.inject(return_chain_on_mainnet, layer=0)
55+
with pytest.raises(InvalidAddress, match='.*ethereum\.eth.*'):
56+
w3.eth.getBalance("ethereum.eth")

web3/utils/normalizers.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
to_text,
3232
)
3333
from web3.utils.ens import (
34+
StaticENS,
3435
is_ens_name,
3536
validate_name_has_address,
3637
)
@@ -134,9 +135,20 @@ def abi_address_to_hex(abi_type, data):
134135
def abi_ens_resolver(w3, abi_type, val):
135136
if abi_type == 'address' and is_ens_name(val):
136137
if w3 is None:
137-
raise InvalidAddress("Could not look up name, because no web3 connection available")
138+
raise InvalidAddress(
139+
"Could not look up name %r because no web3"
140+
" connection available" % (val)
141+
)
138142
elif w3.ens is None:
139-
raise InvalidAddress("Could not look up name, because ENS is set to None")
143+
raise InvalidAddress(
144+
"Could not look up name %r because ENS is"
145+
" set to None" % (val)
146+
)
147+
elif int(w3.net.version) is not 1 and not isinstance(w3.ens, StaticENS):
148+
raise InvalidAddress(
149+
"Could not look up name %r because web3 is"
150+
" not connected to mainnet" % (val)
151+
)
140152
else:
141153
return (abi_type, validate_name_has_address(w3.ens, val))
142154
else:

0 commit comments

Comments
 (0)