public static UInt256 CalculateMerkleRoot <T>(IEnumerable <T> merkleTreeNodes) where T : IMerkleTreeNode <T> { var merkleStream = new MerkleStream <T>(); foreach (var node in merkleTreeNodes) { merkleStream.AddNode(node); } merkleStream.FinishPairing(); return(merkleStream.RootNode.Hash); }
public static UInt256 CalculateMerkleRoot(IEnumerable <UInt256> hashes) { var merkleStream = new MerkleStream <MerkleTreeNode>(); var index = 0; foreach (var hash in hashes) { var node = new MerkleTreeNode(index, 0, hash, true); merkleStream.AddNode(node); index++; } merkleStream.FinishPairing(); return(merkleStream.RootNode.Hash); }
public static IEnumerable <T> ReadMerkleTreeNodes <T>(UInt256 merkleRoot, IEnumerable <T> merkleTreeNodes) where T : MerkleTreeNode { var merkleStream = new MerkleStream(); foreach (var node in merkleTreeNodes) { merkleStream.AddNode(node); yield return(node); } merkleStream.FinishPairing(); if (merkleStream.RootNode.Hash != merkleRoot) { throw new InvalidOperationException(); } }