|
|
|
@ -18,6 +18,9 @@ type MerkleNode struct { |
|
|
|
|
|
|
|
|
|
// NewMerkleTree creates a new Merkle tree from a sequence of data
|
|
|
|
|
func NewMerkleTree(data [][]byte) *MerkleTree { |
|
|
|
|
if len(data) == 0 { |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
var nodes []*MerkleNode |
|
|
|
|
|
|
|
|
|
for _, datum := range data { |
|
|
|
@ -49,15 +52,16 @@ func NewMerkleTree(data [][]byte) *MerkleTree { |
|
|
|
|
func NewMerkleNode(left, right *MerkleNode, data []byte) *MerkleNode { |
|
|
|
|
mNode := MerkleNode{} |
|
|
|
|
|
|
|
|
|
if left == nil && right == nil { |
|
|
|
|
hash := sha256.Sum256(data) |
|
|
|
|
mNode.Data = hash[:] |
|
|
|
|
} else { |
|
|
|
|
prevHashes := append(left.Data, right.Data...) |
|
|
|
|
hash := sha256.Sum256(prevHashes) |
|
|
|
|
mNode.Data = hash[:] |
|
|
|
|
prevHashes := []byte{} |
|
|
|
|
if left != nil { |
|
|
|
|
prevHashes = append(prevHashes, left.Data...) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if right != nil { |
|
|
|
|
prevHashes = append(prevHashes, right.Data...) |
|
|
|
|
} |
|
|
|
|
prevHashes = append(prevHashes, data...) |
|
|
|
|
hash := sha256.Sum256(prevHashes) |
|
|
|
|
mNode.Data = hash[:] |
|
|
|
|
mNode.Left = left |
|
|
|
|
mNode.Right = right |
|
|
|
|
|
|
|
|
|