From 34f97852809e40a04059eebc34cd5d13ba3f7160 Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Tue, 12 Nov 2024 11:25:59 +0100 Subject: [PATCH] Add `CryptoApi.getBackupInfo` --- spec/unit/crypto/backup.spec.ts | 9 +++++++++ spec/unit/rust-crypto/rust-crypto.spec.ts | 14 ++++++++++++++ src/client.ts | 2 +- src/crypto-api/index.ts | 8 ++++++++ src/crypto/index.ts | 7 +++++++ src/rust-crypto/rust-crypto.ts | 7 +++++++ 6 files changed, 46 insertions(+), 1 deletion(-) diff --git a/spec/unit/crypto/backup.spec.ts b/spec/unit/crypto/backup.spec.ts index 2d655a9011..7b0136971c 100644 --- a/spec/unit/crypto/backup.spec.ts +++ b/spec/unit/crypto/backup.spec.ts @@ -16,6 +16,7 @@ limitations under the License. */ import "../../olm-loader"; + import { logger } from "../../../src/logger"; import * as olmlib from "../../../src/crypto/olmlib"; import { MatrixClient } from "../../../src/client"; @@ -780,4 +781,12 @@ describe("MegolmBackup", function () { client.stopClient(); }); }); + + describe("getKeyBackupInfo", () => { + it("should return throw an `Not implemented`", async () => { + const client = makeTestClient(cryptoStore); + await client.initCrypto(); + await expect(client.getCrypto()?.getKeyBackupInfo()).rejects.toThrow("Not implemented"); + }); + }); }); diff --git a/spec/unit/rust-crypto/rust-crypto.spec.ts b/spec/unit/rust-crypto/rust-crypto.spec.ts index 117112dd09..8fedadd0d4 100644 --- a/spec/unit/rust-crypto/rust-crypto.spec.ts +++ b/spec/unit/rust-crypto/rust-crypto.spec.ts @@ -1526,6 +1526,20 @@ describe("RustCrypto", () => { failures: 1, }); }); + + describe("getKeyBackupInfo", () => { + it("should return the current key backup info", async () => { + fetchMock.get("path:/_matrix/client/v3/room_keys/version", testData.SIGNED_BACKUP_DATA); + + const rustCrypto = await makeTestRustCrypto(makeMatrixHttpApi()); + await expect(rustCrypto.getKeyBackupInfo()).resolves.toStrictEqual(testData.SIGNED_BACKUP_DATA); + }); + + it("should return null if not available", async () => { + const rustCrypto = await makeTestRustCrypto(makeMatrixHttpApi()); + await expect(rustCrypto.getKeyBackupInfo()).resolves.toBeNull(); + }); + }); }); describe("device dehydration", () => { diff --git a/src/client.ts b/src/client.ts index 6337392bad..999517a107 100644 --- a/src/client.ts +++ b/src/client.ts @@ -3352,7 +3352,7 @@ export class MatrixClient extends TypedEventEmitter { let res: IKeyBackupInfo; diff --git a/src/crypto-api/index.ts b/src/crypto-api/index.ts index 1713bf1a8b..7726ac2450 100644 --- a/src/crypto-api/index.ts +++ b/src/crypto-api/index.ts @@ -516,6 +516,14 @@ export interface CryptoApi { */ isKeyBackupTrusted(info: KeyBackupInfo): Promise; + /** + * Get information about the current key backup. + * Return null if there is no backup. + * + * @returns the key backup information + */ + getKeyBackupInfo(): Promise; + /** * Force a re-check of the key backup and enable/disable it as appropriate. * diff --git a/src/crypto/index.ts b/src/crypto/index.ts index 2e3bef6eec..d562cc30d0 100644 --- a/src/crypto/index.ts +++ b/src/crypto/index.ts @@ -1318,6 +1318,13 @@ export class Crypto extends TypedEventEmitter { + throw new Error("Not implemented"); + } + /** * Determine if a key backup can be trusted. * diff --git a/src/rust-crypto/rust-crypto.ts b/src/rust-crypto/rust-crypto.ts index c2c9b8304f..5b3c760573 100644 --- a/src/rust-crypto/rust-crypto.ts +++ b/src/rust-crypto/rust-crypto.ts @@ -1211,6 +1211,13 @@ export class RustCrypto extends TypedEventEmitter { + return (await this.backupManager.getServerBackupInfo()) || null; + } + /** * Determine if a key backup can be trusted. *