@@ -10,6 +10,7 @@ use crate::io::sqlite_store::SqliteStore;
1010use crate :: liquidity:: LiquiditySource ;
1111use crate :: logger:: { log_error, FilesystemLogger , Logger } ;
1212use crate :: message_handler:: NodeCustomMessageHandler ;
13+ use crate :: payjoin_handler:: PayjoinHandler ;
1314use crate :: payment_store:: PaymentStore ;
1415use crate :: peer_store:: PeerStore ;
1516use crate :: sweep:: OutputSweeper ;
@@ -38,6 +39,7 @@ use lightning::util::persist::{
3839} ;
3940use lightning:: util:: ser:: ReadableArgs ;
4041
42+ use lightning_payjoin:: scheduler:: ChannelScheduler ;
4143use lightning_persister:: fs_store:: FilesystemStore ;
4244
4345use lightning_transaction_sync:: EsploraSyncClient ;
@@ -93,6 +95,12 @@ struct LiquiditySourceConfig {
9395 lsps2_service : Option < ( SocketAddress , PublicKey , Option < String > ) > ,
9496}
9597
98+ #[ derive( Debug , Clone ) ]
99+ struct PayjoinConfig {
100+ payjoin_directory : lightning_payjoin:: Url ,
101+ payjoin_relay : lightning_payjoin:: Url ,
102+ }
103+
96104impl Default for LiquiditySourceConfig {
97105 fn default ( ) -> Self {
98106 Self { lsps2_service : None }
@@ -166,6 +174,7 @@ pub struct NodeBuilder {
166174 chain_data_source_config : Option < ChainDataSourceConfig > ,
167175 gossip_source_config : Option < GossipSourceConfig > ,
168176 liquidity_source_config : Option < LiquiditySourceConfig > ,
177+ payjoin_config : Option < PayjoinConfig > ,
169178}
170179
171180impl NodeBuilder {
@@ -182,12 +191,14 @@ impl NodeBuilder {
182191 let chain_data_source_config = None ;
183192 let gossip_source_config = None ;
184193 let liquidity_source_config = None ;
194+ let payjoin_config = None ;
185195 Self {
186196 config,
187197 entropy_source_config,
188198 chain_data_source_config,
189199 gossip_source_config,
190200 liquidity_source_config,
201+ payjoin_config,
191202 }
192203 }
193204
@@ -242,6 +253,15 @@ impl NodeBuilder {
242253 self
243254 }
244255
256+ /// Configures the [`Node`] instance to source its gossip data from the given RapidGossipSync
257+ /// server.
258+ pub fn set_payjoin_config (
259+ & mut self , payjoin_directory : lightning_payjoin:: Url , payjoin_relay : lightning_payjoin:: Url ,
260+ ) -> & mut Self {
261+ self . payjoin_config = Some ( PayjoinConfig { payjoin_directory, payjoin_relay } ) ;
262+ self
263+ }
264+
245265 /// Configures the [`Node`] instance to source its inbound liquidity from the given
246266 /// [LSPS2](https://github.com/BitcoinAndLightningLayerSpecs/lsp/blob/main/LSPS2/README.md)
247267 /// service.
@@ -362,6 +382,7 @@ impl NodeBuilder {
362382 self . chain_data_source_config . as_ref ( ) ,
363383 self . gossip_source_config . as_ref ( ) ,
364384 self . liquidity_source_config . as_ref ( ) ,
385+ self . payjoin_config . as_ref ( ) ,
365386 seed_bytes,
366387 logger,
367388 vss_store,
@@ -385,6 +406,7 @@ impl NodeBuilder {
385406 self . chain_data_source_config . as_ref ( ) ,
386407 self . gossip_source_config . as_ref ( ) ,
387408 self . liquidity_source_config . as_ref ( ) ,
409+ self . payjoin_config . as_ref ( ) ,
388410 seed_bytes,
389411 logger,
390412 kv_store,
@@ -458,6 +480,13 @@ impl ArcedNodeBuilder {
458480 self . inner . write ( ) . unwrap ( ) . set_gossip_source_rgs ( rgs_server_url) ;
459481 }
460482
483+ /// Configures the [`Node`] instance to use payjoin.
484+ pub fn set_payjoin_config (
485+ & self , payjoin_directory : lightning_payjoin:: Url , payjoin_relay : lightning_payjoin:: Url ,
486+ ) {
487+ self . inner . write ( ) . unwrap ( ) . set_payjoin_config ( payjoin_directory, payjoin_relay) ;
488+ }
489+
461490 /// Configures the [`Node`] instance to source its inbound liquidity from the given
462491 /// [LSPS2](https://github.com/BitcoinAndLightningLayerSpecs/lsp/blob/main/LSPS2/README.md)
463492 /// service.
@@ -522,8 +551,9 @@ impl ArcedNodeBuilder {
522551fn build_with_store_internal < K : KVStore + Sync + Send + ' static > (
523552 config : Arc < Config > , chain_data_source_config : Option < & ChainDataSourceConfig > ,
524553 gossip_source_config : Option < & GossipSourceConfig > ,
525- liquidity_source_config : Option < & LiquiditySourceConfig > , seed_bytes : [ u8 ; 64 ] ,
526- logger : Arc < FilesystemLogger > , kv_store : Arc < K > ,
554+ liquidity_source_config : Option < & LiquiditySourceConfig > ,
555+ payjoin_config : Option < & PayjoinConfig > , seed_bytes : [ u8 ; 64 ] , logger : Arc < FilesystemLogger > ,
556+ kv_store : Arc < K > ,
527557) -> Result < Node < K > , BuildError > {
528558 // Initialize the on-chain wallet and chain access
529559 let xprv = bitcoin:: bip32:: ExtendedPrivKey :: new_master ( config. network . into ( ) , & seed_bytes)
@@ -556,6 +586,7 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
556586 log_error ! ( logger, "Failed to set up wallet: {}" , e) ;
557587 BuildError :: WalletSetupFailed
558588 } ) ?;
589+ let channel_scheduler = Arc :: new ( tokio:: sync:: Mutex :: new ( ChannelScheduler :: new ( ) ) ) ;
559590
560591 let ( blockchain, tx_sync, tx_broadcaster, fee_estimator) = match chain_data_source_config {
561592 Some ( ChainDataSourceConfig :: Esplora ( server_url) ) => {
@@ -566,6 +597,7 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
566597 let tx_broadcaster = Arc :: new ( TransactionBroadcaster :: new (
567598 tx_sync. client ( ) . clone ( ) ,
568599 Arc :: clone ( & logger) ,
600+ Arc :: clone ( & channel_scheduler) ,
569601 ) ) ;
570602 let fee_estimator = Arc :: new ( OnchainFeeEstimator :: new (
571603 tx_sync. client ( ) . clone ( ) ,
@@ -584,6 +616,7 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
584616 let tx_broadcaster = Arc :: new ( TransactionBroadcaster :: new (
585617 tx_sync. client ( ) . clone ( ) ,
586618 Arc :: clone ( & logger) ,
619+ Arc :: clone ( & channel_scheduler) ,
587620 ) ) ;
588621 let fee_estimator = Arc :: new ( OnchainFeeEstimator :: new (
589622 tx_sync. client ( ) . clone ( ) ,
@@ -945,6 +978,17 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
945978 } ;
946979
947980 let ( stop_sender, _) = tokio:: sync:: watch:: channel ( ( ) ) ;
981+ let payjoin_handler = if let Some ( payjoin_config) = payjoin_config {
982+ Some ( Arc :: new ( PayjoinHandler :: new (
983+ Arc :: clone ( & wallet) ,
984+ Arc :: clone ( & channel_manager) ,
985+ Arc :: clone ( & channel_scheduler) ,
986+ payjoin_config. payjoin_directory . clone ( ) ,
987+ payjoin_config. payjoin_relay . clone ( ) ,
988+ ) ) )
989+ } else {
990+ None
991+ } ;
948992
949993 let is_listening = Arc :: new ( AtomicBool :: new ( false ) ) ;
950994 let latest_wallet_sync_timestamp = Arc :: new ( RwLock :: new ( None ) ) ;
@@ -965,6 +1009,8 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
9651009 channel_manager,
9661010 chain_monitor,
9671011 output_sweeper,
1012+ payjoin_handler,
1013+ channel_scheduler,
9681014 peer_manager,
9691015 keys_manager,
9701016 network_graph,
0 commit comments