-
Notifications
You must be signed in to change notification settings - Fork 2
/
mode-auto.go
370 lines (331 loc) · 11.4 KB
/
mode-auto.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
366
367
368
369
370
package web3protocol
import (
"errors"
"fmt"
"math/big"
"mime"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
)
func (client *Client) parseAutoModeUrl(web3Url *Web3URL) (err error) {
// Special case : No path, or "/" : we call empty calldata
if web3Url.UrlParts.Path == "" || web3Url.UrlParts.Path == "/" {
web3Url.ContractCallMode = ContractCallModeCalldata
web3Url.Calldata = []byte{}
web3Url.ContractReturnProcessing = ContractReturnProcessingDecodeABIEncodedBytes
return
}
pathnameParts := strings.Split(web3Url.UrlParts.Path, "/")
// Get method name
methodName := pathnameParts[1]
if methodName == "" {
return &ErrorWithHttpCode{http.StatusBadRequest, "Missing method name"}
}
validMethodName, err := regexp.MatchString("^[a-zA-Z$_][a-zA-Z0-9$_]*$", methodName)
if err != nil {
return err
}
if validMethodName == false {
return &ErrorWithHttpCode{http.StatusBadRequest, "Invalid method name"}
}
web3Url.ContractCallMode = ContractCallModeMethod
web3Url.MethodName = methodName
// Resolver for domain name in args :
// We use the chain from the initial lookup (erc-6821 allows use to switch chain)
domainNameResolverChainId := web3Url.ChainId
if web3Url.HostDomainNameResolverChainId > 0 {
domainNameResolverChainId = web3Url.HostDomainNameResolverChainId
}
for _, pathnamePart := range pathnameParts[2:] {
abiType, _, value, err := client.parseArgument(pathnamePart, domainNameResolverChainId)
if err != nil {
return err
}
web3Url.MethodArgs = append(web3Url.MethodArgs, abiType)
web3Url.MethodArgValues = append(web3Url.MethodArgValues, value)
}
// Return processing: By default ABI-encoded bytes
web3Url.ContractReturnProcessing = ContractReturnProcessingDecodeABIEncodedBytes
// Process the query values
parsedQuery, err := ParseQuery(web3Url.UrlParts.Query)
if err != nil {
return &ErrorWithHttpCode{http.StatusBadRequest, "Unable to parse the query of the URL"}
}
// Check that we only have the allowed one
for _, queryValue := range parsedQuery {
if queryValue.Name != "returns" &&
queryValue.Name != "returnTypes" &&
queryValue.Name != "mime.content" &&
queryValue.Name != "mime.type" {
return &ErrorWithHttpCode{http.StatusBadRequest, "Unsupported query attribute"}
}
}
// Process the ?returns / ?returnTypes query
selectedLastQueryParam := parsedQuery.getLastByNames([]string{"returns", "returnTypes"})
returnTypes := selectedLastQueryParam.Value
if returnTypes != "" {
if len(returnTypes) < 2 {
return &ErrorWithHttpCode{http.StatusBadRequest, "Invalid returns attribute"}
}
if string(returnTypes[0]) != "(" || string(returnTypes[len(returnTypes)-1]) != ")" {
return &ErrorWithHttpCode{http.StatusBadRequest, "Invalid returns attribute"}
}
if returnTypes == "()" {
// We will return the raw bytes, JSON encoded
web3Url.ContractReturnProcessing = ContractReturnProcessingRawBytesJsonEncoded
} else {
// Ok at this stage we know we are going to return JSON-encoded vars
web3Url.ContractReturnProcessing = ContractReturnProcessingJsonEncodeValues
// Remove parenthesis
returnTypes = returnTypes[1 : len(returnTypes)-1]
// Do the types parsing
argMarshalings, err := parseReturnSignature(returnTypes)
if err != nil {
return err
}
// Convert the abi.ArgumentMarshaling into proper abi.Types
for _, argMarshaling := range argMarshalings {
abiType, err := abi.NewType(argMarshaling.Type, "", argMarshaling.Components)
if err != nil {
// We should not enter here, double checking
return errors.New("Return attribute processing error: " + argMarshaling.Type)
}
web3Url.JsonEncodedValueTypes = append(web3Url.JsonEncodedValueTypes, abiType)
}
}
}
// If we are still returning decoded ABI-encoded bytes,
// Get the mime type to use, from an argument
if web3Url.ContractReturnProcessing == ContractReturnProcessingDecodeABIEncodedBytes && len(pathnameParts) >= 3 /** At least an argument */ {
lastPathnamePartParts := strings.Split(pathnameParts[len(pathnameParts)-1], ".")
if len(lastPathnamePartParts) > 1 {
// If no mime type is found, this will return empty string
web3Url.DecodedABIEncodedBytesMimeType = mime.TypeByExtension("." + lastPathnamePartParts[len(lastPathnamePartParts)-1])
}
}
// ERC-7087 extension for milme type override
if web3Url.ContractReturnProcessing == ContractReturnProcessingDecodeABIEncodedBytes {
selectedLastQueryParam := parsedQuery.getLastByNames([]string{"mime.content", "mime.type"})
if selectedLastQueryParam.Name == "mime.content" {
web3Url.DecodedABIEncodedBytesMimeType = selectedLastQueryParam.Value
} else if selectedLastQueryParam.Name == "mime.type" {
mimeType := mime.TypeByExtension("." + selectedLastQueryParam.Value)
// If not found, keep the previous value
if mimeType != "" {
web3Url.DecodedABIEncodedBytesMimeType = mimeType
}
}
}
return
}
// parseArgument parses a [TYPE!]VALUE string into an abi.Type. The type will be auto-detected if TYPE not provided
func (client *Client) parseArgument(argument string, nsChain int) (abiType abi.Type, typeName string, argValue interface{}, err error) {
// URI-percent-encoding decoding
var decodedArgument string
decodedArgument, err = url.PathUnescape(argument)
if err != nil {
err = &ErrorWithHttpCode{http.StatusBadRequest, "Unable to URI-percent decode: " + argument}
return
}
ss := strings.Split(decodedArgument, "!")
// Type specified
if len(ss) >= 2 {
typeName = ss[0]
argValueStr := strings.Join(ss[1:], "!")
// If there is a number at the right of the type, extract it
var typeWithoutSize string
typeSize := 0
var typeSizeRegexp *regexp.Regexp
typeSizeRegexp, err = regexp.Compile(`^([^0-9]+)([1-9][0-9]*)$`)
if err != nil {
return
}
matches := typeSizeRegexp.FindStringSubmatch(typeName)
// Type with size
if matches != nil {
typeWithoutSize = matches[1]
typeSize, err = strconv.Atoi(matches[2])
if err != nil {
return
}
// Type with no size
} else {
typeWithoutSize = typeName
}
switch typeWithoutSize {
case "uint", "int":
// uint/int are aliases of uint256/int256
if typeSize == 0 {
typeSize = 256
typeName = fmt.Sprintf("%v%v", typeWithoutSize, typeSize)
}
// Type size must be from 8 to 256, by steps of 8
if typeSize < 8 || typeSize > 256 || typeSize%8 != 0 {
err = &ErrorWithHttpCode{http.StatusBadRequest, "Invalid argument type: " + typeName}
return
}
b := new(big.Int)
n, ok := b.SetString(argValueStr, 0)
if !ok {
err = &ErrorWithHttpCode{http.StatusBadRequest, "Argument is not a number: " + argValueStr}
return
}
if typeWithoutSize == "uint" && n.Cmp(new(big.Int)) == -1 {
err = &ErrorWithHttpCode{http.StatusBadRequest, "Number is negative: " + argValueStr}
return
}
argValue = n
case "bytes":
// "bytes", no type size
if typeSize == 0 {
if !has0xPrefix(argValueStr) || !isHex(argValueStr[2:]) {
err = &ErrorWithHttpCode{http.StatusBadRequest, "Argument is not a valid hex string: " + argValueStr}
return
}
argValue = common.FromHex(argValueStr)
// "bytesXX", with a type size
} else {
if typeSize > 32 {
err = &ErrorWithHttpCode{http.StatusBadRequest, "Invalid argument type: " + typeName}
return
}
if !has0xPrefix(argValueStr) || !isHex(argValueStr[2:]) {
err = &ErrorWithHttpCode{http.StatusBadRequest, "Argument is not a valid hex string: " + argValueStr}
return
}
if len(argValueStr[2:]) != 2*typeSize {
err = &ErrorWithHttpCode{http.StatusBadRequest, "Argument has not the correct length: " + argValueStr}
return
}
argValue = common.HexToHash(argValueStr)
}
case "address":
var addr common.Address
addr, _, err = client.getAddressFromNameService(nsChain, argValueStr)
if err != nil {
return
}
argValue = addr
case "string":
argValue = argValueStr
case "bool":
if argValueStr != "false" && argValueStr != "true" {
err = &ErrorWithHttpCode{http.StatusBadRequest, "Argument must be 'true' or 'false'"}
return
}
argValue = argValueStr == "true"
default:
err = &ErrorWithHttpCode{http.StatusBadRequest, "Unknown type: " + typeName}
return
}
abiType, _ = abi.NewType(typeName, "", nil)
// No type specified : we autodetect
} else {
argValueStr := ss[0]
// uint256 autodetection
var numberRegexp *regexp.Regexp
numberRegexp, _ = regexp.Compile(`^[0-9]+$`)
matches := numberRegexp.FindStringSubmatch(argValueStr)
if matches != nil {
n := new(big.Int)
n, _ = n.SetString(argValueStr, 10)
// treat it as uint256
typeName = "uint256"
abiType, _ = abi.NewType(typeName, "", nil)
argValue = n
return
}
// Hex address, bytes32, bytes autodetection
if has0xPrefix(argValueStr) && isHex(argValueStr[2:]) {
if len(argValueStr) == 40+2 {
argValue = common.HexToAddress(argValueStr)
typeName = "address"
abiType, _ = abi.NewType(typeName, "", nil)
return
} else if len(argValueStr) == 64+2 {
argValue = common.HexToHash(argValueStr)
typeName = "bytes32"
abiType, _ = abi.NewType(typeName, "", nil)
return
} else {
argValue = common.FromHex(argValueStr[2:])
typeName = "bytes"
abiType, _ = abi.NewType(typeName, "", nil)
return
}
}
// Bool autodetection
if argValueStr == "true" || argValueStr == "false" {
argValue = argValueStr == "true"
typeName = "bool"
abiType, _ = abi.NewType(typeName, "", nil)
return
}
// Domain name address autodetection
var addr common.Address
addr, _, err = client.getAddressFromNameService(nsChain, argValueStr)
if err == nil {
argValue = addr
typeName = "address"
abiType, _ = abi.NewType(typeName, "", nil)
return abiType, "address", addr, nil
}
}
return
}
// Recursively parse the ?returns= signature
// It follows the syntax of the argument part of the ABI method signature
func parseReturnSignature(text string) (result []abi.ArgumentMarshaling, err error) {
// Separate parts
parts := []string{}
if len(text) > 0 {
tupleDeepness := 0
lastCommaPos := -1
for i, char := range text {
if char == []rune("(")[0] {
tupleDeepness++
} else if char == []rune(")")[0] {
tupleDeepness--
} else if char == []rune(",")[0] && tupleDeepness == 0 {
parts = append(parts, string([]rune(text)[lastCommaPos+1:i]))
lastCommaPos = i
}
}
parts = append(parts, string([]rune(text)[lastCommaPos+1:]))
}
// Process parts
for _, part := range parts {
// Look for tuple
tupleRegexp, _ := regexp.Compile(`^\((?P<tupleComponents>.+)\)(?P<arrayDef>[\[\]0-9]*)$`)
tupleMatches := tupleRegexp.FindStringSubmatch(part)
if len(tupleMatches) > 0 {
tupleComponents, err := parseReturnSignature(tupleMatches[1])
if err != nil {
return result, err
}
result = append(result, abi.ArgumentMarshaling{Type: "tuple" + tupleMatches[2], Name: "x", Components: tupleComponents})
// Basic type
} else {
if part == "" {
return result, &ErrorWithHttpCode{http.StatusBadRequest, "Return attribute: missing type"}
}
// Aliases uint and int are allowed
if part == "uint" {
part = "uint256"
} else if part == "int" {
part = "int256"
}
_, err := abi.NewType(part, "", nil)
if err != nil {
return result, &ErrorWithHttpCode{http.StatusBadRequest, "Return attribute: Invalid type: " + part}
}
result = append(result, abi.ArgumentMarshaling{Type: part, Name: "x"})
}
}
return
}