diff --git a/bindings/ldk_node.udl b/bindings/ldk_node.udl index 4b69fd03..46327757 100644 --- a/bindings/ldk_node.udl +++ b/bindings/ldk_node.udl @@ -43,6 +43,7 @@ interface Builder { Node build(); [Throws=BuildError] Node build_with_fs_store(); + void restore_encoded_channel_monitors(sequence monitors); }; interface Node { @@ -92,8 +93,6 @@ interface Node { boolean verify_signature([ByRef]sequence msg, [ByRef]string sig, [ByRef]PublicKey pkey); [Throws=NodeError] sequence get_encoded_channel_monitors(); - [Throws=NodeError] - void restore_encoded_channel_monitors(sequence monitors); void force_close_all_channels_without_broadcasting_txn(); }; diff --git a/src/builder.rs b/src/builder.rs index 56f3f373..ad2c38c7 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -15,8 +15,8 @@ use crate::payment::store::PaymentStore; use crate::peer_store::PeerStore; use crate::tx_broadcaster::TransactionBroadcaster; use crate::types::{ - ChainMonitor, ChannelManager, DynStore, GossipSync, Graph, KeysManager, MessageRouter, - OnionMessenger, PeerManager, + ChainMonitor, ChannelManager, DynStore, GossipSync, Graph, KeyValue, KeysManager, + MessageRouter, OnionMessenger, PeerManager, }; use crate::wallet::Wallet; use crate::{LogLevel, Node}; @@ -172,6 +172,7 @@ pub struct NodeBuilder { chain_data_source_config: Option, gossip_source_config: Option, liquidity_source_config: Option, + monitors_to_restore: Option>, } impl NodeBuilder { @@ -187,15 +188,23 @@ impl NodeBuilder { let chain_data_source_config = None; let gossip_source_config = None; let liquidity_source_config = None; + let monitors_to_restore = None; Self { config, entropy_source_config, chain_data_source_config, gossip_source_config, liquidity_source_config, + monitors_to_restore, } } + /// Alby: set monitors to restore when restoring SCB + pub fn restore_encoded_channel_monitors(&mut self, monitors: Vec) -> &mut Self { + self.monitors_to_restore = Some(monitors); + self + } + /// Configures the [`Node`] instance to source its wallet entropy from a seed file on disk. /// /// If the given file does not exist a new random seed file will be generated and @@ -316,6 +325,7 @@ impl NodeBuilder { ) .map_err(|_| BuildError::KVStoreSetupFailed)?, ); + self.build_with_store(kv_store) } @@ -381,6 +391,17 @@ impl NodeBuilder { )?; let config = Arc::new(self.config.clone()); + // Alby: Restore encoded channel monitors for a recovery of last resort + if self.monitors_to_restore.is_some() { + let monitors = self.monitors_to_restore.clone().unwrap(); + for monitor in monitors { + let result = kv_store.write("monitors", "", &monitor.key, &monitor.value); + if result.is_err() { + log_error!(logger, "Failed to restore monitor: {}", result.unwrap_err()); + } + } + } + build_with_store_internal( config, self.chain_data_source_config.as_ref(), @@ -420,6 +441,11 @@ impl ArcedNodeBuilder { Self { inner } } + /// Alby: set monitors to restore when restoring SCB + pub fn restore_encoded_channel_monitors(&self, monitors: Vec) { + self.inner.write().unwrap().restore_encoded_channel_monitors(monitors); + } + /// Configures the [`Node`] instance to source its wallet entropy from a seed file on disk. /// /// If the given file does not exist a new random seed file will be generated and diff --git a/src/lib.rs b/src/lib.rs index d12c4f50..9db31e57 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1566,23 +1566,6 @@ impl Node { return Ok(entries); } - /// Alby: Restore encoded channel monitors for a recovery of last resort - pub fn restore_encoded_channel_monitors(&self, monitors: Vec) -> Result<(), Error> { - let channel_monitor_store = Arc::clone(&self.kv_store); - let channel_monitor_logger = Arc::clone(&self.logger); - - for monitor in monitors { - channel_monitor_store.write("monitors", "", &monitor.key, &monitor.value).map_err( - |e| { - log_error!(channel_monitor_logger, "Failed to restore monitor: {}", e); - Error::ConnectionFailed - }, - )?; - } - - return Ok(()); - } - /// Retrieves an overview of all known balances. pub fn list_balances(&self) -> BalanceDetails { let cur_anchor_reserve_sats =