/
uint256.go
81 lines (70 loc) · 1.97 KB
/
uint256.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
package helper
import (
"encoding/hex"
"encoding/json"
"fmt"
"strings"
)
const uint256Size = 32
// UInt256 is a 32 byte long unsigned integer.
type UInt256 [uint256Size]uint8
// UInt256FromString attempts to decode the given string (in BE representation) into an UInt256.
func UInt256FromString(s string) (u UInt256, err error) {
s = strings.TrimPrefix(s, "0x")
if len(s) != uint256Size*2 {
return u, fmt.Errorf("expected string size of %d got %d", uint256Size*2, len(s))
}
b, err := hex.DecodeString(s)
if err != nil {
return u, err
}
return UInt256FromBytes(ReverseBytes(b))
}
// UInt256FromBytes attempts to decode the given bytes (in LE representation) into an UInt256.
func UInt256FromBytes(b []byte) (u UInt256, err error) {
if len(b) != uint256Size {
return u, fmt.Errorf("expected []byte of size %d got %d", uint256Size, len(b))
}
copy(u[:], b)
return u, nil
}
// Bytes returns a byte slice representation of u.
func (u UInt256) Bytes() []byte {
return u[:]
}
// Equals returns true if both UInt256 values are the same.
func (u UInt256) Equals(other UInt256) bool {
return u == other
}
// String implements the stringer interface.
func (u UInt256) String() string {
return hex.EncodeToString(ReverseBytes(u.Bytes()))
}
// UnmarshalJSON implements the json unmarshaller interface.
func (u *UInt256) UnmarshalJSON(data []byte) (err error) {
var js string
if err = json.Unmarshal(data, &js); err != nil {
return err
}
js = strings.TrimPrefix(js, "0x")
*u, err = UInt256FromString(js)
return err
}
// MarshalJSON implements the json marshaller interface.
func (u UInt256) MarshalJSON() ([]byte, error) {
return []byte(`"0x` + u.String() + `"`), nil
}
// CompareTo compares two UInt256 with each other. Possible output: 1, -1, 0
// 1 implies u > other.
// -1 implies u < other.
// 0 implies u = other.
func (u UInt256) CompareTo(other UInt256) int {
for k := len(u.Bytes()) - 1; k >= 0; k-- {
if u[k] < other[k] {
return -1
} else {
return 1
}
}
return 0
}