atagen

matrix-rust-sdk

public
Star0
Watch0
ssh://brewery@pls.lobotomise.me:33/atagen/matrix-rust-sdk.git

Blame: crates/matrix-sdk-crypto/README.md

7ff8adafe9e4atagen2026-10-22 12:30 UTC1
A no-network-IO implementation of a state machine that handles end-to-end
7ff8adafe9e4atagen2026-10-22 12:30 UTC2
encryption for [Matrix] clients.
7ff8adafe9e4atagen2026-10-22 12:30 UTC3
7ff8adafe9e4atagen2026-10-22 12:30 UTC4
If you're just trying to write a Matrix client or bot in Rust, you're probably
7ff8adafe9e4atagen2026-10-22 12:30 UTC5
looking for [matrix-sdk] instead.
7ff8adafe9e4atagen2026-10-22 12:30 UTC6
7ff8adafe9e4atagen2026-10-22 12:30 UTC7
However, if you're looking to add end-to-end encryption to an existing Matrix
7ff8adafe9e4atagen2026-10-22 12:30 UTC8
client or library, read on.
7ff8adafe9e4atagen2026-10-22 12:30 UTC9
7ff8adafe9e4atagen2026-10-22 12:30 UTC10
The state machine works in a push/pull manner:
7ff8adafe9e4atagen2026-10-22 12:30 UTC11
7ff8adafe9e4atagen2026-10-22 12:30 UTC12
- you push state changes and events retrieved from a Matrix homeserver /sync
7ff8adafe9e4atagen2026-10-22 12:30 UTC13
  response into the state machine
7ff8adafe9e4atagen2026-10-22 12:30 UTC14
- you pull requests that you'll need to send back to the homeserver out of the
7ff8adafe9e4atagen2026-10-22 12:30 UTC15
  state machine
