"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.deserializeSignAppDataReq = exports.serializeSignAppDataReq = exports.cSignAppDataReqVersion = void 0;
const Context_1 = require("../p2p/Context");
const Helpers_1 = require("./ajv/Helpers");
const AJVSchemaEnum_1 = require("./enum/AJVSchemaEnum");
const AppObjEnum_1 = require("./enum/AppObjEnum");
const TypeIdentifierEnum_1 = require("./enum/TypeIdentifierEnum");
exports.cSignAppDataReqVersion = 1;
function serializeSignAppDataReq(stream, obj, root = false) {
    if (root) {
        stream.writeUInt16(TypeIdentifierEnum_1.TypeIdentifierEnum.cSignAppDataReq);
    }
    stream.writeUInt8(exports.cSignAppDataReqVersion);
    stream.writeString(obj.type);
    stream.writeUInt8(obj.nodesToSign);
    stream.writeString(obj.hash);
    stream.writeBuffer(Context_1.stateManager.app.binarySerializeObject(AppObjEnum_1.AppObjEnum.AppData, obj.appData));
}
exports.serializeSignAppDataReq = serializeSignAppDataReq;
function deserializeSignAppDataReq(stream) {
    const version = stream.readUInt8();
    if (version > exports.cSignAppDataReqVersion) {
        throw new Error(`SignAppDataReq version mismatch, version: ${version}`);
    }
    const result = {
        type: stream.readString(),
        nodesToSign: stream.readUInt8(),
        hash: stream.readString(),
        appData: Context_1.stateManager.app.binaryDeserializeObject(AppObjEnum_1.AppObjEnum.AppData, stream.readBuffer()),
    };
    const errors = (0, Helpers_1.verifyPayload)(AJVSchemaEnum_1.AJVSchemaEnum.SignAppDataReq, result);
    if (errors && errors.length > 0) {
        throw new Error('Data validation error');
    }
    return result;
}
exports.deserializeSignAppDataReq = deserializeSignAppDataReq;
//# sourceMappingURL=SignAppDataReq.js.map