The Ethereum Name Service is analogous to the Domain Name Service. It enables users and developers to use human-friendly names in place of error-prone hexadecimal addresses, content hashes, and more.
The ens
module is included with web3.py. It provides an interface to look up an address from a name, set up your own address, and more.
Create an ~ens.main.ENS
object (named ns
below) in one of three ways:
- Automatic detection
- Specify an instance or list of
providers
- From an existing
web3.Web3
object
# automatic detection
from ens.auto import ns
# or, with a provider
from web3 import IPCProvider
from ens import ENS
provider = IPCProvider(...)
ns = ENS(provider)
# or, with a w3 instance
# Note: This inherits the w3 middlewares from the w3 instance and adds a stalecheck middleware to the middleware onion
from ens import ENS
w3 = Web3(...)
ns = ENS.fromWeb3(w3)
from ens.auto import ns
# look up the hex representation of the address for a name
eth_address = ns.address('jasoncarver.eth')
assert eth_address == '0x5B2063246F2191f18F2675ceDB8b28102e957458'
The ENS
module has no opinion as to which TLD you can use, but will not infer a TLD if it is not provided with the name.
domain = ns.name('0x5B2063246F2191f18F2675ceDB8b28102e957458')
# name() also accepts the bytes version of the address
assert ns.name(b'[ c$o!\x91\xf1\x8f&u\xce\xdb\x8b(\x10.\x95tX') == domain
# confirm that the name resolves back to the address that you looked up:
assert ns.address(domain) == '0x5B2063246F2191f18F2675ceDB8b28102e957458'
eth_address = ns.owner('exchange.eth')
Do you want to set up your name so that ~ens.main.ENS.address
will show the address it points to?
ns.setup_address('jasoncarver.eth', '0x5B2063246F2191f18F2675ceDB8b28102e957458')
You must already be the owner of the domain (or its parent).
In the common case where you want to point the name to the owning address, you can skip the address
ns.setup_address('jasoncarver.eth')
You can claim arbitrarily deep subdomains. Gas costs scale up with the number of subdomains!
ns.setup_address('supreme.executive.power.derives.from.a.mandate.from.the.masses.jasoncarver.eth')
Wait for the transaction to be mined, then:
assert ns.address('supreme.executive.power.derives.from.a.mandate.from.the.masses.jasoncarver.eth') == \
'0x5B2063246F2191f18F2675ceDB8b28102e957458'
Do you want to set up your address so that ~ens.main.ENS.name
will show the name that points to it?
This is like Caller ID. It enables you and others to take an account and determine what name points to it. Sometimes this is referred to as "reverse" resolution.
ns.setup_name('jasoncarver.eth', '0x5B2063246F2191f18F2675ceDB8b28102e957458')
Note
Do not rely on reverse resolution for security.
Anyone can claim any "caller ID". Only forward resolution implies that the owner of the name gave their stamp of approval.
If you don't supply the address, ~ens.main.ENS.setup_name
will assume you want the address returned by ~ens.main.ENS.address
.
ns.setup_name('jasoncarver.eth')
If the name doesn't already point to an address, ~ens.main.ENS.setup_name
will call ~ens.main.ENS.setup_address
for you.
Wait for the transaction to be mined, then:
assert ns.name('0x5B2063246F2191f18F2675ceDB8b28102e957458') == 'jasoncarver.eth'