Skip to content

Commit

Permalink
update merklepath to use merkletreeparent
Browse files Browse the repository at this point in the history
  • Loading branch information
shruggr committed Oct 18, 2024
1 parent 8d865f1 commit cb7bd65
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
24 changes: 8 additions & 16 deletions transaction/merklepath.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"sort"

"github.com/bitcoin-sv/go-sdk/chainhash"
crypto "github.com/bitcoin-sv/go-sdk/primitives/hash"
"github.com/bitcoin-sv/go-sdk/transaction/chaintracker"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -41,17 +40,14 @@ func (ip IndexedPath) GetOffsetLeaf(layer int, offset uint64) *PathElement {
left := ip.GetOffsetLeaf(layer-1, prevOffset)
right := ip.GetOffsetLeaf(layer-1, prevOffset+1)
if left != nil && right != nil {
var digest []byte
if right.Duplicate != nil && *right.Duplicate {
digest = append(left.Hash.CloneBytes(), left.Hash.CloneBytes()...)
} else {
digest = append(left.Hash.CloneBytes(), right.Hash.CloneBytes()...)
}

pathElement := &PathElement{
Offset: offset,
}
pathElement.Hash, _ = chainhash.NewHash(crypto.Sha256d(digest))
if right.Duplicate != nil && *right.Duplicate {
pathElement.Hash = MerkleTreeParent(left.Hash, left.Hash)
} else {
pathElement.Hash = MerkleTreeParent(left.Hash, right.Hash)
}
return pathElement
}
return nil
Expand Down Expand Up @@ -253,20 +249,16 @@ func (mp *MerklePath) ComputeRoot(txid *chainhash.Hash) (*chainhash.Hash, error)
if leaf == nil {
return nil, fmt.Errorf("we do not have a hash for this index at height: %v", height)
}
var digest []byte

if leaf.Duplicate != nil && *leaf.Duplicate {
digest = append(workingHash.CloneBytes(), workingHash.CloneBytes()...)
workingHash = MerkleTreeParent(workingHash, workingHash)
} else {
leafBytes := leaf.Hash
if (offset % 2) != 0 {
digest = append(workingHash.CloneBytes(), leafBytes.CloneBytes()...)
workingHash = MerkleTreeParent(workingHash, leafBytes)
} else {
digest = append(leafBytes.CloneBytes(), workingHash.CloneBytes()...)
workingHash = MerkleTreeParent(leafBytes, workingHash)
}
}

workingHash, _ = chainhash.NewHash(crypto.Sha256d(digest))
}
return workingHash, nil
}
Expand Down
2 changes: 1 addition & 1 deletion transaction/merkletreeparent.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func flipTwoArrays(a, b []byte) []byte {

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

0 comments on commit cb7bd65

Please sign in to comment.