Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/miguelmota/merkletreejs-solidity
Construct merkle trees with MerkleTree.js and verify merkle proofs in Solidity.
https://github.com/miguelmota/merkletreejs-solidity
blockchain ethereum example merkle merkle-hash-trees merkle-proof merkle-root merkle-tree smart-contracts solidity verification
Last synced: 3 months ago
JSON representation
Construct merkle trees with MerkleTree.js and verify merkle proofs in Solidity.
- Host: GitHub
- URL: https://github.com/miguelmota/merkletreejs-solidity
- Owner: miguelmota
- License: mit
- Archived: true
- Created: 2018-10-23T06:23:38.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2022-03-13T08:21:52.000Z (almost 3 years ago)
- Last Synced: 2024-07-05T13:54:47.490Z (7 months ago)
- Topics: blockchain, ethereum, example, merkle, merkle-hash-trees, merkle-proof, merkle-root, merkle-tree, smart-contracts, solidity, verification
- Language: JavaScript
- Homepage: https://github.com/miguelmota/merkletreejs-solidity
- Size: 35.2 KB
- Stars: 132
- Watchers: 2
- Forks: 22
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MerkleTree.js Solidity example
> Construct merkle trees with [MerkleTree.js](https://github.com/miguelmota/merkletreejs) and verify merkle proofs in [Solidity](https://github.com/ethereum/solidity).
## Example
[`contracts/MerkleProof.sol`](./contracts/MerkleProof.sol)
```solidity
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`](./test/merkleproof.js)
```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)) // trueconst 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
```bash
make test
```## License
MIT