Skip to content

Commit 63d19f1

Browse files
committed
Use aggregate method for disabling key storage
in matrix-org/matrix-js-sdk#4742
1 parent 24c5431 commit 63d19f1

File tree

1 file changed

+12
-28
lines changed

1 file changed

+12
-28
lines changed

src/components/viewmodels/settings/encryption/KeyStoragePanelViewModel.ts

+12-28
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { useCallback, useEffect, useState } from "react";
99
import { logger } from "matrix-js-sdk/src/logger";
1010

1111
import { useMatrixClientContext } from "../../../../contexts/MatrixClientContext";
12-
import { BACKUP_DISABLED_ACCOUNT_DATA_KEY } from "../../../../DeviceListener";
12+
import DeviceListener, { BACKUP_DISABLED_ACCOUNT_DATA_KEY } from "../../../../DeviceListener";
1313

1414
interface KeyStoragePanelState {
1515
/**
@@ -67,6 +67,12 @@ export function useKeyStoragePanelViewModel(): KeyStoragePanelState {
6767
async (enable: boolean) => {
6868
setPendingValue(enable);
6969
try {
70+
// stop the device listener since enabling or (especially) disabling key storage must be
71+
// done with a sequence of API calls that will put the account in a slightly different
72+
// state each time, so suppress any warning toasts until the process is finished (when
73+
// we'll turn it back on again.)
74+
DeviceListener.sharedInstance().stop();
75+
7076
const crypto = matrixClient.getCrypto();
7177
if (!crypto) {
7278
logger.error("Can't change key backup status: no crypto module available");
@@ -85,34 +91,11 @@ export function useKeyStoragePanelViewModel(): KeyStoragePanelState {
8591
// Set the flag so that EX no longer thinks the user wants backup disabled
8692
await matrixClient.setAccountData(BACKUP_DISABLED_ACCOUNT_DATA_KEY, { disabled: false });
8793
} else {
88-
// Get the key backup version we're using
89-
const info = await crypto.getKeyBackupInfo();
90-
if (!info?.version) {
91-
logger.error("Can't delete key backup version: no version available");
92-
return;
93-
}
94-
95-
// Bye bye backup
96-
await crypto.deleteKeyBackupVersion(info.version);
97-
98-
// also turn off 4S, since this is also storing keys on the server.
99-
// Delete the cross signing keys from secret storage
100-
await matrixClient.deleteAccountData("m.cross_signing.master");
101-
await matrixClient.deleteAccountData("m.cross_signing.self_signing");
102-
await matrixClient.deleteAccountData("m.cross_signing.user_signing");
103-
// and the key backup key (we just turned it off anyway)
104-
await matrixClient.deleteAccountData("m.megolm_backup.v1");
105-
106-
// Delete the key information
107-
const defaultKey = await matrixClient.secretStorage.getDefaultKeyId();
108-
if (defaultKey) {
109-
await matrixClient.deleteAccountData(`m.secret_storage.key.${defaultKey}`);
110-
111-
// ...and the default key pointer
112-
await matrixClient.deleteAccountData("m.secret_storage.default_key");
113-
}
94+
// This method will delete the key backup as well as server side recovery keys and other
95+
// server-side crypto data.
96+
await crypto.disableKeyStorage();
11497

115-
// finally, set a flag to say that the user doesn't want key backup.
98+
// Set a flag to say that the user doesn't want key backup.
11699
// Element X uses this to determine whether to set up automatically,
117100
// so this will stop EX turning it back on spontaneously.
118101
await matrixClient.setAccountData(BACKUP_DISABLED_ACCOUNT_DATA_KEY, { disabled: true });
@@ -121,6 +104,7 @@ export function useKeyStoragePanelViewModel(): KeyStoragePanelState {
121104
await checkStatus();
122105
} finally {
123106
setPendingValue(undefined);
107+
DeviceListener.sharedInstance().start(matrixClient);
124108
}
125109
},
126110
[setPendingValue, checkStatus, matrixClient],

0 commit comments

Comments
 (0)