encoding.gno
1.16 Kb · 46 lines
1package mpt
2
3// keybytesToHex mirrors keybytesToHex.
4// Reference:
5// https://github.com/ethereum/go-ethereum/blob/v1.10.26/trie/encoding.go#L96-L104
6func keybytesToHex(str []byte) []byte {
7 l := len(str)*2 + 1
8 nibbles := make([]byte, l)
9
10 for i, b := range str {
11 nibbles[i*2] = b / 16
12 nibbles[i*2+1] = b % 16
13 }
14
15 nibbles[l-1] = 16
16
17 return nibbles
18}
19
20// compactToHex mirrors compactToHex exactly, reusing keybytesToHex the same
21// way: base[0] is compact's flag nibble, <2 means extension (drop the
22// terminator keybytesToHex appended), and the low bit of the flag says
23// whether to chop 1 or 2 leading nibbles (odd vs even length).
24// Reference:
25// https://github.com/ethereum/go-ethereum/blob/v1.10.26/trie/encoding.go#L82-L92
26func compactToHex(compact []byte) []byte {
27 if len(compact) == 0 {
28 return compact
29 }
30
31 base := keybytesToHex(compact)
32 if base[0] < 2 {
33 base = base[:len(base)-1]
34 }
35
36 chop := 2 - base[0]&1
37
38 return base[chop:]
39}
40
41// hasTerm reports whether a hex key has the terminator flag.
42// Reference:
43// https://github.com/ethereum/go-ethereum/blob/v1.10.26/trie/encoding.go#L143-L145
44func hasTerm(s []byte) bool {
45 return len(s) > 0 && s[len(s)-1] == 16
46}