Parsh / Merkle-Tree

Merkle tree(Bitcoin-style) implementation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Merkle Tree Implementation

A quick implementation of Bitcoin style merkle tree (carry forwarding mono-hash rather than dual hashing it)

Prerequisites:

Instructions

How to execute

git clone https://github.com/Parsh/Merkle-Tree.git
cd Merkle-Tree
node Merkle.js

Generating(and logging) the merkle tree

const merkle = new Merkle();
const merkle_root = merkle.generate_merkle_root(txids);
console.log("Merkle Root: ", merkle_root);
merkle.log_merkle_tree();

Fetching merkle path and inclusion status

const txId = "522137b80ce9a66845e05d5abc09a1dad04ec80f774a7e585c6e8db975962d06";
const { hasTransaction, merklePath, merkleRoot } = merkle.hasTransaction(txId);

Self-validation of tx-inclusion using merkle path & root

// user validates the inclusion of the tx by regenerating the root via merkle path
validateUsingMerklePath(txId, merklePath, merkleRoot); // true or false

About

Merkle tree(Bitcoin-style) implementation


Languages

Language:JavaScript 100.0%