@@ -11,6 +11,7 @@ use crate::io::sqlite_store::SqliteStore;
1111use crate :: liquidity:: LiquiditySource ;
1212use crate :: logger:: { log_error, log_info, FilesystemLogger , Logger } ;
1313use crate :: message_handler:: NodeCustomMessageHandler ;
14+ use crate :: payjoin_handler:: PayjoinHandler ;
1415use crate :: payment_store:: PaymentStore ;
1516use crate :: peer_store:: PeerStore ;
1617use crate :: tx_broadcaster:: TransactionBroadcaster ;
@@ -39,6 +40,7 @@ use lightning::util::persist::{
3940use lightning:: util:: ser:: ReadableArgs ;
4041use lightning:: util:: sweep:: OutputSweeper ;
4142
43+ use lightning_payjoin:: scheduler:: ChannelScheduler ;
4244use lightning_persister:: fs_store:: FilesystemStore ;
4345
4446use lightning_transaction_sync:: EsploraSyncClient ;
@@ -94,6 +96,12 @@ struct LiquiditySourceConfig {
9496 lsps2_service : Option < ( SocketAddress , PublicKey , Option < String > ) > ,
9597}
9698
99+ #[ derive( Debug , Clone ) ]
100+ struct PayjoinConfig {
101+ payjoin_directory : lightning_payjoin:: Url ,
102+ payjoin_relay : lightning_payjoin:: Url ,
103+ }
104+
97105impl Default for LiquiditySourceConfig {
98106 fn default ( ) -> Self {
99107 Self { lsps2_service : None }
@@ -173,6 +181,7 @@ pub struct NodeBuilder {
173181 chain_data_source_config : Option < ChainDataSourceConfig > ,
174182 gossip_source_config : Option < GossipSourceConfig > ,
175183 liquidity_source_config : Option < LiquiditySourceConfig > ,
184+ payjoin_config : Option < PayjoinConfig > ,
176185}
177186
178187impl NodeBuilder {
@@ -189,12 +198,14 @@ impl NodeBuilder {
189198 let chain_data_source_config = None ;
190199 let gossip_source_config = None ;
191200 let liquidity_source_config = None ;
201+ let payjoin_config = None ;
192202 Self {
193203 config,
194204 entropy_source_config,
195205 chain_data_source_config,
196206 gossip_source_config,
197207 liquidity_source_config,
208+ payjoin_config,
198209 }
199210 }
200211
@@ -249,6 +260,15 @@ impl NodeBuilder {
249260 self
250261 }
251262
263+ /// Configures the [`Node`] instance to source its gossip data from the given RapidGossipSync
264+ /// server.
265+ pub fn set_payjoin_config (
266+ & mut self , payjoin_directory : lightning_payjoin:: Url , payjoin_relay : lightning_payjoin:: Url ,
267+ ) -> & mut Self {
268+ self . payjoin_config = Some ( PayjoinConfig { payjoin_directory, payjoin_relay } ) ;
269+ self
270+ }
271+
252272 /// Configures the [`Node`] instance to source its inbound liquidity from the given
253273 /// [LSPS2](https://github.com/BitcoinAndLightningLayerSpecs/lsp/blob/main/LSPS2/README.md)
254274 /// service.
@@ -367,6 +387,7 @@ impl NodeBuilder {
367387 self . chain_data_source_config . as_ref ( ) ,
368388 self . gossip_source_config . as_ref ( ) ,
369389 self . liquidity_source_config . as_ref ( ) ,
390+ self . payjoin_config . as_ref ( ) ,
370391 seed_bytes,
371392 logger,
372393 vss_store,
@@ -391,6 +412,7 @@ impl NodeBuilder {
391412 seed_bytes,
392413 logger,
393414 kv_store,
415+ self . payjoin_config . as_ref ( ) ,
394416 )
395417 }
396418}
@@ -461,6 +483,13 @@ impl ArcedNodeBuilder {
461483 self . inner . write ( ) . unwrap ( ) . set_gossip_source_rgs ( rgs_server_url) ;
462484 }
463485
486+ /// Configures the [`Node`] instance to use payjoin.
487+ pub fn set_payjoin_config (
488+ & self , payjoin_directory : lightning_payjoin:: Url , payjoin_relay : lightning_payjoin:: Url ,
489+ ) {
490+ self . inner . write ( ) . unwrap ( ) . set_payjoin_config ( payjoin_directory, payjoin_relay) ;
491+ }
492+
464493 /// Configures the [`Node`] instance to source its inbound liquidity from the given
465494 /// [LSPS2](https://github.com/BitcoinAndLightningLayerSpecs/lsp/blob/main/LSPS2/README.md)
466495 /// service.
@@ -524,7 +553,7 @@ fn build_with_store_internal(
524553 config : Arc < Config > , chain_data_source_config : Option < & ChainDataSourceConfig > ,
525554 gossip_source_config : Option < & GossipSourceConfig > ,
526555 liquidity_source_config : Option < & LiquiditySourceConfig > , seed_bytes : [ u8 ; 64 ] ,
527- logger : Arc < FilesystemLogger > , kv_store : Arc < DynStore > ,
556+ logger : Arc < FilesystemLogger > , kv_store : Arc < DynStore > , payjoin_config : Option < & PayjoinConfig > ,
528557) -> Result < Node , BuildError > {
529558 // Initialize the on-chain wallet and chain access
530559 let xprv = bitcoin:: bip32:: ExtendedPrivKey :: new_master ( config. network . into ( ) , & seed_bytes)
@@ -557,6 +586,7 @@ fn build_with_store_internal(
557586 log_error ! ( logger, "Failed to set up wallet: {}" , e) ;
558587 BuildError :: WalletSetupFailed
559588 } ) ?;
589+ let channel_scheduler = Arc :: new ( tokio:: sync:: Mutex :: new ( ChannelScheduler :: new ( ) ) ) ;
560590
561591 let ( blockchain, tx_sync, tx_broadcaster, fee_estimator) = match chain_data_source_config {
562592 Some ( ChainDataSourceConfig :: Esplora ( server_url) ) => {
@@ -567,6 +597,7 @@ fn build_with_store_internal(
567597 let tx_broadcaster = Arc :: new ( TransactionBroadcaster :: new (
568598 tx_sync. client ( ) . clone ( ) ,
569599 Arc :: clone ( & logger) ,
600+ Arc :: clone ( & channel_scheduler) ,
570601 ) ) ;
571602 let fee_estimator = Arc :: new ( OnchainFeeEstimator :: new (
572603 tx_sync. client ( ) . clone ( ) ,
@@ -585,6 +616,7 @@ fn build_with_store_internal(
585616 let tx_broadcaster = Arc :: new ( TransactionBroadcaster :: new (
586617 tx_sync. client ( ) . clone ( ) ,
587618 Arc :: clone ( & logger) ,
619+ Arc :: clone ( & channel_scheduler) ,
588620 ) ) ;
589621 let fee_estimator = Arc :: new ( OnchainFeeEstimator :: new (
590622 tx_sync. client ( ) . clone ( ) ,
@@ -974,6 +1006,17 @@ fn build_with_store_internal(
9741006 } ;
9751007
9761008 let ( stop_sender, _) = tokio:: sync:: watch:: channel ( ( ) ) ;
1009+ let payjoin_handler = if let Some ( payjoin_config) = payjoin_config {
1010+ Some ( Arc :: new ( PayjoinHandler :: new (
1011+ Arc :: clone ( & wallet) ,
1012+ Arc :: clone ( & channel_manager) ,
1013+ Arc :: clone ( & channel_scheduler) ,
1014+ payjoin_config. payjoin_directory . clone ( ) ,
1015+ payjoin_config. payjoin_relay . clone ( ) ,
1016+ ) ) )
1017+ } else {
1018+ None
1019+ } ;
9771020
9781021 let is_listening = Arc :: new ( AtomicBool :: new ( false ) ) ;
9791022 let latest_wallet_sync_timestamp = Arc :: new ( RwLock :: new ( None ) ) ;
@@ -994,6 +1037,8 @@ fn build_with_store_internal(
9941037 channel_manager,
9951038 chain_monitor,
9961039 output_sweeper,
1040+ payjoin_handler,
1041+ channel_scheduler,
9971042 peer_manager,
9981043 connection_manager,
9991044 keys_manager,
0 commit comments