"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createSignaturePayload = void 0;
const trackerless_network_1 = require("@streamr/trackerless-network");
const utils_1 = require("@streamr/utils");
const StreamMessage_1 = require("../protocol/StreamMessage");
const createSignaturePayload = (opts) => {
    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 === StreamMessage_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 === StreamMessage_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.publicKey,
            Buffer.concat(request.groupKeyIds.map((k) => (0, utils_1.utf8ToBinary)(k)))
        ]);
    }
    else if (opts.messageType === StreamMessage_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}`);
    }
};
exports.createSignaturePayload = createSignaturePayload;
//# sourceMappingURL=createSignaturePayload.js.map