Skip to content

Commit 2ded95d

Browse files
committed
Support ProcessedToDeviceEvent variants returned by receiveSyncChanges
1 parent 3f74785 commit 2ded95d

File tree

8 files changed

+191
-44
lines changed

8 files changed

+191
-44
lines changed

CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
# UNRELEASED
22

3+
- Update matrix-rusk-sdk to `35a2ce9`, which includes:
4+
5+
- Add variants for plain text and encrypted to-device events ([#4935](https://github.com/matrix-org/matrix-rust-sdk/pull/4935))
6+
7+
- **BREAKING**: `OlmMachine.receiveSyncChanges` now returns a list of
8+
`ProcessedToDeviceEvent` instead of a json encoded list of json encoded events.
9+
This allows to make the difference between an event that was sent in clear and
10+
the same event successfully decrypted.
11+
312
# matrix-sdk-crypto-wasm v14.1.0
413

5-
- Update matrix-rusk-sdk to `0.11.0`, which includees:
14+
- Update matrix-rusk-sdk to `0.11.0`, which includes:
615

716
- Add support for the shared history flag defined in
817
[MSC3061](https://github.com/matrix-org/matrix-spec-proposals/pull/3061).

Cargo.lock

Lines changed: 5 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ futures-util = "0.3.27"
6565
getrandom = { version = "0.3.0", features = ["wasm_js"] }
6666
http = "1.1.0"
6767
js-sys = "0.3.49"
68-
matrix-sdk-common = { version = "0.11.0", features = ["js"] }
69-
matrix-sdk-indexeddb = { version = "0.11.0", default-features = false, features = ["e2e-encryption"] }
70-
matrix-sdk-qrcode = { version = "0.11.0", optional = true }
68+
matrix-sdk-common = { git = "https://github.com/matrix-org/matrix-rust-sdk", rev = "35a2ce97d84af8cea6b9e1850a8ff4ff5c66d712", features = ["js"] }
69+
matrix-sdk-indexeddb = { git = "https://github.com/matrix-org/matrix-rust-sdk", rev = "35a2ce97d84af8cea6b9e1850a8ff4ff5c66d712", default-features = false, features = ["e2e-encryption"] }
70+
matrix-sdk-qrcode = { git = "https://github.com/matrix-org/matrix-rust-sdk", rev = "35a2ce97d84af8cea6b9e1850a8ff4ff5c66d712", optional = true }
7171
serde = "1.0.91"
7272
serde_json = "1.0.91"
7373
serde-wasm-bindgen = "0.6.5"
@@ -83,7 +83,8 @@ wasm-bindgen-test = "0.3.37"
8383
vergen-gitcl = { version = "1.0.0", features = ["build"] }
8484

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

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ declare module "./pkg/matrix_sdk_crypto_wasm.js" {
6969
changed_devices: DeviceLists,
7070
one_time_keys_counts: Map<string, number>,
7171
unused_fallback_keys?: Set<string> | null,
72-
): Promise<string>;
72+
): Promise<Array<ProcessedToDeviceEvent>>;
7373
outgoingRequests(): Promise<Array<OutgoingRequest>>;
7474
markRequestAsSent(request_id: string, request_type: RequestType, response: string): Promise<boolean>;
7575
encryptRoomEvent(room_id: RoomId, event_type: string, content: string): Promise<string>;

src/machine.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ use crate::{
4242
store,
4343
store::{RoomKeyInfo, RoomKeyWithheldInfo, StoreHandle},
4444
sync_events,
45-
types::{self, RoomKeyImportResult, RoomSettings, SignatureVerification},
45+
types::{
46+
self, ProcessedToDeviceEvent, RoomKeyImportResult, RoomSettings, SignatureVerification,
47+
},
4648
verification, vodozemac,
4749
};
4850

@@ -298,7 +300,9 @@ impl OlmMachine {
298300
///
299301
/// # Returns
300302
///
301-
/// A list of JSON strings, containing the decrypted to-device events.
303+
/// A list of {@link ProcessedToDeviceEvent} object, containing the
304+
/// serialized processed event and if it was successfully decrypted, sent in
305+
/// clear, unable to decrypt or invalid.
302306
#[wasm_bindgen(js_name = "receiveSyncChanges")]
303307
pub fn receive_sync_changes(
304308
&self,
@@ -341,7 +345,7 @@ impl OlmMachine {
341345
// we discard the list of updated room keys in the result; JS applications are
342346
// expected to use register_room_key_updated_callback to receive updated room
343347
// keys.
344-
let (decrypted_to_device_events, _) = me
348+
let (processed_to_device_events, _) = me
345349
.receive_sync_changes(EncryptionSyncChanges {
346350
to_device_events,
347351
changed_devices: &changed_devices,
@@ -353,7 +357,9 @@ impl OlmMachine {
353357
})
354358
.await?;
355359

356-
Ok(serde_json::to_string(&decrypted_to_device_events)?)
360+
let processed_to_device_events: Vec<ProcessedToDeviceEvent> =
361+
processed_to_device_events.into_iter().map(ProcessedToDeviceEvent::from).collect();
362+
Ok(processed_to_device_events)
357363
}))
358364
}
359365

src/types.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,70 @@ impl From<&RoomSettings> for matrix_sdk_crypto::store::RoomSettings {
362362
}
363363
}
364364
}
365+
366+
/// Represent the type of {@link ProcessedToDeviceEvent}.
367+
#[wasm_bindgen]
368+
#[derive(Debug, Clone)]
369+
pub enum ProcessedToDeviceEventType {
370+
/// A successfully-decrypted encrypted event.
371+
Decrypted,
372+
373+
/// An encrypted event which could not be decrypted.
374+
UnableToDecrypt,
375+
376+
/// An unencrypted event (sent in clear).
377+
PlainText,
378+
379+
/// An invalid to device event that was ignored because it is missing some
380+
/// required information to be processed (like no event `type` for
381+
/// example)
382+
Invalid,
383+
}
384+
385+
/// Represent a ToDevice event after it has been processed by {@link
386+
/// OlmMachine#receiveSyncChanges}.
387+
#[wasm_bindgen(getter_with_clone)]
388+
#[derive(Debug, Clone)]
389+
pub struct ProcessedToDeviceEvent {
390+
/// The type of processed event
391+
#[wasm_bindgen(getter_with_clone, js_name = "type")]
392+
pub processed_type: ProcessedToDeviceEventType,
393+
394+
/// A JSON-encoded string containing the processed event.
395+
/// For the `Decrypted` type this will be the decrypted event as if it was
396+
/// sent in clear (For room keys or secrets some part of the content might
397+
/// have been zeroize'd).
398+
#[wasm_bindgen(readonly, js_name = "wireEvent")]
399+
pub wire_event: JsString,
400+
}
401+
402+
impl From<matrix_sdk_crypto::types::ProcessedToDeviceEvent> for ProcessedToDeviceEvent {
403+
fn from(value: matrix_sdk_crypto::types::ProcessedToDeviceEvent) -> Self {
404+
match value {
405+
matrix_sdk_crypto::types::ProcessedToDeviceEvent::Decrypted(decrypted_event) => {
406+
ProcessedToDeviceEvent {
407+
processed_type: ProcessedToDeviceEventType::Decrypted,
408+
wire_event: decrypted_event.json().get().to_owned().into(),
409+
}
410+
}
411+
matrix_sdk_crypto::types::ProcessedToDeviceEvent::UnableToDecrypt(utd) => {
412+
ProcessedToDeviceEvent {
413+
processed_type: ProcessedToDeviceEventType::UnableToDecrypt,
414+
wire_event: utd.json().get().to_owned().into(),
415+
}
416+
}
417+
matrix_sdk_crypto::types::ProcessedToDeviceEvent::PlainText(plain) => {
418+
ProcessedToDeviceEvent {
419+
processed_type: ProcessedToDeviceEventType::PlainText,
420+
wire_event: plain.json().get().to_owned().into(),
421+
}
422+
}
423+
matrix_sdk_crypto::types::ProcessedToDeviceEvent::Invalid(invalid) => {
424+
ProcessedToDeviceEvent {
425+
processed_type: ProcessedToDeviceEventType::Invalid,
426+
wire_event: invalid.json().get().to_owned().into(),
427+
}
428+
}
429+
}
430+
}
431+
}

tests/helper.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ async function addMachineToMachine(machineToAdd, machine) {
1616
const oneTimeKeyCounts = new Map();
1717
const unusedFallbackKeys = new Set();
1818

19-
const receiveSyncChanges = JSON.parse(
20-
await machineToAdd.receiveSyncChanges(toDeviceEvents, changedDevices, oneTimeKeyCounts, unusedFallbackKeys),
19+
const receiveSyncChanges = await machineToAdd.receiveSyncChanges(
20+
toDeviceEvents,
21+
changedDevices,
22+
oneTimeKeyCounts,
23+
unusedFallbackKeys,
2124
);
2225

2326
expect(receiveSyncChanges).toEqual([]);

0 commit comments

Comments
 (0)