o2-client
Version:
Light Client for O2 Blockchain
51 lines (38 loc) • 1.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.verifyTable = verifyTable;
var _primitive = require('../types/primitive');
var _generic = require('../types/generic');
var _hexadecimal = require('../types/hexadecimal');
var _merklePatricia = require('./merkle-patricia');
var TableKey = (0, _generic.newType)({
fields: [{ name: 'service_id', type: _primitive.Uint16 }, { name: 'table_index', type: _primitive.Uint16 }]
});
/**
* Validate path from tree root to some table
* @param {Object} proof
* @param {string} stateHash
* @param {number} serviceId
* @param {number} tableIndex
* @returns {string}
*/
function verifyTable(proof, stateHash, serviceId, tableIndex) {
// calculate key of table in the root tree
var key = TableKey.hash({
service_id: serviceId,
table_index: tableIndex
});
// validate proof of table existence in root tree
var tableProof = new _merklePatricia.MapProof(proof, _hexadecimal.Hash, _hexadecimal.Hash);
if (tableProof.merkleRoot !== stateHash) {
throw new Error('Table proof is corrupted');
}
// get root hash of the table
var rootHash = tableProof.entries.get(key);
if (typeof rootHash === 'undefined') {
throw new Error('Table not found in the root tree');
}
return rootHash;
}