This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
abi.go
1495 lines (1315 loc) · 33.9 KB
/
abi.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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package abi
import (
"encoding/binary"
"encoding/json"
"fmt"
"io/ioutil"
"math/big"
"reflect"
"regexp"
"strconv"
"strings"
"unsafe" // just for Sizeof
burrow_binary "github.com/hyperledger/burrow/binary"
"github.com/hyperledger/burrow/crypto"
"github.com/hyperledger/burrow/crypto/sha3"
)
// EVM Solidity calls and return values are packed into
// pieces of 32 bytes, including a bool (wasting 255 out of 256 bits)
const ElementSize = 32
type EVMType interface {
GetSignature() string
getGoType() interface{}
pack(v interface{}) ([]byte, error)
unpack(data []byte, offset int, v interface{}) (int, error)
Dynamic() bool
}
var _ EVMType = (*EVMBool)(nil)
type EVMBool struct {
}
func (e EVMBool) GetSignature() string {
return "bool"
}
func (e EVMBool) getGoType() interface{} {
return new(bool)
}
func (e EVMBool) pack(v interface{}) ([]byte, error) {
var b bool
arg := reflect.ValueOf(v)
if arg.Kind() == reflect.String {
val := arg.String()
if strings.EqualFold(val, "true") || val == "1" {
b = true
} else if strings.EqualFold(val, "false") || val == "0" {
b = false
} else {
return nil, fmt.Errorf("%s is not a valid value for EVM Bool type", val)
}
} else if arg.Kind() == reflect.Bool {
b = arg.Bool()
} else {
return nil, fmt.Errorf("%s cannot be converted to EVM Bool type", arg.Kind().String())
}
res := make([]byte, ElementSize)
if b {
res[ElementSize-1] = 1
}
return res, nil
}
func (e EVMBool) unpack(data []byte, offset int, v interface{}) (int, error) {
if len(data)-offset < 32 {
return 0, fmt.Errorf("not enough data")
}
data = data[offset:]
switch v := v.(type) {
case *string:
if data[ElementSize-1] == 1 {
*v = "true"
} else if data[ElementSize-1] == 0 {
*v = "false"
} else {
return 0, fmt.Errorf("unexpected value for EVM bool")
}
case *int8:
*v = int8(data[ElementSize-1])
case *int16:
*v = int16(data[ElementSize-1])
case *int32:
*v = int32(data[ElementSize-1])
case *int64:
*v = int64(data[ElementSize-1])
case *int:
*v = int(data[ElementSize-1])
case *uint8:
*v = uint8(data[ElementSize-1])
case *uint16:
*v = uint16(data[ElementSize-1])
case *uint32:
*v = uint32(data[ElementSize-1])
case *uint64:
*v = uint64(data[ElementSize-1])
case *uint:
*v = uint(data[ElementSize-1])
case *bool:
*v = data[ElementSize-1] == 1
default:
return 0, fmt.Errorf("cannot set type %s for EVM bool", reflect.ValueOf(v).Kind().String())
}
return 32, nil
}
func (e EVMBool) Dynamic() bool {
return false
}
var _ EVMType = (*EVMUint)(nil)
type EVMUint struct {
M uint64
}
func (e EVMUint) GetSignature() string {
return fmt.Sprintf("uint%d", e.M)
}
func (e EVMUint) getGoType() interface{} {
switch e.M {
case 8:
return new(uint8)
case 16:
return new(uint16)
case 32:
return new(uint32)
case 64:
return new(uint64)
default:
return new(big.Int)
}
}
func (e EVMUint) pack(v interface{}) ([]byte, error) {
n := new(big.Int)
arg := reflect.ValueOf(v)
switch arg.Kind() {
case reflect.String:
_, ok := n.SetString(arg.String(), 0)
if !ok {
return nil, fmt.Errorf("Failed to parse `%s", arg.String())
}
if n.Sign() < 0 {
return nil, fmt.Errorf("negative value not allowed for uint%d", e.M)
}
case reflect.Uint8:
fallthrough
case reflect.Uint16:
fallthrough
case reflect.Uint32:
fallthrough
case reflect.Uint64:
fallthrough
case reflect.Uint:
n.SetUint64(arg.Uint())
case reflect.Int8:
fallthrough
case reflect.Int16:
fallthrough
case reflect.Int32:
fallthrough
case reflect.Int64:
fallthrough
case reflect.Int:
x := arg.Int()
if x < 0 {
return nil, fmt.Errorf("negative value not allowed for uint%d", e.M)
}
n.SetInt64(x)
default:
t := reflect.TypeOf(new(uint64))
if reflect.TypeOf(v).ConvertibleTo(t) {
n.SetUint64(reflect.ValueOf(v).Convert(t).Uint())
} else {
return nil, fmt.Errorf("cannot convert type %s to uint%d", arg.Kind().String(), e.M)
}
}
b := n.Bytes()
if uint64(len(b)) > e.M {
return nil, fmt.Errorf("value to large for int%d", e.M)
}
return pad(b, ElementSize, true), nil
}
func (e EVMUint) unpack(data []byte, offset int, v interface{}) (int, error) {
if len(data)-offset < ElementSize {
return 0, fmt.Errorf("not enough data")
}
data = data[offset:]
empty := 0
for empty = 0; empty < ElementSize; empty++ {
if data[empty] != 0 {
break
}
}
length := ElementSize - empty
switch v := v.(type) {
case *string:
b := new(big.Int)
b.SetBytes(data[empty:ElementSize])
*v = b.String()
case *big.Int:
b := new(big.Int)
*v = *b.SetBytes(data[0:ElementSize])
case *uint64:
maxLen := int(unsafe.Sizeof(*v))
if length > maxLen {
return 0, fmt.Errorf("value to large for uint64")
}
*v = binary.BigEndian.Uint64(data[ElementSize-maxLen : ElementSize])
case *uint32:
maxLen := int(unsafe.Sizeof(*v))
if length > maxLen {
return 0, fmt.Errorf("value to large for uint64")
}
*v = binary.BigEndian.Uint32(data[ElementSize-maxLen : ElementSize])
case *uint16:
maxLen := int(unsafe.Sizeof(*v))
if length > maxLen {
return 0, fmt.Errorf("value to large for uint16")
}
*v = binary.BigEndian.Uint16(data[ElementSize-maxLen : ElementSize])
case *uint8:
maxLen := 1
if length > maxLen {
return 0, fmt.Errorf("value to large for uint8")
}
*v = uint8(data[31])
case *int64:
maxLen := int(unsafe.Sizeof(*v))
if length > maxLen || (data[ElementSize-maxLen]&0x80) != 0 {
return 0, fmt.Errorf("value to large for int64")
}
*v = int64(binary.BigEndian.Uint64(data[ElementSize-maxLen : ElementSize]))
case *int32:
maxLen := int(unsafe.Sizeof(*v))
if length > maxLen || (data[ElementSize-maxLen]&0x80) != 0 {
return 0, fmt.Errorf("value to large for int64")
}
*v = int32(binary.BigEndian.Uint32(data[ElementSize-maxLen : ElementSize]))
case *int16:
maxLen := int(unsafe.Sizeof(*v))
if length > maxLen || (data[ElementSize-maxLen]&0x80) != 0 {
return 0, fmt.Errorf("value to large for int16")
}
*v = int16(binary.BigEndian.Uint16(data[ElementSize-maxLen : ElementSize]))
case *int8:
maxLen := 1
if length > maxLen || (data[ElementSize-maxLen]&0x80) != 0 {
return 0, fmt.Errorf("value to large for int8")
}
*v = int8(data[ElementSize-1])
default:
return 0, fmt.Errorf("unable to convert %s to %s", e.GetSignature(), reflect.ValueOf(v).Kind().String())
}
return 32, nil
}
func (e EVMUint) Dynamic() bool {
return false
}
var _ EVMType = (*EVMInt)(nil)
type EVMInt struct {
M uint64
}
func (e EVMInt) getGoType() interface{} {
switch e.M {
case 8:
return new(int8)
case 16:
return new(int16)
case 32:
return new(int32)
case 64:
return new(int64)
default:
return new(big.Int)
}
}
func (e EVMInt) GetSignature() string {
return fmt.Sprintf("int%d", e.M)
}
func (e EVMInt) pack(v interface{}) ([]byte, error) {
n := new(big.Int)
arg := reflect.ValueOf(v)
switch arg.Kind() {
case reflect.String:
_, ok := n.SetString(arg.String(), 0)
if !ok {
return nil, fmt.Errorf("Failed to parse `%s", arg.String())
}
case reflect.Uint8:
fallthrough
case reflect.Uint16:
fallthrough
case reflect.Uint32:
fallthrough
case reflect.Uint64:
fallthrough
case reflect.Uint:
n.SetUint64(arg.Uint())
case reflect.Int8:
fallthrough
case reflect.Int16:
fallthrough
case reflect.Int32:
fallthrough
case reflect.Int64:
fallthrough
case reflect.Int:
n.SetInt64(arg.Int())
default:
t := reflect.TypeOf(new(int64))
if reflect.TypeOf(v).ConvertibleTo(t) {
n.SetInt64(reflect.ValueOf(v).Convert(t).Int())
} else {
return nil, fmt.Errorf("cannot convert type %s to int%d", arg.Kind().String(), e.M)
}
}
b := n.Bytes()
if uint64(len(b)) > e.M {
return nil, fmt.Errorf("value to large for int%d", e.M)
}
res := pad(b, ElementSize, true)
if (res[0] & 0x80) != 0 {
return nil, fmt.Errorf("value to large for int%d", e.M)
}
if n.Sign() < 0 {
// One's complement; i.e. 0xffff is -1, not 0.
n.Add(n, big.NewInt(1))
b := n.Bytes()
res = pad(b, ElementSize, true)
for i := 0; i < len(res); i++ {
res[i] = ^res[i]
}
}
return res, nil
}
func (e EVMInt) unpack(data []byte, offset int, v interface{}) (int, error) {
if len(data)-offset < ElementSize {
return 0, fmt.Errorf("not enough data")
}
data = data[offset:]
sign := (data[0] & 0x80) != 0
empty := 0
for empty = 0; empty < ElementSize; empty++ {
if (sign && data[empty] != 255) || (!sign && data[empty] != 0) {
break
}
}
length := ElementSize - empty
inv := make([]byte, ElementSize)
for i := 0; i < ElementSize; i++ {
if sign {
inv[i] = ^data[i]
} else {
inv[i] = data[i]
}
}
toType := reflect.ValueOf(v).Kind().String()
switch v := v.(type) {
case *string:
b := new(big.Int)
b.SetBytes(inv[empty:ElementSize])
if sign {
*v = b.Sub(big.NewInt(-1), b).String()
} else {
*v = b.String()
}
case *big.Int:
b := new(big.Int)
b.SetBytes(inv[0:ElementSize])
if sign {
*v = *b.Sub(big.NewInt(-1), b)
} else {
*v = *b
}
case *uint64:
if sign {
return 0, fmt.Errorf("cannot convert negative EVM int to %s", toType)
}
maxLen := int(unsafe.Sizeof(*v))
if length > maxLen {
return 0, fmt.Errorf("value to large for uint64")
}
*v = binary.BigEndian.Uint64(data[ElementSize-maxLen : ElementSize])
case *uint32:
if sign {
return 0, fmt.Errorf("cannot convert negative EVM int to %s", toType)
}
maxLen := int(unsafe.Sizeof(*v))
if length > maxLen {
return 0, fmt.Errorf("value to large for int32")
}
*v = binary.BigEndian.Uint32(data[ElementSize-maxLen : ElementSize])
case *uint16:
if sign {
return 0, fmt.Errorf("cannot convert negative EVM int to %s", toType)
}
maxLen := int(unsafe.Sizeof(*v))
if length > maxLen {
return 0, fmt.Errorf("value to large for uint16")
}
*v = binary.BigEndian.Uint16(data[ElementSize-maxLen : ElementSize])
case *int64:
maxLen := int(unsafe.Sizeof(*v))
if length > maxLen || (inv[ElementSize-maxLen]&0x80) != 0 {
return 0, fmt.Errorf("value to large for int64")
}
*v = int64(binary.BigEndian.Uint64(data[ElementSize-maxLen : ElementSize]))
case *int32:
maxLen := int(unsafe.Sizeof(*v))
if length > maxLen || (inv[ElementSize-maxLen]&0x80) != 0 {
return 0, fmt.Errorf("value to large for uint64")
}
*v = int32(binary.BigEndian.Uint32(data[ElementSize-maxLen : ElementSize]))
case *int16:
maxLen := int(unsafe.Sizeof(*v))
if length > maxLen || (inv[ElementSize-maxLen]&0x80) != 0 {
return 0, fmt.Errorf("value to large for uint16")
}
*v = int16(binary.BigEndian.Uint16(data[ElementSize-maxLen : ElementSize]))
default:
return 0, fmt.Errorf("unable to convert %s to %s", e.GetSignature(), toType)
}
return ElementSize, nil
}
func (e EVMInt) fixedSize() int {
return ElementSize
}
func (e EVMInt) Dynamic() bool {
return false
}
var _ EVMType = (*EVMAddress)(nil)
type EVMAddress struct {
}
func (e EVMAddress) getGoType() interface{} {
return new(crypto.Address)
}
func (e EVMAddress) GetSignature() string {
return "address"
}
func (e EVMAddress) pack(v interface{}) ([]byte, error) {
var err error
a, ok := v.(crypto.Address)
if !ok {
s, ok := v.(string)
if ok {
a, err = crypto.AddressFromHexString(s)
if err != nil {
return nil, err
}
}
} else {
b, ok := v.([]byte)
if !ok {
return nil, fmt.Errorf("cannot map to %s to EVM address", reflect.ValueOf(v).Kind().String())
}
a, err = crypto.AddressFromBytes(b)
if err != nil {
return nil, err
}
}
return pad(a[:], ElementSize, true), nil
}
func (e EVMAddress) unpack(data []byte, offset int, v interface{}) (int, error) {
addr, err := crypto.AddressFromBytes(data[offset+ElementSize-crypto.AddressLength : offset+ElementSize])
if err != nil {
return 0, err
}
switch v := v.(type) {
case *string:
*v = addr.String()
case *crypto.Address:
*v = addr
case *([]byte):
*v = data[offset+ElementSize-crypto.AddressLength : offset+ElementSize]
default:
return 0, fmt.Errorf("cannot map EVM address to %s", reflect.ValueOf(v).Kind().String())
}
return ElementSize, nil
}
func (e EVMAddress) Dynamic() bool {
return false
}
var _ EVMType = (*EVMBytes)(nil)
type EVMBytes struct {
M uint64
}
func (e EVMBytes) getGoType() interface{} {
v := make([]byte, e.M)
return &v
}
func (e EVMBytes) pack(v interface{}) ([]byte, error) {
b, ok := v.([]byte)
if !ok {
s, ok := v.(string)
if ok {
b = []byte(s)
} else {
return nil, fmt.Errorf("cannot map to %s to EVM bytes", reflect.ValueOf(v).Kind().String())
}
}
if e.M > 0 {
if uint64(len(b)) > e.M {
return nil, fmt.Errorf("[%d]byte to long for %s", len(b), e.GetSignature())
}
return pad(b, ElementSize, false), nil
} else {
length := EVMUint{M: 256}
p, err := length.pack(len(b))
if err != nil {
return nil, err
}
for i := 0; i < len(b); i += ElementSize {
a := b[i:]
if len(a) == 0 {
break
}
p = append(p, pad(a, ElementSize, false)...)
}
return p, nil
}
}
func (e EVMBytes) unpack(data []byte, offset int, v interface{}) (int, error) {
if e.M == 0 {
s := EVMString{}
return s.unpack(data, offset, v)
}
v2 := reflect.ValueOf(v).Elem()
switch v2.Type().Kind() {
case reflect.String:
start := 0
end := int(e.M)
for start < ElementSize-1 && data[offset+start] == 0 && start < end {
start++
}
for end > start && data[offset+end-1] == 0 {
end--
}
v2.SetString(string(data[offset+start : offset+end]))
case reflect.Array:
fallthrough
case reflect.Slice:
v2.SetBytes(data[offset : offset+int(e.M)])
default:
return 0, fmt.Errorf("cannot map EVM %s to %s", e.GetSignature(), reflect.ValueOf(v).Kind().String())
}
return ElementSize, nil
}
func (e EVMBytes) fixedSize() int {
return ElementSize
}
func (e EVMBytes) Dynamic() bool {
return e.M == 0
}
func (e EVMBytes) GetSignature() string {
if e.M > 0 {
return fmt.Sprintf("bytes%d", e.M)
} else {
return "bytes"
}
}
var _ EVMType = (*EVMString)(nil)
type EVMString struct {
}
func (e EVMString) GetSignature() string {
return "string"
}
func (e EVMString) getGoType() interface{} {
return new(string)
}
func (e EVMString) pack(v interface{}) ([]byte, error) {
b := EVMBytes{M: 0}
return b.pack(v)
}
func (e EVMString) unpack(data []byte, offset int, v interface{}) (int, error) {
lenType := EVMInt{M: 64}
var len int64
l, err := lenType.unpack(data, offset, &len)
if err != nil {
return 0, err
}
offset += l
switch v := v.(type) {
case *string:
*v = string(data[offset : offset+int(len)])
case *[]byte:
*v = data[offset : offset+int(len)]
default:
return 0, fmt.Errorf("cannot map EVM string to %s", reflect.ValueOf(v).Kind().String())
}
return ElementSize, nil
}
func (e EVMString) Dynamic() bool {
return true
}
var _ EVMType = (*EVMFixed)(nil)
type EVMFixed struct {
N, M uint64
signed bool
}
func (e EVMFixed) getGoType() interface{} {
// This is not right, obviously
return new(big.Float)
}
func (e EVMFixed) GetSignature() string {
if e.signed {
return fmt.Sprintf("fixed%dx%d", e.M, e.N)
} else {
return fmt.Sprintf("ufixed%dx%d", e.M, e.N)
}
}
func (e EVMFixed) pack(v interface{}) ([]byte, error) {
// The ABI spec does not describe how this should be packed; go-ethereum abi does not implement this
// need to dig in solidity to find out how this is packed
return nil, fmt.Errorf("packing of %s not implemented, patches welcome", e.GetSignature())
}
func (e EVMFixed) unpack(data []byte, offset int, v interface{}) (int, error) {
// The ABI spec does not describe how this should be packed; go-ethereum abi does not implement this
// need to dig in solidity to find out how this is packed
return 0, fmt.Errorf("unpacking of %s not implemented, patches welcome", e.GetSignature())
}
func (e EVMFixed) fixedSize() int {
return ElementSize
}
func (e EVMFixed) Dynamic() bool {
return false
}
type Argument struct {
Name string
EVM EVMType
IsArray bool
Indexed bool
Hashed bool
ArrayLength uint64
}
const FunctionIDSize = 4
type FunctionID [FunctionIDSize]byte
const EventIDSize = 32
type EventID [EventIDSize]byte
type FunctionSpec struct {
FunctionID FunctionID
Constant bool
Inputs []Argument
Outputs []Argument
}
type EventSpec struct {
EventID EventID
Inputs []Argument
Name string
Anonymous bool
}
type AbiSpec struct {
Constructor FunctionSpec
Fallback FunctionSpec
Functions map[string]FunctionSpec
Events map[string]EventSpec
EventsById map[EventID]EventSpec
}
type ArgumentJSON struct {
Name string
Type string
Components []ArgumentJSON
Indexed bool
}
type AbiSpecJSON struct {
Name string
Type string
Inputs []ArgumentJSON
Outputs []ArgumentJSON
Constant bool
Payable bool
StateMutability string
Anonymous bool
}
func readArgSpec(argsJ []ArgumentJSON) ([]Argument, error) {
args := make([]Argument, len(argsJ))
var err error
for i, a := range argsJ {
args[i].Name = a.Name
args[i].Indexed = a.Indexed
baseType := a.Type
isArray := regexp.MustCompile("(.*)\\[([0-9]+)\\]")
m := isArray.FindStringSubmatch(a.Type)
if m != nil {
args[i].IsArray = true
args[i].ArrayLength, err = strconv.ParseUint(m[2], 10, 32)
if err != nil {
return nil, err
}
baseType = m[1]
} else if strings.HasSuffix(a.Type, "[]") {
args[i].IsArray = true
baseType = strings.TrimSuffix(a.Type, "[]")
}
isM := regexp.MustCompile("(bytes|uint|int)([0-9]+)")
m = isM.FindStringSubmatch(baseType)
if m != nil {
M, err := strconv.ParseUint(m[2], 10, 32)
if err != nil {
return nil, err
}
switch m[1] {
case "bytes":
if M < 1 || M > 32 {
return nil, fmt.Errorf("bytes%d is not valid type", M)
}
args[i].EVM = EVMBytes{M}
case "uint":
if M < 8 || M > 256 || (M%8) != 0 {
return nil, fmt.Errorf("uint%d is not valid type", M)
}
args[i].EVM = EVMUint{M}
case "int":
if M < 8 || M > 256 || (M%8) != 0 {
return nil, fmt.Errorf("uint%d is not valid type", M)
}
args[i].EVM = EVMInt{M}
}
continue
}
isMxN := regexp.MustCompile("(fixed|ufixed)([0-9]+)x([0-9]+)")
m = isMxN.FindStringSubmatch(baseType)
if m != nil {
M, err := strconv.ParseUint(m[2], 10, 32)
if err != nil {
return nil, err
}
N, err := strconv.ParseUint(m[3], 10, 32)
if err != nil {
return nil, err
}
if M < 8 || M > 256 || (M%8) != 0 {
return nil, fmt.Errorf("%s is not valid type", baseType)
}
if N <= 0 || N > 80 {
return nil, fmt.Errorf("%s is not valid type", baseType)
}
if m[1] == "fixed" {
args[i].EVM = EVMFixed{N: N, M: M, signed: true}
} else if m[1] == "ufixed" {
args[i].EVM = EVMFixed{N: N, M: M, signed: false}
} else {
panic(m[1])
}
continue
}
switch baseType {
case "uint":
args[i].EVM = EVMUint{M: 256}
case "int":
args[i].EVM = EVMInt{M: 256}
case "address":
args[i].EVM = EVMAddress{}
case "bool":
args[i].EVM = EVMBool{}
case "fixed":
args[i].EVM = EVMFixed{M: 128, N: 8, signed: true}
case "ufixed":
args[i].EVM = EVMFixed{M: 128, N: 8, signed: false}
case "bytes":
args[i].EVM = EVMBytes{M: 0}
case "string":
args[i].EVM = EVMString{}
default:
// Assume it is a type of Contract
args[i].EVM = EVMAddress{}
}
}
return args, nil
}
func ReadAbiSpec(specBytes []byte) (*AbiSpec, error) {
var specJ []AbiSpecJSON
err := json.Unmarshal(specBytes, &specJ)
if err != nil {
// The abi spec file might a bin file, with the Abi under the Abi field in json
var binFile struct {
Abi []AbiSpecJSON
}
err = json.Unmarshal(specBytes, &binFile)
if err != nil {
return nil, err
}
specJ = binFile.Abi
}
abiSpec := AbiSpec{
Events: make(map[string]EventSpec),
EventsById: make(map[EventID]EventSpec),
Functions: make(map[string]FunctionSpec),
}
for _, s := range specJ {
switch s.Type {
case "constructor":
abiSpec.Constructor.Inputs, err = readArgSpec(s.Inputs)
if err != nil {
return nil, err
}
case "fallback":
abiSpec.Fallback.Inputs = make([]Argument, 0)
abiSpec.Fallback.Outputs = make([]Argument, 0)
case "event":
inputs, err := readArgSpec(s.Inputs)
if err != nil {
return nil, err
}
// Get signature before we deal with hashed types
sig := Signature(s.Name, inputs)
for i := range inputs {
if inputs[i].Indexed && inputs[i].EVM.Dynamic() {
// For Dynamic types, the hash is stored in stead
inputs[i].EVM = EVMBytes{M: 32}
inputs[i].Hashed = true
}
}
ev := EventSpec{Name: s.Name, EventID: GetEventID(sig), Inputs: inputs, Anonymous: s.Anonymous}
abiSpec.Events[ev.Name] = ev
abiSpec.EventsById[ev.EventID] = ev
case "function":
inputs, err := readArgSpec(s.Inputs)
if err != nil {
return nil, err
}
outputs, err := readArgSpec(s.Outputs)
if err != nil {
return nil, err
}
fs := FunctionSpec{Inputs: inputs, Outputs: outputs, Constant: s.Constant}
fs.SetFunctionID(s.Name)
abiSpec.Functions[s.Name] = fs
}
}
return &abiSpec, nil
}
func ReadAbiSpecFile(filename string) (*AbiSpec, error) {
specBytes, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return ReadAbiSpec(specBytes)
}
// MergeAbiSpec takes multiple AbiSpecs and merges them into once structure. Note that
// the same function name or event name can occur in different abis, so there might be
// some information loss.
func MergeAbiSpec(abiSpec []*AbiSpec) *AbiSpec {
newSpec := AbiSpec{
Events: make(map[string]EventSpec),
EventsById: make(map[EventID]EventSpec),
Functions: make(map[string]FunctionSpec),
}
for _, s := range abiSpec {
for n, f := range s.Functions {
newSpec.Functions[n] = f
}
// Different Abis can have the Event name, but with a different signature
// Loop over the signatures, as these are less likely to have collisions
for _, e := range s.EventsById {
newSpec.Events[e.Name] = e
newSpec.EventsById[e.EventID] = e
}
}
return &newSpec
}
func EVMTypeFromReflect(v reflect.Type) Argument {
arg := Argument{Name: v.Name()}
if v == reflect.TypeOf(crypto.Address{}) {
arg.EVM = EVMAddress{}
} else if v == reflect.TypeOf(big.Int{}) {
arg.EVM = EVMInt{M: 256}
} else {
if v.Kind() == reflect.Array {
arg.IsArray = true
arg.ArrayLength = uint64(v.Len())
v = v.Elem()
} else if v.Kind() == reflect.Slice {
arg.IsArray = true
v = v.Elem()
}
switch v.Kind() {
case reflect.Bool:
arg.EVM = EVMBool{}
case reflect.String:
arg.EVM = EVMString{}
case reflect.Uint64:
arg.EVM = EVMUint{M: 64}
case reflect.Int64:
arg.EVM = EVMInt{M: 64}
default:
panic(fmt.Sprintf("no mapping for type %v", v.Kind()))
}
}
return arg
}
// SpecFromStructReflect generates a FunctionSpec where the arguments and return values are
// described a struct. Both args and rets should be set to the return value of reflect.TypeOf()
// with the respective struct as an argument.
func SpecFromStructReflect(fname string, args reflect.Type, rets reflect.Type) *FunctionSpec {
s := FunctionSpec{
Inputs: make([]Argument, args.NumField()),
Outputs: make([]Argument, rets.NumField()),