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

@garbados/merkle-tree

v1.0.3-alpha

Published

An example Merkle tree implementation.

Downloads

10

Readme

js-merkle-tree

Stability NPM Version JS Standard Style Build Status Coverage Status

Generic Merkle trees in JavaScript.

Why?

Merkle trees organize an array of values into a tree whose elements can be rapidly verified as correct. Merkle trees are often used in peer-to-peer technologies so that peers can quickly verify that the data they have received is correct, guarding against data loss or manipulation. Dat, IPFS, and BitCoin all use Merkle trees for this reason.

This library is an effort to create a generic implementation of the datastructure accompanied by thorough testing and documentation.

Install

Use npm:

$ npm i @garbados/merkle-tree

Usage

Table of Contents

MerkleTree

Merkle trees, in JavaScript!

Parameters

  • digestFn Function Digest function (or the name of one) used to hash values.
  • data Array Values to be processed into a Merkle tree.

Examples

const MerkleTree = require('@garbados/merkle-tree')
const tree = new MerkleTree('sha256', [1, 2, 3, 4, 5, 6])
console.log(tree.root)
> b0f83986db9ecaa36bd08d732a99fc461f113b78e75612bade03892cd7bb8d25

proof

Retrieve the proof path for a given leaf node index.

Parameters

  • index Number Index of a leaf node to verify.

Examples

const tree = new MerkleTree('sha256', [1, 2, 3, 4, 5, 6])
const proof = tree.proof(3)
console.log(tree.leaves[3] === proof[0][0])
> true

Returns Array Array of arrays of nodes associated with the given leaf node.

root

Getter for the root node.

Examples

console.log(tree.root)
> b0f83986db9ecaa36bd08d732a99fc461f113b78e75612bade03892cd7bb8d25

Returns String Value of the root node.

depth

Getter for the tree's depth.

Examples

console.log(tree.depth)
> 4

Returns Number Depth of the tree.

levels

Getter for the levels in the tree

Examples

console.log(tree.levels)
> [ [ 1, 2, 3, 4, 5, 6 ],
>   [ '49a64717d5d4cb19952e6eac2946415cf6879adacf9908e7d872332d32c6e684',
>     '8be6d66e9099c68d8feb52ce42478d2153cac2763b784174ae6ae96cd636b596',
>     '2f9cf80b937f44b41379ae3765c65668e5e96241d19d2088e76d72d18ea324b2' ],
>   [ '2450f5c346c26103f2bf4ba7052954556e58a1d577b78e17faa7d54c29cf6741',
>     '340c611ef9c540adf73ee22e41b148f9549c5bd88dfdf1a0792a23d564380dde' ],
>   [ 'b0f83986db9ecaa36bd08d732a99fc461f113b78e75612bade03892cd7bb8d25' ] ]

Returns Array<Array> Array of arrays of each node by level

leaves

Getter for the tree's leaf nodes AKA its initial values.

Examples

console.log(tree.leaves)
> [1, 2, 3, 4, 5, 6]

Returns Array Array of leaf nodes.

digestFn

Convenience wrapper around NodeJS' built-in crypto.

Parameters

  • hashType String String value for a hash algorithm known to the platform's version of OpenSSL.
  • data String The data to hash. Can be any value; it will be converted to a string.

Examples

const digestFn = MerkleTree.digestFn('sha256', 'hello world')
> b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9

Returns String The hash of the given data using the given function.

Tests

To run the project's test suite, use npm

npm test

Contributions

All contributions are welcome: bug reports, feature requests, "why doesn't this work" questions, patches for fixes and features, etc. For all of the above, file an issue or submit a pull request.

License

Apache-2.0