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