From b515ffb05dd6a5999bf36d415c94498d0d5a2740 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20=C5=A0?= Date: Mon, 22 Jul 2019 15:27:10 +0200 Subject: [PATCH 01/12] Detect ENS registry's address for correct network --- ens/main.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/ens/main.py b/ens/main.py index 9789a2d405..e26c40e27c 100644 --- a/ens/main.py +++ b/ens/main.py @@ -30,6 +30,22 @@ ) ENS_MAINNET_ADDR = '0x314159265dD8dbb310642f98f50C066173C1259b' +ENS_ROPSTEN_ADDR = '0x112234455c3a32fd11230c42e7bccd4a84e02010' +ENS_RINKEBY_ADDR = '0xe7410170f87102df0055eb195163a03b7f2bff4a' +ENS_GOERLI_ADDR = '0x112234455c3a32fd11230c42e7bccd4a84e02010' + + +def get_address_for_network(net): + version = net.version + + if version == 1: + return ENS_MAINNET_ADDR + elif version == 3: + return ENS_ROPSTEN_ADDR + elif version == 4: + return ENS_RINKEBY_ADDR + elif version == 5: + return ENS_GOERLI_ADDR class ENS: @@ -57,7 +73,7 @@ def __init__(self, provider=default, addr=None): """ self.web3 = init_web3(provider) - ens_addr = addr if addr else ENS_MAINNET_ADDR + ens_addr = addr if addr else get_address_for_network(net=self.web3.net) self.ens = self.web3.eth.contract(abi=abis.ENS, address=ens_addr) self._resolverContract = self.web3.eth.contract(abi=abis.RESOLVER) From c47d0ee55d4f888f18f90fdf24eb3831ca8dd55f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20=C5=A0?= Date: Mon, 22 Jul 2019 22:41:15 +0200 Subject: [PATCH 02/12] Add UnknownNetwork exception --- ens/exceptions.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ens/exceptions.py b/ens/exceptions.py index 4048c5d2f2..dca5202f14 100644 --- a/ens/exceptions.py +++ b/ens/exceptions.py @@ -1,6 +1,14 @@ import idna +class UnknownNetwork(Exception): + """ + ENS is pre-configured only on some public networks. In order to use it + in local networks, users need to specify registry address manually. + """ + pass + + class AddressMismatch(ValueError): """ In order to set up reverse resolution correctly, the ENS name should first From 2c281f5a6a876a2c6e8019c7b03af69d353bc9d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20=C5=A0?= Date: Mon, 22 Jul 2019 22:50:01 +0200 Subject: [PATCH 03/12] Throw UnknownNetwork exception if network is not known --- ens/main.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ens/main.py b/ens/main.py index e26c40e27c..8f7efa9e26 100644 --- a/ens/main.py +++ b/ens/main.py @@ -11,6 +11,7 @@ REVERSE_REGISTRAR_DOMAIN, ) from ens.exceptions import ( + UnknownNetwork, AddressMismatch, UnauthorizedError, UnownedName, @@ -46,6 +47,11 @@ def get_address_for_network(net): return ENS_RINKEBY_ADDR elif version == 5: return ENS_GOERLI_ADDR + else: + raise UnknownNetwork( + "ENS is not available on the current network by default. " + "You need to manually set the `addr` parameter to the registry's address." + ) class ENS: From 2f6b39c33314ea7768d52b6ed683f090fdc69f03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20=C5=A0?= Date: Mon, 22 Jul 2019 23:05:41 +0200 Subject: [PATCH 04/12] Fix lint and docs & Convert version to int --- ens/main.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ens/main.py b/ens/main.py index 8f7efa9e26..e79a2bd38e 100644 --- a/ens/main.py +++ b/ens/main.py @@ -11,9 +11,9 @@ REVERSE_REGISTRAR_DOMAIN, ) from ens.exceptions import ( - UnknownNetwork, AddressMismatch, UnauthorizedError, + UnknownNetwork, UnownedName, ) from ens.utils import ( @@ -37,7 +37,7 @@ def get_address_for_network(net): - version = net.version + version = int(net.version) if version == 1: return ENS_MAINNET_ADDR @@ -76,6 +76,7 @@ def __init__(self, provider=default, addr=None): :type provider: instance of `web3.providers.base.BaseProvider` :param hex-string addr: the address of the ENS registry on-chain. If not provided, ENS.py will default to the mainnet ENS registry address. + :raises UnknownNetwork: if network is unknown """ self.web3 = init_web3(provider) From c197aa92b944d93333f4c4b169fbf29715d34e2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20=C5=A0?= Date: Mon, 5 Aug 2019 18:12:52 +0200 Subject: [PATCH 05/12] Use checksum addresses --- ens/main.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ens/main.py b/ens/main.py index e79a2bd38e..b9621f8481 100644 --- a/ens/main.py +++ b/ens/main.py @@ -31,9 +31,9 @@ ) ENS_MAINNET_ADDR = '0x314159265dD8dbb310642f98f50C066173C1259b' -ENS_ROPSTEN_ADDR = '0x112234455c3a32fd11230c42e7bccd4a84e02010' -ENS_RINKEBY_ADDR = '0xe7410170f87102df0055eb195163a03b7f2bff4a' -ENS_GOERLI_ADDR = '0x112234455c3a32fd11230c42e7bccd4a84e02010' +ENS_ROPSTEN_ADDR = '0x112234455C3a32FD11230C42E7Bccd4A84e02010' +ENS_RINKEBY_ADDR = '0xe7410170f87102DF0055eB195163A03B7F2Bff4A' +ENS_GOERLI_ADDR = '0x112234455C3a32FD11230C42E7Bccd4A84e02010' def get_address_for_network(net): From d07adaf30cd79d087481e98361db77bdbcda735f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20=C5=A0?= Date: Wed, 7 Aug 2019 18:59:23 +0200 Subject: [PATCH 06/12] Add Geth PoA middleware --- ens/utils.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ens/utils.py b/ens/utils.py index ec10e4b5e2..b8506bbb83 100644 --- a/ens/utils.py +++ b/ens/utils.py @@ -56,12 +56,20 @@ def init_web3(providers=default): def customize_web3(w3): from web3.middleware import make_stalecheck_middleware + from web3.middleware import geth_poa_middleware w3.middleware_onion.remove('name_to_address') + w3.middleware_onion.add( make_stalecheck_middleware(ACCEPTABLE_STALE_HOURS * 3600), name='stalecheck', ) + + w3.middleware_onion.inject( + geth_poa_middleware, + layer=0, + ) + return w3 From 8c98d59c8534bc24e05d907b48a2832fe4564aab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20=C5=A0?= Date: Thu, 8 Aug 2019 00:21:52 +0200 Subject: [PATCH 07/12] Use dict instead of function --- ens/main.py | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/ens/main.py b/ens/main.py index b9621f8481..d09f2e73f0 100644 --- a/ens/main.py +++ b/ens/main.py @@ -30,28 +30,12 @@ raw_name_to_hash, ) -ENS_MAINNET_ADDR = '0x314159265dD8dbb310642f98f50C066173C1259b' -ENS_ROPSTEN_ADDR = '0x112234455C3a32FD11230C42E7Bccd4A84e02010' -ENS_RINKEBY_ADDR = '0xe7410170f87102DF0055eB195163A03B7F2Bff4A' -ENS_GOERLI_ADDR = '0x112234455C3a32FD11230C42E7Bccd4A84e02010' - - -def get_address_for_network(net): - version = int(net.version) - - if version == 1: - return ENS_MAINNET_ADDR - elif version == 3: - return ENS_ROPSTEN_ADDR - elif version == 4: - return ENS_RINKEBY_ADDR - elif version == 5: - return ENS_GOERLI_ADDR - else: - raise UnknownNetwork( - "ENS is not available on the current network by default. " - "You need to manually set the `addr` parameter to the registry's address." - ) +NET_VERSION_TO_ENS_ADDR = { + 1: '0x314159265dD8dbb310642f98f50C066173C1259b', + 3: '0x112234455C3a32FD11230C42E7Bccd4A84e02010', + 4: '0xe7410170f87102DF0055eB195163A03B7F2Bff4A', + 5: '0x112234455C3a32FD11230C42E7Bccd4A84e02010', +} class ENS: @@ -80,7 +64,15 @@ def __init__(self, provider=default, addr=None): """ self.web3 = init_web3(provider) - ens_addr = addr if addr else get_address_for_network(net=self.web3.net) + try: + net_version = int(self.web3.net.version) + ens_addr = addr if addr else NET_VERSION_TO_ENS_ADDR[net_version] + except KeyError: + raise UnknownNetwork( + "ENS is not available on the current network by default. " + "You need to manually set the `addr` parameter to the registry's address." + ) + self.ens = self.web3.eth.contract(abi=abis.ENS, address=ens_addr) self._resolverContract = self.web3.eth.contract(abi=abis.RESOLVER) From 1e5230f4a03bea520e5428f02032d25661897cc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20=C5=A0?= Date: Thu, 8 Aug 2019 14:13:38 +0200 Subject: [PATCH 08/12] Move constants to correct file --- ens/constants.py | 12 ++++++++++++ ens/main.py | 8 +------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/ens/constants.py b/ens/constants.py index 89755a8751..00ab9b4916 100644 --- a/ens/constants.py +++ b/ens/constants.py @@ -8,3 +8,15 @@ EMPTY_ADDR_HEX = '0x' + '00' * 20 REVERSE_REGISTRAR_DOMAIN = 'addr.reverse' + +MAINNET_NET_VERSION = 1 +ROPSTEN_NET_VERSION = 3 +RINKEBY_NET_VERSION = 4 +GOERLI_NET_VERSION = 5 + +NET_VERSION_TO_ENS_ADDR = { + MAINNET_NET_VERSION: '0x314159265dD8dbb310642f98f50C066173C1259b', + ROPSTEN_NET_VERSION: '0x112234455C3a32FD11230C42E7Bccd4A84e02010', + RINKEBY_NET_VERSION: '0xe7410170f87102DF0055eB195163A03B7F2Bff4A', + GOERLI_NET_VERSION: '0x112234455C3a32FD11230C42E7Bccd4A84e02010', +} diff --git a/ens/main.py b/ens/main.py index d09f2e73f0..84cf0ff736 100644 --- a/ens/main.py +++ b/ens/main.py @@ -8,6 +8,7 @@ from ens import abis from ens.constants import ( EMPTY_ADDR_HEX, + NET_VERSION_TO_ENS_ADDR, REVERSE_REGISTRAR_DOMAIN, ) from ens.exceptions import ( @@ -30,13 +31,6 @@ raw_name_to_hash, ) -NET_VERSION_TO_ENS_ADDR = { - 1: '0x314159265dD8dbb310642f98f50C066173C1259b', - 3: '0x112234455C3a32FD11230C42E7Bccd4A84e02010', - 4: '0xe7410170f87102DF0055eB195163A03B7F2Bff4A', - 5: '0x112234455C3a32FD11230C42E7Bccd4A84e02010', -} - class ENS: """ From d851a4d0383496547a00b78566d02f8b42858a21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20=C5=A0?= Date: Sat, 28 Sep 2019 23:12:57 +0200 Subject: [PATCH 09/12] Use default address only if custom is not provided & Except StaleBlockchain error --- ens/main.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/ens/main.py b/ens/main.py index 84cf0ff736..91f687d026 100644 --- a/ens/main.py +++ b/ens/main.py @@ -30,6 +30,7 @@ normalize_name, raw_name_to_hash, ) +from web3.exceptions import StaleBlockchain class ENS: @@ -58,14 +59,17 @@ def __init__(self, provider=default, addr=None): """ self.web3 = init_web3(provider) - try: - net_version = int(self.web3.net.version) - ens_addr = addr if addr else NET_VERSION_TO_ENS_ADDR[net_version] - except KeyError: - raise UnknownNetwork( - "ENS is not available on the current network by default. " - "You need to manually set the `addr` parameter to the registry's address." - ) + if addr: + ens_addr = addr + else: + try: + net_version = int(self.web3.net.version) + ens_addr = NET_VERSION_TO_ENS_ADDR[net_version] + except (StaleBlockchain, KeyError): + raise UnknownNetwork( + "ENS is not available on the current network by default. " + "You need to manually set the `addr` parameter to the registry's address." + ) self.ens = self.web3.eth.contract(abi=abis.ENS, address=ens_addr) self._resolverContract = self.web3.eth.contract(abi=abis.RESOLVER) From c14ec2065e0ad3551b7fbdcbb8af67fec833b156 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20=C5=A0?= Date: Sun, 29 Sep 2019 17:20:57 +0200 Subject: [PATCH 10/12] Add net_version to skipped methods for stalecheck --- ens/main.py | 3 +-- web3/middleware/stalecheck.py | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ens/main.py b/ens/main.py index 91f687d026..c70978c45e 100644 --- a/ens/main.py +++ b/ens/main.py @@ -30,7 +30,6 @@ normalize_name, raw_name_to_hash, ) -from web3.exceptions import StaleBlockchain class ENS: @@ -65,7 +64,7 @@ def __init__(self, provider=default, addr=None): try: net_version = int(self.web3.net.version) ens_addr = NET_VERSION_TO_ENS_ADDR[net_version] - except (StaleBlockchain, KeyError): + except KeyError: raise UnknownNetwork( "ENS is not available on the current network by default. " "You need to manually set the `addr` parameter to the registry's address." diff --git a/web3/middleware/stalecheck.py b/web3/middleware/stalecheck.py index bd2ff372bb..459cd01097 100644 --- a/web3/middleware/stalecheck.py +++ b/web3/middleware/stalecheck.py @@ -6,6 +6,7 @@ SKIP_STALECHECK_FOR_METHODS = set([ 'eth_getBlockByNumber', + 'net_version', ]) From d2fdb692375eb3cf42a49f1526a3b0d4de4a3789 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20=C5=A0?= Date: Sat, 14 Dec 2019 18:30:42 +0100 Subject: [PATCH 11/12] Split addresses into multiple lines --- ens/constants.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/ens/constants.py b/ens/constants.py index 0bcb464c1f..abc1fce040 100644 --- a/ens/constants.py +++ b/ens/constants.py @@ -23,8 +23,12 @@ GOERLI_NET_VERSION = 5 NET_VERSION_TO_ENS_ADDR = { - MAINNET_NET_VERSION: ChecksumAddress(HexAddress(HexStr('0x314159265dD8dbb310642f98f50C066173C1259b'))), - ROPSTEN_NET_VERSION: ChecksumAddress(HexAddress(HexStr('0x112234455C3a32FD11230C42E7Bccd4A84e02010'))), - RINKEBY_NET_VERSION: ChecksumAddress(HexAddress(HexStr('0xe7410170f87102DF0055eB195163A03B7F2Bff4A'))), - GOERLI_NET_VERSION: ChecksumAddress(HexAddress(HexStr('0x112234455C3a32FD11230C42E7Bccd4A84e02010'))), + MAINNET_NET_VERSION: + ChecksumAddress(HexAddress(HexStr('0x314159265dD8dbb310642f98f50C066173C1259b'))), + ROPSTEN_NET_VERSION: + ChecksumAddress(HexAddress(HexStr('0x112234455C3a32FD11230C42E7Bccd4A84e02010'))), + RINKEBY_NET_VERSION: + ChecksumAddress(HexAddress(HexStr('0xe7410170f87102DF0055eB195163A03B7F2Bff4A'))), + GOERLI_NET_VERSION: + ChecksumAddress(HexAddress(HexStr('0x112234455C3a32FD11230C42E7Bccd4A84e02010'))), } From 04faa0200e8b8110ce7b8231802bc06a15915c88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20=C5=A0?= Date: Mon, 3 Feb 2020 19:10:34 +0100 Subject: [PATCH 12/12] Remove old import --- ens/main.py | 1 - 1 file changed, 1 deletion(-) diff --git a/ens/main.py b/ens/main.py index 8ff3b3e1be..567d6a3137 100644 --- a/ens/main.py +++ b/ens/main.py @@ -30,7 +30,6 @@ from ens.exceptions import ( AddressMismatch, UnauthorizedError, - UnknownNetwork, UnownedName, ) from ens.utils import (