Skip to content

Commit cb7bd65

Browse files
committed
update merklepath to use merkletreeparent
1 parent 8d865f1 commit cb7bd65

File tree

2 files changed

+9
-17
lines changed

2 files changed

+9
-17
lines changed

transaction/merklepath.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"sort"
1111

1212
"github.com/bitcoin-sv/go-sdk/chainhash"
13-
crypto "github.com/bitcoin-sv/go-sdk/primitives/hash"
1413
"github.com/bitcoin-sv/go-sdk/transaction/chaintracker"
1514
"github.com/pkg/errors"
1615
)
@@ -41,17 +40,14 @@ func (ip IndexedPath) GetOffsetLeaf(layer int, offset uint64) *PathElement {
4140
left := ip.GetOffsetLeaf(layer-1, prevOffset)
4241
right := ip.GetOffsetLeaf(layer-1, prevOffset+1)
4342
if left != nil && right != nil {
44-
var digest []byte
45-
if right.Duplicate != nil && *right.Duplicate {
46-
digest = append(left.Hash.CloneBytes(), left.Hash.CloneBytes()...)
47-
} else {
48-
digest = append(left.Hash.CloneBytes(), right.Hash.CloneBytes()...)
49-
}
50-
5143
pathElement := &PathElement{
5244
Offset: offset,
5345
}
54-
pathElement.Hash, _ = chainhash.NewHash(crypto.Sha256d(digest))
46+
if right.Duplicate != nil && *right.Duplicate {
47+
pathElement.Hash = MerkleTreeParent(left.Hash, left.Hash)
48+
} else {
49+
pathElement.Hash = MerkleTreeParent(left.Hash, right.Hash)
50+
}
5551
return pathElement
5652
}
5753
return nil
@@ -253,20 +249,16 @@ func (mp *MerklePath) ComputeRoot(txid *chainhash.Hash) (*chainhash.Hash, error)
253249
if leaf == nil {
254250
return nil, fmt.Errorf("we do not have a hash for this index at height: %v", height)
255251
}
256-
var digest []byte
257-
258252
if leaf.Duplicate != nil && *leaf.Duplicate {
259-
digest = append(workingHash.CloneBytes(), workingHash.CloneBytes()...)
253+
workingHash = MerkleTreeParent(workingHash, workingHash)
260254
} else {
261255
leafBytes := leaf.Hash
262256
if (offset % 2) != 0 {
263-
digest = append(workingHash.CloneBytes(), leafBytes.CloneBytes()...)
257+
workingHash = MerkleTreeParent(workingHash, leafBytes)
264258
} else {
265-
digest = append(leafBytes.CloneBytes(), workingHash.CloneBytes()...)
259+
workingHash = MerkleTreeParent(leafBytes, workingHash)
266260
}
267261
}
268-
269-
workingHash, _ = chainhash.NewHash(crypto.Sha256d(digest))
270262
}
271263
return workingHash, nil
272264
}

transaction/merkletreeparent.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func flipTwoArrays(a, b []byte) []byte {
4949

5050
// MerkleTreeParent returns the Merkle Tree parent of two Merkle Tree children.
5151
// The expectation is that the bytes are not reversed.
52-
func MerkleTreeParents(l *chainhash.Hash, r *chainhash.Hash) *chainhash.Hash {
52+
func MerkleTreeParent(l *chainhash.Hash, r *chainhash.Hash) *chainhash.Hash {
5353
concatenated := make([]byte, len(l)+len(r))
5454
copy(concatenated, l[:])
5555
copy(concatenated[len(l):], r[:])

0 commit comments

Comments
 (0)