-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
155 lines (115 loc) · 3.14 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
package types
import (
"fmt"
"strings"
"unicode"
"github.com/sigmachain/sigmachain/helper/hex"
"github.com/sigmachain/sigmachain/helper/keccak"
)
var ZeroAddress = Address{}
var ZeroHash = Hash{}
const (
HashLength = 32
AddressLength = 20
)
type Hash [HashLength]byte
type Address [AddressLength]byte
func min(i, j int) int {
if i < j {
return i
}
return j
}
func BytesToHash(b []byte) Hash {
var h Hash
size := len(b)
min := min(size, HashLength)
copy(h[HashLength-min:], b[len(b)-min:])
return h
}
func (h Hash) Bytes() []byte {
return h[:]
}
func (h Hash) String() string {
return hex.EncodeToHex(h[:])
}
// checksumEncode returns the checksummed address with 0x prefix, as by EIP-55
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md
func (a Address) checksumEncode() string {
addrBytes := a.Bytes() // 20 bytes
// Encode to hex without the 0x prefix
lowercaseHex := hex.EncodeToHex(addrBytes)[2:]
hashedAddress := hex.EncodeToHex(keccak.Keccak256(nil, []byte(lowercaseHex)))[2:]
result := make([]rune, len(lowercaseHex))
// Iterate over each character in the lowercase hex address
for idx, ch := range lowercaseHex {
if ch >= '0' && ch <= '9' || hashedAddress[idx] >= '0' && hashedAddress[idx] <= '7' {
// Numbers in range [0, 9] are ignored (as well as hashed values [0, 7]),
// because they can't be uppercased
result[idx] = ch
} else {
// The current character / hashed character is in the range [8, f]
result[idx] = unicode.ToUpper(ch)
}
}
return "0x" + string(result)
}
func (a Address) Ptr() *Address {
return &a
}
func (a Address) String() string {
return a.checksumEncode()
}
func (a Address) Bytes() []byte {
return a[:]
}
func StringToHash(str string) Hash {
return BytesToHash(stringToBytes(str))
}
func StringToAddress(str string) Address {
return BytesToAddress(stringToBytes(str))
}
func AddressToString(address Address) string {
return string(address[:])
}
func BytesToAddress(b []byte) Address {
var a Address
size := len(b)
min := min(size, AddressLength)
copy(a[AddressLength-min:], b[len(b)-min:])
return a
}
func stringToBytes(str string) []byte {
str = strings.TrimPrefix(str, "0x")
if len(str)%2 == 1 {
str = "0" + str
}
b, _ := hex.DecodeString(str)
return b
}
// UnmarshalText parses a hash in hex syntax.
func (h *Hash) UnmarshalText(input []byte) error {
*h = BytesToHash(stringToBytes(string(input)))
return nil
}
// UnmarshalText parses an address in hex syntax.
func (a *Address) UnmarshalText(input []byte) error {
buf := stringToBytes(string(input))
if len(buf) != AddressLength {
return fmt.Errorf("incorrect length")
}
*a = BytesToAddress(buf)
return nil
}
func (h Hash) MarshalText() ([]byte, error) {
return []byte(h.String()), nil
}
func (a Address) MarshalText() ([]byte, error) {
return []byte(a.String()), nil
}
var (
// EmptyRootHash is the root when there are no transactions
EmptyRootHash = StringToHash("0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
// EmptyUncleHash is the root when there are no uncles
EmptyUncleHash = StringToHash("0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347")
)