1
1
.. _ens_overview :
2
2
3
3
Ethereum Name Service
4
- ================================
4
+ =====================
5
5
6
6
The Ethereum Name Service is analogous to the Domain Name Service. It
7
7
enables users and developers to use human-friendly names in place of error-prone
@@ -19,138 +19,123 @@ Create an :class:`~ens.main.ENS` object (named ``ns`` below) in one of three way
19
19
2. Specify an instance or list of :ref: `providers `
20
20
3. From an existing :class: `web3.Web3 ` object
21
21
22
- ::
22
+ .. code-block :: python
23
23
24
24
# automatic detection
25
25
from ens.auto import ns
26
26
27
-
28
27
# or, with a provider
29
28
from web3 import IPCProvider
30
29
from ens import ENS
31
30
32
31
provider = IPCProvider(... )
33
32
ns = ENS(provider)
34
33
35
-
36
34
# or, with a w3 instance
37
35
# Note: This inherits the w3 middlewares from the w3 instance and adds a stalecheck middleware to the middleware onion
38
36
from ens import ENS
39
-
40
37
w3 = Web3(... )
41
38
ns = ENS .fromWeb3(w3)
42
39
40
+ ....
43
41
44
42
Usage
45
43
-----
46
44
47
- Name info
45
+ Name Info
48
46
~~~~~~~~~
49
47
50
48
.. _ens_get_address :
51
49
52
- Look up the address for an ENS name
53
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
50
+ Get the Address for an ENS Name
51
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
54
52
55
- ::
53
+ .. code-block :: python
56
54
57
55
from ens.auto import ns
58
-
59
-
60
- # look up the hex representation of the address for a name
61
-
62
56
eth_address = ns.address(' jasoncarver.eth' )
63
-
64
57
assert eth_address == ' 0x5B2063246F2191f18F2675ceDB8b28102e957458'
65
58
66
-
67
59
The ``ENS `` module has no opinion as to which TLD you can use,
68
60
but will not infer a TLD if it is not provided with the name.
69
61
62
+ Get the ENS Name for an Address
63
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
70
64
71
- Get name from address
72
- ^^^^^^^^^^^^^^^^^^^^^
73
-
74
- ::
65
+ .. code-block :: python
75
66
76
67
domain = ns.name(' 0x5B2063246F2191f18F2675ceDB8b28102e957458' )
77
68
78
-
79
69
# name() also accepts the bytes version of the address
80
-
81
70
assert ns.name(b ' [ c$o!\x91\xf1\x8f &u\xce\xdb\x8b (\x10 .\x95 tX' ) == domain
82
71
83
-
84
72
# confirm that the name resolves back to the address that you looked up:
85
-
86
73
assert ns.address(domain) == ' 0x5B2063246F2191f18F2675ceDB8b28102e957458'
87
74
88
- Get owner of name
89
- ^^^^^^^^^^^^^^^^^
75
+ .. note :: For accuracy, and as a recommendation from the ENS documentation on
76
+ `reverse resolution <https://docs.ens.domains/dapp-developer-guide/resolving-names#reverse-resolution >`_,
77
+ the ``ENS `` module now verifies that the forward resolution matches the address with every call to get the
78
+ ``name() `` for an address. This is the only sure way to know whether the reverse resolution is correct. Anyone can
79
+ claim any name, only forward resolution implies that the owner of the name gave their stamp of approval.
90
80
91
- ::
81
+ Get the Owner of a Name
82
+ ^^^^^^^^^^^^^^^^^^^^^^^
83
+
84
+ .. code-block :: python
92
85
93
86
eth_address = ns.owner(' exchange.eth' )
94
87
95
- Set up your name
96
- ~~~~~~~~~~~~~~~~
88
+ ....
97
89
98
- Point your name to your address
99
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
90
+ Set Up Your Name and Address
91
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
100
92
101
- Do you want to set up your name so that :meth: ` ~ens.main.ENS.address ` will show the
102
- address it points to?
93
+ Link a Name to an Address
94
+ ^^^^^^^^^^^^^^^^^^^^^^^^^
103
95
104
- ::
96
+ You can set up your name so that :meth: `~ens.main.ENS.address ` will show the address it points to. In order to do so,
97
+ you must already be the owner of the domain (or its parent).
105
98
106
- ns.setup_address('jasoncarver.eth', '0x5B2063246F2191f18F2675ceDB8b28102e957458')
99
+ .. code-block :: python
107
100
108
- You must already be the owner of the domain (or its parent).
101
+ ns.setup_address( ' jasoncarver.eth ' , ' 0x5B2063246F2191f18F2675ceDB8b28102e957458 ' )
109
102
110
- In the common case where you want to point the name to the owning
111
- address, you can skip the address
103
+ In the common case where you want to point the name to the owning address, you can skip the address.
112
104
113
- ::
105
+ .. code-block :: python
114
106
115
107
ns.setup_address(' jasoncarver.eth' )
116
108
117
- You can claim arbitrarily deep subdomains. *Gas costs scale up with the
118
- number of subdomains! *
109
+ You can claim arbitrarily deep subdomains.
119
110
120
- ::
111
+ .. code-block :: python
121
112
122
113
ns.setup_address(' supreme.executive.power.derives.from.a.mandate.from.the.masses.jasoncarver.eth' )
123
114
124
- Wait for the transaction to be mined, then:
125
-
126
- ::
115
+ # wait for the transaction to be mined, then:
116
+ assert (
117
+ ns.address(' supreme.executive.power.derives.from.a.mandate.from.the.masses.jasoncarver.eth' )
118
+ == ' 0x5B2063246F2191f18F2675ceDB8b28102e957458'
119
+ )
127
120
128
- assert ns.address('supreme.executive.power.derives.from.a.mandate.from.the.masses.jasoncarver.eth') == \
129
- '0x5B2063246F2191f18F2675ceDB8b28102e957458'
121
+ .. warning :: Gas costs scale up with the number of subdomains!
130
122
131
- Allow people to find your name using your address
132
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
123
+ Link an Address to a Name
124
+ ^^^^^^^^^^^^^^^^^^^^^^^^^
133
125
134
- Do you want to set up your address so that :meth: `~ens.main.ENS.name ` will show the
135
- name that points to it?
126
+ You can set up your address so that :meth: `~ens.main.ENS.name ` will show the name that points to it.
136
127
137
- This is like Caller ID. It enables you and others to take an account and
138
- determine what name points to it. Sometimes this is referred to as
139
- "reverse" resolution.
128
+ This is like Caller ID. It enables you and others to take an account and determine what name points to it. Sometimes
129
+ this is referred to as "reverse" resolution. The ENS Reverse Resolver is used for this functionality.
140
130
141
- ::
131
+ .. code-block :: python
142
132
143
133
ns.setup_name(' jasoncarver.eth' , ' 0x5B2063246F2191f18F2675ceDB8b28102e957458' )
144
134
145
- .. note :: Do not rely on reverse resolution for security.
146
-
147
- Anyone can claim any "caller ID". Only forward resolution implies that
148
- the owner of the name gave their stamp of approval.
149
-
150
135
If you don't supply the address, :meth: `~ens.main.ENS.setup_name ` will assume you want the
151
136
address returned by :meth: `~ens.main.ENS.address `.
152
137
153
- ::
138
+ .. code-block :: python
154
139
155
140
ns.setup_name(' jasoncarver.eth' )
156
141
@@ -159,26 +144,31 @@ call :meth:`~ens.main.ENS.setup_address` for you.
159
144
160
145
Wait for the transaction to be mined, then:
161
146
162
- ::
147
+ .. code-block :: python
163
148
164
149
assert ns.name(' 0x5B2063246F2191f18F2675ceDB8b28102e957458' ) == ' jasoncarver.eth'
165
150
151
+ ....
152
+
153
+ Text Records
154
+ ~~~~~~~~~~~~
155
+
166
156
Set Text Metadata for an ENS Record
167
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
157
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
168
158
169
159
As the owner of an ENS record, you can add text metadata.
170
160
A list of supported fields can be found in the
171
161
`ENS documentation <https://docs.ens.domains/contract-api-reference/publicresolver#get-text-data >`_.
172
162
You'll need to setup the address first, and then the text can be set:
173
163
174
- ::
164
+ .. code-block :: python
175
165
176
166
ns.setup_address(' jasoncarver.eth' , 0x 5B2063246F2191f18F2675ceDB8b28102e957458 )
177
167
ns.set_text(' jasoncarver.eth' , ' url' , ' https://example.com' )
178
168
179
169
A transaction dictionary can be passed as the last argument if desired:
180
170
181
- ::
171
+ .. code-block :: python
182
172
183
173
transaction_dict = {' from' : ' 0x123...' }
184
174
ns.set_text(' jasoncarver.eth' , ' url' , ' https://example.com' , transaction_dict)
@@ -188,11 +178,61 @@ a transaction dictionary is passed but does not have a ``from`` value,
188
178
the default will be the ``owner ``.
189
179
190
180
Read Text Metadata for an ENS Record
191
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
181
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
192
182
193
183
Anyone can read the data from an ENS Record:
194
184
195
- ::
185
+ .. code-block :: python
196
186
197
187
url = ns.get_text(' jasoncarver.eth' , ' url' )
198
188
assert url == ' https://example.com'
189
+
190
+ ....
191
+
192
+ Working With Resolvers
193
+ ~~~~~~~~~~~~~~~~~~~~~~
194
+
195
+ Get the Resolver for an ENS Record
196
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
197
+
198
+ You can get the resolver for an ENS name via the :meth: `~ens.main.ENS.resolver ` method.
199
+
200
+ .. code-block :: python
201
+
202
+ >> > resolver = ns.resolver(' jasoncarver.eth' )
203
+ >> > resolver.address
204
+ ' 0x5FfC014343cd971B7eb70732021E26C35B744cc4'
205
+
206
+ ....
207
+
208
+ Wildcard Resolution Support
209
+ ---------------------------
210
+
211
+ The ``ENS `` module supports Wildcard Resolution for resolvers that implement the ``ExtendedResolver `` interface
212
+ as described in `ENSIP-10 <https://docs.ens.domains/ens-improvement-proposals/ensip-10-wildcard-resolution >`_.
213
+ Resolvers that implement the extended resolver interface should return ``True `` when calling the
214
+ ``supportsInterface() `` function with the extended resolver interface id ``0x9061b923 `` and should resolve subdomains
215
+ to a unique address.
216
+
217
+ A working example of a resolver that supports wildcard resolution is the resolver for the ``hatch.eth `` record on the
218
+ Ropsten testnet.
219
+
220
+ .. code-block :: python
221
+
222
+ # connect to the Ropsten testnet
223
+ >> > w3 = Web3(WebsocketProvider(" wss://{ropsten_provider} " ))
224
+ >> > ns = ENS .fromWeb3(w3)
225
+
226
+ # get the resolver for `hatch.eth`
227
+ >> > resolver = ns.resolver(' hatch.eth' )
228
+ >> > resolver.address
229
+ ' 0x8fc4C380c5d539aE631daF3Ca9182b40FB21D1ae'
230
+
231
+ # verify extended resolver interface support
232
+ >> > resolver.caller.supportsInterface(' 0x9061b923' )
233
+ True
234
+
235
+ >> > ns.address(' random-subdomain.hatch.eth' )
236
+ ' 0x49D4c4ff230688e4A357bc057e7E35e64d271939'
237
+ >> > ns.address(' another-random-subdomain.hatch.eth' )
238
+ ' 0xb35359B6450B0CbC9BE15A4eE6bcb8c5b0d9fC4A'
0 commit comments