forked from zhangchiqing/merkle-patricia-trie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaf.go
50 lines (39 loc) · 1.01 KB
/
leaf.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 main
import (
"fmt"
"github.com/ethereum/go-ethereum/crypto"
)
type LeafNode struct {
Path []Nibble
Value []byte
}
func NewLeafNodeFromNibbleBytes(nibbles []byte, value []byte) (*LeafNode, error) {
ns, err := FromNibbleBytes(nibbles)
if err != nil {
return nil, fmt.Errorf("could not leaf node from nibbles: %w", err)
}
return NewLeafNodeFromNibbles(ns, value), nil
}
func NewLeafNodeFromNibbles(nibbles []Nibble, value []byte) *LeafNode {
return &LeafNode{
Path: nibbles,
Value: value,
}
}
func NewLeafNodeFromKeyValue(key, value string) *LeafNode {
return NewLeafNodeFromBytes([]byte(key), []byte(value))
}
func NewLeafNodeFromBytes(key, value []byte) *LeafNode {
return NewLeafNodeFromNibbles(FromBytes(key), value)
}
func (l LeafNode) Hash() []byte {
return crypto.Keccak256(l.Serialize())
}
func (l LeafNode) Raw() []interface{} {
path := ToBytes(ToPrefixed(l.Path, true))
raw := []interface{}{path, l.Value}
return raw
}
func (l LeafNode) Serialize() []byte {
return Serialize(l)
}