Skip to content

Commit 3f81674

Browse files
committed
Update e2e doc in README.md
1 parent fbbdb6e commit 3f81674

File tree

1 file changed

+135
-26
lines changed

1 file changed

+135
-26
lines changed

README.md

Lines changed: 135 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -303,44 +303,153 @@ Then visit `http://localhost:8005` to see the API docs.
303303

304304
# End-to-end encryption support
305305

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.
308307

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
312309

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.**
316311

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:
319313

314+
```javascript
315+
// Create a new matrix client
316+
const matrixClient = 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+
await matrixClient.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+
await matrixClient.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+
const matrixClient = 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.
369+
370+
- [CryptoCallbacks#getSecretStorageKey](https://matrix-org.github.io/matrix-js-sdk/interfaces/crypto_api.CryptoCallbacks.html#getSecretStorageKey)
371+
- [CryptoApi#bootstrapSecretStorage](https://matrix-org.github.io/matrix-js-sdk/interfaces/crypto_api.CryptoApi.html#bootstrapSecretStorage)
372+
373+
Also, if you don't have a [key backup](https://spec.matrix.org/v1.12/client-server-api/#server-side-key-backups) you should create one:
374+
375+
```javascript
376+
matrixClient.getCrypto().bootstrapSecretStorage({
377+
...,
378+
setupNewKeyBackup: true,
379+
});
380+
```
381+
382+
Once the key backup and the secret storage are set up, you don't need to set them up again for all your devices.
383+
384+
### Verify a device and cross-signing
385+
386+
### Set up cross-signing
387+
388+
In order to use cross-signing to verify devices, you need to set up cross-signing before using the `CryptoApi`:
389+
390+
```javascript
391+
matrixClient.getCrypto().bootstrapCrossSigning({
392+
authUploadDeviceSigningKeys: (makeRequest) => {
393+
return makeRequest(authDict);
394+
},
395+
});
320396
```
321-
Unable to load crypto module: crypto will be disabled: Error: global.Olm is not defined
397+
398+
The `authUploadDeviceSigningKeys` callback is optional but strongly recommended in order to upload the device signing keys to the server.
399+
400+
- [AuthDict](https://matrix-org.github.io/matrix-js-sdk/types/matrix.AuthDict.html)
401+
- [CryptoApi#bootstrapCrossSigning](https://matrix-org.github.io/matrix-js-sdk/interfaces/crypto_api.CryptoApi.html#bootstrapCrossSigning)
402+
403+
### Verify a device
404+
405+
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+
const matrixClient = 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+
await matrixClient.initRustCrypto();
322429
```
323430

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:
327432

328-
To provide the Olm library in a browser application:
433+
```javascript
434+
// When progress === total === -1, the migration is finished.
435+
matrixClient.on(CryptoEvent.LegacyCryptoStoreMigrationProgress, (progress, total) => {
436+
...
437+
});
438+
```
329439

330-
- download the transpiled libolm (from https://packages.matrix.org/npm/olm/).
331-
- load `olm.js` as a `<script>` _before_ `browser-matrix.js`.
440+
After the migration is finished, you can remove the legacy crypto store and the pickle key from the matrix client creation.
332441

333-
To provide the Olm library in a node.js application:
442+
## Use the `CryptoApi`
334443

335-
- `yarn add https://packages.matrix.org/npm/olm/olm-3.1.4.tgz`
336-
(replace the URL with the latest version you want to use from
337-
https://packages.matrix.org/npm/olm/)
338-
- `global.Olm = require('olm');` _before_ loading `matrix-js-sdk`.
444+
The `CryptoApi` is the main entry point for end-to-end encryption.
445+
446+
```javascript
447+
// If the `CryptoApi` object is `undefined`, the end-to-end encryption is not enabled.
448+
// You must call `initRustCrypto` before.
449+
matrixClient.getCrypto();
450+
```
339451

340-
If you want to package Olm as dependency for your node.js application, you can
341-
use `yarn add https://packages.matrix.org/npm/olm/olm-3.1.4.tgz`. If your
342-
application also works without e2e crypto enabled, add `--optional` to mark it
343-
as an optional dependency.
452+
The CryptoApi documentation is available [here](https://matrix-org.github.io/matrix-js-sdk/interfaces/crypto_api.CryptoApi.html).
344453

345454
# Contributing
346455

0 commit comments

Comments
 (0)