-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathl2_resolver.go
211 lines (184 loc) · 5.65 KB
/
l2_resolver.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
package resolution
import (
"github.com/unstoppabledomains/resolution-go/v3/namingservice"
)
const zeroAddress = "0x0000000000000000000000000000000000000000"
type genericFunctions struct {
L1Function func() (interface{}, error)
L2Function func() (interface{}, error)
ZFunction func() (interface{}, error)
}
func resolveGeneric(functions genericFunctions) (interface{}, error) {
type chanStruct struct {
result interface{}
err error
}
c1 := make(chan chanStruct)
c2 := make(chan chanStruct)
cz := make(chan chanStruct)
returnToChannel := func(f func() (interface{}, error), c chan chanStruct) {
r, e := f()
c <- chanStruct{r, e}
}
go returnToChannel(functions.L1Function, c1)
go returnToChannel(functions.L2Function, c2)
go returnToChannel(functions.ZFunction, cz)
result := <-c2
if result.err != nil {
_, notRegistered := result.err.(*DomainNotRegisteredError)
if notRegistered {
result = <-c1
if result.err != nil {
_, notRegistered := result.err.(*DomainNotRegisteredError)
if notRegistered {
result = <-cz
if result.err != nil {
return nil, result.err
}
}
}
}
}
return result.result, result.err
}
type stringMapFunction func() (map[string]string, error)
type stringMapResolverParams struct {
L1Function stringMapFunction
L2Function stringMapFunction
ZFunction stringMapFunction
}
func resolveStringMap(functions stringMapResolverParams) (map[string]string, error) {
convertToGenericFunction := func(f stringMapFunction) func() (interface{}, error) {
return func() (interface{}, error) {
res, err := f()
return res, err
}
}
res, err := resolveGeneric(genericFunctions{
L1Function: convertToGenericFunction(functions.L1Function),
L2Function: convertToGenericFunction(functions.L2Function),
ZFunction: convertToGenericFunction(functions.ZFunction),
})
strmap, ok := res.(map[string]string)
if ok {
return strmap, err
}
return nil, err
}
type stringFunction func() (string, error)
type stringResolverParams struct {
L1Function stringFunction
L2Function stringFunction
ZFunction stringFunction
}
func resolveString(functions stringResolverParams) (string, error) {
convertToGenericFunction := func(f stringFunction) func() (interface{}, error) {
return func() (interface{}, error) {
res, err := f()
return res, err
}
}
res, err := resolveGeneric(genericFunctions{
L1Function: convertToGenericFunction(functions.L1Function),
L2Function: convertToGenericFunction(functions.L2Function),
ZFunction: convertToGenericFunction(functions.ZFunction),
})
str, ok := res.(string)
if ok {
return str, err
}
return "", err
}
type stringMapLocationFuction func() (map[string]namingservice.Location, error)
type stringMapLocationParams struct {
L1Function stringMapLocationFuction
L2Function stringMapLocationFuction
ZFunction stringMapLocationFuction
}
func resolveLocations(functions stringMapLocationParams) (map[string]namingservice.Location, error) {
type chanStruct struct {
result map[string]namingservice.Location
err error
}
c1 := make(chan chanStruct)
c2 := make(chan chanStruct)
cz := make(chan chanStruct)
returnToChannel := func(f func() (map[string]namingservice.Location, error), c chan chanStruct) {
r, e := f()
c <- chanStruct{r, e}
}
go returnToChannel(functions.L1Function, c1)
go returnToChannel(functions.L2Function, c2)
go returnToChannel(functions.ZFunction, cz)
resultL1 := <-c1
resultL2 := <-c2
resultZ := <-cz
locations := map[string]namingservice.Location{}
for domainName, location := range resultL1.result {
if location.OwnerAddress != zeroAddress {
locations[domainName] = namingservice.Location{
RegistryAddress: location.RegistryAddress,
ResolverAddress: location.ResolverAddress,
OwnerAddress: location.OwnerAddress,
BlockchainProviderUrl: location.BlockchainProviderUrl,
NetworkId: location.NetworkId,
Blockchain: "ETH",
}
return locations, nil
} else {
locations[domainName] = namingservice.Location{
RegistryAddress: "",
ResolverAddress: "",
NetworkId: 0,
Blockchain: "",
OwnerAddress: "",
BlockchainProviderUrl: "",
}
}
}
for domainName, location := range resultL2.result {
if location.OwnerAddress != zeroAddress {
locations[domainName] = namingservice.Location{
RegistryAddress: location.RegistryAddress,
ResolverAddress: location.ResolverAddress,
OwnerAddress: location.OwnerAddress,
BlockchainProviderUrl: location.BlockchainProviderUrl,
NetworkId: location.NetworkId,
Blockchain: "MATIC",
}
return locations, nil
} else {
locations[domainName] = namingservice.Location{
RegistryAddress: "",
ResolverAddress: "",
NetworkId: 0,
Blockchain: "",
OwnerAddress: "",
BlockchainProviderUrl: "",
}
}
}
for domainName, location := range resultZ.result {
if location.OwnerAddress != zeroAddress {
locations[domainName] = namingservice.Location{
RegistryAddress: location.RegistryAddress,
ResolverAddress: location.ResolverAddress,
OwnerAddress: location.OwnerAddress,
BlockchainProviderUrl: location.BlockchainProviderUrl,
NetworkId: location.NetworkId,
Blockchain: "ZIL",
}
} else {
locations[domainName] = namingservice.Location{
RegistryAddress: "",
ResolverAddress: "",
NetworkId: 0,
Blockchain: "",
OwnerAddress: "",
BlockchainProviderUrl: "",
}
return locations, nil
}
}
return locations, nil
}