Code Monkey home page Code Monkey logo

node-merkle-tree's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.