Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add merkletreeparent util functions #53

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions transaction/merkletreeparent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package transaction

import (
"encoding/hex"

"github.com/bitcoin-sv/go-sdk/chainhash"
crypto "github.com/bitcoin-sv/go-sdk/primitives/hash"
"github.com/bitcoin-sv/go-sdk/util"
)

// MerkleTreeParentStr returns the Merkle Tree parent of two MerkleTree children using hex strings instead of bytes.
func MerkleTreeParentStr(leftNode, rightNode string) (string, error) {
l, err := hex.DecodeString(leftNode)
if err != nil {
return "", err
}
r, err := hex.DecodeString(rightNode)
if err != nil {
return "", err
}

return hex.EncodeToString(MerkleTreeParent(l, r)), nil
}

// MerkleTreeParent returns the Merkle Tree parent of two MerkleTree children.
func MerkleTreeParent(leftNode, rightNode []byte) []byte {
concatenated := flipTwoArrays(leftNode, rightNode)

hash := crypto.Sha256d(concatenated)

util.ReverseBytesInPlace(hash)

return hash
}

// flipTwoArrays reverses two byte arrays individually and returns as one concatenated slice
// example:
// for a=[a, b, c], b=[d, e, f] the result is [c, b, a, f, e, d]
func flipTwoArrays(a, b []byte) []byte {
result := make([]byte, 0, len(a)+len(b))
for i := len(a) - 1; i >= 0; i-- {
result = append(result, a[i])
}
for i := len(b) - 1; i >= 0; i-- {
result = append(result, b[i])
}
return result
}

// MerkleTreeParentBytes returns the Merkle Tree parent of two Merkle Tree children.
// The expectation is that the bytes are not reversed.
func MerkleTreeParentBytes(l *chainhash.Hash, r *chainhash.Hash) *chainhash.Hash {
concatenated := make([]byte, len(l)+len(r))
copy(concatenated, l[:])
copy(concatenated[len(l):], r[:])
hash, err := chainhash.NewHash(crypto.Sha256d(concatenated))
if err != nil {
return &chainhash.Hash{}
}
return hash
}
32 changes: 32 additions & 0 deletions transaction/merkletreeparent_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package transaction_test

import (
"encoding/hex"
"testing"

"github.com/bitcoin-sv/go-sdk/transaction"
"github.com/stretchr/testify/require"
)

func TestGetMerkleTreeParentStr(t *testing.T) {
leftNode := "d6c79a6ef05572f0cb8e9a450c561fc40b0a8a7d48faad95e20d93ddeb08c231"
rightNode := "b1ed931b79056438b990d8981ba46fae97e5574b142445a74a44b978af284f98"

expected := "b0d537b3ee52e472507f453df3d69561720346118a5a8c4d85ca0de73bc792be"

parent, err := transaction.MerkleTreeParentStr(leftNode, rightNode)

require.NoError(t, err)
require.Equal(t, expected, parent)
}

func TestGetMerkleTreeParent(t *testing.T) {
leftNode, _ := hex.DecodeString("d6c79a6ef05572f0cb8e9a450c561fc40b0a8a7d48faad95e20d93ddeb08c231")
rightNode, _ := hex.DecodeString("b1ed931b79056438b990d8981ba46fae97e5574b142445a74a44b978af284f98")

expected, _ := hex.DecodeString("b0d537b3ee52e472507f453df3d69561720346118a5a8c4d85ca0de73bc792be")

parent := transaction.MerkleTreeParent(leftNode, rightNode)

require.Equal(t, expected, parent)
}
7 changes: 7 additions & 0 deletions util/bytemanipulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ func ReverseBytes(a []byte) []byte {
return tmp
}

// ReverseBytesInPlace reverses the bytes (little endian/big endian) in place (no extra memory allocation).
func ReverseBytesInPlace(a []byte) {
for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
a[i], a[j] = a[j], a[i]
}
}

// LittleEndianBytes returns a byte array in little endian from an unsigned integer of 32 bytes.
func LittleEndianBytes(v uint32, l uint32) []byte {
buf := make([]byte, 4)
Expand Down