Skip to content

Commit 241af7e

Browse files
committed
crypto: Save private identity in the MemoryStore
1 parent 9e4eda3 commit 241af7e

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pub struct MemoryStore {
5555
sessions: SessionStore,
5656
inbound_group_sessions: GroupSessionStore,
5757
outbound_group_sessions: StdRwLock<Vec<OutboundGroupSession>>,
58+
private_identity: StdRwLock<Option<PrivateCrossSigningIdentity>>,
5859
tracked_users: StdRwLock<Vec<TrackedUser>>,
5960
olm_hashes: StdRwLock<HashMap<String, HashSet<String>>>,
6061
devices: DeviceStore,
@@ -77,6 +78,7 @@ impl Default for MemoryStore {
7778
sessions: SessionStore::new(),
7879
inbound_group_sessions: GroupSessionStore::new(),
7980
outbound_group_sessions: Default::default(),
81+
private_identity: Default::default(),
8082
tracked_users: Default::default(),
8183
olm_hashes: Default::default(),
8284
devices: DeviceStore::new(),
@@ -127,6 +129,10 @@ impl MemoryStore {
127129
fn save_outbound_group_sessions(&self, mut sessions: Vec<OutboundGroupSession>) {
128130
self.outbound_group_sessions.write().unwrap().append(&mut sessions);
129131
}
132+
133+
fn save_private_identity(&self, private_identity: Option<PrivateCrossSigningIdentity>) {
134+
*self.private_identity.write().unwrap() = private_identity;
135+
}
130136
}
131137

132138
type Result<T> = std::result::Result<T, Infallible>;
@@ -141,7 +147,7 @@ impl CryptoStore for MemoryStore {
141147
}
142148

143149
async fn load_identity(&self) -> Result<Option<PrivateCrossSigningIdentity>> {
144-
Ok(None)
150+
Ok(self.private_identity.read().unwrap().clone())
145151
}
146152

147153
async fn next_batch_token(&self) -> Result<Option<String>> {
@@ -160,6 +166,7 @@ impl CryptoStore for MemoryStore {
160166
self.save_sessions(changes.sessions).await;
161167
self.save_inbound_group_sessions(changes.inbound_group_sessions);
162168
self.save_outbound_group_sessions(changes.outbound_group_sessions);
169+
self.save_private_identity(changes.private_identity);
163170

164171
self.save_devices(changes.devices.new);
165172
self.save_devices(changes.devices.changed);
@@ -485,7 +492,10 @@ mod tests {
485492

486493
use crate::{
487494
identities::device::testing::get_device,
488-
olm::{tests::get_account_and_session_test_helper, InboundGroupSession, OlmMessageHash},
495+
olm::{
496+
tests::get_account_and_session_test_helper, InboundGroupSession, OlmMessageHash,
497+
PrivateCrossSigningIdentity,
498+
},
489499
store::{memorystore::MemoryStore, Changes, CryptoStore, PendingChanges},
490500
};
491501

@@ -571,6 +581,22 @@ mod tests {
571581
assert_eq!(loaded_tracked_users.len(), 2);
572582
}
573583

584+
#[async_test]
585+
async fn test_private_identity_store() {
586+
// Given a private identity
587+
let private_identity = PrivateCrossSigningIdentity::empty(user_id!("@u:s"));
588+
589+
// When we save it to the store
590+
let store = MemoryStore::new();
591+
store.save_private_identity(Some(private_identity.clone()));
592+
593+
// Then we can get it out again
594+
let loaded_identity =
595+
store.load_identity().await.expect("failed to load private identity").unwrap();
596+
597+
assert_eq!(loaded_identity.user_id(), user_id!("@u:s"));
598+
}
599+
574600
#[async_test]
575601
async fn test_device_store() {
576602
let device = get_device();

0 commit comments

Comments
 (0)