3.0.0 • Published 1 year ago

@energyweb/merkle-tree v3.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

Green Proof Merkle Tree

This package is a wrapper around merkletreejs and can be used to create unified merkle trees.

Installation

  1. yarn add @energyweb/greenproof-merkle-tree

Usage

  1. Create leaves using exported hash function.
    • Pass string to hash as first argument and key as second argument.
  2. Use createMerkleTree function to create merkle tree from leaves.
  3. Returned function verify can be used to verify if leaf is present in the constructed tree. Tree is the constructed instance of MerkleTree 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.

Questions and Support

For questions and support please use Energy Web's Discord channel

Or reach out to us via email: 247enquiries@energyweb.org

EW-DOS

The Energy Web Decentralized Operating System is a blockchain-based, multi-layer digital infrastructure.

The purpose of EW-DOS is to develop and deploy an open and decentralized digital operating system for the energy sector in support of a low-carbon, customer-centric energy future.

We develop blockchain technology, full-stack applications and middleware packages that facilitate participation of Distributed Energy Resources on the grid and create open market places for transparent and efficient renewable energy trading.

  • To learn about more about the EW-DOS tech stack, see our documentation.

  • For an overview of the energy-sector challenges our use cases address, go here.

For a deep-dive into the motivation and methodology behind our technical solutions, we encourage you to read our White Papers:

Connect with Energy Web