From 2a194224f6adb4f8f1d269ee624e234695f7e3e2 Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Tue, 12 Nov 2024 11:25:59 +0100 Subject: [PATCH 1/2] Add `CryptoApi.getBackupInfo` --- spec/unit/crypto/backup.spec.ts | 8 ++++++++ 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, 45 insertions(+), 1 deletion(-) diff --git a/spec/unit/crypto/backup.spec.ts b/spec/unit/crypto/backup.spec.ts index 2d655a9011..28bb0d4c44 100644 --- a/spec/unit/crypto/backup.spec.ts +++ b/spec/unit/crypto/backup.spec.ts @@ -780,4 +780,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. * From ccc07bb44d2c7e4b6860301573d6e96de7a0a33e Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Wed, 13 Nov 2024 14:24:51 +0100 Subject: [PATCH 2/2] improve doc --- src/crypto-api/index.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/crypto-api/index.ts b/src/crypto-api/index.ts index 8461686964..89ece07acb 100644 --- a/src/crypto-api/index.ts +++ b/src/crypto-api/index.ts @@ -535,7 +535,11 @@ export interface CryptoApi { isKeyBackupTrusted(info: KeyBackupInfo): Promise; /** - * Get information about the current key backup. + * Return the details of the latest backup on the server, when we last checked. + * + * This normally returns a cached value, but if we haven't yet made a request to the server, it will fire one off. + * It will always return the details of the active backup if key backup is enabled. + * * Return null if there is no backup. * * @returns the key backup information