-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathzns.go
300 lines (255 loc) · 7.64 KB
/
zns.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
package resolution
import (
"encoding/json"
"strings"
"time"
"github.com/unstoppabledomains/resolution-go/v3/namingservice"
"github.com/Zilliqa/gozilliqa-sdk/provider"
"github.com/unstoppabledomains/resolution-go/v3/dnsrecords"
)
// Zns is a naming service handles .zil domains resolution.
type Zns struct {
provider ZnsProvider
znsRegistry string
}
// ZnsBuilder is a builder to setup and build instance of Zns service.
type ZnsBuilder interface {
// SetProvider set Zilliqa blockchain provider to communicate with ZNS registry
SetProvider(provider ZnsProvider) ZnsBuilder
// SetProvider set Zilliqa network to communicate with ZNS registry
SetNetwork(network string) ZnsBuilder
// Build Zns instance
Build() (*Zns, error)
}
type znsBuilder struct {
provider ZnsProvider
network string
}
// ZnsProvider ZnsProvider
type ZnsProvider interface {
GetSmartContractSubState(contractAddress string, params ...interface{}) (string, error)
}
// ZnsDomainState State of ZNS domain
type ZnsDomainState struct {
Resolver string
Owner string
Records map[string]string
}
type registrySubState struct {
Result map[string]map[string]struct {
Arguments []string
Argtypes []string
Constructor string
}
}
type resolverSubState struct {
Result map[string]map[string]string
}
const znsDefaultProvider = "https://api.zilliqa.com"
const znsMainnetRegistry = "9611c53BE6d1b32058b2747bdeCECed7e1216793"
const znsTestnetRegistry = "b925add1d5eaf13f40efd43451bf97a22ab3d727"
const znsContractField = "records"
const znsZeroAddress = "0x0000000000000000000000000000000000000000"
// NewZnsBuilder Creates ZNS builder instance.
func NewZnsBuilder() ZnsBuilder {
return &znsBuilder{network: "mainnet"}
}
// SetProvider set Zilliqa blockchain provider to communicate with ZNS registry.
func (zb *znsBuilder) SetProvider(provider ZnsProvider) ZnsBuilder {
zb.provider = provider
return zb
}
// SetNetwork set Zilliqa blockchain network to communicate with ZNS registry.
func (zb *znsBuilder) SetNetwork(network string) ZnsBuilder {
zb.network = network
return zb
}
// Build Zns instance
func (zb *znsBuilder) Build() (*Zns, error) {
if zb.provider == nil {
zb.provider = provider.NewProvider(znsDefaultProvider)
}
znsRegistry := znsMainnetRegistry
if zb.network == "testnet" {
znsRegistry = znsTestnetRegistry
}
return &Zns{provider: zb.provider, znsRegistry: znsRegistry}, nil
}
func (z *Zns) DomainExpiry(domainName string) (time.Time, error) {
return time.Time{}, &MethodIsNotSupportedError{NamingServiceName: namingservice.UNS}
}
// State Get raw data attached to domain.
func (z *Zns) State(domainName string) (*ZnsDomainState, error) {
namehash, err := ZnsNameHash(domainName)
if err != nil {
return nil, err
}
response, err := z.provider.GetSmartContractSubState(z.znsRegistry, znsContractField, []string{namehash})
if err != nil {
return nil, err
}
var registryState registrySubState
err = json.Unmarshal([]byte(response), ®istryState)
if err != nil {
return nil, err
}
registryValues := registryState.Result[znsContractField][namehash].Arguments
if len(registryValues) == 0 {
return nil, &DomainNotRegisteredError{DomainName: domainName}
}
owner, resolver := registryValues[0], registryValues[1]
if owner == znsZeroAddress {
return nil, &DomainNotRegisteredError{DomainName: domainName}
}
if resolver == znsZeroAddress {
return nil, &DomainNotConfiguredError{DomainName: domainName}
}
response, err = z.provider.GetSmartContractSubState(strings.TrimPrefix(resolver, "0x"), znsContractField, []string{})
if err != nil {
return nil, err
}
var resolverState resolverSubState
err = json.Unmarshal([]byte(response), &resolverState)
if err != nil {
return nil, err
}
records := resolverState.Result[znsContractField]
return &ZnsDomainState{Owner: owner, Resolver: resolver, Records: records}, nil
}
func (z *Zns) Records(domainName string, keys []string) (map[string]string, error) {
state, err := z.State(domainName)
if err != nil {
return nil, err
}
records := make(map[string]string, len(keys))
for _, recordKey := range keys {
records[recordKey] = state.Records[recordKey]
}
return records, err
}
func (z *Zns) Record(domainName string, key string) (string, error) {
records, err := z.Records(domainName, []string{key})
if err != nil {
return "", nil
}
return records[key], nil
}
func (z *Zns) Owner(domainName string) (string, error) {
state, err := z.State(domainName)
if err != nil {
return "", err
}
return state.Owner, err
}
func (z *Zns) Resolver(domainName string) (string, error) {
state, err := z.State(domainName)
if err != nil {
return "", err
}
return state.Resolver, err
}
func (z *Zns) Addr(domainName string, ticker string) (string, error) {
key, err := buildCryptoKey(ticker)
if err != nil {
return "", err
}
value, err := z.Record(domainName, key)
if err != nil {
return "", err
}
return value, nil
}
func (z *Zns) AddrVersion(domainName string, ticker string, version string) (string, error) {
key, err := buildCryptoKeyVersion(ticker, version)
if err != nil {
return "", err
}
value, err := z.Record(domainName, key)
if err != nil {
return "", err
}
return value, nil
}
func (z *Zns) Email(domainName string) (string, error) {
value, err := z.Record(domainName, emailKey)
if err != nil {
return "", err
}
return value, nil
}
func (z *Zns) AllRecords(domainName string) (map[string]string, error) {
state, err := z.State(domainName)
if err != nil {
return nil, err
}
return state.Records, err
}
func (z *Zns) IpfsHash(domainName string) (string, error) {
records, err := z.Records(domainName, ipfsKeys)
if err != nil {
return "", err
}
return returnFirstNonEmpty(records, ipfsKeys), nil
}
func (z *Zns) HTTPUrl(domainName string) (string, error) {
records, err := z.Records(domainName, redirectUrlKeys)
if err != nil {
return "", err
}
return returnFirstNonEmpty(records, redirectUrlKeys), nil
}
func (z *Zns) DNS(domainName string, types []dnsrecords.Type) ([]dnsrecords.Record, error) {
keys, err := dnsTypesToCryptoRecordKeys(types)
if err != nil {
return nil, err
}
records, err := z.Records(domainName, keys)
if err != nil {
return nil, err
}
dnsRecords, err := cryptoRecordsToDNS(records)
if err != nil {
return nil, err
}
return dnsRecords, nil
}
func (z *Zns) IsSupportedDomain(domainName string) (bool, error) {
return strings.HasSuffix(domainName, ".zil"), nil
}
func (z *Zns) TokenURI(_ string) (string, error) {
return "", &MethodIsNotSupportedError{NamingServiceName: namingservice.ZNS}
}
func (z *Zns) Locations(domainNames []string) (map[string]namingservice.Location, error) {
locations := make(map[string]namingservice.Location)
for _, domainName := range domainNames {
state, err := z.State(domainName)
if err != nil {
return map[string]namingservice.Location{}, err
}
locations[domainName] = namingservice.Location{
RegistryAddress: z.znsRegistry,
ResolverAddress: state.Resolver,
NetworkId: 1,
Blockchain: "ZIL",
OwnerAddress: state.Owner,
BlockchainProviderUrl: znsDefaultProvider,
}
}
return locations, nil
}
func (z *Zns) TokenURIMetadata(_ string) (TokenMetadata, error) {
return TokenMetadata{}, &MethodIsNotSupportedError{NamingServiceName: namingservice.ZNS}
}
func (z *Zns) Namehash(domainName string) (string, error) {
namehash, err := ZnsNameHash(domainName)
if err != nil {
return "", err
}
return namehash, nil
}
func (z *Zns) Unhash(_ string) (string, error) {
return "", &MethodIsNotSupportedError{NamingServiceName: namingservice.ZNS}
}
func (z *Zns) ReverseOf(_ string) (string, error) {
return "", &MethodIsNotSupportedError{NamingServiceName: namingservice.ZNS}
}