From b33537fd6e98a0d2551a428f0e3536b7c40d912c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Mon, 19 Jul 2021 14:21:11 +0200 Subject: [PATCH] crypto: Use the new CancelInfo struct --- .../android/sdk/internal/crypto/OlmMachine.kt | 7 ++-- .../internal/crypto/RustSasVerification.kt | 8 ++-- .../internal/crypto/VerificationRequest.kt | 7 ++-- rust-sdk/Cargo.toml | 4 +- rust-sdk/src/lib.rs | 4 +- rust-sdk/src/machine.rs | 39 ++++++++++++------- rust-sdk/src/olm.udl | 14 ++++--- 7 files changed, 50 insertions(+), 33 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/OlmMachine.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/OlmMachine.kt index 77ea896b26..365f058c00 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/OlmMachine.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/OlmMachine.kt @@ -208,6 +208,7 @@ internal class QrCodeVerification( get() { refreshData() val inner = this.inner + val cancelInfo = inner?.cancelInfo return if (inner != null) { when { @@ -215,9 +216,9 @@ internal class QrCodeVerification( inner.reciprocated -> VerificationTxState.Started inner.hasBeenConfirmed -> VerificationTxState.WaitingOtherReciprocateConfirm inner.otherSideScanned -> VerificationTxState.QrScannedByOther - inner.isCancelled -> { - val cancelCode = safeValueOf(inner.cancelCode) - val byMe = inner.cancelledByUs ?: false + cancelInfo != null -> { + val cancelCode = safeValueOf(cancelInfo.cancelCode) + val byMe = cancelInfo.cancelledByUs VerificationTxState.Cancelled(cancelCode, byMe) } else -> { diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/RustSasVerification.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/RustSasVerification.kt index 63e2e0f3a4..839bdba5f8 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/RustSasVerification.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/RustSasVerification.kt @@ -82,11 +82,11 @@ internal class SasVerification( override var state: VerificationTxState get() { refreshData() + val cancelInfo = this.inner.cancelInfo return when { - this.inner.isCancelled -> { - val cancelCode = safeValueOf(this.inner.cancelCode) - val byMe = this.inner.cancelledByUs ?: false - VerificationTxState.Cancelled(cancelCode, byMe) + cancelInfo != null -> { + val cancelCode = safeValueOf(cancelInfo.cancelCode) + VerificationTxState.Cancelled(cancelCode, cancelInfo.cancelledByUs) } this.inner.isDone -> VerificationTxState.Verified this.inner.haveWeConfirmed -> VerificationTxState.ShortCodeAccepted diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/VerificationRequest.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/VerificationRequest.kt index 458fabf284..1adc1e8d0f 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/VerificationRequest.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/VerificationRequest.kt @@ -180,11 +180,10 @@ internal class VerificationRequest( fun toPendingVerificationRequest(): PendingVerificationRequest { refreshData() - val code = this.inner.cancelCode - + val cancelInfo = this.inner.cancelInfo val cancelCode = - if (code != null) { - safeValueOf(code) + if (cancelInfo != null) { + safeValueOf(cancelInfo.cancelCode) } else { null } diff --git a/rust-sdk/Cargo.toml b/rust-sdk/Cargo.toml index f4f9a74826..66723de16a 100644 --- a/rust-sdk/Cargo.toml +++ b/rust-sdk/Cargo.toml @@ -25,11 +25,11 @@ features = ["lax_deserialize"] [dependencies.matrix-sdk-common] git = "https://github.com/matrix-org/matrix-rust-sdk/" -rev = "b53518d1b8dd93ac447d1318c2e8aa4e2004dd2f" +rev = "3a8ff2f6b43f312b7582146ed712ff245ef9d5aa" [dependencies.matrix-sdk-crypto] git = "https://github.com/matrix-org/matrix-rust-sdk/" -rev = "b53518d1b8dd93ac447d1318c2e8aa4e2004dd2f" +rev = "3a8ff2f6b43f312b7582146ed712ff245ef9d5aa" features = ["sled_cryptostore"] [dependencies.tokio] diff --git a/rust-sdk/src/lib.rs b/rust-sdk/src/lib.rs index 7d2aef13e0..3eb18a96b3 100644 --- a/rust-sdk/src/lib.rs +++ b/rust-sdk/src/lib.rs @@ -8,8 +8,8 @@ pub use device::Device; pub use error::{CryptoStoreError, DecryptionError, KeyImportError, MachineCreationError}; pub use logger::{set_logger, Logger}; pub use machine::{ - KeyRequestPair, OlmMachine, QrCode, RequestVerificationResult, Sas, ScanResult, StartSasResult, - Verification, VerificationRequest, + CancelInfo, KeyRequestPair, OlmMachine, QrCode, RequestVerificationResult, Sas, ScanResult, + StartSasResult, Verification, VerificationRequest, }; pub use responses::{ DeviceLists, KeysImportResult, OutgoingVerificationRequest, Request, RequestType, diff --git a/rust-sdk/src/machine.rs b/rust-sdk/src/machine.rs index 6bacc250e8..72f269ddce 100644 --- a/rust-sdk/src/machine.rs +++ b/rust-sdk/src/machine.rs @@ -30,9 +30,10 @@ use tokio::runtime::Runtime; use matrix_sdk_common::{deserialized_responses::AlgorithmInfo, uuid::Uuid}; use matrix_sdk_crypto::{ - decrypt_key_export, encrypt_key_export, matrix_qrcode::QrVerificationData, EncryptionSettings, - LocalTrust, OlmMachine as InnerMachine, QrVerification as InnerQr, Sas as InnerSas, - Verification as RustVerification, VerificationRequest as InnerVerificationRequest, + decrypt_key_export, encrypt_key_export, matrix_qrcode::QrVerificationData, + CancelInfo as RustCancelInfo, EncryptionSettings, LocalTrust, OlmMachine as InnerMachine, + QrVerification as InnerQr, Sas as InnerSas, Verification as RustVerification, + VerificationRequest as InnerVerificationRequest, }; use crate::{ @@ -61,8 +62,7 @@ pub struct Sas { pub have_we_confirmed: bool, pub is_cancelled: bool, pub is_done: bool, - pub cancel_code: Option, - pub cancelled_by_us: Option, + pub cancel_info: Option, pub has_been_accepted: bool, pub we_started: bool, pub can_be_presented: bool, @@ -81,8 +81,7 @@ pub struct QrCode { pub other_side_scanned: bool, pub has_been_confirmed: bool, pub reciprocated: bool, - pub cancel_code: Option, - pub cancelled_by_us: Option, + pub cancel_info: Option, } impl From for QrCode { @@ -92,8 +91,7 @@ impl From for QrCode { flow_id: qr.flow_id().as_str().to_owned(), is_cancelled: qr.is_cancelled(), is_done: qr.is_done(), - cancel_code: qr.cancel_code().map(|c| c.to_string()), - cancelled_by_us: qr.cancelled_by_us(), + cancel_info: qr.cancel_info().map(|c| c.into()), reciprocated: qr.reciprocated(), we_started: qr.we_started(), other_side_scanned: qr.has_been_scanned(), @@ -104,6 +102,22 @@ impl From for QrCode { } } +pub struct CancelInfo { + pub reason: String, + pub cancel_code: String, + pub cancelled_by_us: bool, +} + +impl From for CancelInfo { + fn from(c: RustCancelInfo) -> Self { + Self { + reason: c.reason().to_owned(), + cancel_code: c.cancel_code().to_string(), + cancelled_by_us: c.cancelled_by_us(), + } + } +} + pub struct StartSasResult { pub sas: Sas, pub request: OutgoingVerificationRequest, @@ -126,11 +140,10 @@ impl From for Sas { timed_out: sas.timed_out(), supports_emoji: sas.supports_emoji(), have_we_confirmed: sas.have_we_confirmed(), - cancel_code: sas.cancel_code().map(|c| c.as_str().to_owned()), we_started: sas.we_started(), room_id: sas.room_id().map(|r| r.to_string()), - cancelled_by_us: sas.cancelled_by_us(), has_been_accepted: sas.has_been_accepted(), + cancel_info: sas.cancel_info().map(|c| c.into()), } } } @@ -148,11 +161,11 @@ pub struct VerificationRequest { pub is_done: bool, pub is_ready: bool, pub room_id: Option, - pub cancel_code: Option, pub we_started: bool, pub is_passive: bool, pub their_methods: Option>, pub our_methods: Option>, + pub cancel_info: Option, } impl From for VerificationRequest { @@ -165,9 +178,9 @@ impl From for VerificationRequest { is_done: v.is_done(), is_ready: v.is_ready(), room_id: v.room_id().map(|r| r.to_string()), - cancel_code: v.cancel_code().map(|c| c.as_str().to_owned()), we_started: v.we_started(), is_passive: v.is_passive(), + cancel_info: v.cancel_info().map(|c| c.into()), their_methods: v .their_supported_methods() .map(|v| v.into_iter().map(|m| m.to_string()).collect()), diff --git a/rust-sdk/src/olm.udl b/rust-sdk/src/olm.udl index 9f02558e43..ac4618b22d 100644 --- a/rust-sdk/src/olm.udl +++ b/rust-sdk/src/olm.udl @@ -65,6 +65,12 @@ dictionary Device { boolean cross_signing_trusted; }; +dictionary CancelInfo { + string cancel_code; + string reason; + boolean cancelled_by_us; +}; + dictionary StartSasResult { Sas sas; OutgoingVerificationRequest request; @@ -74,10 +80,8 @@ dictionary Sas { string other_user_id; string other_device_id; string flow_id; - string? cancel_code; string? room_id; boolean we_started; - boolean? cancelled_by_us; boolean has_been_accepted; boolean have_we_confirmed; boolean is_done; @@ -85,6 +89,7 @@ dictionary Sas { boolean can_be_presented; boolean timed_out; boolean supports_emoji; + CancelInfo? cancel_info; }; dictionary ScanResult { @@ -97,15 +102,14 @@ dictionary QrCode { string other_user_id; string other_device_id; string flow_id; - string? cancel_code; string? room_id; boolean reciprocated; boolean we_started; boolean has_been_confirmed; - boolean? cancelled_by_us; boolean is_done; boolean is_cancelled; boolean other_side_scanned; + CancelInfo? cancel_info; }; dictionary VerificationRequest { @@ -118,7 +122,7 @@ dictionary VerificationRequest { boolean we_started; boolean is_passive; string? room_id; - string? cancel_code; + CancelInfo? cancel_info; sequence? their_methods; sequence? our_methods;