🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

@chainsafe/persistent-merkle-tree

Package Overview
Dependencies
Maintainers
6
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@chainsafe/persistent-merkle-tree

Merkle tree implemented as a persistent datastructure

1.0.2
latest
Source
npm
Version published
Weekly downloads
152K
5.7%
Maintainers
6
Weekly downloads
 
Created

What is @chainsafe/persistent-merkle-tree?

@chainsafe/persistent-merkle-tree is a JavaScript library that provides efficient and persistent data structures based on Merkle trees. It is designed to be used in blockchain and other applications where data integrity and efficient updates are crucial.

What are @chainsafe/persistent-merkle-tree's main functionalities?

Creating a Merkle Tree

This feature allows you to create a Merkle Tree from an array of leaf nodes. The code sample demonstrates how to create a tree with two leaves and print the root hash.

const { Tree, LeafNode } = require('@chainsafe/persistent-merkle-tree');

const leaves = [new LeafNode(Buffer.from('a')), new LeafNode(Buffer.from('b'))];
const tree = new Tree(leaves);
console.log(tree.root);

Updating a Merkle Tree

This feature allows you to update a specific node in the Merkle Tree. The code sample demonstrates how to update the second leaf node and print the new root hash.

const { Tree, LeafNode } = require('@chainsafe/persistent-merkle-tree');

const leaves = [new LeafNode(Buffer.from('a')), new LeafNode(Buffer.from('b'))];
const tree = new Tree(leaves);
const newLeaf = new LeafNode(Buffer.from('c'));
tree.setNode(1, newLeaf);
console.log(tree.root);

Proof Generation and Verification

This feature allows you to generate and verify Merkle proofs. The code sample demonstrates how to create a proof for the first leaf node and verify its validity.

const { Tree, LeafNode, createProof, verifyProof } = require('@chainsafe/persistent-merkle-tree');

const leaves = [new LeafNode(Buffer.from('a')), new LeafNode(Buffer.from('b'))];
const tree = new Tree(leaves);
const proof = createProof(tree, 0);
const isValid = verifyProof(tree.root, proof, leaves[0]);
console.log(isValid);

Other packages similar to @chainsafe/persistent-merkle-tree

Keywords

hash

FAQs

Package last updated on 13 Mar 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts