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