diff --git a/bindings/matrix-sdk-crypto-js/src/verification.rs b/bindings/matrix-sdk-crypto-js/src/verification.rs index 408e7ee3d..eeda883c6 100644 --- a/bindings/matrix-sdk-crypto-js/src/verification.rs +++ b/bindings/matrix-sdk-crypto-js/src/verification.rs @@ -1,12 +1,13 @@ //! Different verification types. -use js_sys::{Array, JsString}; +use js_sys::{Array, JsString, Promise}; use ruma::events::key::verification::{ cancel::CancelCode as RumaCancelCode, VerificationMethod as RumaVerificationMethod, }; use wasm_bindgen::prelude::*; use crate::{ + future::future_to_promise, identifiers::{DeviceId, RoomId, UserId}, requests, }; @@ -130,6 +131,12 @@ pub struct Sas { inner: matrix_sdk_crypto::Sas, } +impl From for Sas { + fn from(inner: matrix_sdk_crypto::Sas) -> Self { + Self { inner } + } +} + #[wasm_bindgen] impl Sas { /// Get our own user ID. @@ -872,7 +879,36 @@ impl VerificationRequest { .map_err(Into::into) } - // start_sas + /// Transition from this verification request into a SAS verification flow. + #[wasm_bindgen(js_name = "startSas")] + pub fn start_sas(&self) -> Promise { + let me = self.inner.clone(); + + future_to_promise(async move { + match me + .start_sas() + .await? + .map(|(sas, outgoing_verification_request)| -> Result { + let tuple = Array::new(); + + tuple.set(0, Sas::from(sas).into()); + tuple.set( + 1, + OutgoingVerificationRequest::from(outgoing_verification_request) + .try_into()?, + ); + + Ok(tuple) + }) + .transpose() + { + Ok(a) => Ok(a), + Err(_) => { + Err(anyhow::Error::msg("Failed to build the outgoing verification request")) + } + } + }) + } // generate_qr_code if `qrcode` // scan_qr_code if `qrcode` }