Skip to content

Commit c6f0e47

Browse files
committed
refactor(crypto)!: Add an error type to the CryptoStore trait
This makes it easier to implement it. However, using a different error type than CryptoStoreError is a non-trivial change, so left for the coming commits for all stores except the memory store.
1 parent 96c796b commit c6f0e47

File tree

19 files changed

+243
-81
lines changed

19 files changed

+243
-81
lines changed

crates/matrix-sdk-base/src/client.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use futures_signals::signal::ReadOnlyMutable;
2525
use matrix_sdk_common::{instant::Instant, locks::RwLock};
2626
#[cfg(feature = "e2e-encryption")]
2727
use matrix_sdk_crypto::{
28-
store::CryptoStore, EncryptionSettings, OlmError, OlmMachine, ToDeviceRequest,
28+
store::DynCryptoStore, EncryptionSettings, OlmError, OlmMachine, ToDeviceRequest,
2929
};
3030
#[cfg(feature = "e2e-encryption")]
3131
use once_cell::sync::OnceCell;
@@ -82,7 +82,7 @@ pub struct BaseClient {
8282
/// This field is only meant to be used for `OlmMachine` initialization.
8383
/// All operations on it happen inside the `OlmMachine`.
8484
#[cfg(feature = "e2e-encryption")]
85-
crypto_store: Arc<dyn CryptoStore>,
85+
crypto_store: Arc<DynCryptoStore>,
8686
/// The olm-machine that is created once the
8787
/// [`SessionMeta`][crate::session::SessionMeta] is set via
8888
/// [`BaseClient::set_session_meta`]

crates/matrix-sdk-base/src/store/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use async_trait::async_trait;
4242
use dashmap::DashMap;
4343
use matrix_sdk_common::{locks::RwLock, AsyncTraitDeps};
4444
#[cfg(feature = "e2e-encryption")]
45-
use matrix_sdk_crypto::store::{CryptoStore, IntoCryptoStore};
45+
use matrix_sdk_crypto::store::{DynCryptoStore, IntoCryptoStore};
4646
pub use matrix_sdk_store_encryption::Error as StoreEncryptionError;
4747
use ruma::{
4848
api::client::push::get_notifications::v3::Notification,
@@ -839,7 +839,7 @@ impl StateChanges {
839839
#[derive(Clone)]
840840
pub struct StoreConfig {
841841
#[cfg(feature = "e2e-encryption")]
842-
pub(crate) crypto_store: Arc<dyn CryptoStore>,
842+
pub(crate) crypto_store: Arc<DynCryptoStore>,
843843
pub(crate) state_store: Arc<dyn StateStore>,
844844
}
845845

@@ -856,7 +856,7 @@ impl StoreConfig {
856856
pub fn new() -> Self {
857857
Self {
858858
#[cfg(feature = "e2e-encryption")]
859-
crypto_store: Arc::new(matrix_sdk_crypto::store::MemoryStore::new()),
859+
crypto_store: matrix_sdk_crypto::store::MemoryStore::new().into_crypto_store(),
860860
state_store: Arc::new(MemoryStore::new()),
861861
}
862862
}

crates/matrix-sdk-crypto/src/gossiping/machine.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,7 @@ mod tests {
10111011
identities::{LocalTrust, ReadOnlyDevice},
10121012
olm::{Account, OutboundGroupSession, PrivateCrossSigningIdentity, ReadOnlyAccount},
10131013
session_manager::GroupSessionCache,
1014-
store::{Changes, CryptoStore, MemoryStore, Store},
1014+
store::{Changes, IntoCryptoStore, MemoryStore, Store},
10151015
types::{
10161016
events::{
10171017
forwarded_room_key::ForwardedRoomKeyContent,
@@ -1068,7 +1068,7 @@ mod tests {
10681068
let device_id = DeviceId::new();
10691069

10701070
let account = ReadOnlyAccount::new(&user_id, &device_id);
1071-
let store: Arc<dyn CryptoStore> = Arc::new(MemoryStore::new());
1071+
let store = MemoryStore::new().into_crypto_store();
10721072
let identity = Arc::new(Mutex::new(PrivateCrossSigningIdentity::empty(alice_id())));
10731073
let verification = VerificationMachine::new(account, identity.clone(), store.clone());
10741074
let store = Store::new(user_id.to_owned(), identity, store, verification);
@@ -1090,7 +1090,7 @@ mod tests {
10901090
let another_device =
10911091
ReadOnlyDevice::from_account(&ReadOnlyAccount::new(&user_id, alice2_device_id())).await;
10921092

1093-
let store: Arc<dyn CryptoStore> = Arc::new(MemoryStore::new());
1093+
let store = MemoryStore::new().into_crypto_store();
10941094
let identity = Arc::new(Mutex::new(PrivateCrossSigningIdentity::empty(alice_id())));
10951095
let verification = VerificationMachine::new(account, identity.clone(), store.clone());
10961096

crates/matrix-sdk-crypto/src/identities/device.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use crate::{
4141
error::{EventError, OlmError, OlmResult, SignatureError},
4242
identities::{ReadOnlyOwnUserIdentity, ReadOnlyUserIdentities},
4343
olm::{InboundGroupSession, Session, SignedJsonObject, VerifyJson},
44-
store::{Changes, CryptoStore, DeviceChanges, Result as StoreResult},
44+
store::{Changes, DeviceChanges, DynCryptoStore, Result as StoreResult},
4545
types::{
4646
events::{
4747
forwarded_room_key::ForwardedRoomKeyContent,
@@ -570,7 +570,7 @@ impl ReadOnlyDevice {
570570

571571
pub(crate) async fn encrypt(
572572
&self,
573-
store: &dyn CryptoStore,
573+
store: &DynCryptoStore,
574574
event_type: &str,
575575
content: Value,
576576
) -> OlmResult<(Session, Raw<ToDeviceEncryptedEventContent>)> {

crates/matrix-sdk-crypto/src/identities/manager.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ pub(crate) mod testing {
667667
identities::IdentityManager,
668668
machine::testing::response_from_file,
669669
olm::{PrivateCrossSigningIdentity, ReadOnlyAccount},
670-
store::{CryptoStore, MemoryStore, Store},
670+
store::{DynCryptoStore, IntoCryptoStore, MemoryStore, Store},
671671
types::DeviceKeys,
672672
verification::VerificationMachine,
673673
UploadSigningKeysRequest,
@@ -690,10 +690,14 @@ pub(crate) mod testing {
690690
let identity = Arc::new(Mutex::new(identity));
691691
let user_id = Arc::from(user_id());
692692
let account = ReadOnlyAccount::new(&user_id, device_id());
693-
let store: Arc<dyn CryptoStore> = Arc::new(MemoryStore::new());
693+
let store: Arc<DynCryptoStore> = MemoryStore::new().into_crypto_store();
694694
let verification = VerificationMachine::new(account, identity.clone(), store);
695-
let store =
696-
Store::new(user_id.clone(), identity, Arc::new(MemoryStore::new()), verification);
695+
let store = Store::new(
696+
user_id.clone(),
697+
identity,
698+
MemoryStore::new().into_crypto_store(),
699+
verification,
700+
);
697701
IdentityManager::new(user_id, device_id().into(), store)
698702
}
699703

crates/matrix-sdk-crypto/src/identities/user.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ pub(crate) mod tests {
10611061
Device, MasterPubkey, SelfSigningPubkey, UserSigningPubkey,
10621062
},
10631063
olm::{PrivateCrossSigningIdentity, ReadOnlyAccount},
1064-
store::MemoryStore,
1064+
store::{IntoCryptoStore, MemoryStore},
10651065
types::CrossSigningKey,
10661066
verification::VerificationMachine,
10671067
};
@@ -1105,7 +1105,7 @@ pub(crate) mod tests {
11051105
let verification_machine = VerificationMachine::new(
11061106
ReadOnlyAccount::new(second.user_id(), second.device_id()),
11071107
private_identity,
1108-
Arc::new(MemoryStore::new()),
1108+
MemoryStore::new().into_crypto_store(),
11091109
);
11101110

11111111
let first = Device {
@@ -1146,7 +1146,7 @@ pub(crate) mod tests {
11461146
let verification_machine = VerificationMachine::new(
11471147
ReadOnlyAccount::new(device.user_id(), device.device_id()),
11481148
id.clone(),
1149-
Arc::new(MemoryStore::new()),
1149+
MemoryStore::new().into_crypto_store(),
11501150
);
11511151

11521152
let public_identity = identity.to_public_identity().await.unwrap();

crates/matrix-sdk-crypto/src/machine.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use crate::{
6666
requests::{IncomingResponse, OutgoingRequest, UploadSigningKeysRequest},
6767
session_manager::{GroupSessionManager, SessionManager},
6868
store::{
69-
Changes, CryptoStore, DeviceChanges, IdentityChanges, IntoCryptoStore, MemoryStore,
69+
Changes, DeviceChanges, DynCryptoStore, IdentityChanges, IntoCryptoStore, MemoryStore,
7070
Result as StoreResult, SecretImportError, Store,
7171
},
7272
types::{
@@ -153,7 +153,7 @@ impl OlmMachine {
153153
fn new_helper(
154154
user_id: &UserId,
155155
device_id: &DeviceId,
156-
store: Arc<dyn CryptoStore>,
156+
store: Arc<DynCryptoStore>,
157157
account: ReadOnlyAccount,
158158
user_identity: PrivateCrossSigningIdentity,
159159
) -> Self {

crates/matrix-sdk-crypto/src/session_manager/sessions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ mod tests {
415415
identities::{KeysQueryListener, ReadOnlyDevice},
416416
olm::{Account, PrivateCrossSigningIdentity, ReadOnlyAccount},
417417
session_manager::GroupSessionCache,
418-
store::{CryptoStore, MemoryStore, Store},
418+
store::{IntoCryptoStore, MemoryStore, Store},
419419
verification::VerificationMachine,
420420
};
421421

@@ -464,7 +464,7 @@ mod tests {
464464

465465
let users_for_key_claim = Arc::new(DashMap::new());
466466
let account = ReadOnlyAccount::new(user_id, device_id);
467-
let store: Arc<dyn CryptoStore> = Arc::new(MemoryStore::new());
467+
let store = MemoryStore::new().into_crypto_store();
468468
store.save_account(account.clone()).await.unwrap();
469469
let identity = Arc::new(Mutex::new(PrivateCrossSigningIdentity::empty(user_id)));
470470
let verification =

crates/matrix-sdk-crypto/src/store/error.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use std::{fmt::Debug, io::Error as IoError};
15+
use std::{convert::Infallible, fmt::Debug, io::Error as IoError};
1616

1717
use ruma::{IdParseError, OwnedDeviceId, OwnedUserId};
1818
use serde_json::Error as SerdeError;
@@ -92,3 +92,9 @@ impl CryptoStoreError {
9292
Self::Backend(Box::new(error))
9393
}
9494
}
95+
96+
impl From<Infallible> for CryptoStoreError {
97+
fn from(never: Infallible) -> Self {
98+
match never {}
99+
}
100+
}

crates/matrix-sdk-crypto/src/store/memorystore.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use std::{collections::HashMap, sync::Arc};
15+
use std::{collections::HashMap, convert::Infallible, sync::Arc};
1616

1717
use async_trait::async_trait;
1818
use dashmap::{DashMap, DashSet};
@@ -23,8 +23,7 @@ use ruma::{
2323

2424
use super::{
2525
caches::{DeviceStore, GroupSessionStore, SessionStore},
26-
BackupKeys, Changes, CryptoStore, InboundGroupSession, ReadOnlyAccount, Result, RoomKeyCounts,
27-
Session,
26+
BackupKeys, Changes, CryptoStore, InboundGroupSession, ReadOnlyAccount, RoomKeyCounts, Session,
2827
};
2928
use crate::{
3029
gossiping::{GossipRequest, SecretInfo},
@@ -99,9 +98,13 @@ impl MemoryStore {
9998
}
10099
}
101100

101+
type Result<T> = std::result::Result<T, Infallible>;
102+
102103
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
103104
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
104105
impl CryptoStore for MemoryStore {
106+
type Error = Infallible;
107+
105108
async fn load_account(&self) -> Result<Option<ReadOnlyAccount>> {
106109
Ok(None)
107110
}

0 commit comments

Comments
 (0)