"use strict";
/**
 * `verify` submodule. Defines functions used to verify objects against
 * given hashes.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.verifyValidatorList = verifyValidatorList;
exports.verifyArchiverList = verifyArchiverList;
exports.verifyCycleRecord = verifyCycleRecord;
exports.verifyTxList = verifyTxList;
const neverthrow_1 = require("neverthrow");
const Context_1 = require("../Context");
const CycleCreator_1 = require("../CycleCreator");
const lib_types_1 = require("@shardeum-foundation/lib-types");
const queries_1 = require("./queries");
const logger_1 = require("../../logger");
/**
 * Verifies if the hash of a given object matches the expected hash.
 *
 * This function hashes a 'HashableObject' and compares it to an expected hash value. If the hashes match, it returns a successful result with value true. If the hashes don't match, it returns an error with a detailed message describing the mismatch.
 *
 * @param object - The object to be hashed and verified.
 * @param expectedHash - The expected hash string to compare with the hash of the object.
 * @param [objectName='some object'] - An optional name for the object, used in the error message in case of a hash mismatch.
 *
 * @returns Returns a Result object. On successful hash verification, returns 'ok' with value true. On mismatch, returns 'err' with an Error object detailing the mismatch.
 */
function verify(object, expectedHash, objectName = 'some object') {
    console.log(`hashing ${objectName}:`, lib_types_1.Utils.safeStringify(object));
    const newHash = Context_1.crypto.hash(object);
    console.log(`got ${newHash}`);
    return newHash === expectedHash
        ? (0, neverthrow_1.ok)(true)
        : (0, neverthrow_1.err)(new Error(`hash mismatch for ${objectName}: expected ${expectedHash}, got ${newHash}`));
}
/** Verifies that the hash of the validator list matches the expected hash. */
function verifyValidatorList(validatorList, expectedHash) {
    if (logger_1.logFlags.p2pSyncDebug)
        info(`verifyValidatorList ${expectedHash}  ${lib_types_1.Utils.safeStringify(validatorList)} `);
    return verify(validatorList, expectedHash, 'validator list');
}
/** Verifies that the hash of the archiver list matches the expected hash. */
function verifyArchiverList(archiverList, expectedHash) {
    return verify(archiverList, expectedHash, 'archiver list');
}
/** Verifies that the hash of the cycle record matches the expected hash. */
function verifyCycleRecord(cycleRecord, expectedHash) {
    const actualHash = (0, CycleCreator_1.makeCycleMarker)(cycleRecord);
    // verify that the hash of the CycleRecord matches the expected hash
    if (actualHash !== expectedHash)
        return (0, neverthrow_1.err)(new Error(`hash mismatch for cycle: expected ${expectedHash}, got ${actualHash}`));
    return (0, neverthrow_1.ok)(true);
}
function verifyTxList(txList, expectedHash) {
    const actualHash = Context_1.crypto.hash(txList);
    // verify that the hash of the CycleRecord matches the expected hash
    if (actualHash !== expectedHash)
        return (0, neverthrow_1.err)(new Error(`hash mismatch for txList: expected ${expectedHash}, got ${actualHash}`));
    return (0, neverthrow_1.ok)(true);
}
function info(...msg) {
    const entry = `SyncV2: ${msg.join(' ')}`;
    queries_1.p2pLogger.info(entry);
}
//# sourceMappingURL=verify.js.map