diff --git a/mutiny-core/src/lib.rs b/mutiny-core/src/lib.rs index 69b545f01..4d29407b8 100644 --- a/mutiny-core/src/lib.rs +++ b/mutiny-core/src/lib.rs @@ -62,6 +62,7 @@ use crate::{ PAYMENT_INBOUND_PREFIX_KEY, PAYMENT_OUTBOUND_PREFIX_KEY, TRANSACTION_DETAILS_PREFIX_KEY, }, }; +use anyhow::Context; use bdk_chain::ConfirmationTime; use bip39::Mnemonic; pub use bitcoin; @@ -804,7 +805,6 @@ impl MutinyWalletBuilder { log_debug!(logger, "checking device lock"); if !config.skip_device_lock { let start = Instant::now(); - log_debug!(logger, "Checking device lock"); if let Some(lock) = self.storage.get_device_lock()? { log_info!(logger, "Current device lock: {lock:?}"); } @@ -817,6 +817,25 @@ impl MutinyWalletBuilder { if let Some(lock) = self.storage.get_device_lock()? { log_info!(logger, "New device lock: {lock:?}"); } + + // Wait device lock syncing + let device_id = self.storage.get_device_id()?; + let max_retries = 10; + let mut retries = 0; + while !self + .storage + .fetch_device_lock() + .await? + .is_some_and(|lock| lock.is_last_locker(&device_id)) + { + log_info!(logger, "Waiting device lock syncing... {retries}"); + retries += 1; + if retries > max_retries { + log_error!(logger, "Can't syncing device lock to VSS"); + return Err(MutinyError::AlreadyRunning); + } + sleep(300).await + } } log_debug!(logger, "finished checking device lock"); @@ -856,7 +875,12 @@ impl MutinyWalletBuilder { if let Some(cb) = self.ln_event_callback.clone() { nm_builder.with_ln_event_callback(cb); } - let node_manager = Arc::new(nm_builder.build().await?); + let node_manager = Arc::new( + nm_builder + .build() + .await + .with_context(|| "build node manager")?, + ); log_trace!( logger, diff --git a/mutiny-wasm/src/indexed_db.rs b/mutiny-wasm/src/indexed_db.rs index 2b0607b1a..f10e123c2 100644 --- a/mutiny-wasm/src/indexed_db.rs +++ b/mutiny-wasm/src/indexed_db.rs @@ -1,7 +1,8 @@ -use anyhow::anyhow; +use anyhow::{anyhow, Context}; use async_trait::async_trait; use bip39::Mnemonic; use futures::lock::Mutex; +use futures::FutureExt; use gloo_utils::format::JsValueSerdeExt; use lightning::util::logger::Logger; use lightning::{log_debug, log_error, log_info}; @@ -375,7 +376,12 @@ impl IndexedDbStorage { log_info!(logger, "Read {} keys from vss", keys.len()); let mut futs = Vec::with_capacity(keys.len()); for kv in keys { - futs.push(Self::handle_vss_key(kv, vss, &map, logger)); + let key = kv.key.clone(); + futs.push( + Self::handle_vss_key(kv, vss, &map, logger).then(|r| async move { + r.with_context(|| format!("handle vss key {}", key)) + }), + ); } let results = futures::future::try_join_all(futs).await?; @@ -454,11 +460,14 @@ impl IndexedDbStorage { let obj = vss.get_object(&kv.key).await?; return Ok(Some((kv.key, obj.value))); } - KEYCHAIN_STORE_KEY => match current.get_data::(&kv.key)? { + KEYCHAIN_STORE_KEY => match current + .get_data::(&kv.key) + .with_context(|| "read keychain data from storage")? + { Some(local) => { if local.version < kv.version { let obj = vss.get_object(&kv.key).await?; - if serde_json::from_value::(obj.value.clone()).is_ok() { + if serde_json::from_value::(obj.value.clone()).is_ok() { return Ok(Some((kv.key, obj.value))); } }