项目作者: xpepermint

项目描述 :
Merkle tree proof implementation
高级语言: TypeScript
项目地址: git://github.com/xpepermint/merkle-tree-proof.git
创建时间: 2018-11-12T12:53:43Z
项目社区:https://github.com/xpepermint/merkle-tree-proof

开源协议:

下载


Build Status NPM Version

This is a merkle tree proof implementation for calculating merkle root hash from an array of values and exposing merkle leafs. It’s a lightweight open source package for the server and browser (using module bundler), written with TypeScript. The source code is available on GitHub where you can also find our issue tracker.

Getting started

First, create an array of values.

Index Type Description
0 String First name of a user.
1 String Middle name of a user.
2 String Last name of a user.
3 String Email address of a user.
4 Integer Age of a user.

This can be converted into a merkle tree where leafs represent the values and the root represents the proof.

  1. [0,0]
  2. 99999 MerkleNode
  3. [1,0] [1,1]
  4. 88888 55555 MerkleNode
  5. [2,0] [2,1] [2,2]
  6. 66666 77777 55555 MerkleNode
  7. [3,0] [3,1] [3,2] [3,3] [3,4]
  8. 11111 22222 33333 44444 55555 MerkleNode
  9. ----- ----- ----- ----- —----
  10. a b c d e MerkleValue

Usage

Create an instance of a merkle tree.

  1. import { createHash } from 'crypto';
  2. import { Merkle } from '@0xcert/merkle';
  3. const merkle = new Merkle({
  4. algo: (v) => createHash(`sha256`).update(v).digest('hex'),
  5. });

Build merkle tree nodes from a list of values.

  1. const allNodes = merkle.build([
  2. { index: 0, value: 'a' },
  3. { index: 1, value: 'b' },
  4. { index: 2, value: 'c' },
  5. { index: 3, value: 'd' },
  6. { index: 4, value: 'e' },
  7. ]);

Create a minimal list of nodes from which a tree root can be calculated.

  1. const recipeNodes = await merkle.pack(
  2. allNodes,
  3. [0, 2] // expose values `a` and `c`
  4. );

Recalculate the tree root hash object.

  1. const rootNode = await merkle.calculate([
  2. // values
  3. { index: 0, value: 'a' },
  4. ], [
  5. // nodes
  6. { level: 1, index: 0, hash: '0x...' },
  7. { level: 1, index: 1, hash: '0x...' },
  8. { level: 2, index: 0, hash: '0x...' },
  9. ], 4);

License (MIT)

  1. Copyright (c) 2017+ Kristijan Sedlak <xpepermint@gmail.com>
  2. Permission is hereby granted, free of charge, to any person obtaining a copy
  3. of this software and associated modelation files (the "Software"), to deal
  4. in the Software without restriction, including without limitation the rights
  5. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  6. copies of the Software, and to permit persons to whom the Software is
  7. furnished to do so, subject to the following conditions:
  8. The above copyright notice and this permission notice shall be included in
  9. all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  14. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  15. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  16. THE SOFTWARE.