pub struct MerkleTree {
pub leaves: Vec<String>,
pub nodes: Vec<String>,
}Expand description
Binary Merkle tree whose leaves are CID strings.
§Hash function
Leaf hashes are FNV-1a(cid.as_bytes()).
Internal nodes use FNV-1a(left_hex ++ right_hex).
When a level has an odd number of nodes the last node is duplicated
(standard Bitcoin/IPFS convention).
All hash values are stored and returned as 16-character lowercase hex strings.
Fields§
§leaves: Vec<String>Original CID strings used as leaves (in the order they were supplied).
nodes: Vec<String>All tree nodes stored level-by-level, leaves first.
Layout: [ leaf_hashes … | level-1 hashes … | … | root ]
Implementations§
Source§impl MerkleTree
impl MerkleTree
Sourcepub fn build(cids: &[String]) -> Self
pub fn build(cids: &[String]) -> Self
Build a Merkle tree from a slice of CID strings.
Returns an empty tree (no nodes, no root) when cids is empty.
Sourcepub fn root(&self) -> Option<&str>
pub fn root(&self) -> Option<&str>
Return the Merkle root as a hex string, or None for an empty tree.
Sourcepub fn proof_for(&self, index: usize) -> Option<Vec<String>>
pub fn proof_for(&self, index: usize) -> Option<Vec<String>>
Return the Merkle proof for the leaf at index.
The proof is a list of sibling hashes from leaf level up to (but not
including) the root. Returns None if index is out of range or
the tree is empty.
Sourcepub fn verify_proof(
leaf: &str,
index: usize,
proof: &[String],
root: &str,
) -> bool
pub fn verify_proof( leaf: &str, index: usize, proof: &[String], root: &str, ) -> bool
Verify a Merkle proof.
leaf— the raw CID string whose membership is being verifiedindex— zero-based position of the leaf in the original listproof— sibling hashes returned byMerkleTree::proof_forroot— expected Merkle root hex string
Returns true iff the recomputed root matches root.
Trait Implementations§
Source§impl Clone for MerkleTree
impl Clone for MerkleTree
Source§fn clone(&self) -> MerkleTree
fn clone(&self) -> MerkleTree
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for MerkleTree
impl Debug for MerkleTree
Source§impl<'de> Deserialize<'de> for MerkleTree
impl<'de> Deserialize<'de> for MerkleTree
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl Eq for MerkleTree
Source§impl PartialEq for MerkleTree
impl PartialEq for MerkleTree
Source§fn eq(&self, other: &MerkleTree) -> bool
fn eq(&self, other: &MerkleTree) -> bool
self and other values to be equal, and is used by ==.Source§impl Serialize for MerkleTree
impl Serialize for MerkleTree
impl StructuralPartialEq for MerkleTree
Auto Trait Implementations§
impl Freeze for MerkleTree
impl RefUnwindSafe for MerkleTree
impl Send for MerkleTree
impl Sync for MerkleTree
impl Unpin for MerkleTree
impl UnsafeUnpin for MerkleTree
impl UnwindSafe for MerkleTree
Blanket Implementations§
impl<T> Allocation for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more