"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const pako_1 = __importDefault(require("pako"));
const Bytes_1 = require("@wireio/sdk-core/chain/Bytes");
const serializer_1 = require("@wireio/sdk-core/serializer");
const Base64u = __importStar(require("@wireio/sdk-core/signing/Base64u"));
const ChainId_1 = require("@wireio/sdk-core/signing/ChainId");
const SigningRequest_1 = require("@wireio/sdk-core/signing/SigningRequest");
const transactions_js_1 = require("../support/transactions.js");
const SigningRequestCompressionFlag = 1 << 7;
const SigningRequestScheme = "esr:";
const LargeInfoKey = "large";
const SigningRequestInflateLimitMessage = /Signing request zlib output exceeds/;
const InvalidInflateLimitMessage = /Signing request inflate limit must be a non-negative safe integer/;
/** Create a raw-deflated signing request byte stream. */
function createCompressedRequestBytes(payload, version = 2) {
    const compressedPayload = pako_1.default.deflateRaw(payload);
    const data = new Uint8Array(1 + compressedPayload.byteLength);
    data[0] = version | SigningRequestCompressionFlag;
    data.set(compressedPayload, 1);
    return data;
}
/** Create a raw-deflated signing request URI. */
function createCompressedRequestUri(payload, version = 2) {
    return `${SigningRequestScheme}//${Base64u.encode(createCompressedRequestBytes(payload, version))}`;
}
describe("SigningRequest", () => {
    describe("from", () => {
        it("decodes compressed signing request URIs with bounded raw zlib inflation", () => {
            const transaction = (0, transactions_js_1.createEmptyTransaction)();
            const serializedTransaction = serializer_1.Serializer.encode({ object: transaction });
            const request = SigningRequest_1.SigningRequest.fromTransaction(ChainId_1.ChainName.SYS, serializedTransaction);
            const uri = createCompressedRequestUri(request.getData(), request.version);
            const parsedRequest = SigningRequest_1.SigningRequest.from(uri);
            expect(parsedRequest.data.req.variantName).toBe("transaction");
            expect(parsedRequest.getRawTransaction().equals(transaction)).toBe(true);
        });
    });
    describe("fromData", () => {
        it("rejects compressed request payloads above the default inflated byte limit", () => {
            const payload = new Uint8Array(SigningRequest_1.SigningRequestCompression.DefaultMaxInflatedBytes + 1);
            const data = createCompressedRequestBytes(payload);
            expect(() => SigningRequest_1.SigningRequest.fromData(data)).toThrow(SigningRequestInflateLimitMessage);
        });
        it("honors custom compressed request inflated byte limits", () => {
            const transaction = (0, transactions_js_1.createEmptyTransaction)();
            const serializedTransaction = serializer_1.Serializer.encode({ object: transaction });
            const request = SigningRequest_1.SigningRequest.fromTransaction(ChainId_1.ChainName.SYS, serializedTransaction);
            const payload = request.getData();
            const data = createCompressedRequestBytes(payload, request.version);
            expect(() => SigningRequest_1.SigningRequest.fromData(data, {
                maxInflatedBytes: payload.byteLength - 1
            })).toThrow(SigningRequestInflateLimitMessage);
        });
        it("parses valid compressed requests above the default limit when explicitly raised", () => {
            const transaction = (0, transactions_js_1.createEmptyTransaction)();
            const largeInfoValue = Bytes_1.Bytes.from("A".repeat(SigningRequest_1.SigningRequestCompression.DefaultMaxInflatedBytes + 1), "utf8");
            const request = SigningRequest_1.SigningRequest.createSync({
                transaction,
                info: {
                    [LargeInfoKey]: largeInfoValue
                }
            });
            const payload = request.getData();
            const data = createCompressedRequestBytes(payload, request.version);
            expect(() => SigningRequest_1.SigningRequest.fromData(data)).toThrow(SigningRequestInflateLimitMessage);
            const parsedRequest = SigningRequest_1.SigningRequest.fromData(data, {
                maxInflatedBytes: payload.byteLength
            });
            expect(parsedRequest.getRawTransaction().equals(transaction)).toBe(true);
            expect(parsedRequest.data.info[0].key).toBe(LargeInfoKey);
            expect(parsedRequest.data.info[0].value.equals(largeInfoValue)).toBe(true);
        });
        it.each([Number.NaN, -1, 1.5])("rejects invalid maxInflatedBytes value %s", maxInflatedBytes => {
            const transaction = (0, transactions_js_1.createEmptyTransaction)();
            const serializedTransaction = serializer_1.Serializer.encode({ object: transaction });
            const request = SigningRequest_1.SigningRequest.fromTransaction(ChainId_1.ChainName.SYS, serializedTransaction);
            const data = createCompressedRequestBytes(request.getData(), request.version);
            expect(() => SigningRequest_1.SigningRequest.fromData(data, { maxInflatedBytes })).toThrow(InvalidInflateLimitMessage);
        });
    });
    describe("fromTransaction", () => {
        it("encodes the transaction request variant through the generic varuint writer", () => {
            const transaction = (0, transactions_js_1.createEmptyTransaction)();
            const serializedTransaction = serializer_1.Serializer.encode({ object: transaction });
            const request = SigningRequest_1.SigningRequest.fromTransaction(ChainId_1.ChainName.SYS, serializedTransaction);
            expect(request.data.req.variantName).toBe("transaction");
            expect(request.getRawTransaction().equals(transaction)).toBe(true);
        });
    });
});
//# sourceMappingURL=SigningRequest.test.js.map