From 0327b4f8fc197df2a749c21ca140fbecad684dec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Wed, 9 Mar 2022 14:30:49 +0100 Subject: [PATCH] indexeddb: Add methods to create a StoreConfig --- crates/matrix-sdk-indexeddb/Cargo.toml | 2 +- crates/matrix-sdk-indexeddb/src/lib.rs | 56 ++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/crates/matrix-sdk-indexeddb/Cargo.toml b/crates/matrix-sdk-indexeddb/Cargo.toml index 9e0d6d918..06c7b1168 100644 --- a/crates/matrix-sdk-indexeddb/Cargo.toml +++ b/crates/matrix-sdk-indexeddb/Cargo.toml @@ -5,7 +5,7 @@ edition = "2021" [features] default = ["encryption"] -encryption = ["matrix-sdk-crypto"] +encryption = ["matrix-sdk-base/encryption", "matrix-sdk-crypto"] [package.metadata.docs.rs] default-target = "wasm32-unknown-unknown" diff --git a/crates/matrix-sdk-indexeddb/src/lib.rs b/crates/matrix-sdk-indexeddb/src/lib.rs index 8ecfae46d..db5056035 100644 --- a/crates/matrix-sdk-indexeddb/src/lib.rs +++ b/crates/matrix-sdk-indexeddb/src/lib.rs @@ -1,3 +1,6 @@ +#[cfg(target_arch = "wasm32")] +use matrix_sdk_base::store::StoreConfig; + mod safe_encode; #[cfg(target_arch = "wasm32")] @@ -12,3 +15,56 @@ mod cryptostore; pub use cryptostore::IndexeddbStore as CryptoStore; #[cfg(target_arch = "wasm32")] pub use state_store::IndexeddbStore as StateStore; + +#[cfg(target_arch = "wasm32")] +#[cfg(feature = "encryption")] +/// Create a [`StateStore`] and a [`CryptoStore`] that use the same name and +/// passphrase. +pub async fn open_stores_with_name( + name: impl Into, + passphrase: Option<&str>, +) -> Result<(Box, Box), anyhow::Error> { + let name = name.into(); + + if let Some(passphrase) = passphrase { + let state_store = StateStore::open_with_passphrase(name.clone(), passphrase).await?; + let crypto_store = CryptoStore::open_with_passphrase(name, passphrase).await?; + Ok((Box::new(state_store), Box::new(crypto_store))) + } else { + let state_store = StateStore::open_with_name(name.clone()).await?; + let crypto_store = CryptoStore::open_with_name(name).await?; + Ok((Box::new(state_store), Box::new(crypto_store))) + } +} + +#[cfg(target_arch = "wasm32")] +/// Create a [`StoreConfig`] with an opened indexeddb [`StateStore`] that uses +/// the given name and passphrase. If `encryption` is enabled, a [`CryptoStore`] +/// with the same parameters is also opened. +pub async fn make_config( + name: impl Into, + passphrase: Option<&str>, +) -> Result { + let mut config = StoreConfig::new(); + let name = name.into(); + + #[cfg(feature = "encryption")] + { + let (state_store, crypto_store) = open_stores_with_name(name, passphrase).await?; + config = config.state_store(state_store); + config = config.crypto_store(crypto_store); + } + + #[cfg(not(feature = "encryption"))] + { + let state_store = if let Some(passphrase) = passphrase { + StateStore::open_with_passphrase(name, passphrase).await? + } else { + StateStore::open_with_name(name).await? + }; + + config = config.state_store(Box::new(state_store)); + } + + Ok(config) +}