Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit 1159efa

Browse files
Merge pull request #818 from MutinyWallet/device-lock-on-connect
Check device lock on connecting to peer
2 parents a72a87c + c264604 commit 1159efa

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

mutiny-core/src/node.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,7 @@ impl<S: MutinyStorage> Node<S> {
700700
#[cfg(target_arch = "wasm32")]
701701
&self.websocket_proxy_addr,
702702
&peer_connection_info,
703+
&self.persister.storage,
703704
self.logger.clone(),
704705
self.peer_manager.clone(),
705706
self.fee_estimator.clone(),
@@ -1648,7 +1649,7 @@ pub(crate) fn scoring_params() -> ProbabilisticScoringFeeParameters {
16481649

16491650
#[allow(clippy::too_many_arguments)]
16501651
async fn start_reconnection_handling<S: MutinyStorage>(
1651-
storage: &impl MutinyStorage,
1652+
storage: &S,
16521653
node_pubkey: PublicKey,
16531654
#[cfg(target_arch = "wasm32")] websocket_proxy_addr: String,
16541655
peer_man: Arc<dyn PeerManager>,
@@ -1699,6 +1700,7 @@ async fn start_reconnection_handling<S: MutinyStorage>(
16991700
#[cfg(target_arch = "wasm32")]
17001701
&websocket_proxy_addr_copy_proxy,
17011702
&PubkeyConnectionInfo::new(lsp.connection_string.as_str()).unwrap(),
1703+
&storage_copy,
17021704
proxy_logger.clone(),
17031705
peer_man_proxy.clone(),
17041706
proxy_fee_estimator.clone(),
@@ -1801,6 +1803,7 @@ async fn start_reconnection_handling<S: MutinyStorage>(
18011803
#[cfg(target_arch = "wasm32")]
18021804
&websocket_proxy_addr,
18031805
&peer_connection_info,
1806+
&connect_storage,
18041807
connect_logger.clone(),
18051808
connect_peer_man.clone(),
18061809
connect_fee_estimator.clone(),

mutiny-core/src/peermanager.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ impl<S: MutinyStorage> RoutingMessageHandler for GossipMessageHandler<S> {
297297
pub(crate) async fn connect_peer_if_necessary<S: MutinyStorage>(
298298
#[cfg(target_arch = "wasm32")] websocket_proxy_addr: &str,
299299
peer_connection_info: &PubkeyConnectionInfo,
300+
storage: &S,
300301
logger: Arc<MutinyLogger>,
301302
peer_manager: Arc<dyn PeerManager>,
302303
fee_estimator: Arc<MutinyFeeEstimator<S>>,
@@ -308,6 +309,15 @@ pub(crate) async fn connect_peer_if_necessary<S: MutinyStorage>(
308309
{
309310
Ok(())
310311
} else {
312+
// make sure we have the device lock before connecting
313+
// otherwise we could cause force closes
314+
if let Some(lock) = storage.fetch_device_lock().await? {
315+
let id = storage.get_device_id()?;
316+
if lock.is_locked(&id) {
317+
return Err(MutinyError::AlreadyRunning);
318+
}
319+
}
320+
311321
// first check to see if the fee rate is mostly up to date
312322
// if not, we need to have updated fees or force closures
313323
// could occur due to UpdateFee message conflicts.

mutiny-core/src/storage.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,8 @@ pub trait MutinyStorage: Clone + Sized + 'static {
397397
let lock = DeviceLock { time, device };
398398
self.set_data(DEVICE_LOCK_KEY, lock, Some(time))
399399
}
400+
401+
async fn fetch_device_lock(&self) -> Result<Option<DeviceLock>, MutinyError>;
400402
}
401403

402404
#[derive(Clone)]
@@ -551,6 +553,10 @@ impl MutinyStorage for MemoryStorage {
551553
async fn clear() -> Result<(), MutinyError> {
552554
Ok(())
553555
}
556+
557+
async fn fetch_device_lock(&self) -> Result<Option<DeviceLock>, MutinyError> {
558+
self.get_device_lock()
559+
}
554560
}
555561

556562
// Dummy implementation for testing or if people want to ignore persistence
@@ -614,6 +620,10 @@ impl MutinyStorage for () {
614620
async fn clear() -> Result<(), MutinyError> {
615621
Ok(())
616622
}
623+
624+
async fn fetch_device_lock(&self) -> Result<Option<DeviceLock>, MutinyError> {
625+
self.get_device_lock()
626+
}
617627
}
618628

619629
#[derive(Clone)]

mutiny-wasm/src/indexed_db.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,17 @@ impl MutinyStorage for IndexedDbStorage {
760760

761761
Ok(())
762762
}
763+
764+
async fn fetch_device_lock(&self) -> Result<Option<DeviceLock>, MutinyError> {
765+
match self.vss.as_ref() {
766+
None => self.get_device_lock(),
767+
Some(vss) => {
768+
let json = vss.get_object(DEVICE_LOCK_KEY).await?;
769+
let device_lock = serde_json::from_value(json.value)?;
770+
Ok(Some(device_lock))
771+
}
772+
}
773+
}
763774
}
764775

765776
#[cfg(test)]

0 commit comments

Comments
 (0)