/
hash.go
160 lines (135 loc) · 3.07 KB
/
hash.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
package prototyp
import (
"database/sql/driver"
"encoding/hex"
"strings"
"github.com/0xsequence/ethkit/go-ethereum/common"
)
// Hash is a type alias for common.Hash used for data normalization
// with JSON/Database marshalling.
//
// NOTE: when used with a db like postgres, the column type must be a `bytea`
type Hash string
func HashFromString(s string) Hash {
return Hash(strings.ToLower(s))
}
func HashFromBytes(src []byte) Hash {
return Hash("0x" + hex.EncodeToString(src))
}
type Hexer interface {
Hex() string
}
func ToHash(h Hexer) Hash {
return HashFromString(h.Hex())
}
func (h Hash) ToAddress() common.Address {
return common.HexToAddress(string(h))
}
func (h Hash) ToHash() common.Hash {
return common.HexToHash(string(h))
}
func (h Hash) ToShortHash() Hash {
// in case hash is shorter then we expect, return just its value
if len(h) < 10 {
return h
}
// return short-hash if already is len(0x12345678) == 10
if len(h) == 10 {
return h
}
// return short-hash as 0x plus first 8 bytes of the blockHash,
// which assumes the blockHash will always have 0x prefix.
return h[0:10]
}
// UnmarshalText implements encoding.TextMarshaler.
func (h *Hash) MarshalText() ([]byte, error) {
return []byte(h.String()), nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
func (h *Hash) UnmarshalText(src []byte) error {
*h = HashFromString(string(src))
return nil
}
func (h *Hash) ExtensionType() int8 {
return 10
}
func (h *Hash) Len() int {
return len(h.String())
}
func (h *Hash) MarshalBinaryTo(b []byte) error {
copy(b[:], h.String())
return nil
}
func (h *Hash) UnmarshalBinary(b []byte) error {
*h = HashFromString(string(b))
return nil
}
func (h Hash) String() string {
return string(h)
}
func (h Hash) IsZeroValue() bool {
if h.String() == "" {
return true
}
if h.String() == "0x" {
return true
}
if h.String() == "0x00000000" {
return true
}
if h.String() == "0x0000000000000000000000000000000000000000" {
return true
}
if h.String() == "0x0000000000000000000000000000000000000000000000000000000000000000" {
return true
}
return false
}
func (h Hash) IsValidAddress() bool {
if len(h) <= 2 || h[0:2] != "0x" {
return false
}
if len(h) != 42 {
return false
}
return true
}
func (h Hash) IsValidTxnHash() bool {
if h[0:2] != "0x" {
return false
}
if len(h) != 66 {
return false
}
return true
}
func (h Hash) Bytes() []byte {
return h.Hash().Bytes()
}
func (h *Hash) Hash() common.Hash {
return common.HexToHash(h.String())
}
func (h Hash) Value() (driver.Value, error) {
s := h.String()
if len(s) < 2 {
return []byte{}, nil
}
return hex.DecodeString(s[2:])
}
func (h *Hash) Scan(src interface{}) error {
// NOTE: the 'scany' package we use is unable to scan values of
// *string, aka *prototyp.Hash, when needing to have a nullable Hash
// please use the HashMaybe type instead.
if src == nil {
return nil
}
*h = HashFromBytes(src.([]byte))
return nil
}
func ToHashList[T Hexer](list []T) []Hash {
result := make([]Hash, 0, len(list))
for _, a := range list {
result = append(result, ToHash(a))
}
return result
}