"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.deserializeAppliedReceipt2 = exports.serializeAppliedReceipt2 = exports.cAppliedReceipt2Version = void 0;
const AppliedVote_1 = require("./AppliedVote");
const Sign_1 = require("./Sign");
const TypeIdentifierEnum_1 = require("./enum/TypeIdentifierEnum");
exports.cAppliedReceipt2Version = 1;
function serializeAppliedReceipt2(stream, obj, root = false) {
    if (root) {
        stream.writeUInt16(TypeIdentifierEnum_1.TypeIdentifierEnum.cAppliedReceipt2);
    }
    stream.writeUInt8(exports.cAppliedReceipt2Version);
    stream.writeString(obj.txid);
    stream.writeUInt8(obj.result ? 1 : 0);
    (0, AppliedVote_1.serializeAppliedVote)(stream, obj.appliedVote);
    // Check if confirmOrChallenge is defined
    // confirmOrChallenge is not part of consensus anymore. Adding this here for backwards compatibility.
    stream.writeUInt8(0);
    stream.writeUInt16(obj.signatures.length);
    for (let i = 0; i < obj.signatures.length; i++) {
        (0, Sign_1.serializeSign)(stream, obj.signatures[i]);
    }
    stream.writeString(obj.app_data_hash);
}
exports.serializeAppliedReceipt2 = serializeAppliedReceipt2;
function deserializeAppliedReceipt2(stream) {
    const version = stream.readUInt8();
    if (version > exports.cAppliedReceipt2Version) {
        throw new Error(`AppliedReceipt2Deserializer expected version ${exports.cAppliedReceipt2Version}, got ${version}`);
    }
    const txid = stream.readString();
    const result = stream.readUInt8() === 1;
    const appliedVote = (0, AppliedVote_1.deserializeAppliedVote)(stream);
    const signaturesLength = stream.readUInt16();
    const signatures = [];
    for (let i = 0; i < signaturesLength; i++) {
        signatures.push((0, Sign_1.deserializeSign)(stream));
    }
    const app_data_hash = stream.readString();
    return {
        txid,
        result,
        appliedVote,
        signatures,
        app_data_hash,
    };
}
exports.deserializeAppliedReceipt2 = deserializeAppliedReceipt2;
//# sourceMappingURL=AppliedReceipt2.js.map