turt2live / matrix-bot-sdk

TypeScript/JavaScript SDK for Matrix bots
MIT License
199 stars 69 forks source link

Encrypted rooms does not works #363

Closed vitonsky closed 5 months ago

vitonsky commented 5 months ago

Describe the bug

Decryption of messages in encrypted rooms always end with error Error: Can't find the room key to decrypt the event

To Reproduce

Write example code:

import { MatrixAuth, MatrixClient, RustSdkCryptoStorageProvider, SimpleFsStorageProvider } from 'matrix-bot-sdk';

(async () => {
    const storageProvider = new SimpleFsStorageProvider("./.local/matrix-bot/bot.json"); // or any other IStorageProvider
    const cryptoProvider = new RustSdkCryptoStorageProvider("./.local/matrix-bot/rustCrypto");

    const homeServerUrl = 'https://matrix.org'; // where the bot can reach the homeserver at

    const auth = new MatrixAuth(homeServerUrl);
    const accessToken = await auth.passwordLogin(LOGIN, PASSWORD).then((client) => client.accessToken);
    const client = new MatrixClient(homeServerUrl, accessToken, storageProvider, cryptoProvider);

    // log messages
    client.on("room.message", (_roomId: string, event) => {
        if (!event['content']?.['msgtype']) return;

        // handle message here. It'll be decrypted already.
        console.log("Message event", event);
    });

    // auto join
    client.on("room.invite", (roomId: string, inviteEvent: any) => {
        console.log('inviteEvent', inviteEvent);
        client.joinRoom(roomId);
    });

    // This will set up crypto if needed and prepare the client for automatically decrypting and encrypting messages. Simply
    // use the client like you would without encryption and it should just work.
    client.start().then(() => console.log("Bot started!"));
})();

Steps to reproduce the behavior:

Expected behavior

Actual behavior

Error looks like that:

MatrixHttpClient (REQ-48) { errcode: 'M_NOT_FOUND', error: 'Event not found.' }
MatrixClientLite Decryption error on SECRET_ID_HERE:matrix.org SECRET_ID_HERE [Error: Can't find the room key to decrypt the event] {
  code: 'GenericFailure'
}

Additional context Messages appears well in case encryption is disabled

turt2live commented 5 months ago

The bot-sdk can't handle keys from before its join event.

vitonsky commented 5 months ago

The bot-sdk can't handle keys from before its join event.

How i can change code to make encryption work?

It looks bot-sdk have bug independent of join event.

alexgaill commented 3 months ago

The bot-sdk can't handle keys from before its join event.

This behavior not seems to be the right one. this mean that if i want to offer the bot services to everyone in my company I have to ask bot to create a room and invite everybody?

thanks for your answer.