UNPKG

merkle-tree-lib

Version:

Merkle Tree implementation with BIP340 tagged hash support.

63 lines (62 loc) 3.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * Advanced usage example for Merkle Tree Library * Demonstrates custom hash strategies and tree updates */ const __1 = require("../"); // Example 1: Using factory to create hash strategies console.log('=== EXAMPLE 1: CUSTOM HASH STRATEGIES ==='); // Sample data const balances = [ 'account1:100.50', 'account2:2500.75', 'account3:0.05', 'account4:7890.00', 'account5:123.45' ]; // Create strategies using the factory const leafStrategy = __1.HashStrategyFactory.createStrategy(__1.HashStrategyType.TAGGED_SHA256, { tag: 'BALANCE_LEAF' }); const branchStrategy = __1.HashStrategyFactory.createStrategy(__1.HashStrategyType.TAGGED_SHA256, { tag: 'BALANCE_BRANCH' }); // Create the tree with custom strategies const balanceTree = new __1.MerkleTree(balances, leafStrategy, branchStrategy); console.log('Tree with custom hash strategies:'); console.log(`- Root: ${balanceTree.getRootHex()}`); console.log(`- Leaf count: ${balanceTree.getLeafCount()}`); console.log(`- Leaf hash strategy: ${leafStrategy.getAlgorithmName()}`); console.log(`- Branch hash strategy: ${branchStrategy.getAlgorithmName()}`); // Example 2: Tree updates and verification console.log('\n=== EXAMPLE 2: TREE UPDATES ==='); // Generate a proof before update const accountIdx = 2; // 'account3:0.05' const originalProof = balanceTree.generateProof(accountIdx); console.log(`Original account data: ${balances[accountIdx]}`); console.log(`Original root: ${balanceTree.getRootHex()}`); // Update the leaf const updatedBalance = 'account3:150.00'; // Changed from 0.05 to 150.00 console.log(`\nUpdating to: ${updatedBalance}`); balanceTree.updateLeaf(accountIdx, updatedBalance); console.log(`New root after update: ${balanceTree.getRootHex()}`); // Generate a new proof after update const newProof = balanceTree.generateProof(accountIdx); // Verify both proofs const verifier = new __1.MerkleProofVerifier(leafStrategy, branchStrategy); const originalVerification = verifier.verify(originalProof); console.log(`\nOriginal proof verification: ${originalVerification ? 'VALID ✓' : 'INVALID ✗ (Expected - root changed)'}`); const newVerification = verifier.verify(newProof); console.log(`New proof verification: ${newVerification ? 'VALID ✓' : 'INVALID ✗'}`); // Example 3: Finding items and tree visualization console.log('\n=== EXAMPLE 3: FINDING ITEMS & VISUALIZATION ==='); // Find an account by its data const searchAccount = 'account5:123.45'; const foundIndex = balanceTree.findLeaf(searchAccount); console.log(`Looking for "${searchAccount}": ${foundIndex >= 0 ? `Found at index ${foundIndex}` : 'Not found'}`); // Tree visualization (simplified) console.log('\nTree structure visualization:'); const levels = balanceTree.exportTree(); levels.forEach((level, i) => { const indent = ' '.repeat(Math.pow(2, levels.length - i - 1) - 1); const spacing = ' '.repeat(Math.pow(2, levels.length - i) - 1); const nodes = level.map(hash => hash.substring(0, 6) + '...'); console.log(`${indent}${nodes.join(spacing)}`); });