Crates.io | simple-merkle-tree |
lib.rs | simple-merkle-tree |
version | 0.1.0 |
source | src |
created_at | 2021-08-28 21:34:53.800752 |
updated_at | 2021-08-28 21:34:53.800752 |
description | Easy to use replica of Solidity abi.encodePacked. |
homepage | https://github.com/roberts-ivanovs/simple-merkle-tree |
repository | |
max_upload_size | |
id | 443572 |
size | 14,694 |
To be used hand-in-hand with Solidity, that's why it's using keccak256
for hashing!
Example usage
use simple_merkle_tree::MerkleTree;
let elements = (0..4)
.map(|el| format!("item-string-{:}", el).into_bytes())
.collect::<Vec<Vec<u8>>>();
let tree = MerkleTree::new(elements.clone());
let a = &elements[0];
let b = &elements[1];
let c = &elements[2];
let d = &elements[3];
let h_a = MerkleTree::hash(a); // Part of the proof
let h_b = MerkleTree::hash(b);
let h_c = MerkleTree::hash(c); // Part of the proof
let h_d = MerkleTree::hash(d); // Part of the proof
let h_ab = MerkleTree::combined_hash(&h_a, &h_b); // Part of the proof
let h_cd = MerkleTree::combined_hash(&h_c, &h_d);
let h_abcd = MerkleTree::combined_hash(&h_ab, &h_cd);
let proof = tree.get_proof(d).unwrap();
assert_eq!(proof.len(), 2);
assert_eq!(
vec![hex::encode(h_c), hex::encode(h_ab),],
proof
.iter()
.map(|e| hex::encode(e))
.collect::<Vec<String>>()
);
let root = tree.get_root();
assert_eq!(hex::encode(h_abcd), hex::encode(root));