diff options
| author | Santo Cariotti <santo@dcariotti.me> | 2025-07-07 15:14:12 +0000 |
|---|---|---|
| committer | Santo Cariotti <santo@dcariotti.me> | 2025-07-07 15:14:12 +0000 |
| commit | 5fb45710f57f95eec527958400f8ec0a049c5fe4 (patch) | |
| tree | 5a698f60ceec8954fcc5b2e19cbc7b9beea06a43 /src/proof.rs | |
| parent | 2ef7371f7a4eefe7478cad43cb4922efaa12876a (diff) | |
Make tree for folders
Diffstat (limited to 'src/proof.rs')
| -rw-r--r-- | src/proof.rs | 30 |
1 files changed, 17 insertions, 13 deletions
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<Node>) -> 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<H> Proofer for DefaultProofer<H> @@ -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) } } |
