forked from Chia-Network/go-chia-libs
/
bytes100.go
65 lines (56 loc) · 1.49 KB
/
bytes100.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
package types
import (
"bytes"
"encoding/hex"
"fmt"
)
// Bytes100 Helper type with custom json handling for [100]byte
type Bytes100 [100]byte
// String Converts to hex string
func (b Bytes100) String() string {
return fmt.Sprintf("0x%s", hex.EncodeToString(b[:]))
}
// MarshalJSON marshals Bytes into hex for json
func (b Bytes100) MarshalJSON() ([]byte, error) {
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 *Bytes100) 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, 100)
_, err := hex.Decode(dest, data)
if err != nil {
return err
}
b100, err := BytesToBytes100(dest)
if err != nil {
return err
}
*b = b100
return err
}
// Bytes100ToBytes returns []byte from [100]byte
func Bytes100ToBytes(bytes100 Bytes100) Bytes {
return bytes100[:]
}
// BytesToBytes100 Returns Bytes100 from Bytes
// If input is shorter than 100 bytes, the end will be padded
// If the input is longer than 100 bytes, an error will be returned
func BytesToBytes100(bytes Bytes) (Bytes100, error) {
var fixedLen Bytes100
if len(bytes) > 100 {
return fixedLen, fmt.Errorf("input bytes is longer than 100 bytes")
}
copy(fixedLen[:], bytes)
return fixedLen, nil
}