-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.go
450 lines (395 loc) · 10.6 KB
/
type.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
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package abi
import (
"encoding/hex"
"fmt"
"math/big"
"reflect"
"regexp"
"strconv"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
)
const (
IntTy byte = iota
UintTy
BoolTy
StringTy
SliceTy
AddressTy
FixedBytesTy
BytesTy
HashTy
FixedPointTy
FunctionTy
)
// Type is the reflection of the supported argument type
type Type struct {
IsSlice, IsArray bool
SliceSize int
Elem *Type
Kind reflect.Kind
Type reflect.Type
Size int
T byte // Our own type checking
stringKind string // holds the unparsed string for deriving signatures
}
var (
// fullTypeRegex parses the abi types
//
// Types can be in the format of:
//
// Input = Type [ "[" [ Number ] "]" ] Name .
// Type = [ "u" ] "int" [ Number ] [ x ] [ Number ].
//
// Examples:
//
// string int uint fixed
// string32 int8 uint8 uint[]
// address int256 uint256 fixed128x128[2]
fullTypeRegex = regexp.MustCompile(`([a-zA-Z0-9]+)(\[([0-9]*)\])?`)
// typeRegex parses the abi sub types
typeRegex = regexp.MustCompile("([a-zA-Z]+)(([0-9]+)(x([0-9]+))?)?")
)
// NewType creates a new reflection type of abi type given in t.
func NewType(t string) (typ Type, err error) {
res := fullTypeRegex.FindAllStringSubmatch(t, -1)[0]
// check if type is slice and parse type.
switch {
case res[3] != "":
// err is ignored. Already checked for number through the regexp
typ.SliceSize, _ = strconv.Atoi(res[3])
typ.IsArray = true
case res[2] != "":
typ.IsSlice, typ.SliceSize = true, -1
case res[0] == "":
return Type{}, fmt.Errorf("abi: type parse error: %s", t)
}
if typ.IsArray || typ.IsSlice {
sliceType, err := NewType(res[1])
if err != nil {
return Type{}, err
}
typ.Elem = &sliceType
typ.stringKind = sliceType.stringKind + t[len(res[1]):]
// Although we know that this is an array, we cannot return
// as we don't know the type of the element, however, if it
// is still an array, then don't determine the type.
if typ.Elem.IsArray || typ.Elem.IsSlice {
return typ, nil
}
}
// parse the type and size of the abi-type.
parsedType := typeRegex.FindAllStringSubmatch(res[1], -1)[0]
// varSize is the size of the variable
var varSize int
if len(parsedType[3]) > 0 {
var err error
varSize, err = strconv.Atoi(parsedType[2])
if err != nil {
return Type{}, fmt.Errorf("abi: error parsing variable size: %v", err)
}
}
// varType is the parsed abi type
varType := parsedType[1]
// substitute canonical integer
if varSize == 0 && (varType == "int" || varType == "uint") {
varSize = 256
t += "256"
}
// only set stringKind if not array or slice, as for those,
// the correct string type has been set
if !(typ.IsArray || typ.IsSlice) {
typ.stringKind = t
}
switch varType {
case "int":
typ.Kind, typ.Type = reflectIntKindAndType(false, varSize)
typ.Size = varSize
typ.T = IntTy
case "uint":
typ.Kind, typ.Type = reflectIntKindAndType(true, varSize)
typ.Size = varSize
typ.T = UintTy
case "bool":
typ.Kind = reflect.Bool
typ.T = BoolTy
case "address":
typ.Kind = reflect.Array
typ.Type = address_t
typ.Size = 20
typ.T = AddressTy
case "string":
typ.Kind = reflect.String
typ.Size = -1
typ.T = StringTy
case "bytes":
sliceType, _ := NewType("uint8")
typ.Elem = &sliceType
if varSize == 0 {
typ.IsSlice = true
typ.T = BytesTy
typ.SliceSize = -1
} else {
typ.IsArray = true
typ.T = FixedBytesTy
typ.SliceSize = varSize
}
case "function":
sliceType, _ := NewType("uint8")
typ.Elem = &sliceType
typ.IsArray = true
typ.T = FunctionTy
typ.SliceSize = 24
default:
return Type{}, fmt.Errorf("unsupported arg type: %s", t)
}
return
}
// String implements Stringer
func (t Type) String() (out string) {
return t.stringKind
}
func (t Type) Pack(v interface{}) ([]byte, error) {
// pretty.Println("type", t)
if (t.IsSlice || t.IsArray) && t.T != BytesTy && t.T != FixedBytesTy && t.T != FunctionTy {
return t.encodeSlice(v)
}
if v == nil {
return nil, errors.Errorf("Expected %s got nil", t.String())
}
switch t.T {
case IntTy:
return t.encodeIntTy(v)
case UintTy:
return t.encodeUintTy(v)
case StringTy:
return t.encodeString(v)
case BytesTy:
return t.encodeBytes(v)
case AddressTy:
return t.encodeAddress(v)
case FixedBytesTy:
return t.encodeFixedBytes(v)
case BoolTy:
return t.encodeBool(v)
}
return nil, nil
}
func (t Type) hexStringToBytes(v string) ([]byte, error) {
if strings.HasPrefix(v, "0x") {
v = v[2:]
}
bytes, err := hex.DecodeString(v)
if err != nil {
return nil, errors.Errorf("Expected %s in hex got: %v", t.String(), v)
}
return bytes, nil
}
func (t Type) encodeBool(v interface{}) ([]byte, error) {
switch v := v.(type) {
case bool:
if v {
return t.encodeUintTy(int64(1))
}
return t.encodeUintTy(int64(0))
default:
return nil, errors.Errorf("Expected %s got: %v", t.String(), v)
}
}
func (t Type) encodeString(v interface{}) ([]byte, error) {
switch v := v.(type) {
case string:
return packBytesSlice([]byte(v), len(v)), nil
default:
return nil, errors.Errorf("Expected %s got: %v", t.String(), v)
}
}
func (t Type) encodeBytes(v interface{}) ([]byte, error) {
switch v := v.(type) {
case string:
bytes, err := t.hexStringToBytes(v)
if err != nil {
return nil, err
}
return packBytesSlice(bytes, len(bytes)), nil
case []byte:
return packBytesSlice(v, len(v)), nil
default:
return nil, errors.Errorf("Expected %s in hex got: %v", t.String(), v)
}
}
func (t Type) encodeAddress(v interface{}) ([]byte, error) {
switch v := v.(type) {
case string:
bytes, err := t.hexStringToBytes(v)
if err != nil {
return nil, err
}
if len(bytes) != 20 {
return nil, errors.Errorf("Expected %s to have 20 bytes got: %v", t.String(), len(bytes))
}
return common.LeftPadBytes(bytes, 32), nil
case []byte:
bytes := v
if len(bytes) == 20 {
return nil, errors.Errorf("Expected %s to have 20 bytes got: %v", t.String(), len(bytes))
}
return common.LeftPadBytes(bytes, 32), nil
default:
return nil, errors.Errorf("Expected %s in hex got: %v", t.String(), v)
}
}
func (t Type) encodeFixedBytes(v interface{}) ([]byte, error) {
switch v := v.(type) {
case string:
bytes, err := t.hexStringToBytes(v)
if err != nil {
return nil, err
}
if len(bytes) > t.SliceSize {
return nil, errors.Errorf("Expected %s to have %d bytes got: %d", t.String(), t.SliceSize, len(bytes))
}
return common.LeftPadBytes(bytes, 32), nil
case []byte:
bytes := v
if len(bytes) > t.SliceSize {
return nil, errors.Errorf("Expected %s to have %d bytes got: %d", t.String(), t.SliceSize, len(bytes))
}
return common.LeftPadBytes(bytes, 32), nil
default:
return nil, errors.Errorf("Expected %s in hex got: %v", t.String(), v)
}
}
func (t Type) encodeSlice(v interface{}) ([]byte, error) {
var vv []interface{}
if v != nil {
var ok bool
vv, ok = v.([]interface{})
if !ok {
return nil, errors.Errorf("Expected %s got: %v", t.String(), v)
}
}
var packed []byte
for i := 0; i < len(vv); i++ {
data, err := t.Elem.Pack(vv[i])
if err != nil {
return nil, errors.Wrapf(err, "%s at %d", t.String(), i)
}
packed = append(packed, data...)
}
if t.IsSlice {
return packBytesSlice(packed, len(vv)), nil
}
return packed, nil
}
// TODO: handle truncation
func (t Type) encodeIntTy(v interface{}) ([]byte, error) {
fmt.Println("encode int", v)
switch v := v.(type) {
case int, int8, int16, int32, int64:
n, err := strconv.Atoi(fmt.Sprintf("%d", v))
if err != nil {
return nil, errors.Errorf("Expected %s got: %v", t.String(), v)
}
i := big.NewInt(int64(n))
return U256(i), nil
case *big.Int:
return U256(v), nil
case float64:
f := big.NewFloat(v)
if !f.IsInt() {
return nil, errors.Errorf("Expected %s got: %v", t.String(), v)
}
i, _ := f.Int(nil)
return U256(i), nil
case string:
var i big.Int
_, ok := i.SetString(v, 0)
if !ok {
return nil, errors.Errorf("Expected big number string for %s got: %v", t.String(), v)
}
return U256(&i), nil
default:
return nil, errors.Errorf("Expected %s got: %v", t.String(), v)
}
}
func (t Type) encodeUintTy(v interface{}) ([]byte, error) {
switch v := v.(type) {
case int, int8, int16, int32, int64,
uint, uint8, uint16, uint32, uint64:
n, err := strconv.Atoi(fmt.Sprintf("%d", v))
if err != nil {
return nil, errors.Errorf("Expected %s got: %v", t.String(), v)
}
if n < 0 {
return nil, errors.Errorf("Expected %s got: %v", t.String(), v)
}
i := big.NewInt(int64(n))
return U256(i), nil
case *big.Int:
if v.Sign() == -1 {
return nil, errors.Errorf("Expected %s got: %v", t.String(), v)
}
return U256(v), nil
case float64:
f := big.NewFloat(v)
if !f.IsInt() || f.Sign() == -1 {
return nil, errors.Errorf("Expected %s got: %v", t.String(), f.String())
}
i, _ := f.Int(nil)
return U256(i), nil
case string:
var i big.Int
_, ok := i.SetString(v, 0)
if !ok {
return nil, errors.Errorf("Expected big number string for %s got: %v", t.String(), v)
}
return U256(&i), nil
default:
return nil, errors.Errorf("Expected %s got: %v", t.String(), v)
}
}
func (t Type) pack(v reflect.Value) ([]byte, error) {
// dereference pointer first if it's a pointer
v = indirect(v)
if err := typeCheck(t, v); err != nil {
return nil, err
}
if (t.IsSlice || t.IsArray) && t.T != BytesTy && t.T != FixedBytesTy && t.T != FunctionTy {
var packed []byte
for i := 0; i < v.Len(); i++ {
val, err := t.Elem.pack(v.Index(i))
if err != nil {
return nil, err
}
packed = append(packed, val...)
}
if t.IsSlice {
return packBytesSlice(packed, v.Len()), nil
} else if t.IsArray {
return packed, nil
}
}
return packElement(t, v), nil
}
// requireLengthPrefix returns whether the type requires any sort of length
// prefixing.
func (t Type) requiresLengthPrefix() bool {
return t.T != FixedBytesTy && (t.T == StringTy || t.T == BytesTy || t.IsSlice)
}