GithubHelp home page GithubHelp logo

dungga7541 / merkletreejs-solidity Goto Github PK

View Code? Open in Web Editor NEW

This project forked from miguelmota/merkletreejs-solidity

0.0 0.0 0.0 36 KB

Construct merkle trees with MerkleTree.js and verify merkle proofs in Solidity.

Home Page: https://github.com/miguelmota/merkletreejs-solidity

License: MIT License

JavaScript 78.81% Makefile 3.60% Solidity 17.58%

merkletreejs-solidity's Introduction

MerkleTree.js Solidity example

Construct merkle trees with MerkleTree.js and verify merkle proofs in Solidity.

Example

contracts/MerkleProof.sol

pragma solidity ^0.5.2;

contract MerkleProof {
  function verify(
    bytes32 root,
    bytes32 leaf,
    bytes32[] memory proof
  )
    public
    pure
    returns (bool)
  {
    bytes32 computedHash = leaf;

    for (uint256 i = 0; i < proof.length; i++) {
      bytes32 proofElement = proof[i];

      if (computedHash <= proofElement) {
        // Hash(current computed hash + current element of the proof)
        computedHash = keccak256(abi.encodePacked(computedHash, proofElement));
      } else {
        // Hash(current element of the proof + current computed hash)
        computedHash = keccak256(abi.encodePacked(proofElement, computedHash));
      }
    }

    // Check if the computed hash (root) is equal to the provided root
    return computedHash == root;
  }
}

test/merkleproof.js

const MerkleProof = artifacts.require('MerkleProof')
const MerkleTree = require('merkletreejs')
const keccak256 = require('keccak256')

const contract = await MerkleProof.new()

const leaves = ['a', 'b', 'c', 'd'].map(v => keccak256(v))
const tree = new MerkleTree(leaves, keccak256, { sort: true })
const root = tree.getHexRoot()
const leaf = keccak256('a')
const proof = tree.getHexProof(leaf)
console.log(await contract.verify.call(root, leaf, proof)) // true

const badLeaves = ['a', 'b', 'x', 'd'].map(v => keccak256(v))
const badTree = new MerkleTree(badLeaves, keccak256, { sort: true })
const badProof = badTree.getHexProof(leaf)
console.log(await contract.verify.call(root, leaf, badProof)) // false

Test

make test

License

MIT

merkletreejs-solidity's People

Contributors

miguelmota avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.