From 2cbdca1f58288f4868ad30e8dd6e13b1896719f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Sat, 25 Jul 2020 10:24:44 +0200 Subject: [PATCH] crypto: Make it easier to create canceled SasState. --- matrix_sdk_crypto/src/verification/sas.rs | 51 ++++++++++++----------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/matrix_sdk_crypto/src/verification/sas.rs b/matrix_sdk_crypto/src/verification/sas.rs index 01d498a0..5d4885d0 100644 --- a/matrix_sdk_crypto/src/verification/sas.rs +++ b/matrix_sdk_crypto/src/verification/sas.rs @@ -412,6 +412,15 @@ impl SasState { pub fn device_id(&self) -> &DeviceId { &self.ids.account.device_id() } + + pub fn cancel(self, cancel_code: CancelCode) -> SasState { + SasState { + inner: self.inner, + ids: self.ids, + verification_flow_id: self.verification_flow_id, + state: Arc::new(Canceled::new(cancel_code)), + } + } } impl SasState { @@ -504,6 +513,21 @@ impl SasState { event: &ToDeviceEvent, ) -> Result, SasState> { if let StartEventContent::MSasV1(content) = &event.content { + let sas = SasState { + inner: Arc::new(Mutex::new(OlmSas::new())), + + ids: SasIds { + account, + other_device, + }, + + verification_flow_id: Arc::new(content.transaction_id.clone()), + + state: Arc::new(Started { + protocol_definitions: content.clone(), + }), + }; + if !content .key_agreement_protocols .contains(&KeyAgreementProtocol::Curve25519HkdfSha256) @@ -518,32 +542,9 @@ impl SasState { .short_authentication_string .contains(&ShortAuthenticationString::Emoji)) { - Err(SasState { - inner: Arc::new(Mutex::new(OlmSas::new())), - - ids: SasIds { - account, - other_device, - }, - verification_flow_id: Arc::new(content.transaction_id.clone()), - - state: Arc::new(Canceled::new(CancelCode::UnknownMethod)), - }) + Err(sas.cancel(CancelCode::UnknownMethod)) } else { - Ok(SasState { - inner: Arc::new(Mutex::new(OlmSas::new())), - - ids: SasIds { - account, - other_device, - }, - - verification_flow_id: Arc::new(content.transaction_id.clone()), - - state: Arc::new(Started { - protocol_definitions: content.clone(), - }), - }) + Ok(sas) } } else { Err(SasState {