Skip to content
This repository was archived by the owner on Oct 22, 2024. It is now read-only.

Commit 9420138

Browse files
committed
add setting to enable invisible crypto mode in js-sdk
1 parent 75918f5 commit 9420138

File tree

5 files changed

+75
-0
lines changed

5 files changed

+75
-0
lines changed

src/MatrixClientPeg.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
import { VerificationMethod } from "matrix-js-sdk/src/types";
2525
import * as utils from "matrix-js-sdk/src/utils";
2626
import { logger } from "matrix-js-sdk/src/logger";
27+
import { CryptoMode } from "matrix-js-sdk/src/crypto-api";
2728

2829
import createMatrixClient from "./utils/createMatrixClient";
2930
import SettingsStore from "./settings/SettingsStore";
@@ -343,6 +344,10 @@ class MatrixClientPegClass implements IMatrixClientPeg {
343344
});
344345

345346
StorageManager.setCryptoInitialised(true);
347+
348+
if (SettingsStore.getValue("feature_invisible_crypto")) {
349+
this.matrixClient.getCrypto()!.setCryptoMode(CryptoMode.Invisible);
350+
}
346351
// TODO: device dehydration and whathaveyou
347352
return;
348353
}

src/i18n/strings/en_EN.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,6 +1444,8 @@
14441444
"group_widgets": "Widgets",
14451445
"hidebold": "Hide notification dot (only display counters badges)",
14461446
"html_topic": "Show HTML representation of room topics",
1447+
"invisible_crypto": "Invisible cryptography",
1448+
"invisible_crypto_description": "Invisible cryptography is an experimental cryptography mode that uses cross-signing to provide a cleaner cryptography experience. If you enable this mode, you may be unable to communicate with users who have not cross-signed their devices.",
14471449
"join_beta": "Join the beta",
14481450
"join_beta_reload": "Joining the beta will reload %(brand)s.",
14491451
"jump_to_date": "Jump to date (adds /jumptodate and jump to date headers)",

src/settings/Settings.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Please see LICENSE files in the repository root for full details.
1010
import React, { ReactNode } from "react";
1111

1212
import { _t, _td, TranslationKey } from "../languageHandler";
13+
import InvisibleCryptoController from "./controllers/InvisibleCryptoController";
1314
import {
1415
NotificationBodyEnabledController,
1516
NotificationsEnabledController,
@@ -316,6 +317,16 @@ export const SETTINGS: { [setting: string]: ISetting } = {
316317
supportedLevelsAreOrdered: true,
317318
default: false,
318319
},
320+
"feature_invisible_crypto": {
321+
isFeature: true,
322+
labsGroup: LabGroup.Encryption,
323+
controller: new InvisibleCryptoController(),
324+
displayName: _td("labs|invisible_crypto"),
325+
description: _td("labs|invisible_crypto_description"),
326+
supportedLevels: LEVELS_DEVICE_ONLY_SETTINGS_WITH_CONFIG_PRIORITISED,
327+
supportedLevelsAreOrdered: true,
328+
default: false,
329+
},
319330
"useOnlyCurrentProfiles": {
320331
supportedLevels: LEVELS_ACCOUNT_SETTINGS,
321332
displayName: _td("settings|disable_historical_profile"),
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
Copyright 2024 New Vector Ltd.
3+
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
4+
Please see LICENSE files in the repository root for full details.
5+
*/
6+
7+
import { CryptoMode } from "matrix-js-sdk/src/crypto-api";
8+
9+
import SettingController from "./SettingController";
10+
import { MatrixClientPeg } from "../../MatrixClientPeg";
11+
import { SettingLevel } from "../SettingLevel";
12+
13+
export default class InvisibleCryptoController extends SettingController {
14+
public onChange(level: SettingLevel, roomId: string, newValue: any): void {
15+
const crypto = MatrixClientPeg.safeGet().getCrypto();
16+
if (crypto) {
17+
crypto.setCryptoMode(newValue ? CryptoMode.Invisible : CryptoMode.Legacy);
18+
}
19+
}
20+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Copyright 2024 New Vector Ltd.
3+
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
4+
Please see LICENSE files in the repository root for full details.
5+
*/
6+
7+
import { CryptoApi, CryptoMode } from "matrix-js-sdk/src/crypto-api";
8+
9+
import { MatrixClientPeg } from "../../../src/MatrixClientPeg";
10+
import { stubClient } from "../../test-utils";
11+
import InvisibleCryptoController from "../../../src/settings/controllers/InvisibleCryptoController";
12+
import { SettingLevel } from "../../../src/settings/SettingLevel";
13+
14+
describe("InvisibleCryptoController", () => {
15+
afterEach(() => {
16+
jest.clearAllMocks();
17+
});
18+
19+
it("tracks enabling and disabling", () => {
20+
const cli = stubClient();
21+
const setCryptoModeMock = jest.fn();
22+
cli.getCrypto = jest.fn(() => {
23+
return {
24+
setCryptoMode: setCryptoModeMock,
25+
} as unknown as CryptoApi;
26+
});
27+
jest.spyOn(MatrixClientPeg, "safeGet").mockReturnValue(cli);
28+
29+
const controller = new InvisibleCryptoController();
30+
31+
controller.onChange(SettingLevel.DEVICE, "", true);
32+
expect(setCryptoModeMock).toHaveBeenCalledWith(CryptoMode.Invisible);
33+
34+
controller.onChange(SettingLevel.DEVICE, "", false);
35+
expect(setCryptoModeMock).toHaveBeenCalledWith(CryptoMode.Legacy);
36+
});
37+
});

0 commit comments

Comments
 (0)