npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@accumulators/incremental-merkle-tree

v4.2.3

Published

A TypeScript implementation of Incremental Merkle Trees with multiproofs

Downloads

27

Readme

Incremental Merkle Tree

Incremental Merkle Tree is a structure that contains a constant amount of hashes, allows updating a given hash and proving efficiently. Time complexity of both operations is O(log tree_size).

Example

import MemoryStore from "@accumulators/memory";
import { KeccakHasher } from "@accumulators/hashers";
import { IncrementalMerkleTree } from "@accumulators/incremental-merkle-tree";

const store = new MemoryStore();
const hasher = new KeccakHasher();

const tree = await IncrementalMerkleTree.initialize(4, "0x0", hasher, store);

const proof = await tree.getInclusionProof(2);
console.log(await tree.verifyProof(2, "0x0", proof)); // true

await tree.update(2, "0x0", "0x1", proof);

console.log(await tree.verifyProof(2, "0x0", proof)); // false
console.log(await tree.verifyProof(2, "0x1", proof)); // true

Functions


initialize(treeSize: number, nullValue: string, hasher: Hasher, store: Store, treeId?: string)

Creates a new Incremental Merkle Tree of a given treeSize and fills all leaves with value nullValue. Returns the promise of IncrementalMerkleTree.

All the values in store have keys prefixes with treeId. If you want to recreate an existing Merkle Tree, you need to provide the same treeId as the original one. In case you want to create a new tree, you can omit treeId and it will be generated automatically. treeId can be later accessed using the property (e.g. myTree.treeId).


getRoot()

Returns the promise of a string of hexadecimal number which is the root hash of the tree.


getInclusionProof(index: number)

Returns the promise of string[] for a given index (0-based). The array contains hashes of all siblings of nodes on the path from the leaf to the root.


verifyProof(index: number, value: string, proof: string[])

Returns the promise of boolean which is true if the value is in the tree at index (0-based) and false otherwise.

proof is an array of hashes returned by getInclusionProof.


update(index: number, oldValue: string, newValue: string, proof: string[])

Changes the value of the leaf at index (0-based) from oldValue to newValue. proof is an array of hashes returned by getInclusionProof.

If provided oldValue or proof is incorrect, the function will throw an error.


getInclusionMultiProof(indexes: number[])

Returns the promise of string[] for a given indexes (0-based). The array contains hashes of all siblings of nodes on the path from the leaves to the root.


verifyMultiProof(indexes: number[], values: string[], proof: string[])

Returns the promise of boolean which is true if and only if for every i value at position indexes[i] (0-based) is equal to values[i] and proof is a valid proof for all of them.