"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.cMakeReceiptReqVersion = void 0;
exports.serializeMakeReceiptReq = serializeMakeReceiptReq;
exports.deserializeMakeReceiptReq = deserializeMakeReceiptReq;
const lib_types_1 = require("@shardeum-foundation/lib-types");
const TypeIdentifierEnum_1 = require("./enum/TypeIdentifierEnum");
const Helpers_1 = require("./ajv/Helpers");
const AJVSchemaEnum_1 = require("./enum/AJVSchemaEnum");
exports.cMakeReceiptReqVersion = 1;
function serializeMakeReceiptReq(stream, obj, root = false) {
    if (root) {
        stream.writeInt16(TypeIdentifierEnum_1.TypeIdentifierEnum.cMakeReceiptReq);
    }
    stream.writeUInt8(exports.cMakeReceiptReqVersion);
    stream.writeString(obj.sign.owner);
    stream.writeString(obj.sign.sig);
    stream.writeString(obj.address);
    stream.writeString(obj.addressHash);
    stream.writeString(lib_types_1.Utils.safeStringify(obj.value));
    stream.writeBigUInt64(BigInt(obj.when));
    stream.writeString(obj.source);
    stream.writeString(obj.txId);
    stream.writeString(obj.afterStateHash);
}
function deserializeMakeReceiptReq(stream) {
    const version = stream.readUInt8();
    if (version > exports.cMakeReceiptReqVersion) {
        throw new Error(`Invalid version ${version} for MakeReceiptReq`);
    }
    const obj = {
        sign: {
            owner: stream.readString(),
            sig: stream.readString(),
        },
        address: stream.readString(),
        addressHash: stream.readString(),
        value: lib_types_1.Utils.safeJsonParse(stream.readString()),
        when: Number(stream.readBigUInt64()),
        source: stream.readString(),
        txId: stream.readString(),
        afterStateHash: stream.readString(),
    };
    const errors = (0, Helpers_1.verifyPayload)(AJVSchemaEnum_1.AJVSchemaEnum.MakeReceiptReq, obj);
    if (errors && errors.length > 0) {
        throw new Error('Data validation error');
    }
    return obj;
}
//# sourceMappingURL=MakeReceipReq.js.map