Skip to content

Commit

Permalink
Enable additional tlds via environment variables for ens
Browse files Browse the repository at this point in the history
  • Loading branch information
njgheorghita committed Jan 14, 2019
1 parent fce2e20 commit 11d06e2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion ens/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@

MIN_ETH_LABEL_LENGTH = 7

RECOGNIZED_TLDS = ['eth', 'reverse', 'test', 'luxe', 'xyz']
DEFAULT_RECOGNIZED_TLDS = ['eth', 'reverse', 'test', 'luxe', 'xyz']

REVERSE_REGISTRAR_DOMAIN = 'addr.reverse'
20 changes: 16 additions & 4 deletions ens/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import copy
import datetime
import functools
import os

from eth_utils import (
is_same_address,
Expand All @@ -14,9 +14,9 @@
ACCEPTABLE_STALE_HOURS,
AUCTION_START_GAS_CONSTANT,
AUCTION_START_GAS_MARGINAL,
DEFAULT_RECOGNIZED_TLDS,
EMPTY_SHA3_BYTES,
MIN_ETH_LABEL_LENGTH,
RECOGNIZED_TLDS,
REVERSE_REGISTRAR_DOMAIN,
)
from ens.exceptions import (
Expand Down Expand Up @@ -114,13 +114,25 @@ def label_to_name(label, recognized_tlds):
pieces = label.split('.')
if pieces[-1] not in recognized_tlds:
raise InvalidTLD(
f"Label does not have a recognized TLD. TLD must match one of: {recognized_tlds}."
f"The label: {label} has an unsupported TLD of {pieces[-1]}. "
f"ENS.py by default supports the following TLDs: {recognized_tlds}. "
"If you'd like to use an unsupported TLD, please set the environment variable: "
"'ENS_RECOGNIZED_TLDS' to a string of desired TLDs separated by a colon (:)."
)
return '.'.join(pieces)


def dot_eth_name(label):
return label_to_name(label, RECOGNIZED_TLDS)
recognized_tlds = get_recognized_tlds()
return label_to_name(label, recognized_tlds)


def get_recognized_tlds():
if 'ENS_RECOGNIZED_TLDS' in os.environ:
override_tlds = os.environ['ENS_RECOGNIZED_TLDS'].split(':')
return set(DEFAULT_RECOGNIZED_TLDS + override_tlds)
else:
return DEFAULT_RECOGNIZED_TLDS


def name_to_label(name, registrar):
Expand Down
2 changes: 1 addition & 1 deletion tests/ens/test_setup_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def test_set_address(ens, name, full_name, namehash_hex, TEST_ADDRESS):
),
)
def test_set_address_raises_exception_with_invalid_or_missing_tld(ens, name, TEST_ADDRESS):
with pytest.raises(InvalidTLD, match="Label does not have a recognized TLD."):
with pytest.raises(InvalidTLD, match="ENS.py by default supports the following TLDs"):
ens.setup_address(name, TEST_ADDRESS)


Expand Down
2 changes: 1 addition & 1 deletion tests/ens/test_setup_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def test_setup_name(ens, name, normalized_name, namehash_hex):
def test_cannot_setup_name_with_missing_or_invalid_tld(ens, name):
address = ens.web3.eth.accounts[3]
assert not ens.name(address)
with pytest.raises(InvalidTLD, match="Label does not have a recognized TLD."):
with pytest.raises(InvalidTLD, match="ENS.py by default supports the following TLDs"):
ens.setup_name(name, address)


Expand Down

0 comments on commit 11d06e2

Please sign in to comment.