Ethereum Merkle Tree Library (eth-merkle-tree-rs)
A Rust library and command-line interface (CLI) for working with Ethereum's Merkle tree structure.
Table of Contents
Installation
To include eth-merkle-tree-rs in your project, add it to your Cargo.toml:
[]
= "0.6"
= "0.4"
= "0.10"
[]
= "0.6"
= "0.4"
= "2.0"
Library Usage
Constructing the Merkle Tree
use MerkleTree;
let data = vec!;
let tree = new.expect;
let root = tree.root.expect;
println!;
or
use MerkleTree;
let data = vec!;
let tree = new.expect;
let root = tree.root.expect;
println!;
Visualizing the Tree
Can visualize the tree structure using the provided visualization tools:
Note: To visualize the Merkle Tree, ensure that Graphviz is installed on your system.
use graphviz;
graphviz.expect;
CLI Usage
The library also comes with a command-line interface for interacting with Merkle trees.
Visualize the Tree
To visualize the tree, use the -v or --visualize flag:
Generate a Merkle Proof
To generate a Merkle proof for a specific leaf:
Validating a proof in Solidity
Once the proof has been generated, it can be validated in Solidity using MerkleProof as in the following example:
pragma solidity >=0.8.0 <0.9.0;
import "@openzeppelin/contracts/utils/cryptography/MerkleProof.sol";
contract Verifier {
bytes32 private root;
constructor(bytes32 _root) {
root = _root;
}
function verify(
bytes32[] memory proof,
address addr,
uint256 amount
) public view {
bytes32 leaf = keccak256(abi.encode(addr, amount));
require(MerkleProof.verify(proof, root, leaf), "Invalid proof");
}
function verify(
bytes32[] memory proof,
address addr
) public view {
bytes32 leaf = keccak256(abi.encodePacked(addr));
require(MerkleProof.verify(proof, root, leaf), "Invalid proof");
}
}
License
This project is licensed under the MIT License. See the LICENSE file for details.