From 2e80073f28a51216943ca47b3cfcd8a0007bbd95 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Tue, 7 Feb 2023 18:13:00 +0100 Subject: [PATCH] refactor(crypto): Use IntoCryptoStore for OlmMachine::with_store --- bindings/matrix-sdk-crypto-js/src/machine.rs | 35 ++++++++------------ crates/matrix-sdk-crypto/src/machine.rs | 11 +++--- crates/matrix-sdk-crypto/src/store/traits.rs | 6 ++++ 3 files changed, 24 insertions(+), 28 deletions(-) diff --git a/bindings/matrix-sdk-crypto-js/src/machine.rs b/bindings/matrix-sdk-crypto-js/src/machine.rs index 66b06e04d..079e632f3 100644 --- a/bindings/matrix-sdk-crypto-js/src/machine.rs +++ b/bindings/matrix-sdk-crypto-js/src/machine.rs @@ -70,16 +70,7 @@ impl OlmMachine { future_to_promise(async move { let store = match (store_name, store_passphrase) { - // We need this `#[cfg]` because `IndexeddbCryptoStore` - // implements `CryptoStore` only on `target_arch = - // "wasm32"`. Without that, we could have a compilation - // error when checking the entire workspace. In - // practise, it doesn't impact this crate because it's - // always compiled for `wasm32`. - #[cfg(target_arch = "wasm32")] (Some(store_name), Some(mut store_passphrase)) => { - use std::sync::Arc; - use zeroize::Zeroize; let store = Some( @@ -87,8 +78,7 @@ impl OlmMachine { &store_name, &store_passphrase, ) - .await - .map(Arc::new)?, + .await?, ); store_passphrase.zeroize(); @@ -96,15 +86,9 @@ impl OlmMachine { store } - #[cfg(target_arch = "wasm32")] - (Some(store_name), None) => { - use std::sync::Arc; - Some( - matrix_sdk_indexeddb::IndexeddbCryptoStore::open_with_name(&store_name) - .await - .map(Arc::new)?, - ) - } + (Some(store_name), None) => Some( + matrix_sdk_indexeddb::IndexeddbCryptoStore::open_with_name(&store_name).await?, + ), (None, Some(_)) => { return Err(anyhow::Error::msg( @@ -113,11 +97,18 @@ impl OlmMachine { )) } - _ => None, + (None, None) => None, }; Ok(OlmMachine { inner: match store { + // We need this `#[cfg]` because `IndexeddbCryptoStore` + // implements `CryptoStore` only on `target_arch = + // "wasm32"`. Without that, we could have a compilation + // error when checking the entire workspace. In practice, + // it doesn't impact this crate because it's always + // compiled for `wasm32`. + #[cfg(target_arch = "wasm32")] Some(store) => { matrix_sdk_crypto::OlmMachine::with_store( user_id.as_ref(), @@ -126,7 +117,7 @@ impl OlmMachine { ) .await? } - None => { + _ => { matrix_sdk_crypto::OlmMachine::new(user_id.as_ref(), device_id.as_ref()) .await } diff --git a/crates/matrix-sdk-crypto/src/machine.rs b/crates/matrix-sdk-crypto/src/machine.rs index 0c5cdc186..a74552766 100644 --- a/crates/matrix-sdk-crypto/src/machine.rs +++ b/crates/matrix-sdk-crypto/src/machine.rs @@ -66,8 +66,8 @@ use crate::{ requests::{IncomingResponse, OutgoingRequest, UploadSigningKeysRequest}, session_manager::{GroupSessionManager, SessionManager}, store::{ - Changes, CryptoStore, DeviceChanges, IdentityChanges, MemoryStore, Result as StoreResult, - SecretImportError, Store, + Changes, CryptoStore, DeviceChanges, IdentityChanges, IntoCryptoStore, MemoryStore, + Result as StoreResult, SecretImportError, Store, }, types::{ events::{ @@ -145,9 +145,7 @@ impl OlmMachine { /// /// * `device_id` - The unique id of the device that owns this machine. pub async fn new(user_id: &UserId, device_id: &DeviceId) -> Self { - let store: Arc = Arc::new(MemoryStore::new()); - - OlmMachine::with_store(user_id, device_id, store) + OlmMachine::with_store(user_id, device_id, MemoryStore::new()) .await .expect("Reading and writing to the memory store always succeeds") } @@ -235,8 +233,9 @@ impl OlmMachine { pub async fn with_store( user_id: &UserId, device_id: &DeviceId, - store: Arc, + store: impl IntoCryptoStore, ) -> StoreResult { + let store = store.into_crypto_store(); let account = match store.load_account().await? { Some(account) => { if user_id != account.user_id() || device_id != account.device_id() { diff --git a/crates/matrix-sdk-crypto/src/store/traits.rs b/crates/matrix-sdk-crypto/src/store/traits.rs index 709309012..86ebd87bf 100644 --- a/crates/matrix-sdk-crypto/src/store/traits.rs +++ b/crates/matrix-sdk-crypto/src/store/traits.rs @@ -189,3 +189,9 @@ where self } } + +impl IntoCryptoStore for Arc { + fn into_crypto_store(self) -> Arc { + self + } +}