Skip to content

Commit 3342f11

Browse files
committed
crypto: Save private identity in the MemoryStore
1 parent 1cdf6c8 commit 3342f11

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<BTreeMap<OwnedRoomId, 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(),
@@ -130,6 +132,10 @@ impl MemoryStore {
130132
.unwrap()
131133
.extend(sessions.into_iter().map(|s| (s.room_id().to_owned(), s)));
132134
}
135+
136+
fn save_private_identity(&self, private_identity: Option<PrivateCrossSigningIdentity>) {
137+
*self.private_identity.write().unwrap() = private_identity;
138+
}
133139
}
134140

135141
type Result<T> = std::result::Result<T, Infallible>;
@@ -144,7 +150,7 @@ impl CryptoStore for MemoryStore {
144150
}
145151

146152
async fn load_identity(&self) -> Result<Option<PrivateCrossSigningIdentity>> {
147-
Ok(None)
153+
Ok(self.private_identity.read().unwrap().clone())
148154
}
149155

150156
async fn next_batch_token(&self) -> Result<Option<String>> {
@@ -163,6 +169,7 @@ impl CryptoStore for MemoryStore {
163169
self.save_sessions(changes.sessions).await;
164170
self.save_inbound_group_sessions(changes.inbound_group_sessions);
165171
self.save_outbound_group_sessions(changes.outbound_group_sessions);
172+
self.save_private_identity(changes.private_identity);
166173

167174
self.save_devices(changes.devices.new);
168175
self.save_devices(changes.devices.changed);
@@ -482,7 +489,10 @@ mod tests {
482489

483490
use crate::{
484491
identities::device::testing::get_device,
485-
olm::{tests::get_account_and_session_test_helper, InboundGroupSession, OlmMessageHash},
492+
olm::{
493+
tests::get_account_and_session_test_helper, InboundGroupSession, OlmMessageHash,
494+
PrivateCrossSigningIdentity,
495+
},
486496
store::{memorystore::MemoryStore, Changes, CryptoStore, PendingChanges},
487497
};
488498

@@ -568,6 +578,22 @@ mod tests {
568578
assert_eq!(loaded_tracked_users.len(), 2);
569579
}
570580

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

0 commit comments

Comments
 (0)