forked from web3-protocol/web3protocol-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathens.go
334 lines (309 loc) · 10.9 KB
/
ens.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
package web3protocol
import (
"context"
"fmt"
"mime"
"net/http"
"strings"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
// log "github.com/sirupsen/logrus"
"golang.org/x/net/idna"
"golang.org/x/crypto/sha3"
)
type ArgInfo struct {
methodSignature string
mimeType string
calldata string
}
var (
EmptyString = strings.Repeat("0", 62) + "20" + strings.Repeat("0", 64)
EmptyAddress = strings.Repeat("0", 64)
p = idna.New(idna.MapForLookup(), idna.StrictDomainName(false), idna.Transitional(false))
)
// Normalize normalizes a name according to the ENS rules
func Normalize(input string) (output string, err error) {
output, err = p.ToUnicode(input)
if err != nil {
return
}
// If the name started with a period then ToUnicode() removes it, but we want to keep it
if strings.HasPrefix(input, ".") && !strings.HasPrefix(output, ".") {
output = "." + output
}
return
}
// LabelHash generates a simple hash for a piece of a name.
func LabelHash(label string) (hash [32]byte, err error) {
normalizedLabel, err := Normalize(label)
if err != nil {
return
}
sha := sha3.NewLegacyKeccak256()
if _, err = sha.Write([]byte(normalizedLabel)); err != nil {
return
}
sha.Sum(hash[:0])
return
}
// NameHash generates a hash from a name that can be used to
// look up the name in ENS
func NameHash(name string) (hash [32]byte, err error) {
if name == "" {
return
}
normalizedName, err := Normalize(name)
if err != nil {
return
}
parts := strings.Split(normalizedName, ".")
for i := len(parts) - 1; i >= 0; i-- {
if hash, err = nameHashPart(hash, parts[i]); err != nil {
return
}
}
return
}
func nameHashPart(currentHash [32]byte, name string) (hash [32]byte, err error) {
sha := sha3.NewLegacyKeccak256()
if _, err = sha.Write(currentHash[:]); err != nil {
return
}
nameSha := sha3.NewLegacyKeccak256()
if _, err = nameSha.Write([]byte(name)); err != nil {
return
}
nameHash := nameSha.Sum(nil)
if _, err = sha.Write(nameHash); err != nil {
return
}
sha.Sum(hash[:0])
return
}
// If the read is failed, the address will be read with the `addr` record
func (client *Client) getAddressFromNameService(nameServiceChain int, nameWithSuffix string) (common.Address, int, error) {
if common.IsHexAddress(nameWithSuffix) {
return common.HexToAddress(nameWithSuffix), 0, nil
}
// Not a domain name? It now has to have a dot to be a domain name, or it is just an invalid address
if len(strings.Split(nameWithSuffix, ".")) == 1 {
return common.Address{}, 0, &ErrorWithHttpCode{http.StatusBadRequest, "Unrecognized domain name"}
}
nsInfo, rpc, we := client.getConfigs(nameServiceChain, nameWithSuffix)
if we != nil {
return common.Address{}, 0, we
}
ethClient, err := ethclient.Dial(rpc)
if err != nil {
return common.Address{}, 0, &ErrorWithHttpCode{http.StatusInternalServerError, "internal server error"}
}
defer ethClient.Close()
nameHash, _ := NameHash(nameWithSuffix)
node := common.BytesToHash(nameHash[:]).Hex()
resolver, e := client.getResolver(ethClient, nsInfo.ResolverAddress, node, nameServiceChain, nameWithSuffix)
if e != nil {
return common.Address{}, 0, e
}
return client.resolve(ethClient, nameServiceChain, resolver, []string{"addr", "bytes32!" + node})
}
// When webHandler is True, the address will be read with specific webHandler field first;
// If the read is failed, the address will be read with the `addr` record
func (client *Client) getAddressFromNameServiceWebHandler(nameServiceChain int, nameWithSuffix string) (common.Address, int, error) {
if common.IsHexAddress(nameWithSuffix) {
return common.HexToAddress(nameWithSuffix), 0, nil
}
nsInfo, rpc, we := client.getConfigs(nameServiceChain, nameWithSuffix)
if we != nil {
return common.Address{}, 0, we
}
ethClient, err := ethclient.Dial(rpc)
if err != nil {
return common.Address{}, 0, &ErrorWithHttpCode{http.StatusInternalServerError, "internal server error"}
}
defer ethClient.Close()
nameHash, _ := NameHash(nameWithSuffix)
node := common.BytesToHash(nameHash[:]).Hex()
resolver, e := client.getResolver(ethClient, nsInfo.ResolverAddress, node, nameServiceChain, nameWithSuffix)
if e != nil {
return common.Address{}, 0, e
}
var args []string
var returnTp string
if nsInfo.Id == DomainNameServiceW3NS {
args = []string{"webHandler", "bytes32!" + node}
returnTp = "(address)"
} else if nsInfo.Id == DomainNameServiceENS {
args = []string{"text", "bytes32!" + node, "string!contentcontract"}
returnTp = "(string)"
}
msg, _, e := client.parseArguments(nameServiceChain, resolver, args)
if e != nil {
return common.Address{}, 0, e
}
bs, we := handleCallContract(ethClient, msg)
if we != nil {
return common.Address{}, 0, we
}
if common.Bytes2Hex(bs) != EmptyString {
res, we := parseOutput(bs, returnTp)
if we == nil {
return client.parseChainSpecificAddress(res[0].(string))
}
}
return client.resolve(ethClient, nameServiceChain, resolver, []string{"addr", "bytes32!" + node})
}
func (client *Client) resolve(ethClient *ethclient.Client, nameServiceChain int, resolver common.Address, args []string) (common.Address, int, error) {
msg, _, e := client.parseArguments(nameServiceChain, resolver, args)
if e != nil {
return common.Address{}, 0, e
}
bs, err := ethClient.CallContract(context.Background(), msg, nil)
if err != nil || common.Bytes2Hex(bs) == EmptyAddress {
return common.Address{}, 0, &ErrorWithHttpCode{http.StatusNotFound, err.Error()}
}
res, e := parseOutput(bs, "address")
if e != nil {
return common.Address{}, 0, e
}
return client.parseChainSpecificAddress(res[0].(string))
}
func (client *Client) getResolver(ethClient *ethclient.Client, nsAddr common.Address, node string, nameServiceChain int, nameWithSuffix string) (common.Address, error) {
msg, _, e := client.parseArguments(nameServiceChain, nsAddr,
[]string{"resolver", "bytes32!" + node})
if e != nil {
return common.Address{}, e
}
bs, e := handleCallContract(ethClient, msg)
if e != nil {
return common.Address{}, e
}
if common.Bytes2Hex(bs) == EmptyAddress {
return common.Address{}, &ErrorWithHttpCode{http.StatusNotFound, "Cannot resolve domain name"}
}
return common.BytesToAddress(bs), nil
}
func (client *Client) getConfigs(nameServiceChain int, nameWithSuffix string) (DomainNameServiceChainConfig, string, error) {
ss := strings.Split(nameWithSuffix, ".")
if len(ss) <= 1 {
return DomainNameServiceChainConfig{}, "", &ErrorWithHttpCode{http.StatusBadRequest, "invalid domain name: " + nameWithSuffix}
}
suffix := ss[len(ss)-1]
chainInfo, ok := client.Config.Chains[nameServiceChain]
if !ok {
return DomainNameServiceChainConfig{}, "", &ErrorWithHttpCode{http.StatusBadRequest, fmt.Sprintf("unsupported chain: %v", nameServiceChain)}
}
domainNameService := client.Config.GetDomainNameServiceBySuffix(suffix)
if domainNameService == "" {
return DomainNameServiceChainConfig{}, "", &ErrorWithHttpCode{http.StatusBadRequest, "Unsupported domain name suffix: " + suffix}
}
nsInfo, ok := chainInfo.DomainNameServices[domainNameService]
if !ok {
return DomainNameServiceChainConfig{}, "", &ErrorWithHttpCode{http.StatusBadRequest, "Unsupported domain name suffix: " + suffix}
}
return nsInfo, chainInfo.RPC, nil
}
// support chainSpecificAddress from EIP-3770
func (client *Client) parseChainSpecificAddress(addr string) (common.Address, int, error) {
if common.IsHexAddress(addr) {
return common.HexToAddress(addr), 0, nil
}
ss := strings.Split(addr, ":")
if len(ss) != 2 {
return common.Address{}, 0, &ErrorWithHttpCode{http.StatusBadRequest, "invalid contract address from name service: " + addr}
}
chainName := ss[0]
chainId := client.Config.GetChainIdByShortName(strings.ToLower(chainName))
if chainId == 0 {
return common.Address{}, 0, &ErrorWithHttpCode{http.StatusBadRequest, "unsupported chain short name from name service: " + addr}
}
if !common.IsHexAddress(ss[1]) {
return common.Address{}, 0, &ErrorWithHttpCode{http.StatusBadRequest, "invalid contract address from name service: " + addr}
}
return common.HexToAddress(ss[1]), chainId, nil
}
// parseArguments parses a [METHOD_NAME, ARG0, ARG1, ...] string array into an ethereum message with provided address, and return the mime type if end with type extension
func (client *Client) parseArguments(nameServiceChain int, addr common.Address, args []string) (ethereum.CallMsg, ArgInfo, error) {
msig := "("
mimeType := ""
var arguments abi.Arguments = make([]abi.Argument, 0)
values := make([]interface{}, 0)
for i := 1; i < len(args); i++ {
if len(args[i]) == 0 {
continue
}
ty, typeStr, value, err := client.parseArgument(args[i], nameServiceChain)
if err != nil {
return ethereum.CallMsg{}, ArgInfo{}, err
}
arguments = append(arguments, abi.Argument{Type: ty})
values = append(values, value)
if i != 1 {
msig = msig + ","
}
msig = msig + typeStr
ss := strings.Split(args[i], ".")
if i == len(args)-1 && len(ss) > 1 {
mimeType = mime.TypeByExtension("." + ss[len(ss)-1])
}
}
dataField, err := arguments.Pack(values...)
if err != nil {
return ethereum.CallMsg{}, ArgInfo{}, &ErrorWithHttpCode{http.StatusBadRequest, err.Error()}
}
msig = msig + ")"
var calldata []byte
var argInfo ArgInfo
// skip parsing the calldata if there's no argument or the method signature(args[0]) is empty
if len(args) != 0 && args[0] != "" {
h := crypto.Keccak256Hash(append([]byte(args[0]), msig...))
mid := h[0:4]
calldata = append(mid, dataField...)
argInfo.methodSignature = args[0] + msig
}
msg := ethereum.CallMsg{
From: common.HexToAddress("0x0000000000000000000000000000000000000000"),
To: &addr,
Gas: 0,
GasPrice: nil,
GasFeeCap: nil,
GasTipCap: nil,
Data: calldata,
Value: nil,
}
argInfo.mimeType = mimeType
argInfo.calldata = "0x" + common.Bytes2Hex(calldata)
return msg, argInfo, nil
}
// parseOutput parses the bytes into actual values according to the returnTypes string
func parseOutput(output []byte, userTypes string) ([]interface{}, error) {
returnTypes := "(bytes)"
if userTypes == "()" {
return []interface{}{"0x" + common.Bytes2Hex(output)}, nil
} else if userTypes != "" {
returnTypes = userTypes
}
returnArgs := strings.Split(strings.Trim(returnTypes, "()"), ",")
var argsArray abi.Arguments
for _, arg := range returnArgs {
ty, err := abi.NewType(arg, "", nil)
if err != nil {
return nil, &ErrorWithHttpCode{http.StatusBadRequest, err.Error()}
}
argsArray = append(argsArray, abi.Argument{Name: "", Type: ty, Indexed: false})
}
var res []interface{}
res, err := argsArray.UnpackValues(output)
if err != nil {
return nil, &ErrorWithHttpCode{http.StatusBadRequest, err.Error()}
}
if userTypes != "" {
for i, arg := range argsArray {
// get the type of the return value
res[i], _ = JsonEncodeAbiTypeValue(arg.Type, res[i])
}
}
return res, nil
}