diff --git a/spec/integ/crypto/verification.spec.ts b/spec/integ/crypto/verification.spec.ts index 668641aea..d95097fcf 100644 --- a/spec/integ/crypto/verification.spec.ts +++ b/spec/integ/crypto/verification.spec.ts @@ -1259,14 +1259,11 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st const requestId = await requestPromises.get("m.megolm_backup.v1"); + const keyBackupIsCached = emitPromise(aliceClient, CryptoEvent.KeyBackupDecryptionKeyCached); + await sendBackupGossipAndExpectVersion(requestId!, BACKUP_DECRYPTION_KEY_BASE64, matchingBackupInfo); - // We are lacking a way to signal that the secret has been received, so we wait a bit.. - jest.useRealTimers(); - await new Promise((resolve) => { - setTimeout(resolve, 500); - }); - jest.useFakeTimers(); + await keyBackupIsCached; // the backup secret should be cached const cachedKey = await aliceClient.getCrypto()!.getSessionBackupPrivateKey(); diff --git a/src/client.ts b/src/client.ts index 4121314ec..99862b55d 100644 --- a/src/client.ts +++ b/src/client.ts @@ -951,6 +951,7 @@ type CryptoEvents = | CryptoEvent.KeyBackupStatus | CryptoEvent.KeyBackupFailed | CryptoEvent.KeyBackupSessionsRemaining + | CryptoEvent.KeyBackupDecryptionKeyCached | CryptoEvent.RoomKeyRequest | CryptoEvent.RoomKeyRequestCancellation | CryptoEvent.VerificationRequest @@ -2359,6 +2360,7 @@ export class MatrixClient extends TypedEventEmitter void; [CryptoEvent.KeyBackupFailed]: (errcode: string) => void; [CryptoEvent.KeyBackupSessionsRemaining]: (remaining: number) => void; + + /** + * Fires when the backup decryption key is received and cached. + * + * @param version - The version of the backup for which we have the key for. + */ + [CryptoEvent.KeyBackupDecryptionKeyCached]: (version: string) => void; [CryptoEvent.KeySignatureUploadFailure]: ( failures: IUploadKeySignaturesResponse["failures"], source: "checkOwnCrossSigningTrust" | "afterCrossSigningLocalKeyChange" | "setDeviceVerification", diff --git a/src/rust-crypto/backup.ts b/src/rust-crypto/backup.ts index 6a492b657..f8c23e19d 100644 --- a/src/rust-crypto/backup.ts +++ b/src/rust-crypto/backup.ts @@ -154,8 +154,7 @@ export class RustBackupManager extends TypedEventEmitter { + await this.olmMachine.saveBackupDecryptionKey(backupDecryptionKey, version); + // Emit an event that we have a new backup decryption key, so that the sdk can start + // importing keys from backup if needed. + this.emit(CryptoEvent.KeyBackupDecryptionKeyCached, version); + } + private keyBackupCheckInProgress: Promise | null = null; /** Helper for `checkKeyBackup` */ @@ -393,7 +402,7 @@ export class RustBackupManager extends TypedEventEmitter void; [CryptoEvent.KeyBackupSessionsRemaining]: (remaining: number) => void; [CryptoEvent.KeyBackupFailed]: (errCode: string) => void; + [CryptoEvent.KeyBackupDecryptionKeyCached]: (version: string) => void; }; diff --git a/src/rust-crypto/rust-crypto.ts b/src/rust-crypto/rust-crypto.ts index b16c0c51a..9e8694ef0 100644 --- a/src/rust-crypto/rust-crypto.ts +++ b/src/rust-crypto/rust-crypto.ts @@ -150,6 +150,7 @@ export class RustCrypto extends TypedEventEmitter void; + + [CryptoEvent.KeyBackupDecryptionKeyCached]: (version: string) => void; } & RustBackupCryptoEventMap;