From 0b0f045f752122229b7571cad73a8eb617621f96 Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Fri, 14 Jan 2022 14:26:08 +0000 Subject: [PATCH] Enable the crypto storage --- src/index.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 1434225..8613e78 100644 --- a/src/index.ts +++ b/src/index.ts @@ -21,6 +21,7 @@ import { MatrixClient, PantalaimonClient, RichConsoleLogger, + RustSdkCryptoStorageProvider, SimpleFsStorageProvider } from "matrix-bot-sdk"; import config from "./config"; @@ -44,13 +45,26 @@ if (config.health.healthz.enabled) { (async function () { const storagePath = path.isAbsolute(config.dataPath) ? config.dataPath : path.join(__dirname, '../', config.dataPath); const storage = new SimpleFsStorageProvider(path.join(storagePath, "bot.json")); + let cryptoStorage: RustSdkCryptoStorageProvider|undefined; + + if (config.encryption.enabled && config.pantalaimon.use) { + throw Error('Cannot enable both pantalaimon and encryption at the same time. Remove one from the config.'); + } + + if (config.encryption.enabled) { + cryptoStorage = new RustSdkCryptoStorageProvider(storagePath); + } let client: MatrixClient; if (config.pantalaimon.use) { const pantalaimon = new PantalaimonClient(config.homeserverUrl, storage); client = await pantalaimon.createClientWithCredentials(config.pantalaimon.username, config.pantalaimon.password); } else { - client = new MatrixClient(config.homeserverUrl, config.accessToken, storage); + client = new MatrixClient(config.homeserverUrl, config.accessToken, storage, cryptoStorage); + if (config.encryption.enabled) { + const joinedRooms = await client.getJoinedRooms(); + await client.crypto.prepare(joinedRooms); + } } patchMatrixClient(); config.RUNTIME.client = client;