From d21137d910b70562abc9ee331869dcba6eb2c6e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Mon, 28 Jun 2021 09:57:32 +0200 Subject: [PATCH] crypto: Add a state for when we confirmed the QR code --- .../android/sdk/internal/crypto/OlmMachine.kt | 7 +-- .../verification/RustVerificationService.kt | 44 +++++++++---------- rust-sdk/Cargo.toml | 4 +- rust-sdk/src/machine.rs | 4 +- rust-sdk/src/olm.udl | 1 + 5 files changed, 32 insertions(+), 28 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 59e122d518..04a51c723c 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 @@ -174,14 +174,15 @@ internal class QrCodeVerification( get() { refreshData() val state = when { - this.inner.isDone -> VerificationTxState.Verified + this.inner.isDone -> VerificationTxState.Verified + this.inner.hasBeenConfirmed -> VerificationTxState.WaitingOtherReciprocateConfirm this.inner.otherSideScanned -> VerificationTxState.QrScannedByOther - this.inner.isCancelled -> { + this.inner.isCancelled -> { val cancelCode = safeValueOf(this.inner.cancelCode) val byMe = this.inner.cancelledByUs ?: false VerificationTxState.Cancelled(cancelCode, byMe) } - else -> { + else -> { VerificationTxState.None } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/verification/RustVerificationService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/verification/RustVerificationService.kt index d9af61029c..b326639de7 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/verification/RustVerificationService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/verification/RustVerificationService.kt @@ -116,26 +116,25 @@ constructor( // } } - suspend fun onEvent(event: Event) { - when (event.getClearType()) { - EventType.KEY_VERIFICATION_START -> {} - EventType.KEY_VERIFICATION_CANCEL -> {} - EventType.KEY_VERIFICATION_ACCEPT -> {} - EventType.KEY_VERIFICATION_KEY -> { - onKeyReceived(event) - } - EventType.KEY_VERIFICATION_MAC -> {} - EventType.KEY_VERIFICATION_READY -> {} - EventType.KEY_VERIFICATION_DONE -> { - onDone(event) - } - MessageType.MSGTYPE_VERIFICATION_REQUEST -> { - onRequestReceived(event) - } - else -> { - // ignore - } - } + fun onEvent(event: Event) = when (event.getClearType()) { + EventType.KEY_VERIFICATION_START -> onStart(event) + EventType.KEY_VERIFICATION_CANCEL -> {} + EventType.KEY_VERIFICATION_ACCEPT -> {} + EventType.KEY_VERIFICATION_KEY -> onKey(event) + EventType.KEY_VERIFICATION_MAC -> {} + EventType.KEY_VERIFICATION_READY -> {} + EventType.KEY_VERIFICATION_DONE -> onDone(event) + MessageType.MSGTYPE_VERIFICATION_REQUEST -> onRequest(event) + else -> {} + } + + private fun onStart(event: Event) { + val content = event.getClearContent().toModel() ?: return + val flowId = content.transactionId ?: return + val sender = event.senderId ?: return + + val verification = this.getExistingTransaction(sender, flowId) ?: return + dispatchTxUpdated(verification) } private fun onDone(event: Event) { @@ -146,7 +145,8 @@ constructor( val verification = this.getExistingTransaction(sender, flowId) ?: return dispatchTxUpdated(verification) } - private fun onKeyReceived(event: Event) { + + private fun onKey(event: Event) { val content = event.getClearContent().toModel() ?: return val flowId = content.transactionId ?: return val sender = event.senderId ?: return @@ -155,7 +155,7 @@ constructor( dispatchTxUpdated(verification) } - private fun onRequestReceived(event: Event) { + private fun onRequest(event: Event) { val content = event.getClearContent().toModel() ?: return val flowId = content.transactionId val sender = event.senderId ?: return diff --git a/rust-sdk/Cargo.toml b/rust-sdk/Cargo.toml index 30556a48b4..7d1b6e73c2 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 = "b62f725bead1f44b2038784e5321909f71a4af4a" +rev = "3bd43a16086415c17faa969432f18d9ed9e9c272" [dependencies.matrix-sdk-crypto] git = "https://github.com/matrix-org/matrix-rust-sdk/" -rev = "b62f725bead1f44b2038784e5321909f71a4af4a" +rev = "3bd43a16086415c17faa969432f18d9ed9e9c272" features = ["sled_cryptostore"] [dependencies.tokio] diff --git a/rust-sdk/src/machine.rs b/rust-sdk/src/machine.rs index 417cf54c83..aa2b581e07 100644 --- a/rust-sdk/src/machine.rs +++ b/rust-sdk/src/machine.rs @@ -78,6 +78,7 @@ pub struct QrCode { pub is_done: bool, pub we_started: bool, pub other_side_scanned: bool, + pub has_been_confirmed: bool, pub cancel_code: Option, pub cancelled_by_us: Option, } @@ -92,7 +93,8 @@ impl From for QrCode { cancel_code: qr.cancel_code().map(|c| c.to_string()), cancelled_by_us: qr.cancelled_by_us(), we_started: qr.we_started(), - other_side_scanned: qr.is_scanned(), + other_side_scanned: qr.has_been_scanned(), + has_been_confirmed: qr.has_been_confirmed(), other_device_id: qr.other_device_id().to_string(), room_id: qr.room_id().map(|r| r.to_string()), } diff --git a/rust-sdk/src/olm.udl b/rust-sdk/src/olm.udl index 47cb8a6c08..9b488d6962 100644 --- a/rust-sdk/src/olm.udl +++ b/rust-sdk/src/olm.udl @@ -93,6 +93,7 @@ dictionary QrCode { string? cancel_code; string? room_id; boolean we_started; + boolean has_been_confirmed; boolean? cancelled_by_us; boolean is_done; boolean is_cancelled;