Minor cleanups to initialiseDehydration (#29261)

* dehydration: fix documentation

* initialiseDehydration: improve name

... to make it clearer that it does nothing if dehydration is disabled

* initialiseDehydration: remove dependency on MatrixClientPeg

We're trying to move away from relying on `MatrixClientPeg` everywhere, and
this is a particularly easy win.
This commit is contained in:
Richard van der Hoff 2025-02-14 10:59:02 +00:00 committed by GitHub
parent c47ce59478
commit 09db599fe0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 12 additions and 11 deletions

View File

@ -41,7 +41,7 @@ import PlatformPeg from "./PlatformPeg";
import { formatList } from "./utils/FormattingUtils";
import SdkConfig from "./SdkConfig";
import { setDeviceIsolationMode } from "./settings/controllers/DeviceIsolationModeController.ts";
import { initialiseDehydration } from "./utils/device/dehydration";
import { initialiseDehydrationIfEnabled } from "./utils/device/dehydration";
export interface IMatrixClientCreds {
homeserverUrl: string;
@ -347,7 +347,7 @@ class MatrixClientPegClass implements IMatrixClientPeg {
// is a new login, we will start dehydration after Secret Storage is
// unlocked.
try {
await initialiseDehydration({ onlyIfKeyCached: true, rehydrate: false }, this.matrixClient);
await initialiseDehydrationIfEnabled(this.matrixClient, { onlyIfKeyCached: true, rehydrate: false });
} catch (e) {
// We may get an error dehydrating, such as if cross-signing and
// SSSS are not set up yet. Just log the error and continue.

View File

@ -37,7 +37,7 @@ import Spinner from "../../../../components/views/elements/Spinner";
import InteractiveAuthDialog from "../../../../components/views/dialogs/InteractiveAuthDialog";
import { type IValidationResult } from "../../../../components/views/elements/Validation";
import PassphraseConfirmField from "../../../../components/views/auth/PassphraseConfirmField";
import { initialiseDehydration } from "../../../../utils/device/dehydration";
import { initialiseDehydrationIfEnabled } from "../../../../utils/device/dehydration";
// I made a mistake while converting this and it has to be fixed!
enum Phase {
@ -273,7 +273,7 @@ export default class CreateSecretStorageDialog extends React.PureComponent<IProp
setupNewKeyBackup: !backupInfo,
});
}
await initialiseDehydration({ createNewKey: true });
await initialiseDehydrationIfEnabled(cli, { createNewKey: true });
this.setState({
phase: Phase.Stored,

View File

@ -20,7 +20,7 @@ import { type Device, type SecretStorage } from "matrix-js-sdk/src/matrix";
import { MatrixClientPeg } from "../MatrixClientPeg";
import { AccessCancelledError, accessSecretStorage } from "../SecurityManager";
import { asyncSome } from "../utils/arrays";
import { initialiseDehydration } from "../utils/device/dehydration";
import { initialiseDehydrationIfEnabled } from "../utils/device/dehydration";
export enum Phase {
Loading = 0,
@ -149,7 +149,7 @@ export class SetupEncryptionStore extends EventEmitter {
);
resolve();
await initialiseDehydration();
await initialiseDehydrationIfEnabled(cli);
if (backupInfo) {
await cli.getCrypto()?.loadSessionBackupPrivateKeyFromSecretStorage();

View File

@ -10,7 +10,6 @@ import { logger } from "matrix-js-sdk/src/logger";
import { type CryptoApi, type StartDehydrationOpts } from "matrix-js-sdk/src/crypto-api";
import type { MatrixClient } from "matrix-js-sdk/src/matrix";
import { MatrixClientPeg } from "../../MatrixClientPeg";
/**
* Check if device dehydration is enabled.
@ -38,11 +37,13 @@ async function deviceDehydrationEnabled(client: MatrixClient, crypto: CryptoApi
* the configuration), rehydrate a device (if available) and create
* a new dehydrated device.
*
* @param createNewKey: force a new dehydration key to be created, even if one
* already exists. This is used when we reset secret storage.
* @param client - MatrixClient to use for the operation
* @param opts - options for the startDehydration operation, if one is performed.
*/
export async function initialiseDehydration(opts: StartDehydrationOpts = {}, client?: MatrixClient): Promise<void> {
client = client || MatrixClientPeg.safeGet();
export async function initialiseDehydrationIfEnabled(
client: MatrixClient,
opts: StartDehydrationOpts = {},
): Promise<void> {
const crypto = client.getCrypto();
if (await deviceDehydrationEnabled(client, crypto)) {
logger.log("Device dehydration enabled");