Skip to content

feat: custom merkle proof #60

Merged
sczembor merged 5 commits intomasterfrom
stan/custom-merkle-proof
Aug 18, 2025
Merged

feat: custom merkle proof #60
sczembor merged 5 commits intomasterfrom
stan/custom-merkle-proof

Conversation

@sczembor
Copy link
Contributor

@sczembor sczembor commented Aug 14, 2025

Description

Closes: #46


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • added appropriate labels to the PR
  • provided a link to the relevant issue or specification
  • added a changelog entry to CHANGELOG.md
  • included doc comments for public functions
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary

Summary by Sourcery

Replace merkletreejs with a custom BitcoinMerkleTree implementation and update the indexer and SuiClient to use it for Merkle proof generation and validation

New Features:

  • Introduce BitcoinMerkleTree for constructing Bitcoin-style Merkle trees and generating proofs

Enhancements:

  • Refactor Indexer to use BitcoinMerkleTree, include merkle root in proof data, and strengthen proof validation logic
  • Update SuiClient proof encoding to use direct little-endian buffer arrays

Tests:

  • Add unit tests for BitcoinMerkleTree root calculation and proof generation
  • Revise indexer tests to assert proof path structure and root matching

Signed-off-by: sczembor <stanislaw.czembor@gmail.com>
Signed-off-by: sczembor <stanislaw.czembor@gmail.com>
@sczembor sczembor requested a review from a team as a code owner August 14, 2025 13:14
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Aug 14, 2025

Reviewer's Guide

This PR replaces the external merkletreejs dependency with a custom BitcoinMerkleTree implementation for constructing and verifying Bitcoin Merkle proofs, refactors the Indexer’s proof generation and validation logic to use the new tree, and updates the test suite accordingly.

Sequence diagram for Merkle proof generation with custom BitcoinMerkleTree

sequenceDiagram
    participant Indexer
    participant BitcoinMerkleTree
    participant Block
    participant Transaction
    Indexer->>Block: Get transactions
    Indexer->>BitcoinMerkleTree: constructMerkleTree(block)
    BitcoinMerkleTree->>Transaction: Get hashes
    BitcoinMerkleTree-->>Indexer: Return tree
    Indexer->>BitcoinMerkleTree: getTxProof(tree, targetTx)
    BitcoinMerkleTree-->>Indexer: Return proofPath
    Indexer->>Block: Compare calculated root with block.merkleRoot
Loading

Class diagram for the new BitcoinMerkleTree and Indexer changes

classDiagram
    class BitcoinMerkleTree {
        -tree: MerkleNode[][]
        -root: Buffer
        +constructor(transactions: Transaction[])
        +getRoot(bigEndian: boolean): Buffer
        +getProof(targetTx: Transaction): Buffer[]
    }
    class MerkleNode {
        +hash: Buffer
        +preimage: Buffer
    }
    class Indexer {
        +constructMerkleTree(block: Block): BitcoinMerkleTree | null
        +getTxProof(tree: BitcoinMerkleTree, targetTx: Transaction): Buffer[] | null
    }
    BitcoinMerkleTree o-- MerkleNode
    Indexer --> BitcoinMerkleTree
Loading

Class diagram for ProofResult structure update

classDiagram
    class ProofResult {
        +proofPath: Buffer[]
        +merkleRoot: string
    }
Loading

File-Level Changes

Change Details Files
Add custom BitcoinMerkleTree class
  • Implemented tree building and proof generation from raw bitcoin-js transactions
  • Double-SHA256 hashing logic handling odd sibling duplication
  • API for getRoot (big/little endian) and getProof for a target transaction
btcindexer/src/bitcoin-merkle-tree.ts
Refactor Indexer to use BitcoinMerkleTree
  • constructMerkleTree returns BitcoinMerkleTree instead of merkletreejs
  • getTxProof now returns Buffer[] and handles errors
  • Soundness check compares block.merkleRoot with calculated root and logs detailed mismatch
  • Storage payload adjusted to include proofPath and hex merkleRoot
btcindexer/src/btcindexer.ts
Update tests for the new Merkle proof flow
  • Replaced merkletreejs imports and SHA256 usage with custom tree and sha256 helper
  • Refactored checkTxProof to verify proofPath array and merkleRoot only
  • Removed extraneous console.log and test signature mismatches
  • Added new unit tests in bitcoin-merkle-tree.test.ts for root and proof verification
btcindexer/src/btcindexer.test.ts
btcindexer/src/bitcoin-merkle-tree.test.ts

Possibly linked issues

  • #0: The PR introduces a custom Bitcoin Merkle tree and modifies the proof generation, directly matching the issue's need for updated proof construction logic due to new SPV logic.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Signed-off-by: sczembor <stanislaw.czembor@gmail.com>
@robert-zaremba robert-zaremba requested a review from Copilot August 14, 2025 22:32
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR replaces the merkletreejs library with a custom BitcoinMerkleTree implementation to generate Bitcoin-style Merkle proofs and handle proof validation.

Key Changes:

  • Implements custom BitcoinMerkleTree class for constructing Merkle trees and generating proofs
  • Updates proof data structure to include merkle root alongside proof path
  • Simplifies SuiClient proof encoding by removing unnecessary byte reversal operations

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
packages/btcindexer/src/bitcoin-merkle-tree.ts New custom implementation of Bitcoin Merkle tree with proof generation
packages/btcindexer/src/bitcoin-merkle-tree.test.ts Unit tests for the new BitcoinMerkleTree implementation
packages/btcindexer/src/btcindexer.ts Refactored to use BitcoinMerkleTree and updated proof validation logic
packages/btcindexer/src/btcindexer.test.ts Updated test assertions to work with new proof structure
packages/btcindexer/src/sui_client.ts Simplified proof encoding by removing buffer reversal
packages/btcindexer/src/sui_client.test.ts Updated test to use new proof structure with merkle root
Comments suppressed due to low confidence (1)

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@robert-zaremba
Copy link
Contributor

There are a few linter issues - see github comments in the PR (in files, not above)

Copy link
Contributor

@vuvoth vuvoth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

sczembor and others added 2 commits August 18, 2025 13:54
Signed-off-by: sczembor <43810037+sczembor@users.noreply.github.com>
Signed-off-by: sczembor <stanislaw.czembor@gmail.com>
@sczembor sczembor merged commit b08435e into master Aug 18, 2025
12 checks passed
@sczembor sczembor deleted the stan/custom-merkle-proof branch August 18, 2025 12:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

nbtc indexing: Update proof construction logic

4 participants