@energyweb/greenproof-merkle-tree
v2.0.6
Published
This package is a wrapper around `merkletreejs` and can be used to create unified merkle trees.
Downloads
28
Keywords
Readme
Green Proof Merkle Tree
This package is a wrapper around merkletreejs
and can be used to create unified merkle trees.
Installation
yarn add @energyweb/greenproof-merkle-tree
Usage
- Create leaves using exported
hash
function.- Pass
string
to hash as first argument andkey
as second argument.
- Pass
- Use
createMerkleTree
function to create merkle tree from leaves. - Returned function
verify
can be used to verify if leaf is present in the constructed tree. Tree is the constructed instance ofMerkleTree
from merkletreejs library.
Full example:
import {createMerkleTree, hash} from '@energyweb/greenproof-merkle-tree';
const leavesForMerkleTree = [
hash('my data'),
hash(JSON.stringify({someData: 1, otherData: '123'})),
];
const {tree, verify} = createMerkleTree(leavesForMerkleTree);
verify(hash('my data')) // `true` -> leaf is present in merkle tree'
verify(JSON.stringify({someData: 1, otherData: '123'})) // `true` -> leaf is in merkle tree
verify(hash('not existing data')) // `false` -> leaf is not in merkle tree
verify(JSON.stringify({someData: 1})) // `false` -> leaf is not in merkle tree
NOTE:
key
is used to differentiate between leaves with the same values. For example you can have leaves:
const johnFromPoland = hash('John', 'PL');
const johnFromUSA = hash('John', 'USA');
const {verify} = createMerkleTree([johnFromPoland, johnFromUSA]);
verify(johnFromPoland) // true
verify(johnFromUSA) // true
verify(hash('John', 'DE')) // false, as there was no John with such a key in the tree
Refer to unit tests for more examples.