National Palace Museum

# npm

Miss any of our Open RFC calls?Watch the recordings here! »

## merkle

0.6.0 • Public • Published

# Merkle

Builds a Merkle tree using either sha512, sha256, ripemd160, whirlpool, sha1, md5 or none algorithms.

## Usage

### Working ONLY with lowercase

For historical reasons, hashes were systematically uppercased which could lead to wrong trees (see issue #8).

We've added an extra parameter to avoid this case alteration, so you can work exclusively with lowercase hashes:

var use_uppercase = false;
merkle('sha256', use_uppercase);

We plan to remove this syntax for v1.0.0 and always use lowercase hashes.

### Extract tree data

You can get tree root using:

Get tree depth:

Get tree number of levels (depth + level of leaves):

Get tree number of nodes

Get a tree level nodes:

### Install globally for merkle command

Installing it globally will introduce the merkle command (using sha1 as default):

By default, merkle returns the root of the merkle tree:

But it can be asked for some level:

Or even all levels:

You can also change of hash algorithm:

And just extract some computation statistics:

Finally, you can ask for help:

## Concepts

Here is an example of Merkle tree with 5 leaves (taken from Tree Hash EXchange format (THEX)):

ROOT=H(H+E)
/        \
/          \
H=H(F+G)          E
/       \           \
/         \           \
F=H(A+B)         G=H(C+D)     E
/     \           /     \      \
/       \         /       \      \
A         B       C         D      E

Note: H() is some hash function

Where A,B,C,D,E may be already hashed data. If not, those leaves are turned into hashed data (using either sha1, md5 or clear algorithm).

With such a tree structure, merkle considers the tree has exactly 6 nodes: [ROOT,H,E,F,G,E]. For a given level, nodes are just an array.

Adding a Z value would alter the E branch of the tree:

ROOT'=H(H+E')
/            \
/              \
H=H(F+G)              E'
/       \               \
/         \               \
F=H(A+B)          G=H(C+D)       E'=H(E+Z)
/     \           /     \         /     \
/       \         /       \       /       \
A         B       C         D     E         Z

ROOT changed to ROOT', E to E', but H did not.

This software is provided under MIT license.

## Keywords

npm i merkle

41

0.6.0

MIT

### Repository

github.com/c-geek/merkle