-
Notifications
You must be signed in to change notification settings - Fork 8
/
bytes48.go
65 lines (56 loc) · 1.46 KB
/
bytes48.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"
)
// Bytes48 Helper type with custom json handling for [48]byte
type Bytes48 [48]byte
// String Converts to hex string
func (b Bytes48) String() string {
return fmt.Sprintf("0x%s", hex.EncodeToString(b[:]))
}
// MarshalJSON marshals Bytes into hex for json
func (b Bytes48) 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 *Bytes48) 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, 48)
_, err := hex.Decode(dest, data)
if err != nil {
return err
}
b48, err := BytesToBytes48(dest)
if err != nil {
return err
}
*b = b48
return err
}
// Bytes48ToBytes returns []byte from [48]byte
func Bytes48ToBytes(bytes48 Bytes48) Bytes {
return bytes48[:]
}
// BytesToBytes48 Returns Bytes48 from Bytes
// If input is shorter than 48 bytes, the end will be padded
// If the input is longer than 48 bytes, an error will be returned
func BytesToBytes48(bytes Bytes) (Bytes48, error) {
var fixedLen Bytes48
if len(bytes) > 48 {
return fixedLen, fmt.Errorf("input bytes is longer than 48 bytes")
}
copy(fixedLen[:], bytes)
return fixedLen, nil
}