Crate merkle_tree_stream[][src]

Example

use merkle_tree_stream::{DefaultNode, HashMethods, MerkleTreeStream, Node, PartialNode};
use std::rc::Rc;
use std::vec::Vec;

struct XorHashMethods;
impl HashMethods for XorHashMethods {
  type Node = DefaultNode;
  type Hash = u8;

  fn leaf(&self, leaf: &PartialNode, roots: &[Rc<Self::Node>]) -> Self::Hash {
    // bitwise XOR the data into u8
    leaf.as_ref().unwrap().iter().fold(0, |acc, x| acc ^ x)
  }

  fn parent(&self, a: &Self::Node, b: &Self::Node) -> Self::Hash {
    Node::hash(a).iter().chain(Node::hash(b).iter()).fold(0, |acc, x| acc ^ x)
  }

  fn node(&self, partial_node: &PartialNode, hash: Self::Hash) -> Self::Node {
    Self::Node::from_partial(partial_node, vec![hash])
  }
}

let mut mts = MerkleTreeStream::new(XorHashMethods, Vec::new());
let mut nodes = Vec::new();
mts.next(b"hello", &mut nodes);
mts.next(b"hashed", &mut nodes);
mts.next(b"world", &mut nodes);

Structs

DefaultNode

Node representation.

MerkleTreeStream

A stream that generates a merkle tree based on the incoming data.

PartialNode

Intermediate Node representation. Same as Node, but without the .hash field.

Traits

HashMethods

Functions that need to be implemented for MerkleTreeStream.

Node

Functions that need to be implemented for the Data that MerkleTreeStream works with.