arnaucube
arnaucube/go-merkletree-oldPublic Self-hosted repository
go-merkletree-old/node.go
node.goRaw
 1package merkletree
 2
 3import "bytes"
 4
 5// treeNode is the data structure of an intermediate node of the Merkle Tree
 6type treeNode struct {
 7	ChildL Hash // hash of the left child
 8	ChildR Hash // hash of the right child
 9}
10
11// Bytes returns an array of bytes with the Node data
12func (n *treeNode) Bytes() (b []byte) {
13	b = append(b, n.ChildL[:]...)
14	b = append(b, n.ChildR[:]...)
15	return b
16}
17
18// Ht returns the hash of the full node
19func (n *treeNode) Ht() Hash {
20	h := HashBytes(n.Bytes())
21	return h
22}
23
24// ParseNodeBytes returns a Node struct from an array of bytes
25func parseNodeBytes(b []byte) treeNode {
26	if bytes.Equal(b, EmptyNodeValue[:]) {
27		var node treeNode
28		node.ChildL = EmptyNodeValue
29		node.ChildR = EmptyNodeValue
30		return node
31	}
32	var node treeNode
33	copy(node.ChildL[:], b[:32])
34	copy(node.ChildR[:], b[32:])
35	return node
36}