-
Notifications
You must be signed in to change notification settings - Fork 18
/
common.go
78 lines (58 loc) · 1.33 KB
/
common.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
package types
import (
"reflect"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
)
type Network uint
type WitnessType uint
type ScriptType string
type ScriptSearchMode string
const (
HashLength = 32
NetworkMain Network = iota
NetworkTest
WitnessTypeLock WitnessType = iota
WitnessTypeInputType
WitnessTypeOutputType
ScriptTypeLock ScriptType = "lock"
ScriptTypeType ScriptType = "type"
ScriptSearchModePrefix ScriptSearchMode = "prefix"
ScriptSearchModeExact ScriptSearchMode = "exact"
)
var (
hashT = reflect.TypeOf(Hash{})
)
type Hash [HashLength]byte
func BytesToHash(b []byte) Hash {
var h Hash
h.SetBytes(b)
return h
}
func HexToHash(s string) Hash {
return BytesToHash(common.FromHex(s))
}
func (h *Hash) SetBytes(b []byte) {
if len(b) > len(h) {
b = b[len(b)-HashLength:]
}
copy(h[HashLength-len(b):], b)
}
func (h Hash) Bytes() []byte {
return h[:]
}
func (h Hash) Hex() string {
return hexutil.Encode(h[:])
}
func (h Hash) String() string {
return h.Hex()
}
func (h *Hash) UnmarshalText(input []byte) error {
return hexutil.UnmarshalFixedText("Hash", input, h[:])
}
func (h *Hash) UnmarshalJSON(input []byte) error {
return hexutil.UnmarshalFixedJSON(hashT, input, h[:])
}
func (h Hash) MarshalText() ([]byte, error) {
return hexutil.Bytes(h[:]).MarshalText()
}