@@ -11,6 +11,7 @@ use crate::io::sqlite_store::SqliteStore;
11
11
use crate :: liquidity:: LiquiditySource ;
12
12
use crate :: logger:: { log_error, log_info, FilesystemLogger , Logger } ;
13
13
use crate :: message_handler:: NodeCustomMessageHandler ;
14
+ use crate :: payment:: payjoin:: handler:: PayjoinHandler ;
14
15
use crate :: payment:: store:: PaymentStore ;
15
16
use crate :: peer_store:: PeerStore ;
16
17
use crate :: tx_broadcaster:: TransactionBroadcaster ;
@@ -93,6 +94,11 @@ struct LiquiditySourceConfig {
93
94
lsps2_service : Option < ( SocketAddress , PublicKey , Option < String > ) > ,
94
95
}
95
96
97
+ #[ derive( Debug , Clone ) ]
98
+ struct PayjoinConfig {
99
+ payjoin_relay : payjoin:: Url ,
100
+ }
101
+
96
102
impl Default for LiquiditySourceConfig {
97
103
fn default ( ) -> Self {
98
104
Self { lsps2_service : None }
@@ -132,6 +138,8 @@ pub enum BuildError {
132
138
WalletSetupFailed ,
133
139
/// We failed to setup the logger.
134
140
LoggerSetupFailed ,
141
+ /// Invalid Payjoin configuration.
142
+ InvalidPayjoinConfig ,
135
143
}
136
144
137
145
impl fmt:: Display for BuildError {
@@ -152,6 +160,10 @@ impl fmt::Display for BuildError {
152
160
Self :: KVStoreSetupFailed => write ! ( f, "Failed to setup KVStore." ) ,
153
161
Self :: WalletSetupFailed => write ! ( f, "Failed to setup onchain wallet." ) ,
154
162
Self :: LoggerSetupFailed => write ! ( f, "Failed to setup the logger." ) ,
163
+ Self :: InvalidPayjoinConfig => write ! (
164
+ f,
165
+ "Invalid Payjoin configuration. Make sure the provided arguments are valid URLs."
166
+ ) ,
155
167
}
156
168
}
157
169
}
@@ -172,6 +184,7 @@ pub struct NodeBuilder {
172
184
chain_data_source_config : Option < ChainDataSourceConfig > ,
173
185
gossip_source_config : Option < GossipSourceConfig > ,
174
186
liquidity_source_config : Option < LiquiditySourceConfig > ,
187
+ payjoin_config : Option < PayjoinConfig > ,
175
188
}
176
189
177
190
impl NodeBuilder {
@@ -187,12 +200,14 @@ impl NodeBuilder {
187
200
let chain_data_source_config = None ;
188
201
let gossip_source_config = None ;
189
202
let liquidity_source_config = None ;
203
+ let payjoin_config = None ;
190
204
Self {
191
205
config,
192
206
entropy_source_config,
193
207
chain_data_source_config,
194
208
gossip_source_config,
195
209
liquidity_source_config,
210
+ payjoin_config,
196
211
}
197
212
}
198
213
@@ -247,6 +262,14 @@ impl NodeBuilder {
247
262
self
248
263
}
249
264
265
+ /// Configures the [`Node`] instance to enable payjoin transactions.
266
+ pub fn set_payjoin_config ( & mut self , payjoin_relay : String ) -> Result < & mut Self , BuildError > {
267
+ let payjoin_relay =
268
+ payjoin:: Url :: parse ( & payjoin_relay) . map_err ( |_| BuildError :: InvalidPayjoinConfig ) ?;
269
+ self . payjoin_config = Some ( PayjoinConfig { payjoin_relay } ) ;
270
+ Ok ( self )
271
+ }
272
+
250
273
/// Configures the [`Node`] instance to source its inbound liquidity from the given
251
274
/// [LSPS2](https://github.com/BitcoinAndLightningLayerSpecs/lsp/blob/main/LSPS2/README.md)
252
275
/// service.
@@ -365,6 +388,7 @@ impl NodeBuilder {
365
388
self . chain_data_source_config . as_ref ( ) ,
366
389
self . gossip_source_config . as_ref ( ) ,
367
390
self . liquidity_source_config . as_ref ( ) ,
391
+ self . payjoin_config . as_ref ( ) ,
368
392
seed_bytes,
369
393
logger,
370
394
vss_store,
@@ -386,6 +410,7 @@ impl NodeBuilder {
386
410
self . chain_data_source_config . as_ref ( ) ,
387
411
self . gossip_source_config . as_ref ( ) ,
388
412
self . liquidity_source_config . as_ref ( ) ,
413
+ self . payjoin_config . as_ref ( ) ,
389
414
seed_bytes,
390
415
logger,
391
416
kv_store,
@@ -453,6 +478,11 @@ impl ArcedNodeBuilder {
453
478
self . inner . write ( ) . unwrap ( ) . set_gossip_source_p2p ( ) ;
454
479
}
455
480
481
+ /// Configures the [`Node`] instance to enable payjoin transactions.
482
+ pub fn set_payjoin_config ( & self , payjoin_relay : String ) -> Result < ( ) , BuildError > {
483
+ self . inner . write ( ) . unwrap ( ) . set_payjoin_config ( payjoin_relay) . map ( |_| ( ) )
484
+ }
485
+
456
486
/// Configures the [`Node`] instance to source its gossip data from the given RapidGossipSync
457
487
/// server.
458
488
pub fn set_gossip_source_rgs ( & self , rgs_server_url : String ) {
@@ -521,8 +551,9 @@ impl ArcedNodeBuilder {
521
551
fn build_with_store_internal (
522
552
config : Arc < Config > , chain_data_source_config : Option < & ChainDataSourceConfig > ,
523
553
gossip_source_config : Option < & GossipSourceConfig > ,
524
- liquidity_source_config : Option < & LiquiditySourceConfig > , seed_bytes : [ u8 ; 64 ] ,
525
- logger : Arc < FilesystemLogger > , kv_store : Arc < DynStore > ,
554
+ liquidity_source_config : Option < & LiquiditySourceConfig > ,
555
+ payjoin_config : Option < & PayjoinConfig > , seed_bytes : [ u8 ; 64 ] , logger : Arc < FilesystemLogger > ,
556
+ kv_store : Arc < DynStore > ,
526
557
) -> Result < Node , BuildError > {
527
558
// Initialize the on-chain wallet and chain access
528
559
let xprv = bitcoin:: bip32:: ExtendedPrivKey :: new_master ( config. network . into ( ) , & seed_bytes)
@@ -966,6 +997,17 @@ fn build_with_store_internal(
966
997
let ( stop_sender, _) = tokio:: sync:: watch:: channel ( ( ) ) ;
967
998
let ( event_handling_stopped_sender, _) = tokio:: sync:: watch:: channel ( ( ) ) ;
968
999
1000
+ let payjoin_handler = payjoin_config. map ( |pj_config| {
1001
+ Arc :: new ( PayjoinHandler :: new (
1002
+ Arc :: clone ( & tx_sync) ,
1003
+ Arc :: clone ( & event_queue) ,
1004
+ Arc :: clone ( & logger) ,
1005
+ pj_config. payjoin_relay . clone ( ) ,
1006
+ Arc :: clone ( & payment_store) ,
1007
+ Arc :: clone ( & wallet) ,
1008
+ ) )
1009
+ } ) ;
1010
+
969
1011
let is_listening = Arc :: new ( AtomicBool :: new ( false ) ) ;
970
1012
let latest_wallet_sync_timestamp = Arc :: new ( RwLock :: new ( None ) ) ;
971
1013
let latest_onchain_wallet_sync_timestamp = Arc :: new ( RwLock :: new ( None ) ) ;
@@ -987,6 +1029,7 @@ fn build_with_store_internal(
987
1029
channel_manager,
988
1030
chain_monitor,
989
1031
output_sweeper,
1032
+ payjoin_handler,
990
1033
peer_manager,
991
1034
connection_manager,
992
1035
keys_manager,
0 commit comments