-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.go
121 lines (97 loc) · 3.08 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
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
110
111
112
113
114
115
116
117
118
119
120
121
package identifiers
import (
"bytes"
"crypto/rand"
"encoding/hex"
"errors"
"strings"
)
// Nil is a nil [32]byte value.
// Can be used to represent any nil identifier.
var Nil [32]byte
// RandomFingerprint generates a random 24-byte fingerprint
func RandomFingerprint() (fingerprint [24]byte) {
_, _ = rand.Read(fingerprint[:])
return fingerprint
}
var (
prefix0xString = "0x"
prefix0xBytes = []byte(prefix0xString)
)
var (
ErrMissingHexPrefix = errors.New("missing '0x' prefix")
ErrInvalidLength = errors.New("invalid length")
ErrUnsupportedFlag = errors.New("unsupported flag")
ErrUnsupportedVersion = errors.New("unsupported tag version")
ErrUnsupportedKind = errors.New("unsupported tag kind")
)
// trim0xPrefixString trims the 0x prefix from the given string (if it exists).
func trim0xPrefixString(value string) string {
return strings.TrimPrefix(value, prefix0xString)
}
// trim0xPrefixBytes trims the 0x prefix from the given byte slice (if it exists).
func trim0xPrefixBytes(value []byte) []byte {
return bytes.TrimPrefix(value, prefix0xBytes)
}
// has0xPrefixBytes checks if the given byte slice has a 0x prefix.
func has0xPrefixBytes(value []byte) bool {
return bytes.HasPrefix(value, prefix0xBytes)
}
// decodeHexString decodes the given hex string into a byte slice.
// It trims the 0x prefix (if found) from the string before decoding.
func decodeHexString(str string) ([]byte, error) {
// Trim the 0x prefix from the string (if it exists)
str = trim0xPrefixString(str)
decoded, err := hex.DecodeString(str)
if err != nil {
return nil, err
}
return decoded, nil
}
// trimFingerprint returns the 24 bytes in the middle of the given 32-byte array.
func trimFingerprint(bytes [32]byte) [24]byte {
return [24]byte(bytes[4:28])
}
// trimVariant returns the 4 least-significant bytes of the given 32-byte array.
func trimVariant(bytes [32]byte) [4]byte {
return [4]byte(bytes[28:])
}
// marshal32 is a generic marshal function for 32-byte identifiers.
// To be used in conjunction with MarshalText
func marshal32(data [32]byte) ([]byte, error) {
buffer := make([]byte, 32*2+2)
// Copy the 0x prefix into the buffer
copy(buffer[:2], prefix0xString)
// Hex-encode the copied value into the buffer
hex.Encode(buffer[2:], data[:])
return buffer, nil
}
// unmarshal32 is generic unmarshal function for 32-byte identifiers.
// To be used in conjunction with UnmarshalText
func unmarshal32(data []byte) ([32]byte, error) {
// Assert that the 0x prefix exists
if !has0xPrefixBytes(data) {
return Nil, ErrMissingHexPrefix
}
// Trim the 0x prefix
data = trim0xPrefixBytes(data)
// Check that the data has enough length for the identifier data
if len(data) != 32*2 {
return Nil, ErrInvalidLength
}
// Decode the hex-encoded data
decoded, err := decodeHexString(string(data))
if err != nil {
return Nil, err
}
return [32]byte(decoded), nil
}
// must is correctness enforcer for error handling.
// For use in functions that should never return an error.
// Panics if an error is encountered.
func must[T any](t T, err error) T {
if err != nil {
panic(err)
}
return t
}