7ff8adafe9e4atagen2026-10-22 12:30 UTC16
7ff8adafe9e4atagen2026-10-22 12:30 UTC17
```rust,no_run
7ff8adafe9e4atagen2026-10-22 12:30 UTC18
use std::collections::BTreeMap;
7ff8adafe9e4atagen2026-10-22 12:30 UTC19
7ff8adafe9e4atagen2026-10-22 12:30 UTC20
use matrix_sdk_crypto::{
7ff8adafe9e4atagen2026-10-22 12:30 UTC21
    DecryptionSettings, EncryptionSyncChanges, OlmError, OlmMachine, TrustRequirement,
7ff8adafe9e4atagen2026-10-22 12:30 UTC22
};
7ff8adafe9e4atagen2026-10-22 12:30 UTC23
use ruma::{api::client::sync::sync_events::DeviceLists, device_id, user_id};
7ff8adafe9e4atagen2026-10-22 12:30 UTC24
7ff8adafe9e4atagen2026-10-22 12:30 UTC25
#[tokio::main]
7ff8adafe9e4atagen2026-10-22 12:30 UTC26
async fn main() -> Result<(), OlmError> {
7ff8adafe9e4atagen2026-10-22 12:30 UTC27
    let alice = user_id!("@alice:example.org");
7ff8adafe9e4atagen2026-10-22 12:30 UTC28
    let machine = OlmMachine::new(&alice, device_id!("DEVICEID")).await;
7ff8adafe9e4atagen2026-10-22 12:30 UTC29
7ff8adafe9e4atagen2026-10-22 12:30 UTC30
    let changed_devices = DeviceLists::default();
7ff8adafe9e4atagen2026-10-22 12:30 UTC31
    let one_time_key_counts = BTreeMap::default();
7ff8adafe9e4atagen2026-10-22 12:30 UTC32
    let unused_fallback_keys = Some(Vec::new());
7ff8adafe9e4atagen2026-10-22 12:30 UTC33
    let next_batch_token = "T0K3N".to_owned();
7ff8adafe9e4atagen2026-10-22 12:30 UTC34
7ff8adafe9e4atagen2026-10-22 12:30 UTC35
    let decryption_settings =
7ff8adafe9e4atagen2026-10-22 12:30 UTC36
        DecryptionSettings { sender_device_trust_requirement: TrustRequirement::Untrusted };
7ff8adafe9e4atagen2026-10-22 12:30 UTC37
7ff8adafe9e4atagen2026-10-22 12:30 UTC38
    // Push changes that the server sent to us in a sync response.
7ff8adafe9e4atagen2026-10-22 12:30 UTC39
    let decrypted_to_device = machine
7ff8adafe9e4atagen2026-10-22 12:30 UTC40
        .receive_sync_changes(
7ff8adafe9e4atagen2026-10-22 12:30 UTC41
            EncryptionSyncChanges {
7ff8adafe9e4atagen2026-10-22 12:30 UTC42
                to_device_events: vec![],
7ff8adafe9e4atagen2026-10-22 12:30 UTC43
                changed_devices: &changed_devices,
7ff8adafe9e4atagen2026-10-22 12:30 UTC44
                one_time_keys_counts: &one_time_key_counts,
7ff8adafe9e4atagen2026-10-22 12:30 UTC45
                unused_fallback_keys: unused_fallback_keys.as_deref(),
7ff8adafe9e4atagen2026-10-22 12:30 UTC46
                next_batch_token: Some(next_batch_token),
7ff8adafe9e4atagen2026-10-22 12:30 UTC47
            },
7ff8adafe9e4atagen2026-10-22 12:30 UTC48
            &decryption_settings,
7ff8adafe9e4atagen2026-10-22 12:30 UTC49
        )
7ff8adafe9e4atagen2026-10-22 12:30 UTC50
        .await?;
7ff8adafe9e4atagen2026-10-22 12:30 UTC51
7ff8adafe9e4atagen2026-10-22 12:30 UTC52
    // Pull requests that we need to send out.
7ff8adafe9e4atagen2026-10-22 12:30 UTC53
    let outgoing_requests = machine.outgoing_requests().await?;
7ff8adafe9e4atagen2026-10-22 12:30 UTC54
7ff8adafe9e4atagen2026-10-22 12:30 UTC55
    // Send the requests out here and call machine.mark_request_as_sent().
7ff8adafe9e4atagen2026-10-22 12:30 UTC56
7ff8adafe9e4atagen2026-10-22 12:30 UTC57
    Ok(())
7ff8adafe9e4atagen2026-10-22 12:30 UTC58
}
7ff8adafe9e4atagen2026-10-22 12:30 UTC59
```
7ff8adafe9e4atagen2026-10-22 12:30 UTC60
7ff8adafe9e4atagen2026-10-22 12:30 UTC61
It is recommended to use the [tutorial] to understand how end-to-end encryption
7ff8adafe9e4atagen2026-10-22 12:30 UTC62
works in Matrix and how to add end-to-end encryption support in your Matrix
7ff8adafe9e4atagen2026-10-22 12:30 UTC63
client library.
7ff8adafe9e4atagen2026-10-22 12:30 UTC64
7ff8adafe9e4atagen2026-10-22 12:30 UTC65
[Matrix]: https://matrix.org/
7ff8adafe9e4atagen2026-10-22 12:30 UTC66
[matrix-sdk]: https://github.com/matrix-org/matrix-rust-sdk/
7ff8adafe9e4atagen2026-10-22 12:30 UTC67
7ff8adafe9e4atagen2026-10-22 12:30 UTC68
# Crate feature flags
7ff8adafe9e4atagen2026-10-22 12:30 UTC69
7ff8adafe9e4atagen2026-10-22 12:30 UTC70
The following crate feature flags are available:
7ff8adafe9e4atagen2026-10-22 12:30 UTC71
7ff8adafe9e4atagen2026-10-22 12:30 UTC72
| Feature   | Default | Description                                                                                                                                                                                                                                                                                                                                                                               |
7ff8adafe9e4atagen2026-10-22 12:30 UTC73
| --------- | :-----: | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
7ff8adafe9e4atagen2026-10-22 12:30 UTC74
| `qrcode`  |   No    | Enables QR code based interactive verification                                                                                                                                                                                                                                                                                                                                            |
7ff8adafe9e4atagen2026-10-22 12:30 UTC75
| `js`      |   No    | Enables JavaScript API usage for things like the current system time on WASM (does nothing on other targets)                                                                                                                                                                                                                                                                              |
7ff8adafe9e4atagen2026-10-22 12:30 UTC76
| `testing` |   No    | Provides facilities and functions for tests, in particular for integration testing store implementations. ATTENTION: do not ever use outside of tests, we do not provide any stability warantees on these, these are merely helpers. If you find you _need_ any function provided here outside of tests, please open a Github Issue and inform us about your use case for us to consider. |
7ff8adafe9e4atagen2026-10-22 12:30 UTC77
7ff8adafe9e4atagen2026-10-22 12:30 UTC78
- `testing`: Provides facilities and functions for tests, in particular for
7ff8adafe9e4atagen2026-10-22 12:30 UTC79
  integration testing store implementations. ATTENTION: do not ever use outside
