From c8ca93c9241018b112822370f9aa7448b24df619 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Thu, 1 Oct 2020 11:17:27 +0200 Subject: [PATCH] crytpo: Let the verification machine hold on to a raw CryptoStore. This will later be useful when our higher level store wrapper holds on to a verification machine to return higher level Device objects. --- matrix_sdk_crypto/src/identities/device.rs | 6 +++--- matrix_sdk_crypto/src/identities/manager.rs | 2 +- matrix_sdk_crypto/src/identities/user.rs | 7 ++----- matrix_sdk_crypto/src/key_request.rs | 8 +++----- matrix_sdk_crypto/src/machine.rs | 3 ++- matrix_sdk_crypto/src/store/mod.rs | 4 ++-- matrix_sdk_crypto/src/verification/machine.rs | 16 +++++++--------- matrix_sdk_crypto/src/verification/sas/mod.rs | 14 +++++++------- 8 files changed, 27 insertions(+), 33 deletions(-) diff --git a/matrix_sdk_crypto/src/identities/device.rs b/matrix_sdk_crypto/src/identities/device.rs index 404a5b2a..71b36418 100644 --- a/matrix_sdk_crypto/src/identities/device.rs +++ b/matrix_sdk_crypto/src/identities/device.rs @@ -40,7 +40,7 @@ use crate::{ error::{EventError, OlmError, OlmResult, SignatureError}, identities::{OwnUserIdentity, UserIdentities}, olm::Utility, - store::{caches::ReadOnlyUserDevices, Result as StoreResult, Store}, + store::{caches::ReadOnlyUserDevices, CryptoStore, Result as StoreResult}, verification::VerificationMachine, Sas, ToDeviceRequest, }; @@ -122,7 +122,7 @@ impl Device { content: Value, ) -> OlmResult { self.inner - .encrypt(self.verification_machine.store.clone(), event_type, content) + .encrypt(&**self.verification_machine.store, event_type, content) .await } } @@ -321,7 +321,7 @@ impl ReadOnlyDevice { pub(crate) async fn encrypt( &self, - store: Store, + store: &dyn CryptoStore, event_type: EventType, content: Value, ) -> OlmResult { diff --git a/matrix_sdk_crypto/src/identities/manager.rs b/matrix_sdk_crypto/src/identities/manager.rs index a855dc76..572ed522 100644 --- a/matrix_sdk_crypto/src/identities/manager.rs +++ b/matrix_sdk_crypto/src/identities/manager.rs @@ -381,7 +381,7 @@ pub(crate) mod test { fn manager() -> IdentityManager { let user_id = Arc::new(user_id()); - let store = Store::new(user_id.clone(), Box::new(MemoryStore::new())); + let store = Store::new(user_id.clone(), Arc::new(Box::new(MemoryStore::new()))); IdentityManager::new(user_id, Arc::new(device_id()), store) } diff --git a/matrix_sdk_crypto/src/identities/user.rs b/matrix_sdk_crypto/src/identities/user.rs index ec9cb4f2..a23dbc96 100644 --- a/matrix_sdk_crypto/src/identities/user.rs +++ b/matrix_sdk_crypto/src/identities/user.rs @@ -667,7 +667,7 @@ pub(crate) mod test { Device, ReadOnlyDevice, }, olm::ReadOnlyAccount, - store::{MemoryStore, Store}, + store::MemoryStore, verification::VerificationMachine, }; @@ -736,10 +736,7 @@ pub(crate) mod test { let verification_machine = VerificationMachine::new( ReadOnlyAccount::new(second.user_id(), second.device_id()), - Store::new( - Arc::new(second.user_id().clone()), - Box::new(MemoryStore::new()), - ), + Arc::new(Box::new(MemoryStore::new())), ); let first = Device { diff --git a/matrix_sdk_crypto/src/key_request.rs b/matrix_sdk_crypto/src/key_request.rs index 787c48e2..d45c9c16 100644 --- a/matrix_sdk_crypto/src/key_request.rs +++ b/matrix_sdk_crypto/src/key_request.rs @@ -93,9 +93,7 @@ impl Device { event_type: EventType, content: Value, ) -> OlmResult { - self.inner - .encrypt(self.store.clone(), event_type, content) - .await + self.inner.encrypt(&*self.store, event_type, content).await } } @@ -651,7 +649,7 @@ mod test { fn bob_machine() -> KeyRequestMachine { let user_id = Arc::new(bob_id()); - let store = Store::new(user_id.clone(), Box::new(MemoryStore::new())); + let store = Store::new(user_id.clone(), Arc::new(Box::new(MemoryStore::new()))); KeyRequestMachine::new( user_id, @@ -663,7 +661,7 @@ mod test { fn get_machine() -> KeyRequestMachine { let user_id = Arc::new(alice_id()); - let store = Store::new(user_id.clone(), Box::new(MemoryStore::new())); + let store = Store::new(user_id.clone(), Arc::new(Box::new(MemoryStore::new()))); KeyRequestMachine::new( user_id, diff --git a/matrix_sdk_crypto/src/machine.rs b/matrix_sdk_crypto/src/machine.rs index 7db667b2..23e5878e 100644 --- a/matrix_sdk_crypto/src/machine.rs +++ b/matrix_sdk_crypto/src/machine.rs @@ -125,8 +125,9 @@ impl OlmMachine { ) -> Self { let user_id = Arc::new(user_id.clone()); - let store = Store::new(user_id.clone(), store); + let store = Arc::new(store); let verification_machine = VerificationMachine::new(account.clone(), store.clone()); + let store = Store::new(user_id.clone(), store); let device_id: Arc = Arc::new(device_id); let outbound_group_sessions = Arc::new(DashMap::new()); let key_request_machine = KeyRequestMachine::new( diff --git a/matrix_sdk_crypto/src/store/mod.rs b/matrix_sdk_crypto/src/store/mod.rs index 6fc04bb4..a412f657 100644 --- a/matrix_sdk_crypto/src/store/mod.rs +++ b/matrix_sdk_crypto/src/store/mod.rs @@ -93,10 +93,10 @@ pub(crate) struct Store { } impl Store { - pub fn new(user_id: Arc, store: Box) -> Self { + pub fn new(user_id: Arc, store: Arc>) -> Self { Self { user_id, - inner: Arc::new(store), + inner: store, } } diff --git a/matrix_sdk_crypto/src/verification/machine.rs b/matrix_sdk_crypto/src/verification/machine.rs index 12ca4bc6..ee12d562 100644 --- a/matrix_sdk_crypto/src/verification/machine.rs +++ b/matrix_sdk_crypto/src/verification/machine.rs @@ -27,20 +27,20 @@ use matrix_sdk_common::{ use super::sas::{content_to_request, Sas}; use crate::{ requests::{OutgoingRequest, ToDeviceRequest}, - store::{CryptoStoreError, Store}, + store::{CryptoStore, CryptoStoreError}, ReadOnlyAccount, ReadOnlyDevice, }; #[derive(Clone, Debug)] pub struct VerificationMachine { account: ReadOnlyAccount, - pub(crate) store: Store, + pub(crate) store: Arc>, verifications: Arc>, outgoing_to_device_messages: Arc>, } impl VerificationMachine { - pub(crate) fn new(account: ReadOnlyAccount, store: Store) -> Self { + pub(crate) fn new(account: ReadOnlyAccount, store: Arc>) -> Self { Self { account, store, @@ -233,7 +233,7 @@ mod test { use super::{Sas, VerificationMachine}; use crate::{ requests::OutgoingRequests, - store::{CryptoStore, MemoryStore, Store}, + store::{CryptoStore, MemoryStore}, verification::test::{get_content_from_request, wrap_any_to_device_content}, ReadOnlyAccount, ReadOnlyDevice, }; @@ -258,7 +258,7 @@ mod test { let alice = ReadOnlyAccount::new(&alice_id(), &alice_device_id()); let bob = ReadOnlyAccount::new(&bob_id(), &bob_device_id()); let store = MemoryStore::new(); - let bob_store = Store::new(Arc::new(bob_id()), Box::new(MemoryStore::new())); + let bob_store: Arc> = Arc::new(Box::new(MemoryStore::new())); let bob_device = ReadOnlyDevice::from_account(&bob).await; let alice_device = ReadOnlyDevice::from_account(&alice).await; @@ -269,8 +269,7 @@ mod test { .await .unwrap(); - let machine = - VerificationMachine::new(alice, Store::new(Arc::new(alice_id()), Box::new(store))); + let machine = VerificationMachine::new(alice, Arc::new(Box::new(store))); let (bob_sas, start_content) = Sas::start(bob, alice_device, bob_store, None); machine .receive_event(&mut wrap_any_to_device_content( @@ -286,9 +285,8 @@ mod test { #[test] fn create() { let alice = ReadOnlyAccount::new(&alice_id(), &alice_device_id()); - let user_id = Arc::new(alice_id()); let store = MemoryStore::new(); - let _ = VerificationMachine::new(alice, Store::new(user_id, Box::new(store))); + let _ = VerificationMachine::new(alice, Arc::new(Box::new(store))); } #[tokio::test] diff --git a/matrix_sdk_crypto/src/verification/sas/mod.rs b/matrix_sdk_crypto/src/verification/sas/mod.rs index c52eff3f..95d61541 100644 --- a/matrix_sdk_crypto/src/verification/sas/mod.rs +++ b/matrix_sdk_crypto/src/verification/sas/mod.rs @@ -34,7 +34,7 @@ use matrix_sdk_common::{ use crate::{ identities::{LocalTrust, ReadOnlyDevice, UserIdentities}, - store::{CryptoStoreError, Store}, + store::{CryptoStore, CryptoStoreError}, ReadOnlyAccount, ToDeviceRequest, }; @@ -47,7 +47,7 @@ use sas_state::{ /// Short authentication string object. pub struct Sas { inner: Arc>, - store: Store, + store: Arc>, account: ReadOnlyAccount, other_device: ReadOnlyDevice, other_identity: Option, @@ -104,7 +104,7 @@ impl Sas { pub(crate) fn start( account: ReadOnlyAccount, other_device: ReadOnlyDevice, - store: Store, + store: Arc>, other_identity: Option, ) -> (Sas, StartEventContent) { let (inner, content) = InnerSas::start( @@ -139,7 +139,7 @@ impl Sas { pub(crate) fn from_start_event( account: ReadOnlyAccount, other_device: ReadOnlyDevice, - store: Store, + store: Arc>, event: &ToDeviceEvent, other_identity: Option, ) -> Result { @@ -654,7 +654,7 @@ mod test { }; use crate::{ - store::{MemoryStore, Store}, + store::{CryptoStore, MemoryStore}, verification::test::{get_content_from_request, wrap_any_to_device_content}, ReadOnlyAccount, ReadOnlyDevice, }; @@ -776,8 +776,8 @@ mod test { let bob = ReadOnlyAccount::new(&bob_id(), &bob_device_id()); let bob_device = ReadOnlyDevice::from_account(&bob).await; - let alice_store = Store::new(Arc::new(alice_id()), Box::new(MemoryStore::new())); - let bob_store = Store::new(Arc::new(bob_id()), Box::new(MemoryStore::new())); + let alice_store: Arc> = Arc::new(Box::new(MemoryStore::new())); + let bob_store: Arc> = Arc::new(Box::new(MemoryStore::new())); bob_store .save_devices(&[alice_device.clone()])