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

merkle-tree

v1.0.5

Published

A Merkle Tree skeleton, written in IcedCoffeeScript

Downloads

73

Readme

node-merkle-tree

A JS Merkle Tree implementation, as used by keybase.

Install

npm install merkle-tree

And then

var Base = require('merkle-tree').Base;

Testing

make test

All tests should pass.

API

This module is just a library, and for it to do anything useful, you'll have to subclass the Base class required above. As an example, we provide a subclass of a Merkle-Tree that lives in memory, which can be accessed as follows:

var merkle_mod = require('merkle-tree');

// M = the number of children per interior node.
// N = the maximum number of leaves before a resplit.
var config = new merkle_mod.Config({ N : 4, M : 16 });
var myTree = new merkle_mod.MemTree(config);

// Keys are hashes expressed as hex strings.
var key = "961b6dd3ede3cb8ecbaacbd68de040cd78eb2ed5889130cceb4c49268ea4d506";
var value = { "foo" : 10 };

// We're just inserting one, but you can insert as many as you'd like.
myTree.upsert({'key' : key, 'value' : value}, function(err, new_root_hash) {
	// Finding by default checks the hashes on all interior nodes down the tree.
	// If you want to speed up your 'finds', then you can pass `skip_verify : true`
	// to your find.
	myTree.find({'key' : key, 'skip_verify' : false}, function(err, val2) {
		assert.equal(value, val2);
	});
});


// You can either build a tree one key/value pair at a time, as above, or
// you can build the whole thing at once.
var data = new merkle_mod.SortedMap({
  "list": [
     ["aabbcc", "dog" ],
     ["ddccee", "cat" ],
     ["00aa33", "bird" ]
  ]
});
myTree.build({"sorted_map" : data }, function (err) {
	console.log("done!");
});

To review, the Merkle Tree module provides the following classes:

  • Config -- A configuration object that controls the shape of the tree.
  • SortedMap -- A sorted map of key/value pairs that used for inputting a whole bunch of data at a time, and is also used internally.
  • Base -- A base, abstract tree implementation that needs to specialized.
  • MemTree -- A speciailization of Base; all data lives in memory and disappears when the process ends.

The Base class has the following method calls:

  • build({sortedMap}, cb) --- Build a tree from scratch using the given sorted map of data, and callback when done.
  • upsert({key,value,[txinfo]}, cb) --- Update or insert the given value at the given key. Provide optional txinfo that is passed to the storage engine.
  • find({key}, cb) --- Find the given key in the Merkle tree, starting from the root and going down.

How to Make an On-Disk Tree

The keybase server stores its Merkle tree on disk. It implements the following methods of the Base class to do so:

  • hash_fn(s) -- A function to hash an interior node into a key. Return the hex-string hash of the given string. I'd just use SHA512: require('crypto').createHash('SHA512').update(s).digest('hex').
  • store_node({key, obj, obj_s}, cb) --- Store the node value obj under the key key. For convenience, you are also passed obj_s, the stringification of the object.
  • lookup_node({key},cb) --- Read from disk the node whose key is key. Callback with the parsed (not stringified) object
  • lookup_root(cb) --- Should callback with the hash of the most recent tree root.
  • commit_root({key,txinfo}, cb) --- Store the root hash to disk, optionally with the txinfo transaction info annotation.

For an example of how to do this, see the simple MemTree class.