diff --git a/matrix_sdk_base/examples/state_inspector.rs b/matrix_sdk_base/examples/state_inspector.rs index e0688b12..9bce2d0d 100644 --- a/matrix_sdk_base/examples/state_inspector.rs +++ b/matrix_sdk_base/examples/state_inspector.rs @@ -247,7 +247,7 @@ impl Printer { impl Inspector { fn new(database_path: &str, json: bool, color: bool) -> Self { let printer = Printer::new(json, color); - let store = Store::open_default(database_path, None).unwrap(); + let (store, _) = Store::open_default(database_path, None).unwrap(); Self { store, printer } } diff --git a/matrix_sdk_base/src/client.rs b/matrix_sdk_base/src/client.rs index d56ef51b..d3dfe520 100644 --- a/matrix_sdk_base/src/client.rs +++ b/matrix_sdk_base/src/client.rs @@ -279,7 +279,8 @@ impl BaseClient { /// * `config` - An optional session if the user already has one from a /// previous login call. pub fn new_with_config(config: BaseClientConfig) -> Result { - let store = if let Some(path) = &config.store_path { + #[cfg(not(target_arch = "wasm32"))] + let (store, sled_store) = if let Some(path) = &config.store_path { if config.passphrase.is_some() { info!("Opening an encrypted store in path {}", path.display()); } else { @@ -289,6 +290,28 @@ impl BaseClient { } else { Store::open_temporrary()? }; + #[cfg(target_arch = "wasm32")] + let store = Store::open_memory_store(); + + #[cfg(not(target_arch = "wasm32"))] + let crypto_store = if config.crypto_store.is_none() { + #[cfg(feature = "sled_cryptostore")] + let store: Option> = Some(Box::new( + matrix_sdk_crypto::store::SledStore::open_with_database( + sled_store, + config.passphrase.as_deref().map(|p| p.as_str()), + ) + .map_err(OlmError::Store)?, + )); + #[cfg(not(feature = "sled_cryptostore"))] + let store = config.crypto_store; + + store + } else { + config.crypto_store + }; + #[cfg(target_arch = "wasm32")] + let crypto_store = config.crypto_store; Ok(BaseClient { session: store.session.clone(), @@ -297,7 +320,7 @@ impl BaseClient { #[cfg(feature = "encryption")] olm: Mutex::new(None).into(), #[cfg(feature = "encryption")] - cryptostore: Mutex::new(config.crypto_store).into(), + cryptostore: Mutex::new(crypto_store).into(), store_path: config.store_path.into(), store_passphrase: config.passphrase.into(), event_emitter: RwLock::new(None).into(), diff --git a/matrix_sdk_base/src/store/mod.rs b/matrix_sdk_base/src/store/mod.rs index 3e00052d..158283e9 100644 --- a/matrix_sdk_base/src/store/mod.rs +++ b/matrix_sdk_base/src/store/mod.rs @@ -25,6 +25,7 @@ use matrix_sdk_common::{ locks::RwLock, AsyncTraitDeps, }; +use sled::Db; use crate::{ deserialized_responses::{MemberEvent, StrippedMemberEvent}, @@ -33,9 +34,12 @@ use crate::{ }; mod memory_store; +#[cfg(not(target_arch = "wasm32"))] mod sled_store; -use self::{memory_store::MemoryStore, sled_store::SledStore}; +use self::memory_store::MemoryStore; +#[cfg(not(target_arch = "wasm32"))] +use self::sled_store::SledStore; #[derive(Debug, thiserror::Error)] pub enum StoreError { @@ -138,20 +142,20 @@ impl Store { Self::new(inner) } - pub fn open_default(path: impl AsRef, passphrase: Option<&str>) -> Result { + pub fn open_default(path: impl AsRef, passphrase: Option<&str>) -> Result<(Self, Db)> { let inner = if let Some(passphrase) = passphrase { - Box::new(SledStore::open_with_passphrase(path, passphrase)?) + SledStore::open_with_passphrase(path, passphrase)? } else { - Box::new(SledStore::open_with_path(path)?) + SledStore::open_with_path(path)? }; - Ok(Self::new(inner)) + Ok((Self::new(Box::new(inner.clone())), inner.inner)) } - pub fn open_temporrary() -> Result { - let inner = Box::new(SledStore::open()?); + pub fn open_temporrary() -> Result<(Self, Db)> { + let inner = SledStore::open()?; - Ok(Self::new(inner)) + Ok((Self::new(Box::new(inner.clone())), inner.inner)) } pub(crate) fn get_bare_room(&self, room_id: &RoomId) -> Option { diff --git a/matrix_sdk_base/src/store/sled_store/mod.rs b/matrix_sdk_base/src/store/sled_store/mod.rs index b65c24c9..aa3b1d11 100644 --- a/matrix_sdk_base/src/store/sled_store/mod.rs +++ b/matrix_sdk_base/src/store/sled_store/mod.rs @@ -81,7 +81,7 @@ impl From for StoreError { #[derive(Debug, Clone)] pub struct SledStore { - inner: Db, + pub(crate) inner: Db, store_key: Arc>, session: Tree, account_data: Tree, diff --git a/matrix_sdk_crypto/src/store/mod.rs b/matrix_sdk_crypto/src/store/mod.rs index caaed436..4dd69e06 100644 --- a/matrix_sdk_crypto/src/store/mod.rs +++ b/matrix_sdk_crypto/src/store/mod.rs @@ -46,6 +46,8 @@ pub(crate) mod sled; #[cfg(feature = "sqlite_cryptostore")] pub(crate) mod sqlite; +#[cfg(feature = "sled_cryptostore")] +pub use self::sled::SledStore; pub use memorystore::MemoryStore; pub use pickle_key::{EncryptedPickleKey, PickleKey}; #[cfg(not(target_arch = "wasm32"))] diff --git a/matrix_sdk_crypto/src/store/sled.rs b/matrix_sdk_crypto/src/store/sled.rs index 10e71fd5..ddc28ab7 100644 --- a/matrix_sdk_crypto/src/store/sled.rs +++ b/matrix_sdk_crypto/src/store/sled.rs @@ -81,6 +81,8 @@ impl From> for CryptoStoreError { } impl SledStore { + /// Open the sled based cryptostore at the given path using the given + /// passphrase to encrypt private data. pub fn open_with_passphrase(path: impl AsRef, passphrase: Option<&str>) -> Result { let path = path.as_ref().join("matrix-sdk-crypto"); let db = Config::new().temporary(false).path(path).open()?; @@ -88,6 +90,12 @@ impl SledStore { SledStore::open_helper(db, passphrase) } + /// Create a sled based cryptostore using the given sled database. + /// The given passphrase will be used to encrypt private data. + pub fn open_with_database(db: Db, passphrase: Option<&str>) -> Result { + SledStore::open_helper(db, passphrase) + } + fn open_helper(db: Db, passphrase: Option<&str>) -> Result { let account = db.open_tree("account")?; let private_identity = db.open_tree("private_identity")?; @@ -171,7 +179,7 @@ impl SledStore { Ok(()) } - pub async fn save_changes(&self, changes: Changes) -> Result<()> { + async fn save_changes(&self, changes: Changes) -> Result<()> { let account_pickle = if let Some(a) = changes.account { Some(a.pickle(self.get_pickle_mode()).await) } else {