Verify Commitment Inclusion
Build a sorted-pair Merkle tree, derive a proof for one Block hash, and verify its inclusion against the Merkle root hash. A valid proof confirms only that the Block hash is included in the leaf set represented by that root.
Before You Start
Use the JavaScript Merkle helpers with valid lowercase 0x-prefixed 32-byte leaves. Synthetic Block hashes keep the example independent of production data and network access.
How It Works
- Build a Merkle tree from normalized Block hashes.
- Select a leaf and derive its sibling path.
- Verify its inclusion against the Merkle root.
- Change the leaf or proof and confirm verification fails.
When to Use This Workflow
Use an inclusion proof when a verifier has a trusted Merkle root and needs to check one Block hash without loading every leaf.
- Prerequisites
- Runnable Example
- Expected Output
- Negative Checks
- Contract Boundary
Use the repository root with workspace dependencies installed; no network or deployed contract is required.
The example imports Block and Merkle helpers directly from the repository source and executes entirely off-chain.
Expected result: valid is true for the selected leaf and corresponding sorted-pair proof.
Create verify-inclusion.mjs:
import {deriveBlockHash} from '@alx-protocol/sdk';
import {
buildMerkleTree,
getMerkleProof,
verifyMerkleProof,
} from '@alx-protocol/sdk/merkle';
const leaves = ['alpha', 'beta', 'gamma'].map((value) => deriveBlockHash(value, []));
const {root} = buildMerkleTree(leaves);
const inclusion = getMerkleProof(leaves, leaves[1]);
if (!inclusion) throw new Error('leaf not found');
const valid = verifyMerkleProof(root, inclusion.leaf, inclusion.proof);
console.log(JSON.stringify({root, leaf: inclusion.leaf, proof: inclusion.proof, valid}, null, 2));
if (!valid) process.exitCode = 1;
node verify-inclusion.mjs
The selected leaf verifies; changing the leaf or any proof element makes verification return false.
valid must be true. The output contains the Merkle root, selected leaf, proof sibling array, and result. For a single leaf, the returned root equals the leaf, and an empty proof verifies by direct equality.
A modified leaf or proof fails; an unavailable leaf produces no proof.
- Replace the selected leaf after proof generation: verification returns false.
- Replace any proof element: verification returns false.
- Request a leaf not present in the source leaf set:
getMerkleProofreturns no inclusion result.
Off-chain verification evaluates a supplied Merkle root without a network connection or deployed contract. To pair inclusion with onchain record evidence, verify the Merkle root against a deployed commitment contract that implements the same sorted-pair rule. No supported public ALX contract deployment is currently published.