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

@sinasabet81/merkletreejs

v1.0.6

Published

Small merkle tree library for JS

Downloads

6

Readme

merkletreejs

npm (scoped) npm bundle size (minified)

Usage

// the basics
const MerkleTree = require('@sinasabet81/merkletreejs');

const crypto = require('crypto');

const sha256 = preimage =>
  crypto
    .createHash('sha256')
    .update(preimage)
    .digest();

const preimages = ['hello', 'world'];
const leaves = preimages.map(sha256);

const merkleTree = new MerkleTree(leaves, sha256);

console.log(merkleTree.getPrettyRoot()); // '7305db9b2abccd706c256db3d97e5ff48d677cfe4d3a5904afb7da0e3950e1e2'

console.log(merkleTree.getPrettyLayers());
// [ [ '2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824',
//     '486ea46224d1bb4fb680f34f7c9ad96a8f24ec88be73ea8e5a6c65260e9cb8a7' ],
//   [ '7305db9b2abccd706c256db3d97e5ff48d677cfe4d3a5904afb7da0e3950e1e2' ] ]

console.log(merkleTree.getPrettyProof(leaves[0]));
// { siblings:
//   [ { siblingPosition: 'right',
//       siblingHash:
//        '486ea46224d1bb4fb680f34f7c9ad96a8f24ec88be73ea8e5a6c65260e9cb8a7' } ],
//  leaf:
//   '2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824',
//  root:
//   '7305db9b2abccd706c256db3d97e5ff48d677cfe4d3a5904afb7da0e3950e1e2' }

const proof = merkleTree.getProof(leaves[0]);
const result = merkleTree.verifyProof(proof);
console.log(result); // true

Notes

Naive use of this library could be vulerable to a second preimage attack; see this blog post or this wikipedia section for more info. This attack can be avoided by using a separate hash function for your leaves and inner nodes. An example copied from the test file:

const leafHashFunction = inputBuffer =>
  sha256(Buffer.concat([Buffer.alloc(1, 0), inputBuffer]));
const innerHashFunction = inputBuffer =>
  sha256(Buffer.concat([Buffer.alloc(1, 1), inputBuffer]));

const tree1Preimages = ['hello', 'world', 'foo', 'bar'].map(
  Buffer.from,
);
const tree1Leaves = tree1Preimages.map(leafHashFunction);
const tree1 = new MerkleTree(tree1Leaves, innerHashFunction);
const tree1Root = tree1.getRoot();

const tree2Preimages = [
  Buffer.concat([tree1Leaves[0], tree1Leaves[1]]),
  Buffer.concat([tree1Leaves[2], tree1Leaves[3]]),
];
const tree2Leaves = tree2Preimages.map(leafHashFunction);
const tree2 = new MerkleTree(tree2Leaves, innerHashFunction);
const tree2Root = tree2.getRoot();

expect(tree1Root.equals(tree2Root)).toBeFalsy();