7ff8adafe9e4atagen2026-10-22 12:30 UTC80
  of tests, we do not provide any stability warantees on these, these are merely
7ff8adafe9e4atagen2026-10-22 12:30 UTC81
  helpers. If you find you _need_ any function provided here outside of tests,
7ff8adafe9e4atagen2026-10-22 12:30 UTC82
  please open a Github Issue and inform us about your use case for us to
7ff8adafe9e4atagen2026-10-22 12:30 UTC83
  consider.
7ff8adafe9e4atagen2026-10-22 12:30 UTC84
7ff8adafe9e4atagen2026-10-22 12:30 UTC85
- `_disable-minimum-rotation-period-ms`: Do not use except for testing. Disables
7ff8adafe9e4atagen2026-10-22 12:30 UTC86
  the floor on the rotation period of room keys.
7ff8adafe9e4atagen2026-10-22 12:30 UTC87
7ff8adafe9e4atagen2026-10-22 12:30 UTC88
## Enabling logging
7ff8adafe9e4atagen2026-10-22 12:30 UTC89
7ff8adafe9e4atagen2026-10-22 12:30 UTC90
Users of the `matrix-sdk-crypto` crate can enable log output by depending on the
7ff8adafe9e4atagen2026-10-22 12:30 UTC91
`tracing-subscriber` crate and including the following line in their
7ff8adafe9e4atagen2026-10-22 12:30 UTC92
application (e.g. at the start of `main`):
7ff8adafe9e4atagen2026-10-22 12:30 UTC93
7ff8adafe9e4atagen2026-10-22 12:30 UTC94
```no_compile
7ff8adafe9e4atagen2026-10-22 12:30 UTC95
tracing_subscriber::fmt::init();
7ff8adafe9e4atagen2026-10-22 12:30 UTC96
```
7ff8adafe9e4atagen2026-10-22 12:30 UTC97
7ff8adafe9e4atagen2026-10-22 12:30 UTC98
The log output is controlled via the `RUST_LOG` environment variable by
7ff8adafe9e4atagen2026-10-22 12:30 UTC99
setting it to one of the `error`, `warn`, `info`, `debug` or `trace` levels.
7ff8adafe9e4atagen2026-10-22 12:30 UTC100
The output is printed to stdout.
7ff8adafe9e4atagen2026-10-22 12:30 UTC101
7ff8adafe9e4atagen2026-10-22 12:30 UTC102
The `RUST_LOG` variable also supports a more advanced syntax for filtering
7ff8adafe9e4atagen2026-10-22 12:30 UTC103
log output more precisely, for instance with crate-level granularity. For
7ff8adafe9e4atagen2026-10-22 12:30 UTC104
more information on this, check out the [tracing-subscriber documentation].
7ff8adafe9e4atagen2026-10-22 12:30 UTC105
7ff8adafe9e4atagen2026-10-22 12:30 UTC106
[tracing-subscriber documentation]: https://docs.rs/tracing-subscriber/latest/tracing_subscriber/