-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.go
1175 lines (1014 loc) · 27.6 KB
/
types.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 types
import (
"encoding/json"
"fmt"
"math"
"math/big"
"strings"
"github.com/defiweb/go-rlp"
"github.com/defiweb/go-eth/hexutil"
)
// HashFunc returns the hash for the given input.
type HashFunc func(data ...[]byte) Hash
// Pad is a padding type.
type Pad uint8
const (
PadNone Pad = 0 // PadNone does not allow padding.
PadLeft Pad = 1 // PadLeft pads the input with zeros on the left.
PadRight Pad = 2 // PadRight pads the input with zeros on the right.
)
//
// Address type:
//
// AddressLength is the length of an Ethereum address in bytes.
const AddressLength = 20
// Address represents an Ethereum address encoded as a 20 byte array.
type Address [AddressLength]byte
// ZeroAddress is an address with all zeros.
var ZeroAddress = Address{}
// AddressFromHex parses an address in hex format and returns an Address type.
func AddressFromHex(h string) (a Address, err error) {
err = a.UnmarshalText([]byte(h))
return a, err
}
// AddressFromHexPtr parses an address in hex format and returns an *Address type.
// It returns nil if the address is invalid.
func AddressFromHexPtr(h string) *Address {
a, err := AddressFromHex(h)
if err != nil {
return nil
}
return &a
}
// MustAddressFromHex parses an address in hex format and returns an Address type.
// It panics if the address is invalid.
func MustAddressFromHex(h string) Address {
a, err := AddressFromHex(h)
if err != nil {
panic(err)
}
return a
}
// MustAddressFromHexPtr parses an address in hex format and returns an *Address type.
// It panics if the address is invalid.
func MustAddressFromHexPtr(h string) *Address {
a := MustAddressFromHex(h)
return &a
}
// AddressFromBytes converts a byte slice to an Address type.
func AddressFromBytes(b []byte) (Address, error) {
var a Address
if len(b) != len(a) {
return a, fmt.Errorf("invalid address length %d", len(b))
}
copy(a[:], b)
return a, nil
}
// AddressFromBytesPtr converts a byte slice to an *Address type.
// It returns nil if the address is invalid.
func AddressFromBytesPtr(b []byte) *Address {
a, err := AddressFromBytes(b)
if err != nil {
return nil
}
return &a
}
// MustAddressFromBytes converts a byte slice to an Address type.
// It panics if the address is invalid.
func MustAddressFromBytes(b []byte) Address {
a, err := AddressFromBytes(b)
if err != nil {
panic(err)
}
return a
}
// MustAddressFromBytesPtr converts a byte slice to an *Address type.
// It panics if the address is invalid.
func MustAddressFromBytesPtr(b []byte) *Address {
a := MustAddressFromBytes(b)
return &a
}
// Bytes returns the byte representation of the address.
func (t Address) Bytes() []byte {
return t[:]
}
// String returns the hex representation of the address.
func (t Address) String() string {
return hexutil.BytesToHex(t[:])
}
// Checksum returns the address with the checksum calculated according to
// EIP-55.
//
// HashFunc is the hash function used to calculate the checksum, most likely
// crypto.Keccak256.
func (t Address) Checksum(h HashFunc) string {
hex := []byte(hexutil.BytesToHex(t[:])[2:])
hash := h(hex)
for i, c := range hex {
if c >= '0' && c <= '9' {
continue
}
if hash[i/2]&(uint8(1)<<(((i+1)%2)*4+3)) != 0 {
hex[i] = c ^ 0x20
}
}
return "0x" + string(hex)
}
// IsZero returns true if the address is the zero address.
func (t Address) IsZero() bool {
return t == ZeroAddress
}
func (t Address) MarshalJSON() ([]byte, error) {
return bytesMarshalJSON(t[:]), nil
}
func (t *Address) UnmarshalJSON(input []byte) error {
return fixedBytesUnmarshalJSON(input, t[:])
}
func (t Address) MarshalText() ([]byte, error) {
return bytesMarshalText(t[:]), nil
}
func (t *Address) UnmarshalText(input []byte) error {
return fixedBytesUnmarshalText(input, t[:])
}
func (t Address) EncodeRLP() ([]byte, error) {
return rlp.Encode(rlp.NewBytes(t[:]))
}
func (t *Address) DecodeRLP(data []byte) (int, error) {
r, n, err := rlp.Decode(data)
if err != nil {
return 0, err
}
a, err := r.GetBytes()
if err != nil {
return 0, err
}
if len(a) == 0 {
*t = ZeroAddress
return n, nil
}
if len(a) != AddressLength {
return 0, fmt.Errorf("invalid address length %d", len(a))
}
copy(t[:], a)
return n, nil
}
//
// Hash type:
//
const HashLength = 32
// Hash represents the 32 byte Keccak256 hash of arbitrary data.
type Hash [HashLength]byte
// ZeroHash is a hash with all zeros.
var ZeroHash = Hash{}
// HashFromHex parses a hash in hex format and returns a Hash type.
// If hash is longer than 32 bytes, it returns an error.
func HashFromHex(h string, pad Pad) (Hash, error) {
b, err := hexutil.HexToBytes(h)
if err != nil {
return ZeroHash, err
}
return HashFromBytes(b, pad)
}
// HashFromHexPtr parses a hash in hex format and returns a *Hash type.
// If hash is longer than 32 bytes, it returns an error.
// It returns nil if the hash is invalid.
func HashFromHexPtr(h string, pad Pad) *Hash {
hash, err := HashFromHex(h, pad)
if err != nil {
return nil
}
return &hash
}
// MustHashFromHex parses a hash in hex format and returns a Hash type.
// If hash is longer than 32 bytes, it returns an error.
// It panics if the hash is invalid.
func MustHashFromHex(h string, pad Pad) Hash {
hash, err := HashFromHex(h, pad)
if err != nil {
panic(err)
}
return hash
}
// MustHashFromHexPtr parses a hash in hex format and returns a *Hash type.
// If hash is longer than 32 bytes, it returns an error.
// It panics if the hash is invalid.
func MustHashFromHexPtr(h string, pad Pad) *Hash {
hash := MustHashFromHex(h, pad)
return &hash
}
// HashFromBytes converts a byte slice to a Hash type.
// If bytes is longer than 32 bytes, it returns an error.
func HashFromBytes(b []byte, pad Pad) (Hash, error) {
var h Hash
if len(b) > HashLength {
return ZeroHash, fmt.Errorf("hash too long %d", len(b))
}
switch pad {
case PadLeft:
copy(h[HashLength-len(b):], b)
case PadRight:
copy(h[:], b)
case PadNone:
if len(b) != HashLength {
return ZeroHash, fmt.Errorf("invalid hash length %d", len(b))
}
copy(h[:], b)
}
return h, nil
}
// HashFromBytesPtr converts a byte slice to a *Hash type.
// If bytes is longer than 32 bytes, it returns an error.
// It returns nil if the hash is invalid.
func HashFromBytesPtr(b []byte, pad Pad) *Hash {
h, err := HashFromBytes(b, pad)
if err != nil {
return nil
}
return &h
}
// MustHashFromBytes converts a byte slice to a Hash type.
// If bytes is longer than 32 bytes, it returns an error.
// It panics if the hash is invalid.
func MustHashFromBytes(b []byte, pad Pad) Hash {
h, err := HashFromBytes(b, pad)
if err != nil {
panic(err)
}
return h
}
// MustHashFromBytesPtr converts a byte slice to a *Hash type.
// If bytes is longer than 32 bytes, it returns an error.
// It panics if the hash is invalid.
func MustHashFromBytesPtr(b []byte, pad Pad) *Hash {
h := MustHashFromBytes(b, pad)
return &h
}
// HashFromBigInt converts a big.Int to a Hash type.
// Negative numbers are represented as two's complement.
func HashFromBigInt(i *big.Int) (Hash, error) {
var b []byte
if i.Sign() >= 0 {
b = i.Bytes()
} else {
m := new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), uint(HashLength*8)), big.NewInt(1))
x := new(big.Int).Set(i).And(i, m)
b = x.Bytes()
if len(b) != HashLength || b[0]&0x80 == 0 {
return Hash{}, fmt.Errorf("number too large to convert to hash")
}
}
if len(b) > HashLength {
return Hash{}, fmt.Errorf("number too large to convert to hash")
}
return HashFromBytes(b, PadLeft)
}
// HashFromBigIntPtr converts a big.Int to a *Hash type.
// Negative numbers are represented as two's complement.
// It returns nil if the hash is invalid.
func HashFromBigIntPtr(i *big.Int) *Hash {
h, err := HashFromBigInt(i)
if err != nil {
return nil
}
return &h
}
// MustHashFromBigInt converts a big.Int to a Hash type.
// Negative numbers are represented as two's complement.
// It panics if the hash is invalid.
func MustHashFromBigInt(i *big.Int) Hash {
h, err := HashFromBigInt(i)
if err != nil {
panic(err)
}
return h
}
// MustHashFromBigIntPtr converts a big.Int to a *Hash type.
// Negative numbers are represented as two's complement.
// It panics if the hash is invalid.
func MustHashFromBigIntPtr(i *big.Int) *Hash {
h := MustHashFromBigInt(i)
return &h
}
// Bytes returns hash as a byte slice.
func (t Hash) Bytes() []byte {
return t[:]
}
// String returns the hex string representation of the hash.
func (t Hash) String() string {
return hexutil.BytesToHex(t[:])
}
// IsZero returns true if the hash is the zero hash.
func (t Hash) IsZero() bool {
return t == ZeroHash
}
func (t Hash) MarshalJSON() ([]byte, error) {
return bytesMarshalJSON(t[:]), nil
}
func (t *Hash) UnmarshalJSON(input []byte) error {
return fixedBytesUnmarshalJSON(input, t[:])
}
func (t Hash) MarshalText() ([]byte, error) {
return bytesMarshalText(t[:]), nil
}
func (t *Hash) UnmarshalText(input []byte) error {
return fixedBytesUnmarshalText(input, t[:])
}
func (t Hash) EncodeRLP() ([]byte, error) {
return rlp.Encode(rlp.NewBytes(t[:]))
}
func (t *Hash) DecodeRLP(data []byte) (int, error) {
r, n, err := rlp.Decode(data)
if err != nil {
return 0, err
}
b, err := r.GetBytes()
if err != nil {
return 0, err
}
if len(b) != HashLength {
return 0, fmt.Errorf("invalid hash length %d", len(t))
}
copy(t[:], b)
return n, nil
}
//
// BlockNumber type:
//
// BlockNumber is a type that can hold a block number or a tag.
type BlockNumber struct{ x big.Int }
const (
earliestBlockNumber = -1
latestBlockNumber = -2
pendingBlockNumber = -3
safeBlockNumber = -4
finalizedBlockNumber = -5
)
var (
EarliestBlockNumber = BlockNumber{x: *new(big.Int).SetInt64(earliestBlockNumber)}
LatestBlockNumber = BlockNumber{x: *new(big.Int).SetInt64(latestBlockNumber)}
PendingBlockNumber = BlockNumber{x: *new(big.Int).SetInt64(pendingBlockNumber)}
SafeBlockNumber = BlockNumber{x: *new(big.Int).SetInt64(safeBlockNumber)}
FinalizedBlockNumber = BlockNumber{x: *new(big.Int).SetInt64(finalizedBlockNumber)}
)
// BlockNumberFromHex converts a string to a BlockNumber type.
// The string can be a hex number or one of the following strings:
// "earliest", "latest", "safe", "finalized", "pending".
// If the string is not a valid block number, it returns an error.
func BlockNumberFromHex(h string) (BlockNumber, error) {
b := &BlockNumber{}
err := b.UnmarshalText([]byte(h))
return *b, err
}
// BlockNumberFromHexPtr converts a string to a *BlockNumber type.
// The string can be a hex number or one of the following strings:
// "earliest", "latest", "safe", "finalized", "pending".
// If the string is not a valid block number, it returns nil.
func BlockNumberFromHexPtr(h string) *BlockNumber {
b, err := BlockNumberFromHex(h)
if err != nil {
return nil
}
return &b
}
// MustBlockNumberFromHex converts a string to a BlockNumber type.
// The string can be a hex number or one of the following strings:
// "earliest", "latest", "safe", "finalized", "pending".
// It panics if the string is not a valid block number.
func MustBlockNumberFromHex(h string) BlockNumber {
b, err := BlockNumberFromHex(h)
if err != nil {
panic(err)
}
return b
}
// MustBlockNumberFromHexPtr converts a string to a *BlockNumber type.
// The string can be a hex number or one of the following strings:
// "earliest", "latest", "safe", "finalized", "pending".
// It panics if the string is not a valid block number.
func MustBlockNumberFromHexPtr(h string) *BlockNumber {
b := MustBlockNumberFromHex(h)
return &b
}
// BlockNumberFromUint64 converts an uint64 to a BlockNumber type.
func BlockNumberFromUint64(x uint64) BlockNumber {
return BlockNumber{x: *new(big.Int).SetUint64(x)}
}
// BlockNumberFromUint64Ptr converts an uint64 to a *BlockNumber type.
func BlockNumberFromUint64Ptr(x uint64) *BlockNumber {
b := BlockNumberFromUint64(x)
return &b
}
// BlockNumberFromBigInt converts a big.Int to a BlockNumber type.
func BlockNumberFromBigInt(x *big.Int) BlockNumber {
if x == nil {
return BlockNumber{}
}
return BlockNumber{x: *new(big.Int).Set(x)}
}
// BlockNumberFromBigIntPtr converts a big.Int to a *BlockNumber type.
func BlockNumberFromBigIntPtr(x *big.Int) *BlockNumber {
b := BlockNumberFromBigInt(x)
return &b
}
// IsEarliest returns true if the block tag is "earliest".
func (t *BlockNumber) IsEarliest() bool {
return t.Big().Int64() == earliestBlockNumber
}
// IsLatest returns true if the block tag is "latest".
func (t *BlockNumber) IsLatest() bool {
return t.Big().Int64() == latestBlockNumber
}
// IsPending returns true if the block tag is "pending".
func (t *BlockNumber) IsPending() bool {
return t.Big().Int64() == pendingBlockNumber
}
// IsSafe returns true if the block tag is "safe".
func (t *BlockNumber) IsSafe() bool {
return t.Big().Int64() == safeBlockNumber
}
// IsFinalized returns true if the block tag is "finalized".
func (t *BlockNumber) IsFinalized() bool {
return t.Big().Int64() == finalizedBlockNumber
}
// IsTag returns true if the block tag is used.
func (t *BlockNumber) IsTag() bool {
return t.Big().Sign() < 0
}
// Big returns the big.Int representation of the block number.
func (t *BlockNumber) Big() *big.Int {
return new(big.Int).Set(&t.x)
}
// String returns the string representation of the block number.
func (t *BlockNumber) String() string {
switch {
case t.IsEarliest():
return "earliest"
case t.IsLatest():
return "latest"
case t.IsPending():
return "pending"
case t.IsSafe():
return "safe"
case t.IsFinalized():
return "finalized"
default:
return "0x" + t.x.Text(16)
}
}
func (t BlockNumber) MarshalJSON() ([]byte, error) {
b, err := t.MarshalText()
if err != nil {
return nil, err
}
return naiveQuote(b), nil
}
func (t *BlockNumber) UnmarshalJSON(input []byte) error {
return t.UnmarshalText(naiveUnquote(input))
}
func (t BlockNumber) MarshalText() ([]byte, error) {
switch {
case t.IsEarliest():
return []byte("earliest"), nil
case t.IsLatest():
return []byte("latest"), nil
case t.IsPending():
return []byte("pending"), nil
case t.IsSafe():
return []byte("safe"), nil
case t.IsFinalized():
return []byte("finalized"), nil
default:
return []byte(hexutil.BigIntToHex(&t.x)), nil
}
}
func (t *BlockNumber) UnmarshalText(input []byte) error {
switch strings.ToLower(strings.TrimSpace(string(input))) {
case "earliest":
*t = BlockNumber{x: *new(big.Int).SetInt64(earliestBlockNumber)}
return nil
case "latest":
*t = BlockNumber{x: *new(big.Int).SetInt64(latestBlockNumber)}
return nil
case "pending":
*t = BlockNumber{x: *new(big.Int).SetInt64(pendingBlockNumber)}
return nil
case "safe":
*t = BlockNumber{x: *new(big.Int).SetInt64(safeBlockNumber)}
return nil
case "finalized":
*t = BlockNumber{x: *new(big.Int).SetInt64(finalizedBlockNumber)}
return nil
default:
u, err := hexutil.HexToBigInt(string(input))
if err != nil {
return err
}
if u.Cmp(big.NewInt(math.MaxInt64)) > 0 {
return fmt.Errorf("block number larger than int64")
}
*t = BlockNumber{x: *u}
return nil
}
}
//
// Signature type:
//
// Signature represents the transaction signature.
type Signature struct {
V *big.Int
R *big.Int
S *big.Int
}
// SignatureFromHex parses a hex string into a Signature.
// Hex representation of the signature is hex([R || S || V]).
func SignatureFromHex(h string) (Signature, error) {
b, err := hexutil.HexToBytes(h)
if err != nil {
return Signature{}, err
}
return SignatureFromBytes(b)
}
// SignatureFromHexPtr parses a hex string into a *Signature.
// Hex representation of the signature is hex([R || S || V]).
// It returns nil if the string is not a valid signature.
func SignatureFromHexPtr(h string) *Signature {
sig, err := SignatureFromHex(h)
if err != nil {
return nil
}
return &sig
}
// MustSignatureFromHex parses a hex string into a Signature.
// Hex representation of the signature is hex([R || S || V]).
// It panics if the string is not a valid signature.
func MustSignatureFromHex(h string) Signature {
sig, err := SignatureFromHex(h)
if err != nil {
panic(err)
}
return sig
}
// MustSignatureFromHexPtr parses a hex string into a *Signature.
// Hex representation of the signature is hex([R || S || V]).
// It panics if the string is not a valid signature.
func MustSignatureFromHexPtr(h string) *Signature {
sig, err := SignatureFromHex(h)
if err != nil {
panic(err)
}
return &sig
}
// SignatureFromBytes returns Signature from bytes.
// Byte representation of the signature is [R || S || V].
func SignatureFromBytes(b []byte) (Signature, error) {
if len(b) < 65 {
return Signature{}, fmt.Errorf("signature too short")
}
return Signature{
V: new(big.Int).SetBytes(b[64:]),
R: new(big.Int).SetBytes(b[:32]),
S: new(big.Int).SetBytes(b[32:64]),
}, nil
}
// SignatureFromBytesPtr returns *Signature from bytes.
// Byte representation of the signature is [R || S || V].
// It returns nil if the length of the bytes is not 65.
func SignatureFromBytesPtr(b []byte) *Signature {
sig, err := SignatureFromBytes(b)
if err != nil {
return nil
}
return &sig
}
// MustSignatureFromBytes returns Signature from bytes.
// Byte representation of the signature is [R || S || V].
// It panics if the length of the bytes is not 65.
func MustSignatureFromBytes(b []byte) Signature {
sig, err := SignatureFromBytes(b)
if err != nil {
panic(err)
}
return sig
}
// MustSignatureFromBytesPtr returns *Signature from bytes.
// Byte representation of the signature is [R || S || V].
// It panics if the length of the bytes is not 65.
func MustSignatureFromBytesPtr(b []byte) *Signature {
sig, err := SignatureFromBytes(b)
if err != nil {
panic(err)
}
return &sig
}
// SignatureFromVRS returns Signature from V, R, S values.
func SignatureFromVRS(v, r, s *big.Int) Signature {
return Signature{
V: v,
R: r,
S: s,
}
}
// SignatureFromVRSPtr returns *Signature from V, R, S values.
func SignatureFromVRSPtr(v, r, s *big.Int) *Signature {
sig := SignatureFromVRS(v, r, s)
return &sig
}
// Bytes returns the byte representation of the signature.
// The byte representation is [R || S || V].
func (s Signature) Bytes() []byte {
sv, sr, ss := s.V, s.R, s.S
if sv == nil {
sv = new(big.Int)
}
if sr == nil {
sr = new(big.Int)
}
if ss == nil {
ss = new(big.Int)
}
vb := sv.Bytes()
if len(vb) == 0 {
vb = []byte{0}
}
b := make([]byte, 64+len(vb))
sr.FillBytes(b[:32])
ss.FillBytes(b[32:64])
copy(b[64:], vb)
return b
}
// String returns the hex representation of the signature.
// The hex representation is hex([R || S || V]).
func (s Signature) String() string {
return hexutil.BytesToHex(s.Bytes())
}
// IsZero returns true if the signature is zero.
func (s Signature) IsZero() bool {
if s.V != nil && s.V.Sign() != 0 {
return false
}
if s.R != nil && s.R.Sign() != 0 {
return false
}
if s.S != nil && s.S.Sign() != 0 {
return false
}
return true
}
// Equal returns true if the signature is equal to the given signature.
//
// Nil values are considered as zero.
func (s Signature) Equal(c Signature) bool {
sv, sr, ss := s.V, s.R, s.S
cv, cr, cs := c.V, c.R, c.S
if sv == nil {
sv = new(big.Int)
}
if sr == nil {
sr = new(big.Int)
}
if ss == nil {
ss = new(big.Int)
}
if cv == nil {
cv = new(big.Int)
}
if cr == nil {
cr = new(big.Int)
}
if cs == nil {
cs = new(big.Int)
}
return sv.Cmp(cv) == 0 && sr.Cmp(cr) == 0 && ss.Cmp(cs) == 0
}
func (s Signature) Copy() *Signature {
cpy := &Signature{}
if s.V != nil {
cpy.V = new(big.Int).Set(s.V)
}
if s.R != nil {
cpy.R = new(big.Int).Set(s.R)
}
if s.S != nil {
cpy.S = new(big.Int).Set(s.S)
}
return cpy
}
func (s Signature) MarshalJSON() ([]byte, error) {
return bytesMarshalJSON(s.Bytes()), nil
}
func (s *Signature) UnmarshalJSON(input []byte) error {
var b []byte
if err := bytesUnmarshalJSON(input, &b); err != nil {
return err
}
sig, err := SignatureFromBytes(b)
if err != nil {
return err
}
*s = sig
return nil
}
func (s Signature) MarshalText() ([]byte, error) {
return bytesMarshalText(s.Bytes()), nil
}
func (s *Signature) UnmarshalText(input []byte) error {
var b []byte
if err := bytesUnmarshalText(input, &b); err != nil {
return err
}
sig, err := SignatureFromBytes(b)
if err != nil {
return err
}
*s = sig
return nil
}
//
// Number type:
//
// Number represents a hex-encoded number. This type is used for marshaling
// and unmarshalling JSON numbers. When possible, use big.Int or regular integers
// instead.
type Number struct{ x big.Int }
// NumberFromHex converts a hex string to a Number type.
func NumberFromHex(h string) (Number, error) {
u, err := hexutil.HexToBigInt(h)
if err != nil {
return Number{}, err
}
return Number{x: *u}, nil
}
// NumberFromHexPtr converts a hex string to a *Number type.
func NumberFromHexPtr(h string) *Number {
n, err := NumberFromHex(h)
if err != nil {
return nil
}
return &n
}
// MustNumberFromHex converts a hex string to a Number type. It panics if the
// conversion fails.
func MustNumberFromHex(h string) Number {
n, err := NumberFromHex(h)
if err != nil {
panic(err)
}
return n
}
// MustNumberFromHexPtr converts a hex string to a *Number type. It panics if the
// conversion fails.
func MustNumberFromHexPtr(h string) *Number {
n, err := NumberFromHex(h)
if err != nil {
panic(err)
}
return &n
}
// NumberFromBytes converts a byte slice to a Number type.
func NumberFromBytes(b []byte) Number {
return Number{x: *new(big.Int).SetBytes(b)}
}
// NumberFromBytesPtr converts a byte slice to a *Number type.
func NumberFromBytesPtr(b []byte) *Number {
n := NumberFromBytes(b)
return &n
}
// NumberFromUint64 converts an uint64 to a Number type.
func NumberFromUint64(x uint64) Number {
return Number{x: *new(big.Int).SetUint64(x)}
}
// NumberFromUint64Ptr converts an uint64 to a *Number type.
func NumberFromUint64Ptr(x uint64) *Number {
n := NumberFromUint64(x)
return &n
}
// NumberFromBigInt converts a big.Int to a Number type.
func NumberFromBigInt(x *big.Int) Number {
if x == nil {
return Number{}
}
return Number{x: *x}
}
// NumberFromBigIntPtr converts a big.Int to a *Number type.
func NumberFromBigIntPtr(x *big.Int) *Number {
n := NumberFromBigInt(x)
return &n
}
// Big returns the big.Int representation of the number.
func (t *Number) Big() *big.Int {
return new(big.Int).Set(&t.x)
}
// Bytes returns the byte representation of the number.
func (t *Number) Bytes() []byte {
return t.x.Bytes()
}
// String returns the hex representation of the number.
func (t *Number) String() string {
return hexutil.BigIntToHex(&t.x)
}
func (t Number) MarshalJSON() ([]byte, error) {
return numberMarshalJSON(t.Big()), nil
}
func (t *Number) UnmarshalJSON(input []byte) error {
return numberUnmarshalJSON(input, &t.x)
}
func (t Number) MarshalText() ([]byte, error) {
return numberMarshalText(t.Big()), nil
}
func (t *Number) UnmarshalText(input []byte) error {
return numberUnmarshalText(input, &t.x)
}
//
// Bytes type:
//
// Bytes represents a hex-encoded byte slice. This type is used for marshaling
// and unmarshalling JSON numbers. When possible, use byte slices instead.
type Bytes []byte
// BytesFromHex converts a hex string to a Bytes type.
func BytesFromHex(h string) (Bytes, error) {
return hexutil.HexToBytes(h)
}
// BytesFromHexPtr converts a hex string to a *Bytes type.
// If the input is not a valid hex string, it returns nil.
func BytesFromHexPtr(h string) *Bytes {
b, err := BytesFromHex(h)
if err != nil {
return nil
}
return &b
}
// MustBytesFromHex converts a hex string to a Bytes type. It panics if the
// input is not a valid hex string.
func MustBytesFromHex(h string) Bytes {
b, err := BytesFromHex(h)
if err != nil {
panic(err)
}
return b
}
// MustBytesFromHexPtr converts a hex string to a *Bytes type. It panics if the
// input is not a valid hex string.
func MustBytesFromHexPtr(h string) *Bytes {
b := MustBytesFromHex(h)
return &b
}
// BytesFromString converts a string to a Bytes type.
func BytesFromString(s string) Bytes {
return Bytes(s)
}
// BytesFromStringPtr converts a string to a *Bytes type.
func BytesFromStringPtr(s string) *Bytes {
b := BytesFromString(s)
return &b
}
// PadLeft returns a new byte slice padded with zeros to the given length.
// If the byte slice is longer than the given length, it is truncated leaving
// the rightmost bytes.
func (b Bytes) PadLeft(n int) Bytes {
cp := make([]byte, n)
if len(b) > n {
copy(cp, b[len(b)-n:])
} else {
copy(cp[n-len(b):], b)
}
return cp