Expand description

Example

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

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

  fn leaf(&self, leaf: &PartialNode, _roots: &[Rc<Self::Node>]) -> Self::Hash {
    // bitwise XOR the data into u8
    let hash = match leaf.data() {
      NodeKind::Parent => 0,
      NodeKind::Leaf(data) => data.iter().fold(0, |acc, x| acc ^ x),
    };
    vec![hash]
  }

  fn parent(&self, a: &Self::Node, b: &Self::Node) -> Self::Hash {
    let hash = Node::hash(a).iter().chain(Node::hash(b).iter()).fold(0, |acc, x| acc ^ x);
    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

Node representation.
A stream that generates a merkle tree based on the incoming data.
The parts that make up a full Node from a PartialNode
Intermediate Node representation. Same as Node, but without the .hash field.

Enums

Custom Option type that encodes the presence or absense of data at this node

Traits

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