/
limitation.go
94 lines (74 loc) · 1.78 KB
/
limitation.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
package core
import (
"fmt"
"strings"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/rlp"
)
type RequestData struct {
JsonRpc string `json:"jsonrpc"`
ID int64 `json:"id"`
Method string `json:"method"`
Params []interface{} `json:"params"`
}
// eth_call
// eth_estimateGas
// eth_getLogs
// eth_getBalance
// eth_getCode
// eth_getStorageAt
// eth_getTransactionCount
var DecodeError = fmt.Errorf("decode error")
var DeniedMethod = fmt.Errorf("not allowed method")
var DeniedContract = fmt.Errorf("not allowed contract or address")
func isAllowedMethod(method string) bool {
return currentRunningConfig.allowedMethods[method]
}
func inWhitelist(contractAddress string) bool {
return currentRunningConfig.allowedCallContracts[strings.ToLower(contractAddress)]
}
func isValidCall(req *RequestData) (err error) {
defer func() {
if er := recover(); er != nil {
err = DecodeError
}
}()
if !isAllowedMethod(req.Method) {
return DeniedMethod
}
if req.Method == "eth_getBalance" ||
req.Method == "eth_getTransactionReceipt" {
return nil
}
if req.Method == "eth_call" || req.Method == "eth_estimateGas" {
to := req.Params[0].(map[string]interface{})["to"].(string)
if !inWhitelist(to) {
return DeniedContract
}
return nil
}
if req.Method == "eth_sendRawTransaction" {
// 0. nonce
// 1. gasPrice
// 2. gasLimit
// 3. to
// 4. value
// 5. data
// 6. signature
var fields []interface{}
data := req.Params[0].(string)
bts, _ := hexutil.Decode(data)
err = rlp.DecodeBytes(bts, &fields)
if err != nil {
return DecodeError
}
if !inWhitelist(fields[3].(string)) {
return DeniedContract
}
return nil
}
if isAllowedMethod(req.Method) {
return nil
}
return DeniedContract
}