diff --git a/spec/unit/rust-crypto/rust-crypto.spec.ts b/spec/unit/rust-crypto/rust-crypto.spec.ts index 277c88a74..22640b9bf 100644 --- a/spec/unit/rust-crypto/rust-crypto.spec.ts +++ b/spec/unit/rust-crypto/rust-crypto.spec.ts @@ -850,6 +850,44 @@ describe("RustCrypto", () => { expect(userVerificationStatus.wasCrossSigningVerified()).toBeFalsy(); }); }); + + describe("onUserIdentityUpdated", () => { + it("raises KeyBackupStatus event when identify change", async () => { + const mockHttpApi = new MatrixHttpApi(new TypedEventEmitter(), { + baseUrl: "http://server/", + prefix: "", + onlyData: true, + }); + + const olmMachine = { + getIdentity: jest.fn(), + // Force the backup to be trusted by the olmMachine + verifyBackup: jest.fn().mockResolvedValue({ trusted: jest.fn().mockReturnValue(true) }), + isBackupEnabled: jest.fn().mockReturnValue(true), + getBackupKeys: jest.fn(), + enableBackupV1: jest.fn(), + } as unknown as Mocked; + + const rustCrypto = new RustCrypto( + olmMachine, + mockHttpApi, + testData.TEST_USER_ID, + testData.TEST_DEVICE_ID, + {} as ServerSideSecretStorage, + {} as CryptoCallbacks, + ); + + // Return the key backup + fetchMock.get("path:/_matrix/client/v3/room_keys/version", testData.SIGNED_BACKUP_DATA); + + // Wait for the key backup to be available + const keyBackupStatusPromise = new Promise((resolve) => + rustCrypto.once(CryptoEvent.KeyBackupStatus, resolve), + ); + await rustCrypto.onUserIdentityUpdated(new RustSdkCryptoJs.UserId(testData.TEST_USER_ID)); + expect(await keyBackupStatusPromise).toBe(true); + }); + }); }); /** build a basic RustCrypto instance for testing diff --git a/src/rust-crypto/rust-crypto.ts b/src/rust-crypto/rust-crypto.ts index 997d7d137..46fe69702 100644 --- a/src/rust-crypto/rust-crypto.ts +++ b/src/rust-crypto/rust-crypto.ts @@ -1339,6 +1339,12 @@ export class RustCrypto extends TypedEventEmitter { const newVerification = await this.getUserVerificationStatus(userId.toString()); this.emit(CryptoEvent.UserTrustStatusChanged, userId.toString(), newVerification); + + // If our own user identity has changed, we may now trust the key backup where we did not before. + // So, re-check the key backup status and enable it if available. + if (userId.toString() === this.userId) { + await this.checkKeyBackupAndEnable(); + } } /**