You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -303,44 +303,153 @@ Then visit `http://localhost:8005` to see the API docs.
303
303
304
304
# End-to-end encryption support
305
305
306
-
**This section is outdated.** Use of `libolm` is deprecated and we are replacing it with support
307
-
from the matrix-rust-sdk (https://github.com/element-hq/element-web/issues/21972).
306
+
The matrix-js-sdk uses underneath the [matrix-sdk-crypto-wasm bindings](https://github.com/matrix-org/matrix-rust-sdk-crypto-wasm) of the [matrix-rust-sdk](https://github.com/matrix-org/matrix-rust-sdk/) to provide end-to-end encryption support.
308
307
309
-
The SDK supports end-to-end encryption via the Olm and Megolm protocols, using
310
-
[libolm](https://gitlab.matrix.org/matrix-org/olm). It is left up to the
311
-
application to make libolm available, via the `Olm` global.
308
+
## Initialization
312
309
313
-
It is also necessary to call `await matrixClient.initCrypto()` after creating a new
314
-
`MatrixClient` (but **before** calling `matrixClient.startClient()`) to
315
-
initialise the crypto layer.
310
+
**Do not use `matrixClient.initCrypto()`. This method is deprecated and no longer maintained.**
316
311
317
-
If the `Olm` global is not available, the SDK will show a warning, as shown
318
-
below; `initCrypto()` will also fail.
312
+
To initialize the end-to-end encryption support in the matrix client:
319
313
314
+
```javascript
315
+
// Create a new matrix client
316
+
constmatrixClient=sdk.createClient({
317
+
baseUrl:"http://localhost:8008",
318
+
accessToken: myAccessToken,
319
+
userId: myUserId,
320
+
});
321
+
322
+
// Initialize to enable end-to-end encryption support.
323
+
// This will use an in-memory store.
324
+
awaitmatrixClient.initRustCrypto();
325
+
```
326
+
327
+
To persist the local data, you can use the indexedDB store:
328
+
329
+
```javascript
330
+
// If you not provide a storage key or a password (using a storage key is preferred), the indexedDB store will be uncrypted.
331
+
// The storage key must be a 32 bytes long Uint8Array.
332
+
awaitmatrixClient.initRustCrypto({
333
+
useIndexedDB:true,
334
+
storageKey: my32BytesKey,
335
+
});
336
+
```
337
+
338
+
### Secret storage
339
+
340
+
If your [secret storage](https://spec.matrix.org/v1.12/client-server-api/#secret-storage) is not set up, you need to bootstrap it before using the `CryptoApi`:
341
+
342
+
```javascript
343
+
constmatrixClient=sdk.createClient({
344
+
...,
345
+
cryptoCallbacks: {
346
+
getSecretStorageKey: (keys) => {
347
+
// This function should return the secret storage keys returned in `bootstrapSecretStorage#createSecretStorageKey`
348
+
return mySecretStorageKeys;
349
+
},
350
+
},
351
+
});
352
+
353
+
matrixClient.getCrypto().bootstrapSecretStorage({
354
+
// This will reset the secret storage if it is already set up.
355
+
// If you want to keep the current secret storage, you can set `setupNewSecretStorage` to `false`.
356
+
// If `setupNewSecretStorage` is `true`, you need to fill `createSecretStorageKey`
357
+
setupNewSecretStorage:true,
358
+
// This function will be called if `setupNewSecretStorage` is `true`.
359
+
// You should remember the key you return here, because you will need it to unlock the secret storage.
360
+
// This key should implement the https://matrix-org.github.io/matrix-js-sdk/interfaces/crypto_api.GeneratedSecretStorageKey.html interface.
361
+
createSecretStorageKey: () => {
362
+
return mySecretStorageKey;
363
+
},
364
+
});
365
+
```
366
+
367
+
In the example above, we are setting up a new secret storage. THe secret storage data will be encrypted using the secret storage key returned in `createSecretStorageKey`.
368
+
You should remember this key because you will need it to unlock the secret storage when the `getSecretStorageKey` callback is called.
Once the cross-signing is set up on one of your devices, you can verify another device with two methods:
406
+
407
+
1. Use `CryptoApi#bootstrapCrossSigning`
408
+
409
+
`bootstrapCrossSigning`will call the [CryptoCallbacks#getSecretStorageKey](https://matrix-org.github.io/matrix-js-sdk/interfaces/crypto_api.CryptoCallbacks.html#getSecretStorageKey) provided in [Secret storage chapter](#secret-storage). The device is verified with the private cross-signing keys fetched from the secret storage.
410
+
411
+
2. Request a verification with [CryptoApi#requestOwnUserVerification](https://matrix-org.github.io/matrix-js-sdk/interfaces/crypto_api.CryptoApi.html#requestOwnUserVerification) or [CryptoApi#requestDeviceVerification](https://matrix-org.github.io/matrix-js-sdk/interfaces/crypto_api.CryptoApi.html#requestDeviceVerification).
412
+
413
+
## Migrate from the legacy crypto to the new crypto
414
+
415
+
To migrate from the legacy crypto to the new crypto:
416
+
417
+
```javascript
418
+
// You should provide the legacy crypto store and the pickle key to the matrix client in order to migrate the data.
419
+
constmatrixClient=sdk.createClient({
420
+
cryptoStore: myCryptoStore,
421
+
pickleKey: myPickleKey,
422
+
baseUrl:"http://localhost:8008",
423
+
accessToken: myAccessToken,
424
+
userId: myUserId,
425
+
});
426
+
427
+
// The migration will be done automatically when you call `initRustCrypto`.
428
+
awaitmatrixClient.initRustCrypto();
322
429
```
323
430
324
-
If the crypto layer is not (successfully) initialised, the SDK will continue to
325
-
work for unencrypted rooms, but it will not support the E2E parts of the Matrix
326
-
specification.
431
+
To follow the migration progress, you can listen to the `CryptoEvent.LegacyCryptoStoreMigrationProgress` event:
327
432
328
-
To provide the Olm library in a browser application:
433
+
```javascript
434
+
// When progress === total === -1, the migration is finished.
0 commit comments