-
Notifications
You must be signed in to change notification settings - Fork 13
/
namingservice.go
365 lines (286 loc) · 11.1 KB
/
namingservice.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package resolution
import (
"fmt"
"strings"
"time"
"github.com/unstoppabledomains/resolution-go/v3/dnsrecords"
"github.com/unstoppabledomains/resolution-go/v3/namingservice"
"github.com/unstoppabledomains/resolution-go/v3/utils"
)
// NamingService Unstoppable supports multiple naming services (.zil and .crypto).
// Each naming service implements shared interface and returns similar record types.
type NamingService interface {
// Records Retrieve records of domain.
// Keys must be provided in raw format according to specification.
// Keys specification: https://docs.unstoppabledomains.com/domain-registry-essentials/records-reference.
// Supported keys reference: https://github.com/unstoppabledomains/dot-crypto/blob/master/src/supported-keys/supported-keys.json.
// It returns key-value map of specified keys set on provided domain. Map can contain empty strings if keys are not found.
Records(domainName string, keys []string) (map[string]string, error)
// Record Retrieve single record of domain.
// Keys must be provided in raw format according to specification.
// Keys specification: https://docs.unstoppabledomains.com/domain-registry-essentials/records-reference.
// Supported keys reference: https://github.com/unstoppabledomains/dot-crypto/blob/master/src/supported-keys/supported-keys.json.
// Returns result in string or empty string if no result found.
Record(domainName string, key string) (string, error)
// Addr Retrieve the value of domain's currency ticker.
// Ticker must contain cryptocurrency like: BTC, ETH.
// Returns result in string or empty string if no result found.
Addr(domainName string, ticker string) (string, error)
// AddrVersion Retrieve the version value of domain's currency ticker.
// This method should be used to query multi-chain currency like USDT.
// Returns result in string or empty string if no result found.
AddrVersion(domainName string, ticker string, version string) (string, error)
// Email Retrieve the email of domain.
// Returns result in string or empty string if no result found.
Email(domainName string) (string, error)
// Resolver Retrieve the resolver address.
// Returns result or DomainNotConfiguredError if resolver is not found.
Resolver(domainName string) (string, error)
// Owner Retrieve the owner address.
// Returns result or DomainNotRegisteredError if owner is not found.
Owner(domainName string) (string, error)
// IpfsHash Retrieve hash of IPFS website attached to domain.
IpfsHash(domainName string) (string, error)
// HTTPUrl Retrieve the http redirect url of a domain.
HTTPUrl(domainName string) (string, error)
// AllRecords Retrieve all records of a domain.
// Returns result in string or empty string record is not found.
// Deprecated: This method will be removed in future releases
AllRecords(domainName string) (map[string]string, error)
// Locations Retrieve locations of domains
// Returns key-value map of domain names to location
Locations(domainNames []string) (map[string]namingservice.Location, error)
// DNS Retrieve the DNS records of a domain.
// Returns a set of valid and filtered non-empty DNS records attached to domain.
DNS(domainName string, types []dnsrecords.Type) ([]dnsrecords.Record, error)
// IsSupportedDomain checks whether domain name is supported by the naming service.
IsSupportedDomain(domainName string) (bool, error)
// DomainExpiry checks whether domain name is expired.
DomainExpiry(domainName string) (time.Time, error)
// TokenURI returns ERC721 metadata token URI
TokenURI(domainName string) (string, error)
// TokenURIMetadata returns ERC721 metadata
TokenURIMetadata(domainName string) (TokenMetadata, error)
// Unhash returns a domain name from a hash using TokenMetadata.Name field and ensures it indeed matches the given hash.
// domainHash should be in hex numeric string format, for example: "0x29bf1b111e709f0953848df35e419490fbad5d316690e4de61adc52695ddf9f3"
// ERC721 Token ID could be passed to this method but should be converted to hex numeric string before usage.
//
// Examples of usage:
//
// domainName, err := NamingService.Unhash("0x29bf1b111e709f0953848df35e419490fbad5d316690e4de61adc52695ddf9f3")
// domainName, err := NamingService.Unhash("0x691f36df38168d9297e784f45a87257a70c58c4040d469c6d0b91d253a837e32")
//
// Usage with ERC721 token id:
//
// var erc721TokenID big.Int
// erc721TokenID.SetString("47548000072528700265403562077742902343248290986511625310517899838602191535666", 10)
// domainHash := hex.EncodeToString(erc721TokenID.Bytes())
// domainName, err := NamingService.Unhash(domainHash)
//
Unhash(domainHash string) (string, error)
// Namehash returns a namehash of a domain following the EIP-137 standard
Namehash(domainName string) (string, error)
ReverseOf(addr string) (string, error)
}
// DetectNamingService helper to detect naming service type for provided domain.
// Returns ZNS or UNS for valid domain and error if domain is not valid or not supported by resolution-go library.
func DetectNamingService(domainName string) (string, error) {
chunks := strings.Split(domainName, ".")
if len(chunks) < 2 {
return "", &DomainNotSupportedError{DomainName: domainName}
}
extension := chunks[len(chunks)-1]
if len(extension) == 0 {
return "", &DomainNotSupportedError{DomainName: domainName}
}
if extension == "zil" {
return namingservice.ZNS, nil
}
return namingservice.UNS, nil
}
type Web3Domain struct {
uns *Uns
ens *Ens
}
func normalizeAndValidateDomain(domain string) (string, bool) {
normalizedDomain := utils.NormalizeName(domain)
if normalizedDomain == "" {
return "", false
}
label, tld := utils.SplitDomain(normalizedDomain)
if label == "" || tld == "" {
return "", false
}
return normalizedDomain, true
}
func (w3d Web3Domain) getNamingServiceForDomain(domain string) NamingService {
_, tld := utils.SplitDomain(domain)
switch tld {
case "eth", "kred", "luxe", "xyz":
return w3d.ens
default:
return w3d.uns
}
}
func (w3d Web3Domain) DomainExpiry(domainName string) (time.Time, error) {
domainName, ok := normalizeAndValidateDomain(domainName)
if !ok {
return time.Time{}, &DomainNotSupportedError{DomainName: domainName}
}
namingService := w3d.getNamingServiceForDomain(domainName)
return namingService.DomainExpiry(domainName)
}
func (w3d Web3Domain) Owner(domain string) (string, error) {
domain, ok := normalizeAndValidateDomain(domain)
if !ok {
return "", &DomainNotSupportedError{DomainName: domain}
}
namingService := w3d.getNamingServiceForDomain(domain)
return namingService.Owner(domain)
}
func (w3d Web3Domain) Resolver(domain string) (string, error) {
normalizedDomain, ok := normalizeAndValidateDomain(domain)
if !ok {
return "", &DomainNotSupportedError{DomainName: domain}
}
namingService := w3d.getNamingServiceForDomain(normalizedDomain)
return namingService.Resolver(normalizedDomain)
}
func (w3d Web3Domain) Namehash(domain string) (string, error) {
domain, ok := normalizeAndValidateDomain(domain)
if !ok {
return "", &DomainNotSupportedError{DomainName: domain}
}
namingService := w3d.getNamingServiceForDomain(domain)
return namingService.Namehash(domain)
}
func (w3d Web3Domain) ReverseOf(addr string) (string, error) {
namingServices := []NamingService{w3d.uns, w3d.ens}
for _, namingService := range namingServices {
reverse, err := namingService.ReverseOf(addr)
if err == nil && reverse != "" {
return reverse, nil
}
}
return "", nil
}
func (w3d Web3Domain) MultiReverseOf(addr string) ([]string, error) {
namingServices := []NamingService{w3d.uns, w3d.ens}
domains := []string{}
for _, namingService := range namingServices {
reverse, err := namingService.ReverseOf(addr)
if err == nil && reverse != "" {
domains = append(domains, reverse)
}
}
return domains, nil
}
func (w3d Web3Domain) Addr(domain, token string) (string, error) {
domain, ok := normalizeAndValidateDomain(domain)
if !ok {
return "", &DomainNotSupportedError{DomainName: domain}
}
namingService := w3d.getNamingServiceForDomain(domain)
return namingService.Addr(domain, token)
}
func (w3d Web3Domain) IpfsHash(domain string) (string, error) {
domain, ok := normalizeAndValidateDomain(domain)
if !ok {
return "", &DomainNotSupportedError{DomainName: domain}
}
namingService := w3d.getNamingServiceForDomain(domain)
return namingService.IpfsHash(domain)
}
func (w3d Web3Domain) HTTPUrl(domain string) (string, error) {
domain, ok := normalizeAndValidateDomain(domain)
if !ok {
return "", &DomainNotSupportedError{DomainName: domain}
}
namingService := w3d.getNamingServiceForDomain(domain)
return namingService.HTTPUrl(domain)
}
func (w3d Web3Domain) Email(domain string) (string, error) {
domain, ok := normalizeAndValidateDomain(domain)
if !ok {
return "", &DomainNotSupportedError{DomainName: domain}
}
namingService := w3d.getNamingServiceForDomain(domain)
return namingService.Email(domain)
}
// maybe useless
func (w3d Web3Domain) Locations(domains []string) (map[string]namingservice.Location, error) {
var unsDomains []string
var ensDomains []string
for _, domain := range domains {
domain, ok := normalizeAndValidateDomain(domain)
if !ok {
continue
}
_, tld := utils.SplitDomain(domain)
switch tld {
case "eth", "kred", "luxe", "xyz":
ensDomains = append(ensDomains, domain)
default:
unsDomains = append(unsDomains, domain)
}
}
unsResult, err := w3d.uns.Locations(unsDomains)
if err != nil {
return nil, err
}
ensResult, err := w3d.ens.Locations(ensDomains)
if err != nil {
return nil, err
}
fmt.Println(unsResult)
result := make(map[string]namingservice.Location)
for k, v := range unsResult {
result[k] = v
}
for k, v := range ensResult {
result[k] = v
}
return result, nil
}
func (w3d Web3Domain) DNS(domain string, types []dnsrecords.Type) ([]dnsrecords.Record, error) {
domain, ok := normalizeAndValidateDomain(domain)
if !ok {
return nil, &DomainNotSupportedError{DomainName: domain}
}
namingService := w3d.getNamingServiceForDomain(domain)
return namingService.DNS(domain, types)
}
func (w3d Web3Domain) Record(domain, key string) (string, error) {
domain, ok := normalizeAndValidateDomain(domain)
if !ok {
return "", &DomainNotSupportedError{DomainName: domain}
}
namingService := w3d.getNamingServiceForDomain(domain)
return namingService.Record(domain, key)
}
func (w3d Web3Domain) TokenURI(domain string) (string, error) {
domain, ok := normalizeAndValidateDomain(domain)
if !ok {
return "", &DomainNotSupportedError{DomainName: domain}
}
namingService := w3d.getNamingServiceForDomain(domain)
return namingService.TokenURI(domain)
}
func (w3d Web3Domain) TokenURIMetadata(domain string) (TokenMetadata, error) {
domain, ok := normalizeAndValidateDomain(domain)
if !ok {
return TokenMetadata{}, &DomainNotSupportedError{DomainName: domain}
}
namingService := w3d.getNamingServiceForDomain(domain)
return namingService.TokenURIMetadata(domain)
}
func (w3d Web3Domain) Unhash(domainHash string) (string, error) {
namingServices := []NamingService{w3d.uns, w3d.ens}
for _, namingService := range namingServices {
domainName, err := namingService.Unhash(domainHash)
if err == nil {
return domainName, nil
}
}
return "", nil
}