From a144b1f7b5b1dc4af18382760368781aa755ef36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Tue, 15 Jun 2021 13:16:30 +0200 Subject: [PATCH] rust: Fix the build and update our deps --- .../internal/crypto/DefaultCryptoService.kt | 13 +- rust-sdk/Cargo.toml | 12 +- rust-sdk/src/error.rs | 2 +- rust-sdk/src/machine.rs | 119 ++++++++++++++---- rust-sdk/src/olm.udl | 6 +- rust-sdk/src/responses.rs | 4 +- 6 files changed, 117 insertions(+), 39 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt index c3971058d3..aa307d7c8e 100755 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt @@ -933,17 +933,20 @@ internal class DefaultCryptoService @Inject constructor( cryptoCoroutineScope.launch(coroutineDispatchers.crypto) { val requestPair = olmMachine!!.requestRoomKey(event) - if (requestPair.cancellation != null) { - when (requestPair.cancellation) { + val cancellation = requestPair.cancellation + val request = requestPair.keyRequest + + if (cancellation != null) { + when (cancellation) { is Request.ToDevice -> { - sendToDevice(requestPair.cancellation) + sendToDevice(cancellation) } } } - when(requestPair.keyRequest) { + when(request) { is Request.ToDevice -> { - sendToDevice(requestPair.keyRequest) + sendToDevice(request) } } } diff --git a/rust-sdk/Cargo.toml b/rust-sdk/Cargo.toml index b0702e9955..138c937c0c 100644 --- a/rust-sdk/Cargo.toml +++ b/rust-sdk/Cargo.toml @@ -16,7 +16,7 @@ http = "0.2.4" thiserror = "1.0.25" tracing = "0.1.26" tracing-subscriber = "0.2.18" -uniffi = "0.9.0" +uniffi = "0.12.0" [dependencies.js_int] version = "0.2.0" @@ -24,11 +24,11 @@ features = ["lax_deserialize"] [dependencies.matrix-sdk-common] git = "https://github.com/matrix-org/matrix-rust-sdk/" -rev = "e058191b992f261a1a848b7ec521badf173881d6" +branch = "verification-qr" [dependencies.matrix-sdk-crypto] git = "https://github.com/matrix-org/matrix-rust-sdk/" -rev = "e058191b992f261a1a848b7ec521badf173881d6" +branch = "verification-qr" features = ["sled_cryptostore"] [dependencies.tokio] @@ -36,5 +36,9 @@ version = "1.6.0" default_features = false features = ["rt-multi-thread"] +[dependencies.ruma] +version = "*" +features = ["client-api"] + [build-dependencies] -uniffi_build = "0.9.0" +uniffi_build = "0.12.0" diff --git a/rust-sdk/src/error.rs b/rust-sdk/src/error.rs index 4a17011208..aa24c303b2 100644 --- a/rust-sdk/src/error.rs +++ b/rust-sdk/src/error.rs @@ -1,4 +1,4 @@ -use matrix_sdk_common::identifiers::Error as RumaIdentifierError; +use ruma::identifiers::Error as RumaIdentifierError; use matrix_sdk_crypto::{ store::CryptoStoreError as InnerStoreError, KeyExportError, MegolmError, OlmError, }; diff --git a/rust-sdk/src/machine.rs b/rust-sdk/src/machine.rs index e9ac6d3614..4c6e42d02f 100644 --- a/rust-sdk/src/machine.rs +++ b/rust-sdk/src/machine.rs @@ -4,31 +4,33 @@ use std::{ io::Cursor, }; -use serde::{Deserialize, Serialize}; -use serde_json::value::RawValue; -use tokio::runtime::Runtime; - -use matrix_sdk_common::{ - api::r0::{ - keys::{ - claim_keys::Response as KeysClaimResponse, get_keys::Response as KeysQueryResponse, - upload_keys::Response as KeysUploadResponse, +use js_int::UInt; +use ruma::{ + api::{ + client::r0::{ + keys::{ + claim_keys::Response as KeysClaimResponse, get_keys::Response as KeysQueryResponse, + upload_keys::Response as KeysUploadResponse, + }, + sync::sync_events::{DeviceLists as RumaDeviceLists, ToDevice}, + to_device::send_event_to_device::Response as ToDeviceResponse, }, - sync::sync_events::{DeviceLists as RumaDeviceLists, ToDevice}, - to_device::send_event_to_device::Response as ToDeviceResponse, + IncomingResponse, }, - deserialized_responses::AlgorithmInfo, events::{ room::encrypted::EncryptedEventContent, AnyMessageEventContent, EventContent, SyncMessageEvent, }, - identifiers::{DeviceKeyAlgorithm, RoomId, UserId}, - uuid::Uuid, - IncomingResponse, UInt, + DeviceKeyAlgorithm, RoomId, UserId, }; +use serde::{Deserialize, Serialize}; +use serde_json::value::RawValue; +use tokio::runtime::Runtime; +use matrix_sdk_common::{deserialized_responses::AlgorithmInfo, uuid::Uuid}; use matrix_sdk_crypto::{ decrypt_key_export, encrypt_key_export, EncryptionSettings, OlmMachine as InnerMachine, + OutgoingVerificationRequest, Sas as InnerSas, }; use crate::{ @@ -48,7 +50,24 @@ pub struct Sas { pub other_user_id: String, pub other_device_id: String, pub flow_id: String, - pub request: Request, + pub is_cancelled: bool, + pub is_done: bool, + pub can_be_presented: bool, + pub timed_out: bool, +} + +impl From for Sas { + fn from(sas: InnerSas) -> Self { + Self { + other_user_id: sas.other_user_id().to_string(), + other_device_id: sas.other_device_id().to_string(), + flow_id: sas.flow_id().as_str().to_owned(), + is_cancelled: sas.is_cancelled(), + is_done: sas.is_done(), + can_be_presented: sas.can_be_presented(), + timed_out: sas.timed_out(), + } + } } /// A pair of outgoing room key requests, both of those are sendToDevice @@ -207,7 +226,7 @@ impl OlmMachine { /// # Arguments /// /// * `events` - A serialized array of to-device events we received in the - /// current sync resposne. + /// current sync response. /// /// * `device_changes` - The list of devices that have changed in some way /// since the previous sync. @@ -350,7 +369,7 @@ impl OlmMachine { /// **Note**: A room key needs to be shared with the group of users that are /// members in the given room. If this is not done this method will panic. /// - /// The usual flow to encrypt an evnet using this state machine is as + /// The usual flow to encrypt an event using this state machine is as /// follows: /// /// 1. Get the one-time key claim request to establish 1:1 Olm sessions for @@ -539,6 +558,15 @@ impl OlmMachine { Ok(()) } + pub fn get_verification(&self, flow_id: &str) -> Option { + todo!() + // self.inner.get_verification(flow_id).map(|s| s.into()) + } + + pub fn request_verification(&self) { + todo!() + } + pub fn start_verification(&self, device: &Device) -> Result { let user_id = UserId::try_from(device.user_id.clone())?; let device_id = device.device_id.as_str().into(); @@ -547,13 +575,54 @@ impl OlmMachine { .block_on(self.inner.get_device(&user_id, device_id))? .unwrap(); - let (sas, request) = self.runtime.block_on(device.start_verification())?; + // TODO we need to return the request as well. + let (sas, _) = self.runtime.block_on(device.start_verification())?; - Ok(Sas { - other_user_id: sas.other_user_id().to_string(), - other_device_id: sas.other_device_id().to_string(), - flow_id: sas.flow_id().as_str().to_owned(), - request: request.into(), - }) + Ok(sas.into()) + } + + pub fn accept_verification(&self, flow_id: &str) -> Option { + todo!() + // self.inner + // .get_verification(flow_id) + // .and_then(|s| s.accept().map(|r| r.into())) + } + + pub fn cancel_verification(&self, flow_id: &str) -> Option { + todo!() + // self.inner + // .get_verification(flow_id) + // .and_then(|s| s.cancel().map(|r| r.into())) + } + + pub fn confirm_verification( + &self, + flow_id: &str, + ) -> Result, CryptoStoreError> { + todo!() + // let sas = self.inner.get_verification(flow_id); + + // if let Some(sas) = sas { + // let (request, _) = self.runtime.block_on(sas.confirm())?; + // Ok(request.map(|r| r.into())) + // } else { + // Ok(None) + // } + } + + pub fn get_emoji_index(&self, flow_id: &str) -> Option> { + todo!() + // self.inner.get_verification(flow_id).and_then(|s| { + // s.emoji_index() + // .map(|v| v.iter().map(|i| (*i).into()).collect()) + // }) + } + + pub fn get_decimals(&self, flow_id: &str) -> Option> { + todo!() + // self.inner.get_verification(flow_id).and_then(|s| { + // s.decimals() + // .map(|v| [v.0.into(), v.1.into(), v.2.into()].to_vec()) + // }) } } diff --git a/rust-sdk/src/olm.udl b/rust-sdk/src/olm.udl index d7fb9b728e..6f2b42e626 100644 --- a/rust-sdk/src/olm.udl +++ b/rust-sdk/src/olm.udl @@ -67,7 +67,10 @@ dictionary Sas { string other_user_id; string other_device_id; string flow_id; - Request request; + boolean is_done; + boolean is_cancelled; + boolean can_be_presented; + boolean timed_out; }; dictionary KeyRequestPair { @@ -90,7 +93,6 @@ enum RequestType { "ToDevice", }; -[Threadsafe] interface OlmMachine { [Throws=MachineCreationError] constructor([ByRef] string user_id, [ByRef] string device_id, [ByRef] string path); diff --git a/rust-sdk/src/responses.rs b/rust-sdk/src/responses.rs index d7d52fef98..bda045debf 100644 --- a/rust-sdk/src/responses.rs +++ b/rust-sdk/src/responses.rs @@ -3,8 +3,8 @@ use std::{collections::HashMap, convert::TryFrom}; use http::Response; use serde_json::json; -use matrix_sdk_common::{ - api::r0::{ +use ruma::{ + api::client::r0::{ keys::{ claim_keys::Response as KeysClaimResponse, get_keys::Response as KeysQueryResponse, upload_keys::Response as KeysUploadResponse,