npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

merkle-trees

v0.2.3

Published

js and solidity suite for compact multi-proof appendable Merkle trees.

Downloads

12

Readme

merkle-trees

merkle-trees/js for the compact-multi-proof appendable Merkle tree class.

merkle-trees/eth for compatible smart contracts and library.

A glimpse

Quick example of gas savings of using appendable merkle tree library and smart contracts, over storing elements normally.

const { MerkleTree } = require('merkle-trees/js');

const elementCount = 200;

// The 200 elements the contracts will start with (array of 32-byte Buffers)
const elements = generateElements(elementCount, { seed: 'ff' });

// Convert to 0x-prefixed hex strings
const hexElements = elements.map(e => '0x' + e.toString('hex'));

// The 20 indices to be updated (and therefore proved)
const indices = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199];

// The elements to update with (array of 32-byte Buffers)
const updateElements = generateElements(indices.length, { seed: '11' });

// Convert to 0x-prefixed hex strings
const hexUpdateElements = updateElements.map(e => '0x' + e.toString('hex'));

// The 20 elements to be appended (array of 32-byte Buffers)
const appendElements = generateElements(20, { seed: '22' });

// Convert to 0x-prefixed hex strings
const hexAppendElements = appendElements.map(e => '0x' + e.toString('hex'));

// Launch a simple storage contract
const simpleStorageInstance = await Simple_Storage.new();

// Append the first 200 elements
const result1 = await simpleStorageInstance.append_many(hexElements);
console.log(result1.receipt.gasUsed);   // 4,200,282 gas used

// Use the elements at the 20 above indices, update them, and append 20 more
const result2 = await simpleStorageInstance.update_many_and_append_many(
  indices,
  hexElements,
  hexUpdateElements,
  hexAppendElements
);

console.log(result2.receipt.gasUsed);   // 582,667 gas used

// Launch a contract (using this merkle library) with empty storage
const merkleStorageInstance = await Merkle_Storage_Using_Internal_Lib_Memory_Bytes32_Standard.new();

// Create empty Merkle Tree object
const merkleOptions = {
  compact: true,
  elementPrefix: '00',
};

let merkleTree = new MerkleTree([], merkleOptions);

// Append the first 200 elements to the Merkle Tree
const appendMultiResult = merkleTree.appendMulti(elements, merkleOptions);

// Returned object contains new merkle tree and append proof
const { newMerkleTree: newMerkleTree1, proof: appendProof } = appendMultiResult;

// Proof returns needed proof arguments (array of 32-byte Buffers)
const { compactProof: compactAppendProof } = appendProof;

// Convert to 0x-prefixed hex strings
const hexAppendProof = compactAppendProof.map(p => '0x' + p.toString('hex'));

// Append the first 200 elements to the merkle storage
const result3 = await merkleStorageInstance.append_many(hexElements, hexAppendProof);
console.log(result3.receipt.gasUsed);   // 259,071 gas used (6.2% the gas cost!!)

// Since the contract storage is updated, overwrite the merkle tree with the new one
merkleTree = newMerkleTree1;

// Compare contract and local merkle roots
const retrievedRoot1 = await merkleStorageInstance.root();
console.log('0x' + merkleTree.root.toString('hex') === retrievedRoot1);   // true

// Use the elements at the 20 above indices, update them, and append 20 more elements
const updateAndAppendProof = merkleTree.updateAndAppend(
  indices,
  updateElements,
  appendElements,
  merkleOptions
);

// Returned object contains new merkle tree and combined proof
const { newMerkleTree: newMerkleTree2, proof: combinedProof } = updateAndAppendProof;

// Proof returns needed proof arguments (array of 32-byte Buffers)
const { compactProof: compactCombinedProof } = combinedProof;

// Convert to 0x-prefixed hex strings
const hexCombinedProof = compactCombinedProof.map(p => '0x' + p.toString('hex'));

// Use the elements at the 20 above indices, update them, and append 20 more
const result4 = await contractInstance.update_many_and_append_many(
  hexElements,
  hexUpdateElements,
  hexAppendElements,
  hexCombinedProof
);

console.log(result4.receipt.gasUsed);   // 131,877 gas used (22.6% the gas cost!!)

// Since the contract storage is updated, overwrite the merkle tree with the new one
merkleTree = newMerkleTree2;

// Compare merkle roots
const retrievedRoot2 = await merkleStorageInstance.root();
console.log('0x' + merkleTree.root.toString('hex') === retrievedRoot2);   // true

For more, see js directory, eth directory, and tests.