verity_verify_local/
merkle.rs

1use rs_merkle::{algorithms::Sha256, Hasher, MerkleTree};
2
3/// Generate a merkle tree by providing the leaves as hashed strings
4pub fn generate_merkle_tree(leaves: &Vec<[u8; 32]>) -> MerkleTree<Sha256> {
5    let tree: MerkleTree<Sha256> = MerkleTree::<Sha256>::from_leaves(&leaves);
6    return tree;
7}
8
9/// Validate that the provided root hash is the same as the one derived from building a tree out of the provided leaves
10pub fn validate_merkle_tree(leaves: &Vec<String>, root_hash: &String) -> bool {
11    // gather the leaves from the content of the verified proof
12    // which is either the req/res pair or the has of the session proof
13    let leaves: Vec<[u8; 32]> = leaves
14        .iter()
15        .map(|proof_response| {
16            let proof_byte_content = proof_response.as_bytes();
17
18            Sha256::hash(proof_byte_content)
19        })
20        .collect();
21
22    let merkle_tree = generate_merkle_tree(&leaves);
23
24    let root = merkle_tree.root().unwrap();
25    let derived_root_hash = hex::encode(root);
26    let root_hash = root_hash.to_owned();
27
28    // println!("derived_root_hash: {}", derived_root_hash);
29    // println!("root_hash: {}", root_hash);
30
31    return derived_root_hash == root_hash;
32}