Coder Social home page Coder Social logo

merkletree's Introduction

merlketree

Build Status

Merkle tree data structure in pure Javascript. Always builds complete binary trees.

The proofs are compatible with the chainpoint proof format.

Browser demo

Install

npm install --save merkletree

Usage

import merkletree, { verifyProof } from 'merkletree'

/* with require:
const mtree = require('merkletree')
const merkletree = mtree.default
const verifyProof = mtree.verifyProof
*/

const leaves = [
  'e1566f09e0deea437826514431be6e4bdb4fe10aa54d75aecf0b4cdc1bc4320c',
  '2f7f9092b2d6c5c17cfe2bcf33fc38a41f2e4d4485b198c2b1074bba067e7168',
  'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
  'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
]
const tree = merkletree(leaves)
console.log(tree.root())
/*
6a9a3c86d47f1fe12648c86368ecd9723ff12e3fc34f6ae219d4d9d3e0d60667
*/
const proof = tree.proof('2f7f9092b2d6c5c17cfe2bcf33fc38a41f2e4d4485b198c2b1074bba067e7168')
console.log(proof)

/*
[
  {
    left: 'e1566f09e0deea437826514431be6e4bdb4fe10aa54d75aecf0b4cdc1bc4320c',
    parent: '0fdd6b6895e15115c262f6acb9a6ae0c73248568b740454ab21591f8a533dd7f',
    right: '2f7f9092b2d6c5c17cfe2bcf33fc38a41f2e4d4485b198c2b1074bba067e7168',
  },
  {
    left: '0fdd6b6895e15115c262f6acb9a6ae0c73248568b740454ab21591f8a533dd7f',
    parent: '6a9a3c86d47f1fe12648c86368ecd9723ff12e3fc34f6ae219d4d9d3e0d60667',
    right: '3b7546ed79e3e5a7907381b093c5a182cbf364c5dd0443dfa956c8cca271cc33',
  },
])
*/
const verified = verifyProof(
  '2f7f9092b2d6c5c17cfe2bcf33fc38a41f2e4d4485b198c2b1074bba067e7168', // leaf
  '6a9a3c86d47f1fe12648c86368ecd9723ff12e3fc34f6ae219d4d9d3e0d60667', // expected merkle root
  proof
)
console.log(verified)
/*
true
*/

TOTO

  • memoize hash() calls in tree to speed up branch/root computation

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.