@rsksmart/pmt-builder
v3.0.0
Published
PartialMerkleTree builder library
Downloads
347
Keywords
Readme
pmt-builder
PartialMerkleTree builder library.
This library can be used to build a partial merkle tree for a block of transactions. A partial merkle tree is a pruned version of a full merkle tree, containing only the parts needed to prove a given transaction is part of the tree.
Installation
npm install @rsksmart/pmt-builder
Install dependencies
npm install
Usage
To build the tree, simply call the buildPMT
function and pass the leaves
as a list of strings and a filteredHash
in string format, i.e buildPMT(leaves: string[], filteredHash: string)
First parameter {leaves}: An array of transaction hash of all transactions without witness(txid) in a block. Second parameter {filteredHash}: transaction hash (the hash without witness).
The function returns an object with the following fields: totalTX: the size of {leaves} in the array. Hashes: All the hashes that build the partial merkle tree. This includes the leaves level as well as the upper levels of the tree. flags: The number of flags bytes to follow. Hex: The partial merkle tree serialized in hex format.
Sample Snippet
const pmtBuilder = require("@rsksmart/pmt-builder");
const blockTransactions = [
'8f01832aa125683490e70a9142ccd9c49485b84708180487b5f35dc7795a3afd',
'6cc629897f3f6c3e873e98af8061f54618fe2be6599853841ca97aeb9614a136',
'31c8227345ead8477845fda6d8dc68cc1bca9c094523aef60d857d521aca6937',
'e0714cbc5ed04014e1a1f362612fdb05578a1c8e45257ac6cc0dff0d2e532855',
'0f9e8932b5bbcad80242077205843e42bcca9d84c58b751535ea07a06e4179e6',
'5a4d2446ff81be95eacaaac0fcd06856f9625f6f07391a2bf90b1a065be0e0fc'
];
const resultPmt = pmtBuilder.buildPMT(blockTransactions, blockTransactions[0]);
console.log("Result: ", resultPmt);
Tool Usage
This library can be used via a tool(this tool uses mempool.js api to get transactions) that exists in the tool/pmt-builder.js file via the following command:
node tool/pmt-builder.js network blockHash txHash
,
For example: node tool/pmt-builder.js testnet 00000000000003d91235b675366fc6c26e0ea4b2f125fd292f164247d4e5b07e ac72bd61c72ac8143e5638998f479bdfc5834fce9576fa2054c7be93313abd66
network
: testnet or mainnet
blockHash
: block hash in hex format
txHash
: filtered transaction hash in hex format
Test
To run test:
npm test
For any comments or suggestions, feel free to contribute or reach out at our open slack.