diff --git a/crates/matrix-sdk-sled/src/cryptostore.rs b/crates/matrix-sdk-sled/src/cryptostore.rs index 0eaeba84b..2232ef972 100644 --- a/crates/matrix-sdk-sled/src/cryptostore.rs +++ b/crates/matrix-sdk-sled/src/cryptostore.rs @@ -171,7 +171,7 @@ struct TrackedUser { #[derive(Clone)] pub struct SledStore { account_info: Arc>>, - store_cipher: Arc>, + store_cipher: Option>, path: Option, inner: Db, @@ -222,11 +222,10 @@ impl SledStore { .map_err(|e| CryptoStoreError::Backend(anyhow!(e)))?; let store_cipher = if let Some(passphrase) = passphrase { - Some(Self::get_or_create_store_cipher(passphrase, &db)?) + Some(Self::get_or_create_store_cipher(passphrase, &db)?.into()) } else { None - } - .into(); + }; SledStore::open_helper(db, Some(path), store_cipher) } @@ -235,7 +234,7 @@ impl SledStore { /// The given passphrase will be used to encrypt private data. pub fn open_with_database( db: Db, - store_cipher: Arc>, + store_cipher: Option>, ) -> Result { SledStore::open_helper(db, None, store_cipher) } @@ -245,7 +244,7 @@ impl SledStore { } fn serialize_value(&self, event: &impl Serialize) -> Result, CryptoStoreError> { - if let Some(key) = &*self.store_cipher { + if let Some(key) = &self.store_cipher { key.encrypt_value(event).map_err(|e| CryptoStoreError::Backend(anyhow!(e))) } else { Ok(serde_json::to_vec(event)?) @@ -256,7 +255,7 @@ impl SledStore { &self, event: &[u8], ) -> Result { - if let Some(key) = &*self.store_cipher { + if let Some(key) = &self.store_cipher { key.decrypt_value(event).map_err(|e| CryptoStoreError::Backend(anyhow!(e))) } else { Ok(serde_json::from_slice(event)?) @@ -264,7 +263,7 @@ impl SledStore { } fn encode_key(&self, table_name: &str, key: T) -> Vec { - if let Some(store_cipher) = &*self.store_cipher { + if let Some(store_cipher) = &self.store_cipher { key.encode_secure(table_name, store_cipher).to_vec() } else { key.encode() @@ -357,7 +356,7 @@ impl SledStore { fn open_helper( db: Db, path: Option, - store_cipher: Arc>, + store_cipher: Option>, ) -> Result { let account = db.open_tree("account")?; let private_identity = db.open_tree("private_identity")?; diff --git a/crates/matrix-sdk-sled/src/state_store.rs b/crates/matrix-sdk-sled/src/state_store.rs index f49927229..34658e56f 100644 --- a/crates/matrix-sdk-sled/src/state_store.rs +++ b/crates/matrix-sdk-sled/src/state_store.rs @@ -143,7 +143,7 @@ type Result = std::result::Result; pub struct SledStore { path: Option, pub(crate) inner: Db, - store_cipher: Arc>, + store_cipher: Option>, session: Tree, account_data: Tree, members: Tree, @@ -181,7 +181,7 @@ impl SledStore { fn open_helper( db: Db, path: Option, - store_cipher: Option, + store_cipher: Option>, ) -> Result { let session = db.open_tree(SESSION)?; let account_data = db.open_tree(ACCOUNT_DATA)?; @@ -215,7 +215,7 @@ impl SledStore { Ok(Self { path, inner: db, - store_cipher: store_cipher.into(), + store_cipher, session, account_data, members, @@ -256,7 +256,7 @@ impl SledStore { SledStore::open_helper( db, None, - Some(StoreCipher::new().expect("can't create store cipher")), + Some(StoreCipher::new().expect("can't create store cipher").into()), ) .map_err(|e| e.into()) } @@ -275,7 +275,8 @@ impl SledStore { let cipher = StoreCipher::new()?; db.insert("store_cipher".encode(), cipher.export(passphrase)?)?; cipher - }; + } + .into(); SledStore::open_helper(db, Some(path), Some(store_cipher)) } @@ -300,7 +301,7 @@ impl SledStore { } fn serialize_event(&self, event: &impl Serialize) -> Result, SledStoreError> { - if let Some(key) = &*self.store_cipher { + if let Some(key) = &self.store_cipher { Ok(key.encrypt_value(event)?) } else { Ok(serde_json::to_vec(event)?) @@ -311,7 +312,7 @@ impl SledStore { &self, event: &[u8], ) -> Result { - if let Some(key) = &*self.store_cipher { + if let Some(key) = &self.store_cipher { Ok(key.decrypt_value(event)?) } else { Ok(serde_json::from_slice(event)?) @@ -319,7 +320,7 @@ impl SledStore { } fn encode_key(&self, table_name: &str, key: T) -> Vec { - if let Some(store_cipher) = &*self.store_cipher { + if let Some(store_cipher) = &self.store_cipher { key.encode_secure(table_name, store_cipher).to_vec() } else { key.encode()