-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprotocol.go
525 lines (457 loc) · 18.8 KB
/
protocol.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
package web3protocol
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"regexp"
"strconv"
"strings"
"unicode"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
)
type ResolveModeCacheKey struct {
ChainId int
ContractAddress common.Address
}
type DomainNameService string
const (
DomainNameServiceENS = "ens"
DomainNameServiceW3NS = "w3ns"
)
type ResolveMode string
const (
ResolveModeAuto = "auto"
ResolveModeManual = "manual"
ResolveModeResourceRequests = "resourceRequest"
)
type ContractCallMode string
const (
ContractCallModeCalldata = "calldata"
ContractCallModeMethod = "method"
)
type ContractReturnProcessing string
const (
// Expect the whole returned data to be ABI-encoded bytes. Decode.
ContractReturnProcessingDecodeABIEncodedBytes = "decodeABIEncodedBytes"
// JSON-encode the raw bytes of the returned data
ContractReturnProcessingRawBytesJsonEncoded = "jsonEncodeRawBytes"
// JSON-encode the different return values
ContractReturnProcessingJsonEncodeValues = "jsonEncodeValues"
// Expect a string as first return value, parse it as a dataUrl
// ContractReturnProcessingDataUrl = "dataUrl" // To implement
// Expect a return following the erc5219 spec, will decode it using this spec
ContractReturnProcessingDecodeErc5219Request = "decodeErc5219Request"
)
// A raw splitting of the web3 URL parts
type ParsedWeb3Url struct {
Protocol string
Hostname string
ChainId string
// The PathQuery is the full path, including the Pathname and Query
PathQuery string
Path string
Query string
Fragment string
}
// This contains a web3:// URL parsed and ready to call the main smartcontract
type Web3URL struct {
// The actual url string "web3://...."
Url string
// The request HTTP headers
HttpHeaders map[string]string
// A raw splitting of the web3 URL parts, to be used by the processing
// You should not use this directly outside of this package
UrlParts ParsedWeb3Url
// If the host was a domain name, what domain name service was used?
HostDomainNameResolver DomainNameService
// Chain of the name resolution service
HostDomainNameResolverChainId int
// The contract address (after optional domain name resolution) that is going to be called,
// and its chain location
ContractAddress common.Address // actual address
ChainId int
// The ERC-4804 resolve mode
ResolveMode ResolveMode
// How do we call the smartcontract
// 'calldata' : We use a raw calldata
// 'method': We use the specified method and method parameters
ContractCallMode ContractCallMode
// Attributes for ContractCallModeCalldata
Calldata []byte
// Attributes for ContractCallModeMethod
MethodName string
MethodArgs []abi.Type
MethodArgValues []interface{}
// How to process the return of the contract. See enum for doc
ContractReturnProcessing ContractReturnProcessing
// In case of contractReturnProcessing being decodeABIEncodedBytes,
// this will set the mime type to return
DecodedABIEncodedBytesMimeType string
// In case of ContractReturnProcessing being jsonEncodeValues,
// this will tell us how to ABI-decode the returned data
JsonEncodedValueTypes []abi.Type
}
// This contains the result of a web3:// URL call : the parsed URL, the raw contract return,
// and the bytes output, HTTP code and headers for the browser.
type FetchedWeb3URL struct {
// The web3 URL, parsed
ParsedUrl *Web3URL
// The raw data returned by the contract
ContractReturn []byte
// The processed output, to be returned by the browser
Output io.Reader
// The HTTP code to be returned by the browser
HttpCode int
// The HTTP headers to be returned by the browser
HttpHeaders map[string]string
}
/**
* The main function executing a web3:// URL call.
* For a given full web3:// url ("web3://xxxx"), returns a structure containing
* the bytes output and the HTTP code and headers, as well as plenty of informations on
* how the processing was done.
*
* Unlike FetchUrl(), this function does not use the built-in worker system which
* aggregates identical requests and limits the number of parallel requests.
* On the other hand, it does have the per-RPC limit of parralel requests.
*/
func (client *Client) FetchUrlDirect(url string, httpHeaders map[string]string) (fetchedUrl FetchedWeb3URL, err error) {
// Parse the URL
parsedUrl, err := client.ParseUrl(url, httpHeaders)
if err != nil {
fetchedUrl.ParsedUrl = &parsedUrl
return
}
// Attempt to make a response right away, without a contract call :
// We can do it if we know the output has not changed (see ERC-7774 resource request caching)
earlyFetchedUrl, success, err := client.AttemptEarlyResponse(&parsedUrl)
if err != nil {
fetchedUrl.ParsedUrl = &parsedUrl
return
}
if success {
return earlyFetchedUrl, nil
}
// Fetch the contract return data
contractReturn, err := client.FetchContractReturn(&parsedUrl)
if err != nil {
fetchedUrl.ParsedUrl = &parsedUrl
return
}
// Finally, process the returned data
fetchedUrl, err = client.ProcessContractReturn(&parsedUrl, contractReturn)
if err != nil {
return
}
return
}
/**
* Step 1 : Parse the URL and determine how we are going to call the main contract.
*/
func (client *Client) ParseUrl(url string, httpHeaders map[string]string) (web3Url Web3URL, err error) {
web3Url.Url = url
web3Url.HttpHeaders = httpHeaders
// Check that the URL is ASCII only
for i := 0; i < len(web3Url.Url); i++ {
if web3Url.Url[i] > unicode.MaxASCII {
return web3Url, &Web3ProtocolError{HttpCode: http.StatusBadRequest, Err: errors.New("URL is invalid, contains non-ASCII characters")}
}
}
// Parse the main structure of the URL
web3UrlRegexp, err := regexp.Compile(`^(?P<protocol>[^:]+):\/\/(?P<hostname>[^:\/?#]+)(:(?P<chainId>[1-9][0-9]*))?(?P<pathQuery>(?P<path>\/[^?#]*)?([?](?P<query>[^#]*))?)?(#(?P<fragment>.*)?)?$`)
if err != nil {
return
}
matches := web3UrlRegexp.FindStringSubmatch(web3Url.Url)
if len(matches) == 0 {
return web3Url, &Web3ProtocolError{HttpCode: http.StatusBadRequest, Err: errors.New("Invalid URL format")}
}
for i, name := range web3UrlRegexp.SubexpNames() {
if name == "protocol" {
web3Url.UrlParts.Protocol = matches[i]
} else if name == "hostname" {
web3Url.UrlParts.Hostname = matches[i]
} else if name == "chainId" {
web3Url.UrlParts.ChainId = matches[i]
} else if name == "pathQuery" {
web3Url.UrlParts.PathQuery = matches[i]
} else if name == "path" {
web3Url.UrlParts.Path = matches[i]
} else if name == "query" {
web3Url.UrlParts.Query = matches[i]
} else if name == "fragment" {
web3Url.UrlParts.Fragment = matches[i]
}
}
// Protocol name: 1 name and alias supported
if web3Url.UrlParts.Protocol != "web3" && web3Url.UrlParts.Protocol != "w3" {
return web3Url, &Web3ProtocolError{HttpCode: http.StatusBadRequest, Err: errors.New("Protocol name is invalid")}
}
// Default chain is ethereum mainnet
// Check if we were explicitely asked to go to another chain
web3Url.ChainId = 1
if len(web3Url.UrlParts.ChainId) > 0 {
chainId, err := strconv.Atoi(web3Url.UrlParts.ChainId)
if err != nil {
// Regexp should always get us valid numbers, but we could enter here if overflow
return web3Url, &Web3ProtocolError{HttpCode: http.StatusBadRequest, Err: errors.New(fmt.Sprintf("Unsupported chain %v", web3Url.UrlParts.ChainId))}
}
web3Url.ChainId = chainId
}
// Check that we support the chain
_, ok := client.Config.Chains[web3Url.ChainId]
if !ok {
return web3Url, &Web3ProtocolError{HttpCode: http.StatusBadRequest, Err: errors.New(fmt.Sprintf("Unsupported chain %v", web3Url.ChainId))}
}
// Main hostname : We determine if we need hostname resolution, and do it
if common.IsHexAddress(web3Url.UrlParts.Hostname) {
web3Url.ContractAddress = common.HexToAddress(web3Url.UrlParts.Hostname)
} else {
// Determine name suffix
hostnameParts := strings.Split(web3Url.UrlParts.Hostname, ".")
if len(hostnameParts) <= 1 {
return web3Url, &Web3ProtocolError{HttpCode: http.StatusBadRequest, Err: errors.New("Invalid contract address")}
}
nameServiceSuffix := hostnameParts[len(hostnameParts)-1]
domainNameWithoutSuffix := strings.Join(hostnameParts[0:len(hostnameParts)-1], ".")
if domainNameWithoutSuffix == "" {
return web3Url, &Web3ProtocolError{HttpCode: http.StatusBadRequest, Err: errors.New("Invalid domain name")}
}
// If the chain id was not explicitely requested on the URL, we will use the
// "default home" chain id of the name resolution service
// (e.g. 1 for .eth, 333 for w3q) as the target chain
if len(web3Url.UrlParts.ChainId) == 0 {
domainNameService := client.Config.GetDomainNameServiceBySuffix(nameServiceSuffix)
if domainNameService == "" || client.Config.DomainNameServices[domainNameService].DefaultChainId == 0 {
return web3Url, &Web3ProtocolError{HttpCode: http.StatusBadRequest, Err: errors.New("Unsupported domain name service suffix: " + nameServiceSuffix)}
}
web3Url.ChainId = client.Config.DomainNameServices[domainNameService].DefaultChainId
}
// We will use a nameservice in the current target chain
web3Url.HostDomainNameResolverChainId = web3Url.ChainId
domainNameService := client.Config.GetDomainNameServiceBySuffix(nameServiceSuffix)
if domainNameService == "" {
return web3Url, &Web3ProtocolError{HttpCode: http.StatusBadRequest, Err: errors.New("Unsupported domain name service suffix: " + nameServiceSuffix)}
}
chainConfig, _ := client.Config.Chains[web3Url.HostDomainNameResolverChainId]
_, domainNameServiceSupportedInChain := chainConfig.DomainNameServices[domainNameService]
if domainNameServiceSupportedInChain == false {
return web3Url, &Web3ProtocolError{HttpCode: http.StatusBadRequest, Err: errors.New("Unsupported domain name service suffix: " + nameServiceSuffix)}
}
web3Url.HostDomainNameResolver = domainNameService
// Make the domaine name resolution, cache it
var addr common.Address
var targetChain int
var hit bool
cacheKey := fmt.Sprintf("%v:%v", web3Url.HostDomainNameResolverChainId, web3Url.UrlParts.Hostname)
if client.DomainNameResolutionCache != nil {
addr, targetChain, hit = client.DomainNameResolutionCache.get(cacheKey)
}
if !hit {
var err error
addr, targetChain, err = client.getAddressFromNameServiceInclErc6821(web3Url.HostDomainNameResolverChainId, web3Url.UrlParts.Hostname)
if err != nil {
return web3Url, err
}
if client.DomainNameResolutionCache != nil {
client.DomainNameResolutionCache.add(cacheKey, addr, targetChain)
}
}
web3Url.ContractAddress = addr
if targetChain > 0 {
web3Url.ChainId = targetChain
}
_, ok = client.Config.Chains[web3Url.ChainId]
if !ok {
return web3Url, &Web3ProtocolError{HttpCode: http.StatusBadRequest, Err: errors.New(fmt.Sprintf("unsupported chain id: %v", web3Url.ChainId))}
}
}
// Determine the web3 mode
// 3 modes:
// - Auto : we parse the path and arguments and send them
// - Manual : we forward all the path & arguments as calldata
// - ResourceRequest : we parse the path and arguments and send them
// See if it is cached
resolveModeCacheKey := ResolveModeCacheKey{web3Url.ChainId, web3Url.ContractAddress}
resolveMode, resolveModeIsCached := client.ResolveModeCache.Get(resolveModeCacheKey)
if resolveModeIsCached {
web3Url.ResolveMode = resolveMode
// Not cached: Call the resolveMode in the contract
} else {
resolveModeCalldata, err := methodCallToCalldata("resolveMode", []abi.Type{}, []interface{}{})
if err != nil {
return web3Url, err
}
resolveModeReturn, err := client.callContract(web3Url.ContractAddress, web3Url.ChainId, resolveModeCalldata)
// Determine if the error is a JSON error (the RPC call was successful (HTTP 200) but
// the JSON returned indicate an error (we assume execution error)
isExecutionRevertedError := false
if err != nil {
if wperr, ok := err.(*Web3ProtocolError); ok {
if wperr.Type == Web3ProtocolErrorTypeRPCJsonError {
isExecutionRevertedError = true
}
}
}
// Auto : exact match or empty bytes32 value or empty value (method does not exist or return nothing)
// or execution reverted
if len(resolveModeReturn) == 32 && common.Bytes2Hex(resolveModeReturn) == "6175746f00000000000000000000000000000000000000000000000000000000" ||
len(resolveModeReturn) == 32 && common.Bytes2Hex(resolveModeReturn) == "0000000000000000000000000000000000000000000000000000000000000000" ||
len(resolveModeReturn) == 0 && err == nil ||
isExecutionRevertedError {
web3Url.ResolveMode = ResolveModeAuto
// Manual : exact match
} else if len(resolveModeReturn) == 32 && common.Bytes2Hex(resolveModeReturn) == "6d616e75616c0000000000000000000000000000000000000000000000000000" {
web3Url.ResolveMode = ResolveModeManual
// ResourceRequest : exact match
} else if len(resolveModeReturn) == 32 && common.Bytes2Hex(resolveModeReturn) == "3532313900000000000000000000000000000000000000000000000000000000" {
web3Url.ResolveMode = ResolveModeResourceRequests
// We got an error
} else if err != nil {
return web3Url, err
// Other cases (method returning non recognized value) : throw an error
} else {
return web3Url, &Web3ProtocolError{HttpCode: http.StatusBadRequest, Err: errors.New("Unsupported resolve mode")}
}
// Cache the resolve mode
client.ResolveModeCache.Add(resolveModeCacheKey, web3Url.ResolveMode)
}
// Then process the resolve-mode-specific parts
if web3Url.ResolveMode == ResolveModeManual {
err = client.parseManualModeUrl(&web3Url)
} else if web3Url.ResolveMode == ResolveModeAuto {
err = client.parseAutoModeUrl(&web3Url)
} else if web3Url.ResolveMode == ResolveModeResourceRequests {
err = client.parseResourceRequestModeUrl(&web3Url)
}
if err != nil {
return
}
return
}
/**
* Step 2: Attempt an early response which bypass a contract call.
*/
func (client *Client) AttemptEarlyResponse(web3Url *Web3URL) (fetchedWeb3Url FetchedWeb3URL, success bool, err error) {
// If we are in resource request mode, we check if the resource request is cached
if web3Url.ResolveMode == ResolveModeResourceRequests {
return client.AttemptEarlyResourceRequestModeResponse(web3Url)
}
return fetchedWeb3Url, false, nil
}
/**
* Step 3: Make the call to the main contract.
*/
func (client *Client) FetchContractReturn(web3Url *Web3URL) (contractReturn []byte, err error) {
var calldata []byte
// Compute the calldata
calldata, err = web3Url.ComputeCalldata()
if err != nil {
return contractReturn, err
}
// Do the contract call
contractReturn, err = client.callContract(web3Url.ContractAddress, web3Url.ChainId, calldata)
if err != nil {
return
}
if len(contractReturn) == 0 {
return contractReturn, &Web3ProtocolError{HttpCode: http.StatusBadRequest, Err: errors.New("The contract returned no data (\"0x\").\n\nThis could be due to any of the following:\n - The contract does not have the requested function,\n - The parameters passed to the contract function may be invalid, or\n - The address is not a contract.")}
}
return
}
/**
* Step 4 : Process the data returned by the main contract.
*/
func (client *Client) ProcessContractReturn(web3Url *Web3URL, contractReturn []byte) (fetchedWeb3Url FetchedWeb3URL, err error) {
// Add link to the parsedUrl
fetchedWeb3Url.ParsedUrl = web3Url
// Init the maps
fetchedWeb3Url.HttpHeaders = map[string]string{}
if web3Url.ContractReturnProcessing == "" {
err = errors.New("Missing ContractReturnProcessing field")
return
}
// Returned data is ABI-encoded bytes: We decode them and return them
if web3Url.ContractReturnProcessing == ContractReturnProcessingDecodeABIEncodedBytes {
bytesType, _ := abi.NewType("bytes", "", nil)
argsArguments := abi.Arguments{
abi.Argument{Name: "", Type: bytesType, Indexed: false},
}
// Decode the ABI bytes
unpackedValues, err := argsArguments.UnpackValues(contractReturn)
if err != nil {
return fetchedWeb3Url, &Web3ProtocolError{HttpCode: http.StatusBadRequest, Err: errors.New("Unable to parse contract output")}
}
fetchedWeb3Url.Output = bytes.NewReader(unpackedValues[0].([]byte))
fetchedWeb3Url.HttpCode = 200
// If a MIME type was hinted, inject it
if web3Url.DecodedABIEncodedBytesMimeType != "" {
fetchedWeb3Url.HttpHeaders["Content-Type"] = web3Url.DecodedABIEncodedBytesMimeType
}
// We JSON encode the raw bytes of the returned data
} else if web3Url.ContractReturnProcessing == ContractReturnProcessingRawBytesJsonEncoded {
jsonEncodedOutput, err := json.Marshal([]string{fmt.Sprintf("0x%x", contractReturn)})
if err != nil {
return fetchedWeb3Url, err
}
fetchedWeb3Url.Output = bytes.NewReader(jsonEncodedOutput)
fetchedWeb3Url.HttpCode = 200
fetchedWeb3Url.HttpHeaders["Content-Type"] = "application/json"
// Having a contract return signature, we ABI-decode it and return the result JSON-encoded
} else if web3Url.ContractReturnProcessing == ContractReturnProcessingJsonEncodeValues {
argsArguments := abi.Arguments{}
for _, jsonEncodedValueType := range web3Url.JsonEncodedValueTypes {
argsArguments = append(argsArguments, abi.Argument{Name: "", Type: jsonEncodedValueType, Indexed: false})
}
// Decode the ABI data
unpackedValues, err := argsArguments.UnpackValues(contractReturn)
if err != nil {
return fetchedWeb3Url, &Web3ProtocolError{HttpCode: http.StatusBadRequest, Err: errors.New("Unable to parse contract output")}
}
// Format the data
formattedValues := make([]interface{}, 0)
for i, arg := range argsArguments {
// get the type of the return value
formattedValue, err := JsonEncodeAbiTypeValue(arg.Type, unpackedValues[i])
if err != nil {
return fetchedWeb3Url, err
}
formattedValues = append(formattedValues, formattedValue)
}
// JSON encode the data
jsonEncodedOutput, err := json.Marshal(formattedValues)
if err != nil {
return fetchedWeb3Url, err
}
fetchedWeb3Url.Output = bytes.NewReader(jsonEncodedOutput)
fetchedWeb3Url.HttpCode = 200
fetchedWeb3Url.HttpHeaders["Content-Type"] = "application/json"
// The returned data come from contract implementing ERC5219, process it
} else if web3Url.ContractReturnProcessing == ContractReturnProcessingDecodeErc5219Request {
err = client.ProcessResourceRequestContractReturn(&fetchedWeb3Url, web3Url, contractReturn)
}
return
}
// If ContractCallMode is calldata, returned the stored calldata
// If ContractCallMode is method, compute and return it
func (web3Url *Web3URL) ComputeCalldata() (calldata []byte, err error) {
// Contract call is specified with method and arguments, deduce the calldata from it
if web3Url.ContractCallMode == ContractCallModeMethod {
// Compute the calldata
calldata, err = methodCallToCalldata(web3Url.MethodName, web3Url.MethodArgs, web3Url.MethodArgValues)
if err != nil {
return
}
// Contract call is specified with calldata directly
} else if web3Url.ContractCallMode == ContractCallModeCalldata {
calldata = web3Url.Calldata
// Empty field: This should not happen
} else {
err = errors.New("ContractCallMode is empty")
}
return
}