Skip to content

Support ProcessedToDeviceEvent variants returned by receiveSyncChanges #226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
# UNRELEASED

- Update matrix-rusk-sdk to `35a2ce9`, which includes:

- Add variants for plain text and encrypted to-device events ([#4935](https://github.com/matrix-org/matrix-rust-sdk/pull/4935))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this affect the API exposed by crypto-wasm? If so, it would be good to say how. If not, it would probably be good to say so explicitly.


- **BREAKING**: `OlmMachine.receiveSyncChanges` now returns a list of
`ProcessedToDeviceEvent` instead of a json encoded list of json encoded events.
This allows to make the difference between an event that was sent in clear and
the same event successfully decrypted.

# matrix-sdk-crypto-wasm v14.1.0

- Update matrix-rusk-sdk to `0.11.0`, which includees:
- Update matrix-rusk-sdk to `0.11.0`, which includes:

- Add support for the shared history flag defined in
[MSC3061](https://github.com/matrix-org/matrix-spec-proposals/pull/3061).
Expand Down
15 changes: 5 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ futures-util = "0.3.27"
getrandom = { version = "0.3.0", features = ["wasm_js"] }
http = "1.1.0"
js-sys = "0.3.49"
matrix-sdk-common = { version = "0.11.0", features = ["js"] }
matrix-sdk-indexeddb = { version = "0.11.0", default-features = false, features = ["e2e-encryption"] }
matrix-sdk-qrcode = { version = "0.11.0", optional = true }
matrix-sdk-common = { git = "https://github.com/matrix-org/matrix-rust-sdk", rev = "35a2ce97d84af8cea6b9e1850a8ff4ff5c66d712", features = ["js"] }
matrix-sdk-indexeddb = { git = "https://github.com/matrix-org/matrix-rust-sdk", rev = "35a2ce97d84af8cea6b9e1850a8ff4ff5c66d712", default-features = false, features = ["e2e-encryption"] }
matrix-sdk-qrcode = { git = "https://github.com/matrix-org/matrix-rust-sdk", rev = "35a2ce97d84af8cea6b9e1850a8ff4ff5c66d712", optional = true }
serde = "1.0.91"
serde_json = "1.0.91"
serde-wasm-bindgen = "0.6.5"
Expand All @@ -83,7 +83,8 @@ wasm-bindgen-test = "0.3.37"
vergen-gitcl = { version = "1.0.0", features = ["build"] }

[dependencies.matrix-sdk-crypto]
version = "0.11.0"
git = "https://github.com/matrix-org/matrix-rust-sdk"
rev = "35a2ce97d84af8cea6b9e1850a8ff4ff5c66d712"
default-features = false
features = ["js", "automatic-room-key-forwarding"]

Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ declare module "./pkg/matrix_sdk_crypto_wasm.js" {
changed_devices: DeviceLists,
one_time_keys_counts: Map<string, number>,
unused_fallback_keys?: Set<string> | null,
): Promise<string>;
): Promise<Array<ProcessedToDeviceEvent>>;
outgoingRequests(): Promise<Array<OutgoingRequest>>;
markRequestAsSent(request_id: string, request_type: RequestType, response: string): Promise<boolean>;
encryptRoomEvent(room_id: RoomId, event_type: string, content: string): Promise<string>;
Expand Down
14 changes: 10 additions & 4 deletions src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ use crate::{
store,
store::{RoomKeyInfo, RoomKeyWithheldInfo, StoreHandle},
sync_events,
types::{self, RoomKeyImportResult, RoomSettings, SignatureVerification},
types::{
self, ProcessedToDeviceEvent, RoomKeyImportResult, RoomSettings, SignatureVerification,
},
verification, vodozemac,
};

Expand Down Expand Up @@ -298,7 +300,9 @@ impl OlmMachine {
///
/// # Returns
///
/// A list of JSON strings, containing the decrypted to-device events.
/// A list of {@link ProcessedToDeviceEvent} object, containing the
/// serialized processed event and if it was successfully decrypted, sent in
/// clear, unable to decrypt or invalid.
#[wasm_bindgen(js_name = "receiveSyncChanges")]
pub fn receive_sync_changes(
&self,
Expand Down Expand Up @@ -341,7 +345,7 @@ impl OlmMachine {
// we discard the list of updated room keys in the result; JS applications are
// expected to use register_room_key_updated_callback to receive updated room
// keys.
let (decrypted_to_device_events, _) = me
let (processed_to_device_events, _) = me
.receive_sync_changes(EncryptionSyncChanges {
to_device_events,
changed_devices: &changed_devices,
Expand All @@ -353,7 +357,9 @@ impl OlmMachine {
})
.await?;

Ok(serde_json::to_string(&decrypted_to_device_events)?)
let processed_to_device_events: Vec<ProcessedToDeviceEvent> =
processed_to_device_events.into_iter().map(ProcessedToDeviceEvent::from).collect();
Ok(processed_to_device_events)
}))
}

Expand Down
67 changes: 67 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,70 @@ impl From<&RoomSettings> for matrix_sdk_crypto::store::RoomSettings {
}
}
}

