"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createSignaturePayload = void 0;
const protocol_1 = require("@streamr/protocol");
const utils_1 = require("@streamr/utils");
const trackerless_network_1 = require("@streamr/trackerless-network");
const serializeGroupKey = ({ id, data }) => {
    return JSON.stringify([id, (0, utils_1.binaryToHex)(data)]);
};
const createSignaturePayload = (opts) => {
    if (opts.signatureType == protocol_1.SignatureType.SECP256K1) {
        const header = Buffer.concat([
            Buffer.from(`${opts.messageId.streamId}${opts.messageId.streamPartition}${opts.messageId.timestamp}`
                + `${opts.messageId.sequenceNumber}${opts.messageId.publisherId}${opts.messageId.msgChainId}`),
            (opts.prevMsgRef !== undefined) ? Buffer.from(`${opts.prevMsgRef.timestamp}${opts.prevMsgRef.sequenceNumber}`) : new Uint8Array(0)
        ]);
        if (opts.messageType === protocol_1.StreamMessageType.MESSAGE) {
            const newGroupKeyId = opts.newGroupKey ? Buffer.from(opts.newGroupKey.id) : undefined;
            return Buffer.concat([
                header,
                opts.content,
                newGroupKeyId ?? new Uint8Array(0),
                opts.newGroupKey?.data ?? new Uint8Array(0),
            ]);
        }
        else if (opts.messageType === protocol_1.StreamMessageType.GROUP_KEY_REQUEST) {
            // NOTE: this conversion will be removed in the future when we migrate all usages of
            // protocol package's StreamMessage class to the trackerless-network's StreamMessage class
            const request = trackerless_network_1.GroupKeyRequest.fromBinary(opts.content);
            return Buffer.concat([
                header,
                (0, utils_1.utf8ToBinary)(request.requestId),
                request.recipientId,
                request.rsaPublicKey,
                Buffer.concat(request.groupKeyIds.map((k) => (0, utils_1.utf8ToBinary)(k)))
            ]);
        }
        else if (opts.messageType === protocol_1.StreamMessageType.GROUP_KEY_RESPONSE) {
            // NOTE: this conversion will be removed in the future when we migrate all usages of
            // protocol package's StreamMessage class to the trackerless-network's StreamMessage class
            const response = trackerless_network_1.GroupKeyResponse.fromBinary(opts.content);
            return Buffer.concat([
                header,
                (0, utils_1.utf8ToBinary)(response.requestId),
                response.recipientId,
                Buffer.concat(response.groupKeys.map((k) => Buffer.concat([(0, utils_1.utf8ToBinary)(k.id), k.data])))
            ]);
        }
        else {
            throw new Error(`Assertion failed: unknown message type ${opts.messageType}`);
        }
    }
    else if (opts.signatureType === protocol_1.SignatureType.LEGACY_SECP256K1) {
        const prev = ((opts.prevMsgRef !== undefined) ? `${opts.prevMsgRef.timestamp}${opts.prevMsgRef.sequenceNumber}` : '');
        const newGroupKey = ((opts.newGroupKey !== undefined) ? serializeGroupKey(opts.newGroupKey) : '');
        // In the legacy signature type, encrypted content was signed as a hex-encoded string
        const contentAsString = (opts.encryptionType === protocol_1.EncryptionType.NONE)
            ? (0, utils_1.binaryToUtf8)(opts.content)
            : (0, utils_1.binaryToHex)(opts.content);
        return Buffer.from(`${opts.messageId.streamId}${opts.messageId.streamPartition}${opts.messageId.timestamp}${opts.messageId.sequenceNumber}`
            + `${opts.messageId.publisherId}${opts.messageId.msgChainId}${prev}${contentAsString}${newGroupKey}`);
    }
    else {
        throw new Error(`Unsupported SignatureType: ${protocol_1.SignatureType}`);
    }
};
exports.createSignaturePayload = createSignaturePayload;
//# sourceMappingURL=signature.js.map