diff --git a/src/e2ee/CryptoClient.ts b/src/e2ee/CryptoClient.ts index a5113412..1e592ac7 100644 --- a/src/e2ee/CryptoClient.ts +++ b/src/e2ee/CryptoClient.ts @@ -294,6 +294,7 @@ export class CryptoClient { * @returns {Promise} Resolves when complete. */ public async enableKeyBackup(info: IKeyBackupInfoRetrieved): Promise { + this.client.on("to_device.decrypted", this.onToDeviceMessage); await this.engine.enableKeyBackup(info); } @@ -302,5 +303,12 @@ export class CryptoClient { */ public disableKeyBackup(): void { this.engine.disableKeyBackup(); + this.client.removeListener("to_device.decrypted", this.onToDeviceMessage); } + + private readonly onToDeviceMessage = (msg: IToDeviceMessage): void => { + if (msg.type === "m.room_key") { + this.engine.machine.backupRoomKeys(); + } + }; } diff --git a/src/e2ee/RustEngine.ts b/src/e2ee/RustEngine.ts index d1b3bd83..d18371db 100644 --- a/src/e2ee/RustEngine.ts +++ b/src/e2ee/RustEngine.ts @@ -123,6 +123,9 @@ export class RustEngine { const keysClaim = await this.machine.getMissingSessions(members); if (keysClaim) { await this.processKeysClaimRequest(keysClaim); + if (this.keyBackupVersion !== undefined) { + await this.machine.backupRoomKeys(); + } } });