diff options
Diffstat (limited to 'src/merkletree.rs')
| -rw-r--r-- | src/merkletree.rs | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/src/merkletree.rs b/src/merkletree.rs index c3c9fd7..fdbb5ac 100644 --- a/src/merkletree.rs +++ b/src/merkletree.rs @@ -1,7 +1,7 @@ //! Provides the MerkleTree structure and associated methods for creating and interacting //! with binary Merkle trees using custom hashers. -use crate::{hasher::Hasher, node::Node}; +use crate::{fs, hasher::Hasher, node::Node}; use rayon::prelude::*; /// A binary Merkle tree implementation. @@ -60,6 +60,20 @@ impl MerkleTree { Self::build(hasher, leaves) } + /// Construct a Merkletree from an iter of String-s. + pub fn from_paths<H>(hasher: H, paths: Vec<String>) -> Self + where + H: Hasher + 'static + std::marker::Sync + Clone, + { + let mut leaves = fs::hash_dir(hasher.clone(), paths); + + if leaves.len() % 2 != 0 { + leaves.push(leaves.last().unwrap().clone()); + } + + Self::build(hasher, leaves) + } + /// Constructs the internal nodes of the tree from the leaves upward and computes the root. fn build<H>(hasher: H, nodes: Vec<Node>) -> Self where |
