1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-25 05:23:13 +03:00

Element-R: implement encryption of outgoing events (#3122)

This PR wires up the Rust-SDK into the event encryption path
This commit is contained in:
Richard van der Hoff
2023-02-03 15:58:50 +00:00
committed by GitHub
parent e492a44dde
commit 05bf6428bc
14 changed files with 598 additions and 25 deletions

View File

@@ -2185,7 +2185,11 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
// importing rust-crypto will download the webassembly, so we delay it until we know it will be
// needed.
const RustCrypto = await import("./rust-crypto");
this.cryptoBackend = await RustCrypto.initRustCrypto(this.http, userId, deviceId);
const rustCrypto = await RustCrypto.initRustCrypto(this.http, userId, deviceId);
this.cryptoBackend = rustCrypto;
// attach the event listeners needed by RustCrypto
this.on(RoomMemberEvent.Membership, rustCrypto.onRoomMembership.bind(rustCrypto));
}
/**
@@ -2608,10 +2612,10 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
* @param room - the room the event is in
*/
public prepareToEncrypt(room: Room): void {
if (!this.crypto) {
if (!this.cryptoBackend) {
throw new Error("End-to-end encryption disabled");
}
this.crypto.prepareToEncrypt(room);
this.cryptoBackend.prepareToEncrypt(room);
}
/**
@@ -4392,11 +4396,11 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
return null;
}
if (!this.isRoomEncrypted(event.getRoomId()!)) {
if (!room || !this.isRoomEncrypted(event.getRoomId()!)) {
return null;
}
if (!this.crypto && this.usingExternalCrypto) {
if (!this.cryptoBackend && this.usingExternalCrypto) {
// The client has opted to allow sending messages to encrypted
// rooms even if the room is encrypted, and we haven't setup
// crypto. This is useful for users of matrix-org/pantalaimon
@@ -4417,13 +4421,11 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
return null;
}
if (!this.crypto) {
throw new Error(
"This room is configured to use encryption, but your client does " + "not support encryption.",
);
if (!this.cryptoBackend) {
throw new Error("This room is configured to use encryption, but your client does not support encryption.");
}
return this.crypto.encryptEvent(event, room);
return this.cryptoBackend.encryptEvent(event, room);
}
/**