/// Represent the type of {@link ProcessedToDeviceEvent}.
#[wasm_bindgen]
#[derive(Debug, Clone)]
pub enum ProcessedToDeviceEventType {
/// A successfully-decrypted encrypted event.
Decrypted,

/// An encrypted event which could not be decrypted.
UnableToDecrypt,

/// An unencrypted event (sent in clear).
PlainText,

/// An invalid to device event that was ignored because it is missing some
/// required information to be processed (like no event `type` for
/// example)
Invalid,
}

/// Represent a ToDevice event after it has been processed by {@link
/// OlmMachine#receiveSyncChanges}.
#[wasm_bindgen(getter_with_clone)]
#[derive(Debug, Clone)]
pub struct ProcessedToDeviceEvent {
/// The type of processed event
#[wasm_bindgen(getter_with_clone, js_name = "type")]
pub processed_type: ProcessedToDeviceEventType,

/// A JSON-encoded string containing the processed event.
/// For the `Decrypted` type this will be the decrypted event as if it was
/// sent in clear (For room keys or secrets some part of the content might
/// have been zeroize'd).
#[wasm_bindgen(readonly, js_name = "wireEvent")]
pub wire_event: JsString,
}

impl From<matrix_sdk_crypto::types::ProcessedToDeviceEvent> for ProcessedToDeviceEvent {
fn from(value: matrix_sdk_crypto::types::ProcessedToDeviceEvent) -> Self {
match value {
matrix_sdk_crypto::types::ProcessedToDeviceEvent::Decrypted(decrypted_event) => {
ProcessedToDeviceEvent {
processed_type: ProcessedToDeviceEventType::Decrypted,
wire_event: decrypted_event.json().get().to_owned().into(),
}
}
matrix_sdk_crypto::types::ProcessedToDeviceEvent::UnableToDecrypt(utd) => {
ProcessedToDeviceEvent {
processed_type: ProcessedToDeviceEventType::UnableToDecrypt,
wire_event: utd.json().get().to_owned().into(),
}
}
matrix_sdk_crypto::types::ProcessedToDeviceEvent::PlainText(plain) => {
ProcessedToDeviceEvent {
processed_type: ProcessedToDeviceEventType::PlainText,
wire_event: plain.json().get().to_owned().into(),
}
}
matrix_sdk_crypto::types::ProcessedToDeviceEvent::Invalid(invalid) => {
ProcessedToDeviceEvent {
processed_type: ProcessedToDeviceEventType::Invalid,
wire_event: invalid.json().get().to_owned().into(),
}
}
}
}
}
7 changes: 5 additions & 2 deletions tests/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ async function addMachineToMachine(machineToAdd, machine) {
const oneTimeKeyCounts = new Map();
const unusedFallbackKeys = new Set();

const receiveSyncChanges = JSON.parse(
await machineToAdd.receiveSyncChanges(toDeviceEvents, changedDevices, oneTimeKeyCounts, unusedFallbackKeys),
const receiveSyncChanges = await machineToAdd.receiveSyncChanges(
toDeviceEvents,
changedDevices,
oneTimeKeyCounts,
unusedFallbackKeys,
);

expect(receiveSyncChanges).toEqual([]);
Expand Down
Loading