From 5fb45710f57f95eec527958400f8ec0a049c5fe4 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Mon, 7 Jul 2025 17:14:12 +0200 Subject: Make tree for folders --- src/proof.rs | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) (limited to 'src/proof.rs') diff --git a/src/proof.rs b/src/proof.rs index af0af94..b6b5860 100644 --- a/src/proof.rs +++ b/src/proof.rs @@ -64,6 +64,21 @@ where pub fn new(hasher: H, leaves: Vec) -> Self { Self { hasher, leaves } } + + pub fn verify_hash(&self, proof: &MerkleProof, hash: String, root_hash: &str) -> bool { + let mut current_hash = hash; + // Walk up the tree using the proof path + for proof_node in &proof.path { + let combined: String = match proof_node.child_type { + NodeChildType::Left => format!("{}{}", proof_node.hash, current_hash), + NodeChildType::Right => format!("{}{}", current_hash, proof_node.hash), + }; + current_hash = self.hasher.hash(combined.as_bytes()); + } + + // Check if the computed root matches the expected root + current_hash == root_hash + } } impl Proofer for DefaultProofer @@ -138,19 +153,8 @@ where T: AsRef<[u8]>, { // Start with the hash of the data - let mut current_hash = self.hasher.hash(data.as_ref()); - - // Walk up the tree using the proof path - for proof_node in &proof.path { - let combined: String = match proof_node.child_type { - NodeChildType::Left => format!("{}{}", proof_node.hash, current_hash), - NodeChildType::Right => format!("{}{}", current_hash, proof_node.hash), - }; - current_hash = self.hasher.hash(combined.as_bytes()); - } - - // Check if the computed root matches the expected root - current_hash == root_hash + let hash: String = self.hasher.hash(data.as_ref()); + self.verify_hash(proof, hash, root_hash) } } -- cgit v1.2.3-71-g8e6c