-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
50 lines (39 loc) · 878 Bytes
/
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
package identifiers
import (
"bytes"
"encoding/hex"
"errors"
"strings"
)
var (
prefix0xString = "0x"
prefix0xBytes = []byte(prefix0xString)
)
func trim0xPrefixString(value string) string {
return strings.TrimPrefix(value, prefix0xString)
}
func trim0xPrefixBytes(value []byte) []byte {
return bytes.TrimPrefix(value, prefix0xBytes)
}
func has0xPrefixBytes(value []byte) bool {
return bytes.HasPrefix(value, prefix0xBytes)
}
var (
ErrMissing0xPrefix = errors.New("missing '0x' prefix")
ErrInvalidLength = errors.New("invalid length")
)
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
}
func must[T any](t T, err error) T {
if err != nil {
panic(err)
}
return t
}