项目作者: ameensol

项目描述 :
JS - Solidity sha3 merkle tree bridge. Generate proofs in JS; verify in Solidity.
高级语言: JavaScript
项目地址: git://github.com/ameensol/merkle-tree-solidity.git
创建时间: 2017-03-07T02:05:12Z
项目社区:https://github.com/ameensol/merkle-tree-solidity

开源协议:

下载


merkle-tree-solidity

JS - Solidity sha3 merkle tree bridge. Generate proofs in JS; verify in Solidity.

Install

  1. npm install --save merkle-tree-solidity

Credit

This is partly a port of Raiden’s merkle tree python module and the solidity code is
a close copy.

Merkle Tree
https://github.com/raiden-network/raiden/blob/master/raiden/mtree.py

Solidity
https://github.com/raiden-network/raiden/blob/master/raiden/smart_contracts/NettingChannelLibrary.sol

Usage

  1. import MerkleTree, { checkProof, merkleRoot, checkProofSolidityFactory } from 'merkle-tree-solidity'
  2. import { sha3 } from 'ethereumjs-util'
  3. // create merkle tree
  4. // expects unique 32 byte buffers as inputs (no hex strings)
  5. // if using web3.sha3, convert first -> Buffer(web3.sha3('a'), 'hex')
  6. const elements = [1, 2, 3].map(e => sha3(e))
  7. const merkleTree = new MerkleTree(elements)
  8. // get the merkle root
  9. // returns 32 byte buffer
  10. const root = merkleTree.getRoot()
  11. // for convenience if only the root is desired
  12. // this creates a new MerkleTree under the hood
  13. const easyRoot = merkleRoot(elements)
  14. // generate merkle proof
  15. // returns array of 32 byte buffers
  16. const proof = merkleTree.getProof(elements[0])
  17. // check merkle proof in JS
  18. // returns bool
  19. checkProof(proof, root, elements[0])
  20. // create the contract abstraction
  21. const merkleProof = await deployMerkleProofContract()
  22. // then use the contract directly
  23. // but the contract requires hex prefixed strings, not buffers
  24. merkleProof.checkProof(proof, root, hash) // -> throws
  25. // or create a helper function from the abstraction
  26. // this function converts the buffers to hex prefixed strings
  27. const checkProofSolidity = checkProofSolidityFactory(merkleProof.checkProof)
  28. // check merkle proof in Solidity
  29. // we can now safely pass in the buffers returned by previous methods
  30. await checkProofSolidity(proof, root, elements[0]) // -> true

Ordered Merkle Trees

By default, generating the tree doesn’t preserve leaf order, but we can
optionally do so.

  1. import MerkleTree, { checkProofOrdered, merkleRoot, checkProofOrderedSolidityFactory } from 'merkle-tree-solidity'
  2. import { sha3 } from 'ethereumjs-util'
  3. // create merkle tree
  4. // expects 32 byte buffers as inputs (no hex strings)
  5. // if using web3.sha3, convert first -> Buffer(web3.sha3('a'), 'hex')
  6. const elements = [1, 2, 3].map(e => sha3(e))
  7. // include the 'true' flag when generating the merkle tree
  8. const merkleTree = new MerkleTree(elements, true)
  9. // [same as above]
  10. // get the merkle root
  11. // returns 32 byte buffer
  12. const root = merkleTree.getRoot()
  13. // for convenience if only the root is desired
  14. // this creates a new MerkleTree under the hood
  15. // 2nd arg is "preserveOrder" flag
  16. const easyRoot = merkleRoot(elements, true)
  17. // generate merkle proof
  18. // 2nd argugment is the 1-n index of the element
  19. // returns array of 32 byte buffers
  20. const index = 1
  21. const proof = merkleTree.getProofOrdered(elements[0], index)
  22. // this is useful if you have duplicates in your tree
  23. const elements2 = [3, 2, 3].map(e => sha3(e))
  24. const index2 = 3
  25. const proof2 = merkleTree.getProof(sha3(3), 3)
  26. // check merkle proof of ordered tree in JS
  27. // expects 1-n indexed element position as last param
  28. // returns bool
  29. const index = 1
  30. checkProofOrdered(proof, root, elements[0], index)
  31. // create the contract abstraction
  32. const merkleProof = await deployMerkleProofContract()
  33. // then use the contract directly
  34. // but the contract requires hex prefixed strings, not buffers
  35. merkleProof.checkProofOrdered(proof, root, hash, index) // -> throws
  36. // or create a helper function from the abstraction
  37. // this function converts the buffers to hex prefixed strings
  38. const checkProofOrderedSolidity = checkProofSolidityOrderedFactory(merkleProof.checkProofOrdered)
  39. // check merkle proof in Solidity
  40. // we can now safely pass in the buffers returned by previous methods
  41. await checkProofOrderedSolidity(proof, root, elements[0], index) // -> true

Licence

This project is licensed under the MIT license, Copyright (c) 2016 Ameen Soleimani