@@ -10,12 +10,14 @@ use matrix_sdk_common::ruma::OwnedRoomId;
10
10
use matrix_sdk_crypto:: backups:: {
11
11
SignatureState as InnerSignatureState , SignatureVerification as InnerSignatureVerification ,
12
12
} ;
13
+ use tracing:: warn;
13
14
use wasm_bindgen:: prelude:: * ;
14
15
15
16
use crate :: {
16
17
encryption:: EncryptionAlgorithm ,
17
18
identifiers:: { DeviceKeyId , UserId } ,
18
19
impl_from_to_inner,
20
+ responses:: ToDeviceEncryptionInfo ,
19
21
vodozemac:: Ed25519Signature ,
20
22
} ;
21
23
@@ -395,6 +397,10 @@ pub struct DecryptedToDeviceEvent {
395
397
/// zeroized).
396
398
#[ wasm_bindgen( readonly, getter_with_clone, js_name = "decryptedRawEvent" ) ]
397
399
pub decrypted_raw_event : JsString ,
400
+
401
+ /// The encryption information for the event.
402
+ #[ wasm_bindgen( readonly, getter_with_clone, js_name = "encryptionInfo" ) ]
403
+ pub encryption_info : ToDeviceEncryptionInfo ,
398
404
}
399
405
400
406
#[ wasm_bindgen]
@@ -473,15 +479,41 @@ impl InvalidToDeviceEvent {
473
479
/// Convert an `ProcessedToDeviceEvent` into a `JsValue`, ready to return to
474
480
/// JavaScript.
475
481
///
476
- /// JavaScript has no complex enums like Rust. To return structs of
477
- /// different types, we have no choice other than hiding everything behind a
478
- /// `JsValue`.
482
+ /// JavaScript has no complex enums like Rust. To return structs of different
483
+ /// types, we have no choice other than hiding everything behind a `JsValue`.
484
+ ///
485
+ /// We attempt to map the event onto one of the following types:
486
+ /// * [`DecryptedToDeviceEvent`]
487
+ /// * [`UTDToDeviceEvent`]
488
+ /// * [`PlainTextToDeviceEvent`]
489
+ /// * [`InvalidToDeviceEvent`].
490
+ ///
491
+ /// We then convert that result into a [`JsValue`].
492
+ ///
493
+ /// If the event cannot be mapped into one of those types, we instead return
494
+ /// `None`, indicating the event should be discarded.
479
495
pub fn processed_to_device_event_to_js_value (
480
496
processed_to_device_event : matrix_sdk_crypto:: types:: ProcessedToDeviceEvent ,
481
- ) -> JsValue {
482
- match processed_to_device_event {
483
- matrix_sdk_crypto:: types:: ProcessedToDeviceEvent :: Decrypted { raw, .. } => {
484
- DecryptedToDeviceEvent { decrypted_raw_event : raw. json ( ) . get ( ) . into ( ) } . into ( )
497
+ ) -> Option < JsValue > {
498
+ let result = match processed_to_device_event {
499
+ matrix_sdk_crypto:: types:: ProcessedToDeviceEvent :: Decrypted { raw, encryption_info } => {
500
+ match encryption_info. try_into ( ) {
501
+ Ok ( encryption_info) => DecryptedToDeviceEvent {
502
+ decrypted_raw_event : raw. json ( ) . get ( ) . into ( ) ,
503
+ encryption_info,
504
+ }
505
+ . into ( ) ,
506
+ Err ( e) => {
507
+ // This can only happen if we receive an encrypted to-device event which is
508
+ // encrypted with an algorithm we don't recognise. This
509
+ // shouldn't really happen, unless the wasm bindings have
510
+ // gotten way out of step with the underlying SDK.
511
+ //
512
+ // There's not a lot we can do here: we just throw away the event.
513
+ warn ! ( "Dropping incoming to-device event with invalid encryption_info: {e:?}" ) ;
514
+ return None ;
515
+ }
516
+ }
485
517
}
486
518
matrix_sdk_crypto:: types:: ProcessedToDeviceEvent :: UnableToDecrypt ( utd) => {
487
519
UTDToDeviceEvent { wire_event : utd. json ( ) . get ( ) . into ( ) } . into ( )
@@ -492,5 +524,6 @@ pub fn processed_to_device_event_to_js_value(
492
524
matrix_sdk_crypto:: types:: ProcessedToDeviceEvent :: Invalid ( invalid) => {
493
525
InvalidToDeviceEvent { wire_event : invalid. json ( ) . get ( ) . into ( ) } . into ( )
494
526
}
495
- }
527
+ } ;
528
+ Some ( result)
496
529
}
0 commit comments