-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunpack.go
284 lines (251 loc) · 6.88 KB
/
unpack.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
package go_abi_param
import (
"encoding/binary"
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"math/big"
"reflect"
"strconv"
"strings"
)
// 老版本的数组解析只支持一维,且格式为 aaa,bbb,ccc
// 当前版本引入多维数组,格式变更为:
// [1,2,3]
// [[1,2],[3,4]] [1,2],[3,4]
// [[[[1,2],[11,22]],[3,4]]]
func fmtToBlock(originVal string) string {
if !hasBlock(originVal) {
//return originVal
return fmt.Sprintf("%s%s%s", "[", originVal, "]")
}
return originVal
}
func hasBlock(originVal string) bool {
return originVal[:1] == "[" && originVal[len(originVal)-1:] == "]"
}
func parseUnpackString(value string) ([]interface{}, error) {
value = fmtToBlock(value)
if strings.Count(value, "[") != strings.Count(value, "]") {
return nil, fmt.Errorf("unpaired block")
}
// 可能传入可能值,不一定是非标准的字符串,所以需要将其转换为字符串,再使用 json 包解析
var _value []byte
for i, _v := range []byte(value) {
if _v == '[' {
if []byte(value)[i+1] == '[' {
_value = append(_value, _v)
continue
}
_value = append(_value, _v, '"')
continue
} else if _v == '"' {
_value = append(_value, _v)
continue
} else if _v == ']' {
if len([]byte(value)) > i+1 && []byte(value)[i+1] == ']' {
_value = append(_value, _v)
continue
}
_value = append(_value, '"', _v)
continue
} else if _v == ',' {
_value = append(_value, '"', _v, '"')
continue
} else {
if len([]byte(value)) > i+1 && []byte(value)[i+1] == ']' {
_value = append(_value, _v, '"')
continue
}
_value = append(_value, _v)
}
}
value = strings.ReplaceAll(string(_value), `""`, `"`)
value = strings.ReplaceAll(value, `]",`, `],`)
value = strings.ReplaceAll(value, `,"[`, `,[`)
value = strings.ReplaceAll(value, `]"]`, `]]`)
var parsedData interface{}
err := json.Unmarshal([]byte(value), &parsedData)
if err != nil {
return nil, err
}
if reflect.TypeOf(parsedData).String() != "[]interface {}" {
return nil, fmt.Errorf("parsed data type is interface should be []interface {]")
}
return parsedData.([]interface{}), err
}
func (ap *AbiParam) forEachUnpackForString(t abi.Type, originVal string) (interface{}, error) {
output, err := parseUnpackString(originVal)
if err != nil {
return nil, err
}
if t.Size < 0 {
return nil, fmt.Errorf("cannot parse input array, size is negative (%d)", t.Size)
}
if t.Size > len(output) {
return nil, fmt.Errorf("abi: cannot marshal in to go array: offset %d would go over slice boundary (len=%d)", len(output), t.Size)
}
// this value will become our slice or our array, depending on the type
var refSlice reflect.Value
if t.T == abi.SliceTy {
// reset slice size
t.Size = len(output)
// declare our slice
refSlice = reflect.MakeSlice(t.GetType(), t.Size, t.Size)
} else if t.T == abi.ArrayTy {
// declare our array
refSlice = reflect.New(t.GetType()).Elem()
} else {
return nil, fmt.Errorf("abi: invalid type in array/slice unpacking stage")
}
for i := 0; i < t.Size; i++ {
ap.logger.Debugf("nest type: %s", getType(*t.Elem))
//opVal := unpackDynamicData(t.Size, output)
opVal := ""
if reflect.TypeOf(output).Kind() == reflect.String {
opVal = output[i].(string)
} else {
opVal = unpackDynamicData(output[i])
}
inter, err := ap.parseParam(getType(*t.Elem), opVal)
if err != nil {
return nil, err
}
refSlice.Index(i).Set(reflect.ValueOf(inter))
}
return refSlice.Interface(), nil
}
func unpackDynamicData(ov interface{}) string {
var _val string
iValType := reflect.TypeOf(ov).String()
switch iValType {
case "string":
return ov.(string)
case "[]interface {}":
var _ovArr []string
for i, _ov := range ov.([]interface{}) {
_dData := unpackDynamicData(_ov)
// replace to the original value
if i == 0 {
_dData = "[" + _dData
}
// replace to the original value
if i == len(ov.([]interface{}))-1 {
_dData = _dData + "]"
}
_ovArr = append(_ovArr, _dData)
}
_val = strings.Join(_ovArr, ",")
}
return _val
}
// readFixedBytes uses reflection to create a fixed array to be read from.
func readFixedBytes(t abi.Type, word []byte) (interface{}, error) {
if t.T != abi.FixedBytesTy {
return nil, fmt.Errorf("abi: invalid type in call to make fixed byte array")
}
// convert
array := reflect.New(t.GetType()).Elem()
reflect.Copy(array, reflect.ValueOf(word[0:t.Size]))
return array.Interface(), nil
}
// readFunctionType enforces that standard by always presenting it as a 24-array (address + sig = 24 bytes)
func readFunctionType(t abi.Type, word []byte) (funcTy [24]byte, err error) {
if t.T != abi.FunctionTy {
return [24]byte{}, fmt.Errorf("abi: invalid type in call to make function type byte array")
}
if garbage := binary.BigEndian.Uint64(word[24:32]); garbage != 0 {
err = fmt.Errorf("abi: got improperly encoded function type, got %v", word)
} else {
copy(funcTy[:], word[0:24])
}
return
}
func readBigInt(value string) (*big.Int, error) {
v, ok := big.NewInt(0).SetString(value, 10)
if !ok {
return nil, fmt.Errorf("param %s can not convent to int", value)
}
return v, nil
}
func readAddress(value string) (common.Address, error) {
if value == "" {
return common.Address{}, fmt.Errorf("can't convent param %s to address", value)
}
return common.HexToAddress(value), nil
}
// readBool reads a bool.
func readBool(word string) (bool, error) {
switch word {
case "0", "false":
return false, nil
case "1", "true":
return true, nil
default:
return false, errBadBool
}
}
func readInt8(value string) (int8, error) {
bv, err := readBigInt(value)
if err != nil {
return 0, err
}
return int8(bv.Int64()), nil
}
func readUint8(value string) (uint8, error) {
bv, err := readBigInt(value)
if err != nil {
return 0, err
}
return uint8(bv.Int64()), nil
}
func readInt16(value string) (int16, error) {
v, err := strconv.ParseInt(value, 10, 16)
if err != nil {
return 0, err
}
return int16(v), nil
}
func readUint16(value string) (uint16, error) {
v, err := strconv.ParseUint(value, 10, 16)
if err != nil {
return 0, err
}
return uint16(v), nil
}
func readInt32(value string) (int32, error) {
v, err := strconv.ParseInt(value, 10, 32)
if err != nil {
return 0, err
}
return int32(v), nil
}
func readUint32(value string) (uint32, error) {
v, err := strconv.ParseUint(value, 10, 32)
if err != nil {
return 0, err
}
return uint32(v), nil
}
func readInt64(value string) (int64, error) {
v, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return 0, err
}
return v, nil
}
func readUint64(value string) (uint64, error) {
v, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return 0, err
}
return v, nil
}
func readBytes(value string) ([]byte, error) {
return hexutil.Decode(value)
}
func readString(value string) (string, error) {
return value, nil
}