utils.gno
0.96 Kb · 30 lines
1package cometbls
2
3// maxChainIDLen bounds the source length. The original Solidity routine packs
4// the identifier into a single EVM word, so it is never larger than 32 bytes.
5const maxChainIDLen = 32
6
7// chainIdToString converts a byte identifier into a string by trimming the
8// leading zero padding and returning the remaining bytes. It returns an error
9// when the source is empty, longer than maxChainIDLen, or contains only zero
10// padding, since none of those yield a valid chain-id.
11func chainIdToString(source []byte) (string, error) {
12 if len(source) == 0 {
13 return "", errorWithDetails(ErrInvalidChainID, "empty source")
14 }
15
16 if len(source) > maxChainIDLen {
17 return "", errorWithDetails(ErrInvalidChainID, "source exceeds max length")
18 }
19
20 offset := 0
21 for offset < len(source) && source[offset] == 0 {
22 offset++
23 }
24
25 if offset == len(source) {
26 return "", errorWithDetails(ErrInvalidChainID, "source is all zero padding")
27 }
28
29 return string(source[offset:]), nil
30}