Skip to content

Commit

Permalink
Remove guess_tld optional arg
Browse files Browse the repository at this point in the history
  • Loading branch information
njgheorghita committed Jan 14, 2019
1 parent 922dfea commit 2d8da1c
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 43 deletions.
6 changes: 2 additions & 4 deletions docs/ens_overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,8 @@ Look up the address for an ENS name

assert eth_address == '0x5B2063246F2191f18F2675ceDB8b28102e957458'


# ens.py will assume you want a .eth name if you don't specify a full name

assert ns.address('jasoncarver') == eth_address
# ens.py only support names using one of these recognized TLDs
# ['eth', 'reverse', 'test', 'luxe', 'xyz']


Get name from address
Expand Down
2 changes: 1 addition & 1 deletion ens/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ class UnderfundedBid(ValueError):

class InvalidTLD(ValueError):
'''
Raised if ...
Raised if name does not contain a recognized TLD.
'''
pass
15 changes: 2 additions & 13 deletions ens/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,14 @@ def fromWeb3(cls, web3, addr=None):
'''
return cls(web3.manager.provider, addr=addr)

# INFERS
def address(self, name, guess_tld=True):
def address(self, name):
'''
Look up the Ethereum address that `name` currently points to.
:param str name: an ENS name to look up
:param bool guess_tld: should `name` be appended with '.eth' if no common TLD found?
:raises InvalidName: if `name` has invalid syntax
'''
if guess_tld:
expanded = dot_eth_name(name)
else:
expanded = name
return self.resolve(expanded, 'addr')
return self.resolve(name, 'addr')

def name(self, address):
'''
Expand All @@ -99,7 +93,6 @@ def name(self, address):
return self.resolve(reversed_domain, get='name')
reverse = name

# INFER
@dict_copy
def setup_address(self, name, address=default, transact={}):
'''
Expand Down Expand Up @@ -137,7 +130,6 @@ def setup_address(self, name, address=default, transact={}):
resolver = self._set_resolver(name, transact=transact)
return resolver.setAddr(dot_eth_namehash(name), address, transact=transact)

# INFER
@dict_copy
def setup_name(self, name, address=None, transact={}):
'''
Expand Down Expand Up @@ -181,7 +173,6 @@ def setup_name(self, name, address=None, transact={}):
self.setup_address(name, address, transact=transact)
return self._setup_reverse(name, address, transact=transact)

# INFER
def resolve(self, name, get='addr'):
normal_name = normalize_name(name)
resolver = self.resolver(normal_name)
Expand All @@ -202,7 +193,6 @@ def reverser(self, target_address):
reversed_domain = address_to_reverse_domain(target_address)
return self.resolver(reversed_domain)

# INFER
def owner(self, name):
'''
Get the owner of a name. Note that this may be different from the
Expand All @@ -217,7 +207,6 @@ def owner(self, name):
node = dot_eth_namehash(name)
return self.ens.owner(node)

# INFER
@dict_copy
def setup_owner(self, name, new_owner=default, transact={}):
'''
Expand Down
13 changes: 6 additions & 7 deletions ens/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,18 @@ def is_valid_name(name):
return False


# INFERS
# CAN we get rid of this?
def label_to_name(label, default_tld, recognized_tlds):
def label_to_name(label, recognized_tlds):
label = normalize_name(label)
pieces = label.split('.')
if pieces[-1] not in recognized_tlds:
raise InvalidTLD("no valid tld")
raise InvalidTLD(
f"Label does not have a recognized TLD. TLD must match one of: {recognized_tlds}."
)
return '.'.join(pieces)


def dot_eth_name(label):
return label_to_name(label, 'eth', RECOGNIZED_TLDS)
return label_to_name(label, RECOGNIZED_TLDS)


def name_to_label(name, registrar):
Expand Down Expand Up @@ -193,8 +193,7 @@ def dot_eth_namehash(name):
In normal operation, generating the namehash is handled
behind the scenes. For advanced usage, it is a helpful utility.
!!This will add '.eth' to name if no TLD given. Also, it normalizes the name with
`nameprep
This normalizes the name with `nameprep
<https://github.com/ethereum/EIPs/blob/master/EIPS/eip-137.md#name-syntax>`_
before hashing.
Expand Down
14 changes: 1 addition & 13 deletions tests/ens/test_setup_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@
'tester.eth',
'0x2a7ac1c833d35677c2ff34a908951de142cc1653de6080ad4e38f4c9cc00aafe',
),
# (
# 'tester',
# 'tester.eth',
# '0x2a7ac1c833d35677c2ff34a908951de142cc1653de6080ad4e38f4c9cc00aafe',
# ),
(
'TESTER.eth',
'TESTER.eth',
Expand Down Expand Up @@ -74,14 +69,8 @@ def test_set_address(ens, name, full_name, namehash_hex, TEST_ADDRESS):
ens.setup_address(name, TEST_ADDRESS)
assert is_same_address(ens.address(name), TEST_ADDRESS)

# WHAT? AND VERIFY IN TEST_SETUP_NAME.py
# check that .eth is only appended if guess_tld is True
namehash = Web3.toBytes(hexstr=namehash_hex)
normal_name = ens.nameprep(full_name)
if ens.nameprep(name) == normal_name:
assert is_same_address(ens.address(name, guess_tld=False), TEST_ADDRESS)
else:
assert ens.address(name, guess_tld=False) is None

# check that the correct namehash is set:
assert is_same_address(ens.resolver(normal_name).addr(namehash), TEST_ADDRESS)
Expand All @@ -103,9 +92,8 @@ 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):
# assert ens.address(name) is None
owner = ens.owner('tester.eth')
with pytest.raises(InvalidTLD, match="no valid tld"):
with pytest.raises(InvalidTLD, match="Label does not have a recognized TLD."):
ens.setup_address(name, TEST_ADDRESS)


Expand Down
6 changes: 1 addition & 5 deletions tests/ens/test_setup_name.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ def test_setup_name(ens, name, normalized_name, namehash_hex):
ens.setup_name(name, address)
assert ens.name(address) == normalized_name

# check that .eth is only appended if guess_tld is True
if ens.nameprep(name) != normalized_name:
assert ens.address(name, guess_tld=False) is None

# check that the correct namehash is set:
node = Web3.toBytes(hexstr=namehash_hex)
assert ens.resolver(normalized_name).addr(node) == address
Expand All @@ -93,7 +89,7 @@ def test_cannot_setup_name_with_missing_or_invalid_tld(ens, name):
address = ens.web3.eth.accounts[3]
assert not ens.name(address)
owner = ens.owner('tester.eth')
with pytest.raises(InvalidTLD, match="no valid tld"):
with pytest.raises(InvalidTLD, match="Label does not have a recognized TLD."):
ens.setup_name(name, address)


Expand Down

0 comments on commit 2d8da1c

Please sign in to comment.