forked from maticnetwork/heimdall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbytes.go
109 lines (86 loc) · 2.22 KB
/
bytes.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
package types
import (
"bytes"
"fmt"
jsoniter "github.com/json-iterator/go"
"gopkg.in/yaml.v3"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
)
// HexBytes the main purpose of HexBytes is to enable HEX-encoding for json/encoding.
type HexBytes []byte
// Equals returns boolean for whether two AccAddresses are Equal
func (bz HexBytes) Equals(bz2 HexBytes) bool {
if bz.Empty() && bz2.Empty() {
return true
}
return bytes.Equal(bz.Bytes(), bz2.Bytes())
}
// Empty returns boolean for whether an AccAddress is empty
func (bz HexBytes) Empty() bool {
return len(bz) == 0
}
// Marshal needed for protobuf compatibility
func (bz HexBytes) Marshal() ([]byte, error) {
return bz.Bytes(), nil
}
// Unmarshal needed for protobuf compatibility
func (bz *HexBytes) Unmarshal(data []byte) error {
*bz = data
return nil
}
// MarshalJSON this is the point of Bytes.
func (bz HexBytes) MarshalJSON() ([]byte, error) {
return jsoniter.ConfigFastest.Marshal(bz.String())
}
// MarshalYAML marshals to YAML using Bech32.
func (bz HexBytes) MarshalYAML() (interface{}, error) {
return bz.String(), nil
}
// UnmarshalJSON this is the point of Bytes.
func (bz *HexBytes) UnmarshalJSON(data []byte) error {
var s string
if err := jsoniter.ConfigFastest.Unmarshal(data, &s); err != nil {
return err
}
*bz = common.FromHex(s)
return nil
}
// UnmarshalYAML unmarshals from YAML assuming Bech32 encoding.
func (bz *HexBytes) UnmarshalYAML(data []byte) error {
var s string
if err := yaml.Unmarshal(data, &s); err != nil {
return err
}
*bz = common.FromHex(s)
return nil
}
// Bytes return bytes
func (bz HexBytes) Bytes() []byte {
return bz
}
func (bz HexBytes) String() string {
return hexutil.Encode(bz)
}
// Format format bytes
func (bz HexBytes) Format(s fmt.State, verb rune) {
switch verb {
case 's':
s.Write([]byte(bz.String()))
case 'p':
s.Write([]byte(fmt.Sprintf("%p", bz)))
default:
s.Write([]byte(fmt.Sprintf("%X", bz.Bytes())))
}
}
//
// Utils
//
// BytesToHexBytes returns HexBytes with value b.
func BytesToHexBytes(b []byte) HexBytes {
return HexBytes(b)
}
// HexToHexBytes returns hex bytes
func HexToHexBytes(b string) HexBytes {
return HexBytes(common.FromHex(b))
}