Skip to content

RainBlock/merkle-patricia-tree

Repository files navigation

☔️🌲 RainBlock's In-Memory Merkle Tree

NPM Package Build Status Coverage Status node

@rainblock/merkle-patricia-tree is an in-memory merkle tree which conforms to the specifications of the modified merkle patricia tree used by Ethereum. It is a fork of the EthereumJS library, and released under the same license, however, the API has changed to be synchronous instead of callback based. The goals of @rainblock/merkle-patricia-tree are to be:

  • In-Memory Optimized. @rainblock/merkle-patricia-tree is optimized for in-memory use and does not support persistence.

  • High performance. By taking advantage of in-memory optimizations, @rainblock/merkle-patricia-tree aims to be high performance - currently, it is 2-8x more performant than EthereumJS's merkle tree on standard benchmarks.

  • Well documented. API documentation is automatically generated from the JSdoc embedded in the typescript source, and the source code aims to be commented and maintainable.

  • Ethereum compatible. The root hashes produced by @rainblock/merkle-patricia-tree should produce the same root hashes as other Ethereum merkle tree libraries given the same input data.

Install

Add @rainblock/merkle-patricia-tree to your project with:

npm install @rainblock/merkle-patricia-tree

Usage

Basic API documentation can be found here, but the following example shows basic use of puts and gets and verification:

import {MerklePatriciaTree, VerifyWitness} from '@rainblock/merkle-patricia-tree';
const tree = new MerklePatriciaTree();

tree.put(Buffer.from('a'), Buffer.from('b'));

// Get returns a witness which contains { value, proof }
const witness = tree.get(Buffer.from('a'));

// VerifyWitness will throw an error if the proof doesn't match the given root
VerifyWitness(witness, tree.root);

Benchmarks

Benchmarks can be run by executing npm run benchmark from the package directory.