-
Notifications
You must be signed in to change notification settings - Fork 0
/
abi_encode.go
219 lines (192 loc) · 6.69 KB
/
abi_encode.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
package ethereum
import (
"encoding/hex"
"errors"
"math/big"
"reflect"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
)
// ABI_TUPLE_KEY_ORDER is a specific key that should be defined in tuple structure definition
const ABI_TUPLE_KEY_ORDER = "ABI_TUPLE_KEY_ORDER"
// parseABIParams parse types to proper []string and []abi.ArgumentMarshaling that could be accepted by abi.NewType(),
// also parse the value into proper golang type that (abi.Arguments).Pack accepted
func parseABIParams(types []interface{}, values []interface{}) ([]string, []*[]abi.ArgumentMarshaling, []interface{}, error) {
abiTypesResult := make([]string, len(types))
abiTypesArgumentsResult := make([]*[]abi.ArgumentMarshaling, len(types))
parseValueResult := make([]interface{}, len(values))
for i, ty := range types {
switch typ := ty.(type) {
case string:
abiTypesResult[i] = typ
abiTypesArgumentsResult[i] = nil
pv, err := parseABIValue(typ, values[i])
if err != nil {
return nil, nil, nil, err
}
parseValueResult[i] = pv
case map[string]interface{}:
for structName, spt := range typ {
switch structName {
case "tuple":
if _, ok := spt.(map[string]interface{}); !ok {
return nil, nil, nil, errors.New("failed to parse tuple properties")
}
structProperties := spt.(map[string]interface{})
if _, ok := values[i].(map[string]interface{}); !ok {
return nil, nil, nil, errors.New("failed to parse tuple arguments")
}
value := values[i].(map[string]interface{})
if structProperties[ABI_TUPLE_KEY_ORDER] == nil {
return nil, nil, nil, errors.New("tuple structure should always define key ABI_TUPLE_KEY_ORDER")
}
if _, ok := structProperties[ABI_TUPLE_KEY_ORDER].([]interface{}); !ok {
return nil, nil, nil, errors.New("failed to parse tuple ABI_TUPLE_KEY_ORDER to []interface{}")
}
keyOrder := structProperties[ABI_TUPLE_KEY_ORDER].([]interface{})
if len(keyOrder) != len(value) {
return nil, nil, nil, errors.New("tuple key order param missing")
}
tupleArguments := make([]abi.ArgumentMarshaling, len(keyOrder))
// create dynamic struct fields
sf := make([]reflect.StructField, len(keyOrder))
// create dynamic struct values
sv := make([]reflect.Value, len(keyOrder))
for j, key := range keyOrder {
if _, ok := key.(string); !ok {
return nil, nil, nil, errors.New("failed to parse ABI_TUPLE_KEY_ORDER tuple key to string")
}
stringKey := key.(string)
structProperty := structProperties[stringKey]
abiTypes, abiTypesArguments, parsedValue, err := parseABIParams([]interface{}{structProperty}, []interface{}{value[stringKey]})
if err != nil {
return nil, nil, nil, err
}
abiType := abiTypes[0]
titleKey := strings.Title(stringKey)
// assign tuple arguments
if abiTypesArguments[0] == nil {
tupleArguments[j] = abi.ArgumentMarshaling{
Name: titleKey,
Type: abiType,
}
} else {
tupleArguments[j] = abi.ArgumentMarshaling{
Name: titleKey,
Type: abiType,
Components: *abiTypesArguments[0],
}
}
sf[j] = reflect.StructField{
Name: titleKey,
Type: reflect.TypeOf(parsedValue[0]),
}
sv[j] = reflect.ValueOf(parsedValue[0])
}
// create dynamic struct instance
ds := reflect.StructOf(sf)
e := reflect.New(ds).Elem()
// assign dynamic struct value
for o, f := range sf {
e.FieldByName(f.Name).Set(sv[o])
}
abiTypesResult[i] = structName
abiTypesArgumentsResult[i] = &tupleArguments
parseValueResult[i] = e.Interface()
case "slice":
sliceProperties := spt
if _, ok := values[i].([]interface{}); !ok {
return nil, nil, nil, errors.New("failed to parse slice arguments")
}
targetValues := values[i].([]interface{})
var abiTypes []string
var abiTypesArguments []*[]abi.ArgumentMarshaling
var err error
parsedValues := make([]interface{}, len(targetValues))
for h, targetValue := range targetValues {
var parsedValue []interface{}
abiTypes, abiTypesArguments, parsedValue, err = parseABIParams([]interface{}{sliceProperties}, []interface{}{targetValue})
if err != nil {
return nil, nil, nil, err
}
parsedValues[h] = parsedValue[0]
}
// create dynamic slice
elemType := reflect.ValueOf(parsedValues[0]).Type()
elemSlice := reflect.New(reflect.SliceOf(elemType)).Elem()
for _, dv := range parsedValues {
elemSlice = reflect.Append(elemSlice, reflect.ValueOf(dv))
}
abiTypesResult[i] = abiTypes[0] + "[]"
abiTypesArgumentsResult[i] = abiTypesArguments[0]
parseValueResult[i] = elemSlice.Interface()
default:
return nil, nil, nil, errors.New("not supported definition struct")
}
}
default:
return nil, nil, nil, errors.New("not supported definition struct")
}
}
return abiTypesResult, abiTypesArgumentsResult, parseValueResult, nil
}
// parseABIValue parse the value into proper golang type that (abi.Arguments).Pack accepted
func parseABIValue(abiType string, value interface{}) (interface{}, error) {
if _, ok := value.(string); !ok {
return nil, errors.New("can't parse value not in type string")
}
stringValue := value.(string)
var valuesResult interface{}
switch abiType {
case "address":
valuesResult = common.HexToAddress(stringValue)
case "uint256", "uint128", "uint64", "uint32", "uint16", "uint8":
n, ok := big.NewInt(0).SetString(stringValue, 0)
if !ok {
return nil, errors.New("fail to parse uint number from hex")
}
switch abiType {
case "uint64":
valuesResult = n.Uint64()
case "uint32":
valuesResult = uint32(n.Uint64())
case "uint16":
valuesResult = uint16(n.Uint64())
case "uint8":
valuesResult = uint8(n.Uint64())
default:
valuesResult = n
}
case "bytes32":
b, err := hex.DecodeString(strings.TrimPrefix(stringValue, "0x"))
if err != nil {
return nil, errors.New("fail to decode hex string")
}
if len(b) > 32 {
return nil, errors.New("the length of vaule exceed")
}
var valueBytes [32]byte
copy(valueBytes[:], b[:])
valuesResult = valueBytes
case "bytes":
b, err := hex.DecodeString(strings.TrimPrefix(stringValue, "0x"))
if err != nil {
return nil, errors.New("fail to decode hex string")
}
valuesResult = b
case "string":
valuesResult = value
case "bool":
if stringValue == "true" {
valuesResult = true
} else if stringValue == "false" {
valuesResult = false
} else {
return nil, errors.New("fail to decode bool")
}
default:
return nil, errors.New("unsupported type")
}
return valuesResult, nil
}