refactor(crypto): Use IntoCryptoStore for OlmMachine::with_store

This commit is contained in:
Jonas Platte
2023-02-07 18:13:00 +01:00
committed by Jonas Platte
parent c3d5932a0f
commit 2e80073f28
3 changed files with 24 additions and 28 deletions

View File

@@ -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
}

View File

@@ -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<dyn CryptoStore> = 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<dyn CryptoStore>,
store: impl IntoCryptoStore,
) -> StoreResult<Self> {
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() {

View File

@@ -189,3 +189,9 @@ where
self
}
}
impl IntoCryptoStore for Arc<dyn CryptoStore> {
fn into_crypto_store(self) -> Arc<dyn CryptoStore> {
self
}
}