matrix_sdk/encryption/secret_storage/
futures.rs1use std::{future::IntoFuture, pin::Pin};
16
17use futures_core::Future;
18use matrix_sdk_base::crypto::secret_storage::SecretStorageKey;
19use ruma::events::secret_storage::default_key::SecretStorageDefaultKeyEventContent;
20
21use super::{Result, SecretStorage, SecretStore};
22
23#[derive(Debug)]
25pub struct CreateStore<'a> {
26 pub(super) secret_storage: &'a SecretStorage,
27 pub(super) passphrase: Option<&'a str>,
28}
29
30impl<'a> CreateStore<'a> {
31 pub fn with_passphrase(mut self, passphrase: &'a str) -> Self {
36 self.passphrase = Some(passphrase);
37
38 self
39 }
40}
41
42impl<'a> IntoFuture for CreateStore<'a> {
43 type Output = Result<SecretStore>;
44 #[cfg(target_family = "wasm")]
45 type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + 'a>>;
46 #[cfg(not(target_family = "wasm"))]
47 type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>;
48
49 fn into_future(self) -> Self::IntoFuture {
50 let Self { secret_storage, passphrase } = self;
51
52 Box::pin(async move {
53 let client_copy = secret_storage.client.to_owned();
58 let _guard = client_copy.locks().open_secret_store_lock.lock().await;
59
60 let new_key = if let Some(passphrase) = passphrase {
61 SecretStorageKey::new_from_passphrase(passphrase)
62 } else {
63 SecretStorageKey::new()
64 };
65
66 let content = new_key.event_content().to_owned();
67
68 secret_storage.client.account().set_account_data(content).await?;
69
70 let store = SecretStore { client: secret_storage.client.to_owned(), key: new_key };
71 store.export_secrets().await?;
72
73 let default_key_content =
74 SecretStorageDefaultKeyEventContent::new(store.key.key_id().to_owned());
75
76 store.client.account().set_account_data(default_key_content).await?;
77
78 Ok(store)
79 })
80 }
81}