-
Notifications
You must be signed in to change notification settings - Fork 0
feat: custom merkle proof #60
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f25b7db
poc custom merkle proof implementaiton to mitigate leaf-node weakness
sczembor dd11421
linter
sczembor e98188d
pass correct arguemnt
sczembor b408156
Apply suggestions from code review
sczembor fed3487
remove unsued imports
sczembor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { describe, it, assert } from "vitest"; | ||
| import { Transaction } from "bitcoinjs-lib"; | ||
| import { BitcoinMerkleTree } from "./bitcoin-merkle-tree"; | ||
|
|
||
| // data from https://github.com/gonative-cc/sui-bitcoin-spv/blob/master/tests/test_helpers/proof.py | ||
| const rawTxHex = [ | ||
| "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff07044c86041b0152ffffffff014034152a01000000434104216220ab283b5e2871c332de670d163fb1b7e509fd67db77997c5568e7c25afd988f19cd5cc5aec6430866ec64b5214826b28e0f7a86458073ff933994b47a5cac00000000", | ||
| "01000000042a40ae58b06c3a61ae55dbee05cab546e80c508f71f24ef0cdc9749dac91ea5f000000004a49304602210089c685b37903c4aa62d984929afeaca554d1641f9a668398cd228fb54588f06b0221008a5cfbc5b0a38ba78c4f4341e53272b9cd0e377b2fb740106009b8d7fa693f0b01ffffffff7b999491e30af112b11105cb053bc3633a8a87f44740eb158849a76891ff228b00000000494830450221009a4aa8663ff4017063d2020519f2eade5b4e3e30be69bf9a62b4e6472d1747b2022021ee3b3090b8ce439dbf08a5df31e2dc23d68073ebda45dc573e8a4f74f5cdfc01ffffffffdea82ec2f9e88e0241faa676c13d093030b17c479770c6cc83239436a4327d49000000004a493046022100c29d9de71a34707c52578e355fa0fdc2bb69ce0a957e6b591658a02b1e039d69022100f82c8af79c166a822d305f0832fb800786d831aea419069b3aed97a6edf8f02101fffffffff3e7987da9981c2ae099f97a551783e1b21669ba0bf3aca8fe12896add91a11a0000000049483045022100e332c81781b281a3b35cf75a5a204a2be451746dad8147831255291ebac2604d02205f889a2935270d1bf1ef47db773d68c4d5c6a51bb51f082d3e1c491de63c345601ffffffff0100c817a8040000001976a91420420e56079150b50fb0617dce4c374bd61eccea88ac00000000", | ||
| "010000000265a7293b2d69ba51d554cd32ac7586f7fbeaeea06835f26e03a2feab6aec375f000000004a493046022100922361eaafe316003087d355dd3c0ef3d9f44edae661c212a28a91e020408008022100c9b9c84d53d82c0ba9208f695c79eb42a453faea4d19706a8440e1d05e6cff7501fffffffff6971f00725d17c1c531088144b45ed795a307a22d51ca377c6f7f93675bb03a000000008b483045022100d060f2b2f4122edac61a25ea06396fe9135affdabc66d350b5ae1813bc6bf3f302205d8363deef2101fc9f3d528a8b3907e9d29c40772e587dcea12838c574cb80f801410449fce4a25c972a43a6bc67456407a0d4ced782d4cf8c0a35a130d5f65f0561e9f35198349a7c0b4ec79a15fead66bd7642f17cc8c40c5df95f15ac7190c76442ffffffff0200f2052a010000001976a914c3f537bc307c7eda43d86b55695e46047b770ea388ac00cf7b05000000001976a91407bef290008c089a60321b21b1df2d7f2202f40388ac00000000", | ||
| "01000000014ab7418ecda2b2531eef0145d4644a4c82a7da1edd285d1aab1ec0595ac06b69000000008c493046022100a796490f89e0ef0326e8460edebff9161da19c36e00c7408608135f72ef0e03e0221009e01ef7bc17cddce8dfda1f1a6d3805c51f9ab2f8f2145793d8e85e0dd6e55300141043e6d26812f24a5a9485c9d40b8712215f0c3a37b0334d76b2c24fcafa587ae5258853b6f49ceeb29cd13ebb76aa79099fad84f516bbba47bd170576b121052f1ffffffff0200a24a04000000001976a9143542e17b6229a25d5b76909f9d28dd6ed9295b2088ac003fab01000000001976a9149cea2b6e3e64ad982c99ebba56a882b9e8a816fe88ac00000000", | ||
| ]; | ||
| const transactions = rawTxHex.map((hex) => Transaction.fromHex(hex)); | ||
|
|
||
| describe("BitcoinMerkleTree", () => { | ||
| const tree = new BitcoinMerkleTree(transactions); | ||
|
|
||
| it("should calculate the correct Merkle root", () => { | ||
| const expectedRootHex = "701179cb9a9e0fe709cc96261b6b943b31362b61dacba94b03f9b71a06cc2eff"; | ||
| const expectedRoot = Buffer.from(expectedRootHex, "hex"); // little-endian | ||
|
|
||
| assert.isTrue(tree.getRoot().equals(expectedRoot), "Merkle root dosent match"); | ||
sczembor marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
|
|
||
| it("should generate the correct Merkle proof", () => { | ||
| const targetTx = transactions[1]; | ||
| const proof = tree.getProof(targetTx); | ||
|
|
||
| const expectedProofHex = [ | ||
| "a2fff7e7aa4ffd33f8a05b3a9b6f3cba22826c0232c4784a2aca1c4fe47597f9", | ||
| "9013cd2f322864fe9efd45955aacb36ee21efc4f49a4e2aa393a9ba029f0e6b8", | ||
| ]; | ||
| const expectedProof = expectedProofHex.map((hex) => Buffer.from(hex, "hex")); | ||
|
|
||
| assert.strictEqual(proof.length, 2, "Proof should have 2 elements"); | ||
| assert.isTrue(proof[0].equals(expectedProof[0]), "First proof element is incorrect"); | ||
| assert.isTrue(proof[1].equals(expectedProof[1]), "Second proof element is incorrect"); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import { createHash } from "crypto"; | ||
| import { Transaction } from "bitcoinjs-lib"; | ||
|
|
||
| function sha256(data: Buffer): Buffer { | ||
| return createHash("sha256").update(data).digest(); | ||
| } | ||
|
|
||
| interface MerkleNode { | ||
| hash: Buffer; | ||
| preimage: Buffer; | ||
| } | ||
|
|
||
| export class BitcoinMerkleTree { | ||
| private tree: MerkleNode[][]; | ||
| private readonly root: Buffer; | ||
|
|
||
| constructor(transactions: Transaction[]) { | ||
| if (!transactions || transactions.length === 0) { | ||
| throw new Error("Cannot construct Merkle tree from empty transaction list."); | ||
| } | ||
| const leafNodes: MerkleNode[] = transactions.map((tx) => { | ||
| return { | ||
sczembor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| hash: tx.getHash(), | ||
| preimage: sha256(tx.toBuffer()), | ||
sczembor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
| }); | ||
|
|
||
| this.tree = [leafNodes]; | ||
| this.buildTree(); | ||
| this.root = this.tree[this.tree.length - 1][0].hash; | ||
| } | ||
|
|
||
| private buildTree(): void { | ||
| let currentLevel = this.tree[0]; | ||
| while (currentLevel.length > 1) { | ||
| const nextLevel: MerkleNode[] = []; | ||
|
|
||
| if (currentLevel.length % 2 === 1) { | ||
| currentLevel.push(currentLevel[currentLevel.length - 1]); | ||
| } | ||
|
|
||
| for (let i = 0; i < currentLevel.length; i += 2) { | ||
| const left = currentLevel[i]; | ||
| const right = currentLevel[i + 1]; | ||
|
|
||
| const combined = Buffer.concat([left.hash, right.hash]); | ||
| nextLevel.push({ | ||
| hash: sha256(sha256(combined)), | ||
| preimage: sha256(combined), | ||
| }); | ||
| } | ||
| currentLevel = nextLevel; | ||
| this.tree.push(currentLevel); | ||
| } | ||
| } | ||
|
|
||
| public getRoot(bigEndian = false): Buffer { | ||
| return bigEndian ? Buffer.from(this.root).reverse() : this.root; | ||
| } | ||
|
|
||
| public getProof(targetTx: Transaction): Buffer[] { | ||
| const proof: Buffer[] = []; | ||
| const targetHash = targetTx.getHash(); | ||
|
|
||
| let targetIndex = this.tree[0].findIndex((node) => node.hash.equals(targetHash)); | ||
| if (targetIndex === -1) { | ||
| throw new Error("Target leaf not found in the tree."); | ||
| } | ||
|
|
||
| for (let level = 0; level < this.tree.length - 1; level++) { | ||
| const currentLevelNodes = this.tree[level]; | ||
| let siblingIndex: number; | ||
|
|
||
| const isRightNode = targetIndex % 2 === 1; | ||
| const isLastNodeOnLevel = targetIndex === currentLevelNodes.length - 1; | ||
| const levelHasOddNodes = currentLevelNodes.length % 2 === 1; | ||
|
|
||
| if (isLastNodeOnLevel && levelHasOddNodes) { | ||
| siblingIndex = targetIndex; | ||
| } else if (isRightNode) { | ||
| siblingIndex = targetIndex - 1; | ||
| } else { | ||
| siblingIndex = targetIndex + 1; | ||
| } | ||
|
|
||
| const siblingNode = currentLevelNodes[siblingIndex]; | ||
| proof.push(siblingNode.preimage); | ||
|
|
||
| targetIndex = Math.floor(targetIndex / 2); | ||
| } | ||
| return proof; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.