// Override in derived class to extend the behavior. // Alternatively, we could implement a factory pattern. protected virtual MerkleNode CreateNode(MerkleHash hash) { return(new MerkleNode(hash)); }
public MerkleNode AppendLeaf(MerkleHash hash) => AppendLeaf(CreateNode(hash));
/* * /// <summary> * /// For demo / debugging purposes, we return the pairs of hashes used to verify the audit proof. * /// </summary> * public static List<Tuple<MerkleHash, MerkleHash>> AuditHashPairs(MerkleHash leafHash, List<MerkleProofHash> auditTrail) * { * Contract(() => auditTrail.Count > 0, "Audit trail cannot be empty."); * var auditPairs = new List<Tuple<MerkleHash, MerkleHash>>(); * MerkleHash testHash = leafHash; * * // TODO: Inefficient - compute hashes directly. * foreach (MerkleProofHash auditHash in auditTrail) * { * switch (auditHash.Direction) * { * case MerkleProofHash.Branch.Left: * auditPairs.Add(new Tuple<MerkleHash, MerkleHash>(testHash, auditHash.Hash)); * testHash = MerkleHash.Create(testHash.Value.Concat(auditHash.Hash.Value).ToArray()); * break; * * case MerkleProofHash.Branch.Right: * auditPairs.Add(new Tuple<MerkleHash, MerkleHash>(auditHash.Hash, testHash)); * testHash = MerkleHash.Create(auditHash.Hash.Value.Concat(testHash.Value).ToArray()); * break; * } * } * * return auditPairs; * } * * public static bool VerifyConsistency(MerkleHash oldRootHash, List<MerkleProofHash> proof) * { * MerkleHash hash, lhash, rhash; * * if (proof.Count > 1) * { * lhash = proof[proof.Count - 2].Hash; * int hidx = proof.Count - 1; * hash = rhash = MerkleTree.ComputeHash(lhash, proof[hidx].Hash); * hidx -= 2; * * // foreach (var nextHashNode in proof.Skip(1)) * while (hidx >= 0) * { * lhash = proof[hidx].Hash; * hash = rhash = MerkleTree.ComputeHash(lhash, rhash); * * --hidx; * } * } * else * { * hash = proof[0].Hash; * } * * return hash == oldRootHash; * } */ public static MerkleHash ComputeHash(MerkleHash left, MerkleHash right) { return(MerkleHash.Create(left.Value.Concat(right.Value).ToArray())); }
protected MerkleNode FindLeaf(MerkleHash leafHash) { return(RootNode.Leaves().FirstOrDefault(l => l.Hash == leafHash)); }
protected override MerkleNode CreateNode(MerkleHash hash) { return(new EventMerkleNode(hash)); }
public static EventMerkleNode Create(string s) { return(new EventMerkleNode(MerkleHash.Create(s))); }
public EventMerkleNode(MerkleHash hash) { Hash = hash; }
/// <summary> /// Constructor for a base node (leaf), representing the lowest level of the tree. /// </summary> public MerkleNode(MerkleHash hash) { Hash = hash; }
public MerkleHash ComputeHash(byte[] buffer) { Hash = MerkleHash.Create(buffer); return(Hash); }