"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRedeliveredCount = exports.getDeadLetterQueueName = exports.isValidSignature = exports.signDfnsMessage = exports.parseMessageFromBase64String = exports.getMessageAsBuffer = exports.getKeyId = void 0;
const aws_sdk_1 = require("aws-sdk");
const envs_1 = require("../envs");
const logging_1 = require("../logging");
const constants_1 = require("./constants");
const signatureMessageType = 'RAW';
const signatureAlgorithm = 'ECDSA_SHA_256';
const getKeyId = () => {
    const environment = (0, envs_1.maybeGetEnvParam)('ENVIRONMENT');
    const serviceName = (0, envs_1.maybeGetEnvParam)('SERVICE_NAME');
    const businessDomain = (0, envs_1.maybeGetEnvParam)('BUSINESS_DOMAIN');
    return `alias/${environment}-${businessDomain}-${serviceName}-rabbitmq-signing-key`;
};
exports.getKeyId = getKeyId;
const getMessageAsBuffer = (message) => {
    const messageJson = JSON.stringify(message);
    const messageBuffer = Buffer.from(messageJson);
    return messageBuffer;
};
exports.getMessageAsBuffer = getMessageAsBuffer;
const parseMessageFromBase64String = (base64Message) => {
    return JSON.parse(Buffer.from(base64Message, 'base64').toString('ascii'));
};
exports.parseMessageFromBase64String = parseMessageFromBase64String;
const signDfnsMessage = async (message) => {
    const kms = new aws_sdk_1.KMS();
    const keyId = (0, exports.getKeyId)();
    const res = await kms
        .sign({
        Message: JSON.stringify(message),
        KeyId: keyId,
        SigningAlgorithm: signatureAlgorithm,
        MessageType: signatureMessageType,
    })
        .promise();
    if (!res.Signature) {
        logging_1.logger.error(`Could not get signature for key ${keyId}`);
        throw Error(`Could not sign message`);
    }
    return res.Signature.toString('base64');
};
exports.signDfnsMessage = signDfnsMessage;
const isValidSignature = async (signature, message, publisherServiceName) => {
    var _a;
    const kms = new aws_sdk_1.KMS();
    const verificationResult = await kms
        .verify({
        KeyId: `alias/${publisherServiceName}-rabbitmq-signing-key`,
        Message: message,
        MessageType: signatureMessageType,
        Signature: Buffer.from(signature, 'base64'),
        SigningAlgorithm: signatureAlgorithm,
    })
        .promise();
    return (_a = verificationResult.SignatureValid) !== null && _a !== void 0 ? _a : false;
};
exports.isValidSignature = isValidSignature;
const getDeadLetterQueueName = (queueName) => {
    return `${queueName}-dlq`;
};
exports.getDeadLetterQueueName = getDeadLetterQueueName;
const getRedeliveredCount = (message) => {
    var _a, _b;
    return parseInt(((_b = (_a = message === null || message === void 0 ? void 0 : message.basicProperties) === null || _a === void 0 ? void 0 : _a.headers) === null || _b === void 0 ? void 0 : _b[constants_1.REDELIVERED_MESSAGE_HEADER]) || '0');
};
exports.getRedeliveredCount = getRedeliveredCount;
//# sourceMappingURL=message-signing.js.map