-
Notifications
You must be signed in to change notification settings - Fork 8
/
bytes.go
60 lines (51 loc) · 1.4 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
package types
import (
"bytes"
"encoding/hex"
"fmt"
"strings"
)
// Bytes is a wrapper around []byte that marshals down to hex and more closely matches types in chia-blockchain
type Bytes []byte
// String Converts to hex string
func (b Bytes) String() string {
return fmt.Sprintf("0x%s", hex.EncodeToString(b))
}
// BytesFromHexString parses a hex string into Bytes
func BytesFromHexString(hexstr string) (Bytes, error) {
hexstr = strings.TrimLeft(hexstr, `"`)
hexstr = strings.TrimRight(hexstr, `"`)
hexstr = strings.TrimPrefix(hexstr, `0x`)
hexStrBytes := []byte(hexstr)
dest := make(Bytes, hex.DecodedLen(len(hexStrBytes)))
_, err := hex.Decode(dest, hexStrBytes)
if err != nil {
return nil, err
}
return dest, nil
}
// MarshalJSON marshals Bytes into hex for json
func (b Bytes) MarshalJSON() ([]byte, error) {
if len(b) == 0 {
return []byte("null"), nil
}
dst := make([]byte, hex.EncodedLen(len(b)))
hex.Encode(dst, b)
final := []byte(`"0x`)
final = append(final, dst...)
final = append(final, []byte(`"`)...)
return final, nil
}
// UnmarshalJSON unmarshals hex into []byte
func (b *Bytes) UnmarshalJSON(data []byte) error {
if string(data) == "null" {
return nil
}
data = bytes.TrimLeft(data, `"`)
data = bytes.TrimRight(data, `"`)
data = bytes.TrimPrefix(data, []byte(`0x`))
dest := make(Bytes, hex.DecodedLen(len(data)))
_, err := hex.Decode(dest, data)
*b = dest
return err
}