Skip to content

Commit 6b58448

Browse files
committed
Begin to use cryptoBackend in a few places
1 parent 1f021ed commit 6b58448

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

spec/unit/room.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2890,7 +2890,7 @@ describe("Room", function() {
28902890

28912891
it("should load pending events from from the store and decrypt if needed", async () => {
28922892
const client = new TestClient(userA).client;
2893-
client.crypto = {
2893+
client.crypto = client['cryptoBackend'] = {
28942894
decryptEvent: jest.fn().mockResolvedValue({ clearEvent: { body: "enc" } }),
28952895
} as unknown as Crypto;
28962896
client.store.getPendingEvents = jest.fn(async roomId => [{

src/client.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ import { UIARequest, UIAResponse } from "./@types/uia";
215215
import { LocalNotificationSettings } from "./@types/local_notifications";
216216
import { UNREAD_THREAD_NOTIFICATIONS } from "./@types/sync";
217217
import { buildFeatureSupportMap, Feature, ServerSupport } from "./feature";
218+
import { CryptoBackend } from "./common-crypto/CryptoBackend";
218219

219220
export type Store = IStore;
220221

@@ -1142,7 +1143,8 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
11421143
public urlPreviewCache: { [key: string]: Promise<IPreviewUrlResponse> } = {};
11431144
public identityServer?: IIdentityServerProvider;
11441145
public http: MatrixHttpApi<IHttpOpts & { onlyData: true }>; // XXX: Intended private, used in code.
1145-
public crypto?: Crypto; // XXX: Intended private, used in code.
1146+
public crypto?: Crypto; // libolm crypto implementation. XXX: Intended private, used in code. Being replaced by cryptoBackend
1147+
private cryptoBackend?: CryptoBackend; // one of crypto or rustCrypto
11461148
public cryptoCallbacks: ICryptoCallbacks; // XXX: Intended private, used in code.
11471149
public callEventHandler?: CallEventHandler; // XXX: Intended private, used in code.
11481150
public groupCallEventHandler?: GroupCallEventHandler;
@@ -1449,7 +1451,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
14491451
* clean shutdown.
14501452
*/
14511453
public stopClient(): void {
1452-
this.crypto?.stop(); // crypto might have been initialised even if the client wasn't fully started
1454+
this.cryptoBackend?.stop(); // crypto might have been initialised even if the client wasn't fully started
14531455

14541456
if (!this.clientRunning) return; // already stopped
14551457

@@ -1954,7 +1956,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
19541956
}
19551957

19561958
/**
1957-
* Initialise support for end-to-end encryption in this client
1959+
* Initialise support for end-to-end encryption in this client, using libolm.
19581960
*
19591961
* You should call this method after creating the matrixclient, but *before*
19601962
* calling `startClient`, if you want to support end-to-end encryption.
@@ -1970,7 +1972,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
19701972
);
19711973
}
19721974

1973-
if (this.crypto) {
1975+
if (this.cryptoBackend) {
19741976
logger.warn("Attempt to re-initialise e2e encryption on MatrixClient");
19751977
return;
19761978
}
@@ -2035,7 +2037,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
20352037

20362038
// if crypto initialisation was successful, tell it to attach its event handlers.
20372039
crypto.registerEventHandlers(this as Parameters<Crypto["registerEventHandlers"]>[0]);
2038-
this.crypto = crypto;
2040+
this.cryptoBackend = this.crypto = crypto;
20392041

20402042
// upload our keys in the background
20412043
this.crypto.uploadDeviceKeys().catch((e) => {
@@ -2049,7 +2051,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
20492051
* @returns True if end-to-end is enabled.
20502052
*/
20512053
public isCryptoEnabled(): boolean {
2052-
return !!this.crypto;
2054+
return !!this.cryptoBackend;
20532055
}
20542056

20552057
/**
@@ -2297,21 +2299,21 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
22972299
* @param value - whether to blacklist all unverified devices by default
22982300
*/
22992301
public setGlobalBlacklistUnverifiedDevices(value: boolean): boolean {
2300-
if (!this.crypto) {
2302+
if (!this.cryptoBackend) {
23012303
throw new Error("End-to-end encryption disabled");
23022304
}
2303-
this.crypto.globalBlacklistUnverifiedDevices = value;
2305+
this.cryptoBackend.globalBlacklistUnverifiedDevices = value;
23042306
return value;
23052307
}
23062308

23072309
/**
23082310
* @returns whether to blacklist all unverified devices by default
23092311
*/
23102312
public getGlobalBlacklistUnverifiedDevices(): boolean {
2311-
if (!this.crypto) {
2313+
if (!this.cryptoBackend) {
23122314
throw new Error("End-to-end encryption disabled");
23132315
}
2314-
return this.crypto.globalBlacklistUnverifiedDevices;
2316+
return this.cryptoBackend.globalBlacklistUnverifiedDevices;
23152317
}
23162318

23172319
/**
@@ -2325,10 +2327,10 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
23252327
* @param value - whether error on unknown devices
23262328
*/
23272329
public setGlobalErrorOnUnknownDevices(value: boolean): void {
2328-
if (!this.crypto) {
2330+
if (!this.cryptoBackend) {
23292331
throw new Error("End-to-end encryption disabled");
23302332
}
2331-
this.crypto.globalErrorOnUnknownDevices = value;
2333+
this.cryptoBackend.globalErrorOnUnknownDevices = value;
23322334
}
23332335

23342336
/**
@@ -2337,10 +2339,10 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
23372339
* This API is currently UNSTABLE and may change or be removed without notice.
23382340
*/
23392341
public getGlobalErrorOnUnknownDevices(): boolean {
2340-
if (!this.crypto) {
2342+
if (!this.cryptoBackend) {
23412343
throw new Error("End-to-end encryption disabled");
23422344
}
2343-
return this.crypto.globalErrorOnUnknownDevices;
2345+
return this.cryptoBackend.globalErrorOnUnknownDevices;
23442346
}
23452347

23462348
/**
@@ -2480,10 +2482,10 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
24802482
* the cross-signing pseudo-device.
24812483
*/
24822484
public userHasCrossSigningKeys(): Promise<boolean> {
2483-
if (!this.crypto) {
2485+
if (!this.cryptoBackend) {
24842486
throw new Error("End-to-end encryption disabled");
24852487
}
2486-
return this.crypto.userHasCrossSigningKeys();
2488+
return this.cryptoBackend.userHasCrossSigningKeys();
24872489
}
24882490

24892491
/**
@@ -7216,7 +7218,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
72167218
*/
72177219
public decryptEventIfNeeded(event: MatrixEvent, options?: IDecryptOptions): Promise<void> {
72187220
if (event.shouldAttemptDecryption() && this.isCryptoEnabled()) {
7219-
event.attemptDecryption(this.crypto!, options);
7221+
event.attemptDecryption(this.cryptoBackend!, options);
72207222
}
72217223

72227224
if (event.isBeingDecrypted()) {

src/models/event.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { MatrixError } from "../http-api";
3535
import { TypedEventEmitter } from "./typed-event-emitter";
3636
import { EventStatus } from "./event-status";
3737
import { DecryptionError } from "../crypto/algorithms";
38+
import { CryptoBackend } from "../common-crypto/CryptoBackend";
3839

3940
export { EventStatus } from "./event-status";
4041

@@ -729,7 +730,7 @@ export class MatrixEvent extends TypedEventEmitter<MatrixEventEmittedEvents, Mat
729730
* @returns promise which resolves (to undefined) when the decryption
730731
* attempt is completed.
731732
*/
732-
public async attemptDecryption(crypto: Crypto, options: IDecryptOptions = {}): Promise<void> {
733+
public async attemptDecryption(crypto: CryptoBackend, options: IDecryptOptions = {}): Promise<void> {
733734
// start with a couple of sanity checks.
734735
if (!this.isEncrypted()) {
735736
throw new Error("Attempt to decrypt event which isn't encrypted");
@@ -805,7 +806,7 @@ export class MatrixEvent extends TypedEventEmitter<MatrixEventEmittedEvents, Mat
805806
return recipients;
806807
}
807808

808-
private async decryptionLoop(crypto: Crypto, options: IDecryptOptions = {}): Promise<void> {
809+
private async decryptionLoop(crypto: CryptoBackend, options: IDecryptOptions = {}): Promise<void> {
809810
// make sure that this method never runs completely synchronously.
810811
// (doing so would mean that we would clear decryptionPromise *before*
811812
// it is set in attemptDecryption - and hence end up with a stuck

0 commit comments

Comments
 (0)