matrix-rust-sdk/matrix_sdk_crypto
2021-09-10 17:23:43 +02:00
..
benches feat(crypto): Document our crypto benchmarks 2021-09-09 13:41:28 +02:00
src fix(crypto): Remove some dead code 2021-09-09 20:57:58 +02:00
Cargo.toml chore(common): Bump the version 2021-09-10 17:23:43 +02:00
README.md docs(crypto): Improve the docs and use the readme for the crate docs 2021-09-09 20:57:58 +02:00

A no-io implementation of a state machine that handles E2EE for Matrix clients.

Usage

This is probably not the crate you are looking for, its used internally in the matrix-sdk.

If you're still interested in this crate it can be used to introduce E2EE support into your client or client library.

The state machine works in a push/pull manner, you push state changes and events that we receive from a sync response from the server, and we pull requests that we need to send to the server out of the state machine.

use std::{collections::BTreeMap, convert::TryFrom};

use matrix_sdk_crypto::{OlmMachine, OlmError};
use ruma::{UserId, api::client::r0::sync::sync_events::{ToDevice, DeviceLists}};

#[tokio::main]
async fn main() -> Result<(), OlmError> {
    let alice = UserId::try_from("@alice:example.org").unwrap();
    let machine = OlmMachine::new(&alice, "DEVICEID".into());

    let to_device_events = ToDevice::default();
    let changed_devices = DeviceLists::default();
    let one_time_key_counts = BTreeMap::default();

    // Push changes that the server sent to us in a sync response.
    let decrypted_to_device = machine.receive_sync_changes(
        to_device_events,
        &changed_devices,
        &one_time_key_counts
    ).await?;

    // Pull requests that we need to send out.
    let outgoing_requests = machine.outgoing_requests().await?;

    // Send the requests here out and call machine.mark_request_as_sent().

    Ok(())
}