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

typescript-solidity-merkle-tree

v0.3.0

Published

TypeScript Merkle tree that comes with Solidity proof verifier. Unit tested for your pleasure!

Downloads

25

Readme

typescript-solidity-merkle-tree

npm version

TypeScript Merkle tree that comes with Solidity proof verifier. Unit tested for your pleasure!

  • handles all edge cases: odd numbers of items, empty/duplicates, lists that aren't powers of two
  • simple API based on Buffer's - use whatever data type you want, just convert it
  • works well with Ethereum smart contracts, with its accompanying Solidity verifier and root generator
  • fixed array allocations (perf++)
  • secure against second preimage attacks

Developed as part of our cross-chain communications protocol, 0dex.

Usage

TypeScript

import { MerkleTree } from 'typescript-solidity-merkle-tree';
import { keccak256 } from 'ethereumjs-util';

let items = [
    Buffer.from('12', 'hex'),
    Buffer.from('15', 'hex'),
    Buffer.from('20', 'hex'),
    Buffer.from('25', 'hex')
];

let tree = new MerkleTree(items, keccak256);

let itemToProveIdx = 2;
let proof = tree.generateProof(itemToProveIdx);
tree.verifyProof(proof); // true

tree.root() 
// <Buffer 8c 9f ef 65 d6 b7 e7 73 d3 56 b6 ef cd 62 af 26 ef 51 24 22 f0 f6 16 78 b7 c8 9b 4a 37 6f f3 d3>

// Print tree for debugging (it shows the unhashed leaves on the right)
console.log(tree.toString())

/*
Layer 0 - 
         677034980f47f6cb0a55e7d8674ba838c39165afe34da2fc538f695d4950b38e       12
         33ae796f786efa387ecd29accca89b44e9194cc6689994e4fe5870f88e84e1e8       15
         123aaed9e19ab45afd57abf57eb48ebcdd3940c31f412aceb059602b877e61bf       25
         123aaed9e19ab45afd57abf57eb48ebcdd3940c31f412aceb059602b877e61bf       25
Layer 1 - 
         7e42e1f215cc6f44fa77b0d8e51285787e962eeab58733a1d6ffe703829e50d5
         8ed73d335daaa310ddda00cd4bafa7047fe759d86058c4aef727d02147d9e5cb
Layer 2 - 
         8c9fef65d6b7e773d356b6efcd62af26ef512422f0f61678b7c89b4a376ff3d3
*/

Solidity

pragma solidity ^0.5.0;

import "typescript-solidity-merkle-tree/contracts/MerkleTreeVerifier.sol";

contract YourContract {
    bytes root = 0x00;
    bytes32[] events;

    function computeState() public {
        root = MerkleTreeVerifier._computeMerkleRoot(events);
    }

    function updateState(bytes32[] proof, bool[] proofPaths, bytes32 leaf) public {
        require(MerkleTreeVerifier._verify(proof, proofPaths, root, leaf) == true, "INVALID_PROOF");
        // allow withdraw, etc.
    }
}

Note that if you are using 0x's compiler, you should be aware of naming conflicts if there are other contract files named 'MerkleTreeVerifier.sol'.

Develop

# tests
yarn test:watch
yarn test

# build
yarn build