|
2 | 2 |
|
3 | 3 | use std::sync::Arc;
|
4 | 4 |
|
| 5 | +use js_sys::Promise; |
5 | 6 | use matrix_sdk_crypto::{
|
6 | 7 | store::{DynCryptoStore, IntoCryptoStore, MemoryStore},
|
7 | 8 | types::BackupSecrets,
|
8 | 9 | };
|
| 10 | +use tracing::{dispatcher, instrument::WithSubscriber}; |
9 | 11 | use wasm_bindgen::prelude::*;
|
10 | 12 | use zeroize::{Zeroize, Zeroizing};
|
11 | 13 |
|
12 | 14 | use crate::{
|
13 | 15 | encryption::EncryptionAlgorithm,
|
| 16 | + future::future_to_promise, |
14 | 17 | identifiers::{RoomId, UserId},
|
15 | 18 | impl_from_to_inner,
|
| 19 | + tracing::{logger_to_dispatcher, JsLogger}, |
16 | 20 | vodozemac::Curve25519PublicKey,
|
17 | 21 | };
|
18 | 22 |
|
@@ -44,12 +48,19 @@ impl StoreHandle {
|
44 | 48 | ///
|
45 | 49 | /// * `store_passphrase` - The passphrase that should be used to encrypt the
|
46 | 50 | /// store, for IndexedDB-based stores
|
47 |
| - #[wasm_bindgen(js_name = "open")] |
48 |
| - pub async fn open_for_js( |
| 51 | + /// |
| 52 | + /// * `logger` - An optional logger instance to use for writing log messages |
| 53 | + /// during the open operation. An instance of `JsLogger`. |
| 54 | + #[wasm_bindgen(js_name = "open", unchecked_return_type = "Promise<StoreHandle>")] |
| 55 | + pub fn open_for_js( |
49 | 56 | store_name: Option<String>,
|
50 | 57 | store_passphrase: Option<String>,
|
51 |
| - ) -> Result<StoreHandle, JsError> { |
52 |
| - StoreHandle::open(store_name, store_passphrase).await |
| 58 | + logger: Option<JsLogger>, |
| 59 | + ) -> Promise { |
| 60 | + let _guard = dispatcher::set_default(&logger_to_dispatcher(logger)); |
| 61 | + future_to_promise(async move { |
| 62 | + StoreHandle::open(store_name, store_passphrase).with_current_subscriber().await |
| 63 | + }) |
53 | 64 | }
|
54 | 65 |
|
55 | 66 | pub(crate) async fn open(
|
@@ -108,26 +119,33 @@ impl StoreHandle {
|
108 | 119 | ///
|
109 | 120 | /// * `store_key` - The key that should be used to encrypt the store, for
|
110 | 121 | /// IndexedDB-based stores. Must be a 32-byte array.
|
111 |
| - #[wasm_bindgen(js_name = "openWithKey")] |
112 |
| - pub async fn open_with_key( |
| 122 | + /// |
| 123 | + /// * `logger` - An optional logger instance to use for writing log messages |
| 124 | + /// during the open operation. An instance of `JsLogger`. |
| 125 | + #[wasm_bindgen(js_name = "openWithKey", unchecked_return_type = "Promise<StoreHandle>")] |
| 126 | + pub fn open_with_key( |
113 | 127 | store_name: String,
|
114 | 128 | mut store_key: Vec<u8>,
|
115 |
| - ) -> Result<StoreHandle, JsError> { |
116 |
| - let store_key_array: Zeroizing<[u8; 32]> = Zeroizing::new( |
117 |
| - store_key |
118 |
| - .as_slice() |
119 |
| - .try_into() |
120 |
| - .map_err(|_| JsError::new("Expected a key of length 32"))?, |
121 |
| - ); |
122 |
| - store_key.zeroize(); |
123 |
| - |
124 |
| - let store = matrix_sdk_indexeddb::IndexeddbCryptoStore::open_with_key( |
125 |
| - &store_name, |
126 |
| - &store_key_array, |
127 |
| - ) |
128 |
| - .await?; |
129 |
| - |
130 |
| - Ok(Self { store: store.into_crypto_store() }) |
| 129 | + logger: Option<JsLogger>, |
| 130 | + ) -> Promise { |
| 131 | + let _guard = dispatcher::set_default(&logger_to_dispatcher(logger)); |
| 132 | + future_to_promise(async move { |
| 133 | + let store_key_array: Zeroizing<[u8; 32]> = Zeroizing::new( |
| 134 | + store_key |
| 135 | + .as_slice() |
| 136 | + .try_into() |
| 137 | + .map_err(|_| JsError::new("Expected a key of length 32"))?, |
| 138 | + ); |
| 139 | + store_key.zeroize(); |
| 140 | + |
| 141 | + let store = matrix_sdk_indexeddb::IndexeddbCryptoStore::open_with_key( |
| 142 | + &store_name, |
| 143 | + &store_key_array, |
| 144 | + ) |
| 145 | + .await?; |
| 146 | + |
| 147 | + Ok(Self { store: store.into_crypto_store() }) |
| 148 | + }) |
131 | 149 | }
|
132 | 150 | }
|
133 | 151 |
|
|
0 commit comments