Skip to content

Commit 11d06e2

Browse files
committed
Enable additional tlds via environment variables for ens
1 parent fce2e20 commit 11d06e2

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

ens/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99

1010
MIN_ETH_LABEL_LENGTH = 7
1111

12-
RECOGNIZED_TLDS = ['eth', 'reverse', 'test', 'luxe', 'xyz']
12+
DEFAULT_RECOGNIZED_TLDS = ['eth', 'reverse', 'test', 'luxe', 'xyz']
1313

1414
REVERSE_REGISTRAR_DOMAIN = 'addr.reverse'

ens/utils.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
21
import copy
32
import datetime
43
import functools
4+
import os
55

66
from eth_utils import (
77
is_same_address,
@@ -14,9 +14,9 @@
1414
ACCEPTABLE_STALE_HOURS,
1515
AUCTION_START_GAS_CONSTANT,
1616
AUCTION_START_GAS_MARGINAL,
17+
DEFAULT_RECOGNIZED_TLDS,
1718
EMPTY_SHA3_BYTES,
1819
MIN_ETH_LABEL_LENGTH,
19-
RECOGNIZED_TLDS,
2020
REVERSE_REGISTRAR_DOMAIN,
2121
)
2222
from ens.exceptions import (
@@ -114,13 +114,25 @@ def label_to_name(label, recognized_tlds):
114114
pieces = label.split('.')
115115
if pieces[-1] not in recognized_tlds:
116116
raise InvalidTLD(
117-
f"Label does not have a recognized TLD. TLD must match one of: {recognized_tlds}."
117+
f"The label: {label} has an unsupported TLD of {pieces[-1]}. "
118+
f"ENS.py by default supports the following TLDs: {recognized_tlds}. "
119+
"If you'd like to use an unsupported TLD, please set the environment variable: "
120+
"'ENS_RECOGNIZED_TLDS' to a string of desired TLDs separated by a colon (:)."
118121
)
119122
return '.'.join(pieces)
120123

121124

122125
def dot_eth_name(label):
123-
return label_to_name(label, RECOGNIZED_TLDS)
126+
recognized_tlds = get_recognized_tlds()
127+
return label_to_name(label, recognized_tlds)
128+
129+
130+
def get_recognized_tlds():
131+
if 'ENS_RECOGNIZED_TLDS' in os.environ:
132+
override_tlds = os.environ['ENS_RECOGNIZED_TLDS'].split(':')
133+
return set(DEFAULT_RECOGNIZED_TLDS + override_tlds)
134+
else:
135+
return DEFAULT_RECOGNIZED_TLDS
124136

125137

126138
def name_to_label(name, registrar):

tests/ens/test_setup_address.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def test_set_address(ens, name, full_name, namehash_hex, TEST_ADDRESS):
9292
),
9393
)
9494
def test_set_address_raises_exception_with_invalid_or_missing_tld(ens, name, TEST_ADDRESS):
95-
with pytest.raises(InvalidTLD, match="Label does not have a recognized TLD."):
95+
with pytest.raises(InvalidTLD, match="ENS.py by default supports the following TLDs"):
9696
ens.setup_address(name, TEST_ADDRESS)
9797

9898

tests/ens/test_setup_name.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def test_setup_name(ens, name, normalized_name, namehash_hex):
8989
def test_cannot_setup_name_with_missing_or_invalid_tld(ens, name):
9090
address = ens.web3.eth.accounts[3]
9191
assert not ens.name(address)
92-
with pytest.raises(InvalidTLD, match="Label does not have a recognized TLD."):
92+
with pytest.raises(InvalidTLD, match="ENS.py by default supports the following TLDs"):
9393
ens.setup_name(name, address)
9494

9595

0 commit comments

Comments
 (0)