"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.cRequestStateForTxReqVersion = void 0;
exports.serializeRequestStateForTxReq = serializeRequestStateForTxReq;
exports.deserializeRequestStateForTxReq = deserializeRequestStateForTxReq;
const Helpers_1 = require("./ajv/Helpers");
const AJVSchemaEnum_1 = require("./enum/AJVSchemaEnum");
const TypeIdentifierEnum_1 = require("./enum/TypeIdentifierEnum");
exports.cRequestStateForTxReqVersion = 1;
function serializeRequestStateForTxReq(stream, inp, root = false) {
    if (root) {
        stream.writeUInt16(TypeIdentifierEnum_1.TypeIdentifierEnum.cRequestStateForTxReq);
    }
    stream.writeUInt8(exports.cRequestStateForTxReqVersion);
    stream.writeString(inp.txid);
    stream.writeString(inp.timestamp.toString());
    stream.writeUInt32(inp.keys.length);
    for (let i = 0; i < inp.keys.length; i++) {
        // eslint-disable-next-line security/detect-object-injection
        stream.writeString(inp.keys[i]);
    }
}
function deserializeRequestStateForTxReq(stream) {
    const version = stream.readUInt8();
    if (version !== exports.cRequestStateForTxReqVersion) {
        throw new Error('Unsupported version');
    }
    const txid = stream.readString();
    const timestamp = parseInt(stream.readString());
    const keysLength = stream.readUInt32();
    const keys = new Array(keysLength);
    for (let i = 0; i < keysLength; i++) {
        // eslint-disable-next-line security/detect-object-injection
        keys[i] = stream.readString();
    }
    const errors = (0, Helpers_1.verifyPayload)(AJVSchemaEnum_1.AJVSchemaEnum.RequestStateForTxReq, { txid, timestamp, keys });
    if (errors && errors.length > 0) {
        throw new Error(`AJV: validation error -> ${errors.join(', ')}`);
    }
    return { txid, timestamp, keys };
}
//# sourceMappingURL=RequestStateForTxReq.js.map