You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-12-05 17:02:07 +03:00
Introduce a mechanism for using the rust-crypto-sdk (#2969)
This PR introduces MatrixClient.initRustCrypto, which is similar to initCrypto, except that it will use the Rust crypto SDK instead of the old libolm-based implementation. This is very much not something you want to use in production code right now, because the integration with the rust sdk is extremely skeletal and almost everything crypto-related will raise an exception rather than doing anything useful. It is, however, enough to demonstrate the loading of the wasmified rust sdk in element web, and a react sdk with light modifications can successfully log in and out. Part of vector-im/element-web#21972.
This commit is contained in:
committed by
GitHub
parent
df42014ef5
commit
15ef8fabb7
@@ -211,6 +211,7 @@ import { LocalNotificationSettings } from "./@types/local_notifications";
|
||||
import { UNREAD_THREAD_NOTIFICATIONS } from "./@types/sync";
|
||||
import { buildFeatureSupportMap, Feature, ServerSupport } from "./feature";
|
||||
import { CryptoBackend } from "./common-crypto/CryptoBackend";
|
||||
import { RUST_SDK_STORE_PREFIX } from "./rust-crypto/constants";
|
||||
|
||||
export type Store = IStore;
|
||||
|
||||
@@ -1657,6 +1658,41 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
||||
if (this.cryptoStore) {
|
||||
promises.push(this.cryptoStore.deleteAllData());
|
||||
}
|
||||
|
||||
// delete the stores used by the rust matrix-sdk-crypto, in case they were used
|
||||
const deleteRustSdkStore = async (): Promise<void> => {
|
||||
let indexedDB: IDBFactory;
|
||||
try {
|
||||
indexedDB = global.indexedDB;
|
||||
} catch (e) {
|
||||
// No indexeddb support
|
||||
return;
|
||||
}
|
||||
for (const dbname of [
|
||||
`${RUST_SDK_STORE_PREFIX}::matrix-sdk-crypto`,
|
||||
`${RUST_SDK_STORE_PREFIX}::matrix-sdk-crypto-meta`,
|
||||
]) {
|
||||
const prom = new Promise((resolve, reject) => {
|
||||
logger.info(`Removing IndexedDB instance ${dbname}`);
|
||||
const req = indexedDB.deleteDatabase(dbname);
|
||||
req.onsuccess = (_): void => {
|
||||
logger.info(`Removed IndexedDB instance ${dbname}`);
|
||||
resolve(0);
|
||||
};
|
||||
req.onerror = (e): void => {
|
||||
logger.error(`Failed to remove IndexedDB instance ${dbname}: ${e}`);
|
||||
reject(new Error(`Error clearing storage: ${e}`));
|
||||
};
|
||||
req.onblocked = (e): void => {
|
||||
logger.info(`cannot yet remove IndexedDB instance ${dbname}`);
|
||||
//reject(new Error(`Error clearing storage: ${e}`));
|
||||
};
|
||||
});
|
||||
await prom;
|
||||
}
|
||||
};
|
||||
promises.push(deleteRustSdkStore());
|
||||
|
||||
return Promise.all(promises).then(); // .then to fix types
|
||||
}
|
||||
|
||||
@@ -2065,6 +2101,46 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise support for end-to-end encryption in this client, using the rust matrix-sdk-crypto.
|
||||
*
|
||||
* An alternative to {@link initCrypto}.
|
||||
*
|
||||
* *WARNING*: this API is very experimental, should not be used in production, and may change without notice!
|
||||
* Eventually it will be deprecated and `initCrypto` will do the same thing.
|
||||
*
|
||||
* @experimental
|
||||
*
|
||||
* @returns a Promise which will resolve when the crypto layer has been
|
||||
* successfully initialised.
|
||||
*/
|
||||
public async initRustCrypto(): Promise<void> {
|
||||
if (this.cryptoBackend) {
|
||||
logger.warn("Attempt to re-initialise e2e encryption on MatrixClient");
|
||||
return;
|
||||
}
|
||||
|
||||
const userId = this.getUserId();
|
||||
if (userId === null) {
|
||||
throw new Error(
|
||||
`Cannot enable encryption on MatrixClient with unknown userId: ` +
|
||||
`ensure userId is passed in createClient().`,
|
||||
);
|
||||
}
|
||||
const deviceId = this.getDeviceId();
|
||||
if (deviceId === null) {
|
||||
throw new Error(
|
||||
`Cannot enable encryption on MatrixClient with unknown deviceId: ` +
|
||||
`ensure deviceId is passed in createClient().`,
|
||||
);
|
||||
}
|
||||
|
||||
// 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(userId, deviceId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Is end-to-end crypto enabled for this client.
|
||||
* @returns True if end-to-end is enabled.
|
||||
|
||||
Reference in New Issue
Block a user