UNPKG

postchain-client

Version:

Client library for accessing a Postchain node through REST.

82 lines 3.8 kB
"use strict"; var BinaryTreeFactory = require('../binarytreefactory').BinaryTreeFactory; var MerkleProofTreeFactory = require('./merkleprooftreefactory').MerkleProofTreeFactory; var MerkleHashCalculator = require('../merklehashcalculator').MerkleHashCalculator; var MerkleProofTree = require('./merkleprooftree').MerkleProofTree; var MerkleProofElement = require('./merkleprooftree').MerkleProofElement; var ProofHashedLeaf = require('./merkleprooftree').ProofHashedLeaf; var ProofValueLeaf = require('./merkleprooftree').ProofValueLeaf; var ProofNode = require('./merkleprooftree').ProofNode; var MerkleHashSummary = require('./merklehashcarrier').MerkleHashSummary; /** * * @param {BinaryTreeFactory} treeFactory * @param {MerkleProofTreeFactory} proofFactory */ function MerkleHashSummaryFactory(treeFactory, proofFactory) { this.treeFactory = treeFactory; this.proofFactory = proofFactory; } /** * @param {any} value * @param {MerkleHashCalculator} calculator */ MerkleHashSummaryFactory.prototype.calculateMerkleRoot = function (value, calculator, merkleHashVersion) { var binaryTree = this.treeFactory.build(value, merkleHashVersion); var proofTree = this.proofFactory.buildFromBinaryTree(binaryTree, calculator); return this.calculateMerkleRootOfTree(proofTree, calculator); }; /** * @param {MerkleProofTree} value * @param {MerkleHashCalculator} calculator */ MerkleHashSummaryFactory.prototype.calculateMerkleTreeRoot = function (tree, calculator, merkleHashVersion) { return this.calculateMerkleRootOfTree(tree, calculator, merkleHashVersion); }; /** * @param {MerkleProofTree} proofTree * @param {MerkleHashCalculator} calculator */ MerkleHashSummaryFactory.prototype.calculateMerkleRootOfTree = function (proofTree, calculator, merkleHashVersion) { var calculatedSummary = this.calculateMerkleRootInternal(proofTree.root, calculator, merkleHashVersion); return new MerkleHashSummary(calculatedSummary); }; /** * @param {MerkleProofElement} currentElement * @param {MerkleHashCalculator} calculator */ MerkleHashSummaryFactory.prototype.calculateMerkleRootInternal = function (currentElement, calculator, merkleHashVersion) { if (currentElement instanceof ProofHashedLeaf) { return currentElement.merkleHash; } else if (currentElement instanceof ProofValueLeaf) { var value = currentElement.content; if (calculator.isContainerProofValueLeaf(value)) { // We have a container value to prove, so need to convert the value to a binary tree, and THEN hash it var merkleProofTree = this.buildProofTree(value, calculator, merkleHashVersion); return this.calculateMerkleRootInternal(merkleProofTree.root, calculator, merkleHashVersion); } else { // This is a primitive value, just hash it return calculator.calculateLeafHash(value, merkleHashVersion); } } else if (currentElement instanceof ProofNode) { var left = this.calculateMerkleRootInternal(currentElement.left, calculator, merkleHashVersion); var right = this.calculateMerkleRootInternal(currentElement.right, calculator, merkleHashVersion); return calculator.calculateNodeHash(currentElement.prefix, left, right); } else { throw new Error("Should have handled this type? " + typeof currentElement); } }; /** * @param {any} value * @param {MerkleHashCalculator} calculator */ MerkleHashSummaryFactory.prototype.buildProofTree = function (value, calculator, merkleHashVersion) { var root = this.treeFactory.build(value, merkleHashVersion); return this.proofFactory.buildFromBinaryTree(root, calculator); }; module.exports = { MerkleHashSummaryFactory }; //# sourceMappingURL=merklehashsummaryfactory.js.map