diff --git a/src/secret-storage.ts b/src/secret-storage.ts index 52a8aad998..a82f32dfee 100644 --- a/src/secret-storage.ts +++ b/src/secret-storage.ts @@ -318,6 +318,7 @@ export interface ServerSideSecretStorage { /** * Set the default key ID for encrypting secrets. + * * If keyId is `null`, the default key id value in the account data will be set to an empty object. * This is considered as "disabling" the default key. * @@ -367,19 +368,26 @@ export class ServerSideSecretStorageImpl implements ServerSideSecretStorage { public setDefaultKeyId(keyId: string | null): Promise { return new Promise((resolve, reject) => { const listener = (ev: MatrixEvent): void => { + if (ev.getType() === "m.secret_storage.default_key") { + // Different account data item + return; + } + const content = ev.getContent(); + // If keyId === null, the content should be an empty object. + // Otherwise, keyId should be returned in the content object. const isSameKey = keyId === null ? !Object.keys(content).length : content.key === keyId; - if (ev.getType() === "m.secret_storage.default_key" && isSameKey) { + if (isSameKey) { this.accountDataAdapter.removeListener(ClientEvent.AccountData, listener); resolve(); } }; this.accountDataAdapter.on(ClientEvent.AccountData, listener); - // The spec says that the key should be an object with a `key` property - // https://spec.matrix.org/v1.13/client-server-api/#key-storage - // To delete the default key, we send an empty object like the rust sdk does - // (see https://docs.rs/matrix-sdk/latest/matrix_sdk/encryption/recovery/struct.Recovery.html#method.reset_identity) + // The spec [1] says that the value of the account data entry should be an object with a `key` property. + // It doesn't specify how to delete the default key; we do it by setting the account data to an empty object. + // + // [1]: https://spec.matrix.org/v1.13/client-server-api/#key-storage const newValue: Record | { key: string } = keyId === null ? {} : { key: keyId }; this.accountDataAdapter.setAccountData("m.secret_storage.default_key", newValue).catch((e) => { this.accountDataAdapter.removeListener(ClientEvent.AccountData, listener);