You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用@signalapp/libsignal-client时收到PreKeySignalMessage而非SignalMessage的问题

在TypeScript中使用@signalapp/libsignal-client实现Signal协议时的问题

我正尝试在TypeScript中实现Signal协议,使用*@signalapp/libsignal-client库,但找不到相关实现文档。当前核心问题是:接收端始终收到PreKeySignalMessage*,而我预期应该收到SignalMessage。

相关代码

发送群消息函数

async function sendGroupMessage(
    senderId: string,
    groupMembers: string[],
    message: string,
    sessionStore: SessionStore,
    identityStore: IdentityKeyStore
): Promise<Map<string, CiphertextMessage>> {
    const encryptedMessages = new Map<string, CiphertextMessage>();

    for (const memberId of groupMembers) {
        if (memberId !== senderId) {
            const address = ProtocolAddress.new(memberId, 1);

            const encryptedMessage = await signalEncrypt(
                Buffer.from(message, 'utf-8'),
                address,
                sessionStore,
                identityStore,
                new Date()
            );
            encryptedMessages.set(memberId, encryptedMessage);
        }
    }

    return encryptedMessages;
}

接收群消息函数

async function receiveGroupMessage(
    receiverId: string,
    senderId: string,
    encryptedMessage: CiphertextMessage,
    sessionStore: SessionStore,
    identityStore: IdentityKeyStore
): Promise<string> {
    const address = ProtocolAddress.new(senderId, 1);

    const messageType = encryptedMessage.type();
    switch (messageType) {
        case CiphertextMessageType.Whisper: {
            const signalMessage = SignalMessage.deserialize(encryptedMessage.serialize());
            const decryptedContent = await signalDecrypt(
                signalMessage,
                address,
                sessionStore,
                identityStore
            );
            return decryptedContent.toString('utf-8');
        }
        case CiphertextMessageType.PreKey: {
            console.error("Unexpected PreKeySignalMessage received. The session might not have been established correctly.");
            return "Error: PreKeySignalMessage received instead of SignalMessage.";
        }
        default:
            throw new Error(`Unsupported message type: ${messageType}`);
    }
}

运行环境

Node.js 18+

内容的提问来源于stack exchange,提问作者alisterion

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 07:44:54