From 9fc7d1c78bdfd1c1d552abc2e3ed81147d8f322f Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Tue, 7 Feb 2023 18:15:44 +0100 Subject: [PATCH] refactor(indexeddb): Use IndexeddbCryptoStoreError in CryptoStore impl --- .../matrix-sdk-indexeddb/src/crypto_store.rs | 178 +++++------------- 1 file changed, 44 insertions(+), 134 deletions(-) diff --git a/crates/matrix-sdk-indexeddb/src/crypto_store.rs b/crates/matrix-sdk-indexeddb/src/crypto_store.rs index e06b04107..ca26cd7b4 100644 --- a/crates/matrix-sdk-indexeddb/src/crypto_store.rs +++ b/crates/matrix-sdk-indexeddb/src/crypto_store.rs @@ -317,7 +317,38 @@ impl IndexeddbCryptoStore { fn get_account_info(&self) -> Option { self.account_info.read().unwrap().clone() } +} +// Small hack to have the following macro invocation act as the appropriate +// trait impl block on wasm, but still be compiled on non-wasm as a regular +// impl block otherwise. +// +// The trait impl doesn't compile on non-wasm due to unfulfilled trait bounds, +// this hack allows us to still have most of rust-analyzer's IDE functionality +// within the impl block without having to set it up to check things against +// the wasm target (which would disable many other parts of the codebase). +#[cfg(target_arch = "wasm32")] +macro_rules! impl_crypto_store { + ( $($body:tt)* ) => { + #[async_trait(?Send)] + impl CryptoStore for IndexeddbCryptoStore { + type Error = IndexeddbCryptoStoreError; + + $($body)* + } + }; +} + +#[cfg(not(target_arch = "wasm32"))] +macro_rules! impl_crypto_store { + ( $($body:tt)* ) => { + impl IndexeddbCryptoStore { + $($body)* + } + }; +} + +impl_crypto_store! { async fn save_changes(&self, changes: Changes) -> Result<()> { let mut stores: Vec<&str> = [ (changes.account.is_some() || changes.private_identity.is_some(), KEYS::CORE), @@ -538,7 +569,7 @@ impl IndexeddbCryptoStore { Ok(users) } - async fn load_outbound_group_session( + async fn get_outbound_group_session( &self, room_id: &RoomId, ) -> Result> { @@ -565,9 +596,13 @@ impl IndexeddbCryptoStore { Ok(None) } } - async fn get_outgoing_key_request_helper(&self, key: &str) -> Result> { + + async fn get_outgoing_secret_requests( + &self, + request_id: &TransactionId, + ) -> Result> { // in this internal we expect key to already be escaped or encrypted - let jskey = JsValue::from_str(key); + let jskey = JsValue::from_str(request_id.as_str()); let dbs = [KEYS::OUTGOING_SECRET_REQUESTS, KEYS::UNSENT_SECRET_REQUESTS]; let tx = self.inner.transaction_on_multi_with_mode(&dbs, IdbTransactionMode::Readonly)?; @@ -615,6 +650,11 @@ impl IndexeddbCryptoStore { } } + async fn save_account(&self, account: ReadOnlyAccount) -> Result<()> { + self.save_changes(Changes { account: Some(account), ..Default::default() }) + .await + } + async fn load_identity(&self) -> Result> { if let Some(pickle) = self .inner @@ -829,7 +869,7 @@ impl IndexeddbCryptoStore { .await? .and_then(|i| i.as_string()); if let Some(id) = id { - self.get_outgoing_key_request_helper(&id).await + self.get_outgoing_secret_requests(id.as_str().into()).await } else { Ok(None) } @@ -921,136 +961,6 @@ impl Drop for IndexeddbCryptoStore { } } -#[cfg(target_arch = "wasm32")] -#[async_trait(?Send)] -impl CryptoStore for IndexeddbCryptoStore { - type Error = CryptoStoreError; - - async fn load_account(&self) -> Result, CryptoStoreError> { - self.load_account().await.map_err(|e| e.into()) - } - - async fn save_account(&self, account: ReadOnlyAccount) -> Result<(), CryptoStoreError> { - self.save_changes(Changes { account: Some(account), ..Default::default() }) - .await - .map_err(|e| e.into()) - } - - async fn load_identity(&self) -> Result, CryptoStoreError> { - self.load_identity().await.map_err(|e| e.into()) - } - - async fn save_changes(&self, changes: Changes) -> Result<(), CryptoStoreError> { - self.save_changes(changes).await.map_err(|e| e.into()) - } - - async fn get_sessions( - &self, - sender_key: &str, - ) -> Result>>>, CryptoStoreError> { - self.get_sessions(sender_key).await.map_err(|e| e.into()) - } - - async fn get_inbound_group_session( - &self, - room_id: &RoomId, - session_id: &str, - ) -> Result, CryptoStoreError> { - self.get_inbound_group_session(room_id, session_id).await.map_err(|e| e.into()) - } - - async fn get_inbound_group_sessions( - &self, - ) -> Result, CryptoStoreError> { - self.get_inbound_group_sessions().await.map_err(|e| e.into()) - } - - async fn get_outbound_group_session( - &self, - room_id: &RoomId, - ) -> Result, CryptoStoreError> { - self.load_outbound_group_session(room_id).await.map_err(|e| e.into()) - } - - async fn inbound_group_session_counts(&self) -> Result { - self.inbound_group_session_counts().await.map_err(|e| e.into()) - } - - async fn inbound_group_sessions_for_backup( - &self, - limit: usize, - ) -> Result, CryptoStoreError> { - self.inbound_group_sessions_for_backup(limit).await.map_err(|e| e.into()) - } - - async fn reset_backup_state(&self) -> Result<(), CryptoStoreError> { - self.reset_backup_state().await.map_err(|e| e.into()) - } - - async fn load_backup_keys(&self) -> Result { - self.load_backup_keys().await.map_err(|e| e.into()) - } - - async fn save_tracked_users(&self, users: &[(&UserId, bool)]) -> Result<(), CryptoStoreError> { - self.save_tracked_users(users).await.map_err(Into::into) - } - - async fn load_tracked_users(&self) -> Result, CryptoStoreError> { - self.load_tracked_users().await.map_err(Into::into) - } - - async fn get_device( - &self, - user_id: &UserId, - device_id: &DeviceId, - ) -> Result, CryptoStoreError> { - self.get_device(user_id, device_id).await.map_err(|e| e.into()) - } - - async fn get_user_devices( - &self, - user_id: &UserId, - ) -> Result, CryptoStoreError> { - self.get_user_devices(user_id).await.map_err(|e| e.into()) - } - - async fn get_user_identity( - &self, - user_id: &UserId, - ) -> Result, CryptoStoreError> { - self.get_user_identity(user_id).await.map_err(|e| e.into()) - } - - async fn is_message_known(&self, hash: &OlmMessageHash) -> Result { - self.is_message_known(hash).await.map_err(|e| e.into()) - } - - async fn get_outgoing_secret_requests( - &self, - request_id: &TransactionId, - ) -> Result, CryptoStoreError> { - self.get_outgoing_key_request_helper(request_id.as_str()).await.map_err(|e| e.into()) - } - - async fn get_secret_request_by_info( - &self, - key_info: &SecretInfo, - ) -> Result, CryptoStoreError> { - self.get_secret_request_by_info(key_info).await.map_err(|e| e.into()) - } - - async fn get_unsent_secret_requests(&self) -> Result, CryptoStoreError> { - self.get_unsent_secret_requests().await.map_err(|e| e.into()) - } - - async fn delete_outgoing_secret_requests( - &self, - request_id: &TransactionId, - ) -> Result<(), CryptoStoreError> { - self.delete_outgoing_secret_requests(request_id).await.map_err(|e| e.into()) - } -} - #[cfg(all(test, target_arch = "wasm32"))] mod tests { use matrix_sdk_crypto::cryptostore_integration_tests;