1
- use crate :: channel_scheduler:: ChannelScheduler ;
2
1
use crate :: config:: {
3
2
Config , BDK_CLIENT_CONCURRENCY , BDK_CLIENT_STOP_GAP , DEFAULT_ESPLORA_SERVER_URL ,
4
3
WALLET_KEYS_SEED_LEN ,
@@ -12,7 +11,11 @@ use crate::io::sqlite_store::SqliteStore;
12
11
use crate :: liquidity:: LiquiditySource ;
13
12
use crate :: logger:: { log_error, log_info, FilesystemLogger , Logger } ;
14
13
use crate :: message_handler:: NodeCustomMessageHandler ;
15
- use crate :: payjoin_handler:: { PayjoinReceiver , PayjoinSender } ;
14
+ use crate :: payjoin_receiver:: {
15
+ enroll_payjoin_receivers, PayjoinLightningReceiver , PayjoinReceiver ,
16
+ } ;
17
+ use crate :: payjoin_scheduler:: PayjoinScheduler ;
18
+ use crate :: payjoin_sender:: PayjoinSender ;
16
19
use crate :: payment:: store:: PaymentStore ;
17
20
use crate :: peer_store:: PeerStore ;
18
21
use crate :: tx_broadcaster:: TransactionBroadcaster ;
@@ -97,12 +100,17 @@ struct LiquiditySourceConfig {
97
100
}
98
101
99
102
#[ derive( Debug , Clone ) ]
100
- struct PayjoinConfig {
103
+ struct PayjoinReceiverConfig {
101
104
payjoin_directory : payjoin:: Url ,
102
105
payjoin_relay : payjoin:: Url ,
103
106
ohttp_keys : Option < payjoin:: OhttpKeys > ,
104
107
}
105
108
109
+ #[ derive( Debug , Clone ) ]
110
+ struct PayjoinSenderConfig {
111
+ payjoin_relay : payjoin:: Url ,
112
+ }
113
+
106
114
impl Default for LiquiditySourceConfig {
107
115
fn default ( ) -> Self {
108
116
Self { lsps2_service : None }
@@ -182,7 +190,8 @@ pub struct NodeBuilder {
182
190
chain_data_source_config : Option < ChainDataSourceConfig > ,
183
191
gossip_source_config : Option < GossipSourceConfig > ,
184
192
liquidity_source_config : Option < LiquiditySourceConfig > ,
185
- payjoin_config : Option < PayjoinConfig > ,
193
+ payjoin_receiver_config : Option < PayjoinReceiverConfig > ,
194
+ payjoin_sender_config : Option < PayjoinSenderConfig > ,
186
195
}
187
196
188
197
impl NodeBuilder {
@@ -198,14 +207,16 @@ impl NodeBuilder {
198
207
let chain_data_source_config = None ;
199
208
let gossip_source_config = None ;
200
209
let liquidity_source_config = None ;
201
- let payjoin_config = None ;
210
+ let payjoin_receiver_config = None ;
211
+ let payjoin_sender_config = None ;
202
212
Self {
203
213
config,
204
214
entropy_source_config,
205
215
chain_data_source_config,
206
216
gossip_source_config,
207
217
liquidity_source_config,
208
- payjoin_config,
218
+ payjoin_receiver_config,
219
+ payjoin_sender_config,
209
220
}
210
221
}
211
222
@@ -262,11 +273,19 @@ impl NodeBuilder {
262
273
263
274
/// Configures the [`Node`] instance to source its gossip data from the given RapidGossipSync
264
275
/// server.
265
- pub fn set_payjoin_config (
276
+ pub fn set_payjoin_sender_config ( & mut self , payjoin_relay : payjoin:: Url ) -> & mut Self {
277
+ self . payjoin_sender_config = Some ( PayjoinSenderConfig { payjoin_relay } ) ;
278
+ self
279
+ }
280
+
281
+ /// Configures the [`Node`] instance to source its gossip data from the given RapidGossipSync
282
+ /// server.
283
+ pub fn set_payjoin_receiver_config (
266
284
& mut self , payjoin_directory : payjoin:: Url , payjoin_relay : payjoin:: Url ,
267
285
ohttp_keys : Option < payjoin:: OhttpKeys > ,
268
286
) -> & mut Self {
269
- self . payjoin_config = Some ( PayjoinConfig { payjoin_directory, payjoin_relay, ohttp_keys } ) ;
287
+ self . payjoin_receiver_config =
288
+ Some ( PayjoinReceiverConfig { payjoin_directory, payjoin_relay, ohttp_keys } ) ;
270
289
self
271
290
}
272
291
@@ -391,7 +410,8 @@ impl NodeBuilder {
391
410
seed_bytes,
392
411
logger,
393
412
vss_store,
394
- self . payjoin_config . as_ref ( ) ,
413
+ self . payjoin_receiver_config . as_ref ( ) ,
414
+ self . payjoin_sender_config . as_ref ( ) ,
395
415
)
396
416
}
397
417
@@ -413,7 +433,8 @@ impl NodeBuilder {
413
433
seed_bytes,
414
434
logger,
415
435
kv_store,
416
- self . payjoin_config . as_ref ( ) ,
436
+ self . payjoin_receiver_config . as_ref ( ) ,
437
+ self . payjoin_sender_config . as_ref ( ) ,
417
438
)
418
439
}
419
440
}
@@ -484,18 +505,6 @@ impl ArcedNodeBuilder {
484
505
self . inner . write ( ) . unwrap ( ) . set_gossip_source_rgs ( rgs_server_url) ;
485
506
}
486
507
487
- /// Configures the [`Node`] instance to use payjoin.
488
- pub fn set_payjoin_config (
489
- & self , payjoin_directory : payjoin:: Url , payjoin_relay : payjoin:: Url ,
490
- ohttp_keys : Option < payjoin:: OhttpKeys > ,
491
- ) {
492
- self . inner . write ( ) . unwrap ( ) . set_payjoin_config (
493
- payjoin_directory,
494
- payjoin_relay,
495
- ohttp_keys,
496
- ) ;
497
- }
498
-
499
508
/// Configures the [`Node`] instance to source its inbound liquidity from the given
500
509
/// [LSPS2](https://github.com/BitcoinAndLightningLayerSpecs/lsp/blob/main/LSPS2/README.md)
501
510
/// service.
@@ -559,7 +568,9 @@ fn build_with_store_internal(
559
568
config : Arc < Config > , chain_data_source_config : Option < & ChainDataSourceConfig > ,
560
569
gossip_source_config : Option < & GossipSourceConfig > ,
561
570
liquidity_source_config : Option < & LiquiditySourceConfig > , seed_bytes : [ u8 ; 64 ] ,
562
- logger : Arc < FilesystemLogger > , kv_store : Arc < DynStore > , payjoin_config : Option < & PayjoinConfig > ,
571
+ logger : Arc < FilesystemLogger > , kv_store : Arc < DynStore > ,
572
+ payjoin_receiver_config : Option < & PayjoinReceiverConfig > ,
573
+ payjoin_sender_config : Option < & PayjoinSenderConfig > ,
563
574
) -> Result < Node , BuildError > {
564
575
// Initialize the on-chain wallet and chain access
565
576
let xprv = bitcoin:: bip32:: ExtendedPrivKey :: new_master ( config. network . into ( ) , & seed_bytes)
@@ -592,7 +603,7 @@ fn build_with_store_internal(
592
603
log_error ! ( logger, "Failed to set up wallet: {}" , e) ;
593
604
BuildError :: WalletSetupFailed
594
605
} ) ?;
595
- let channel_scheduler = Arc :: new ( tokio:: sync:: Mutex :: new ( ChannelScheduler :: new ( ) ) ) ;
606
+ let payjoin_scheduler = Arc :: new ( tokio:: sync:: Mutex :: new ( PayjoinScheduler :: new ( ) ) ) ;
596
607
597
608
let ( blockchain, tx_sync, tx_broadcaster, fee_estimator) = match chain_data_source_config {
598
609
Some ( ChainDataSourceConfig :: Esplora ( server_url) ) => {
@@ -603,7 +614,7 @@ fn build_with_store_internal(
603
614
let tx_broadcaster = Arc :: new ( TransactionBroadcaster :: new (
604
615
tx_sync. client ( ) . clone ( ) ,
605
616
Arc :: clone ( & logger) ,
606
- Arc :: clone ( & channel_scheduler ) ,
617
+ Arc :: clone ( & payjoin_scheduler ) ,
607
618
) ) ;
608
619
let fee_estimator = Arc :: new ( OnchainFeeEstimator :: new (
609
620
tx_sync. client ( ) . clone ( ) ,
@@ -622,7 +633,7 @@ fn build_with_store_internal(
622
633
let tx_broadcaster = Arc :: new ( TransactionBroadcaster :: new (
623
634
tx_sync. client ( ) . clone ( ) ,
624
635
Arc :: clone ( & logger) ,
625
- Arc :: clone ( & channel_scheduler ) ,
636
+ Arc :: clone ( & payjoin_scheduler ) ,
626
637
) ) ;
627
638
let fee_estimator = Arc :: new ( OnchainFeeEstimator :: new (
628
639
tx_sync. client ( ) . clone ( ) ,
@@ -1012,29 +1023,49 @@ fn build_with_store_internal(
1012
1023
} ;
1013
1024
1014
1025
let ( stop_sender, _) = tokio:: sync:: watch:: channel ( ( ) ) ;
1015
- let ( payjoin_receiver, payjoin_sender) = if let Some ( payjoin_config) = payjoin_config {
1016
- let payjoin_receiver = match PayjoinReceiver :: enroll (
1017
- & payjoin_config. ohttp_keys ,
1018
- & payjoin_config. payjoin_directory ,
1019
- & payjoin_config. payjoin_relay ,
1020
- Arc :: clone ( & channel_scheduler) ,
1021
- Arc :: clone ( & wallet) ,
1022
- Arc :: clone ( & channel_manager) ,
1023
- Arc :: clone ( & logger) ,
1024
- ) {
1025
- Ok ( r) => Some ( Arc :: new ( r) ) ,
1026
- Err ( _e) => None ,
1027
- } ;
1026
+ let payjoin_sender = if let Some ( payjoin_sender_config) = payjoin_sender_config {
1028
1027
let payjoin_sender = PayjoinSender :: new (
1029
1028
Arc :: clone ( & logger) ,
1030
1029
Arc :: clone ( & wallet) ,
1031
- & payjoin_config. payjoin_relay ,
1032
- & payjoin_config. payjoin_directory ,
1030
+ & payjoin_sender_config. payjoin_relay ,
1033
1031
) ;
1034
- ( payjoin_receiver , Some ( Arc :: new ( payjoin_sender) ) )
1032
+ Some ( Arc :: new ( payjoin_sender) )
1035
1033
} else {
1036
- ( None , None )
1034
+ None
1037
1035
} ;
1036
+ let ( payjoin_receiver, payjoin_lightning_receiver) =
1037
+ if let Some ( payjoin_receiver_config) = payjoin_receiver_config {
1038
+ let enrollement = enroll_payjoin_receivers (
1039
+ & payjoin_receiver_config. ohttp_keys ,
1040
+ & payjoin_receiver_config. payjoin_directory ,
1041
+ & payjoin_receiver_config. payjoin_relay ,
1042
+ )
1043
+ . ok ( ) ;
1044
+ if let Some ( enrollement) = enrollement {
1045
+ let ( payjoin_enrollement, lightning_enrollement, ohttp_keys) = enrollement;
1046
+ dbg ! ( "Enrolled payjoin receiver" ) ;
1047
+ let payjoin_receiver = PayjoinReceiver :: new (
1048
+ Arc :: clone ( & logger) ,
1049
+ Arc :: clone ( & wallet) ,
1050
+ payjoin_enrollement,
1051
+ ohttp_keys. clone ( ) ,
1052
+ ) ;
1053
+
1054
+ let payjoin_lightning_receiver = PayjoinLightningReceiver :: new (
1055
+ Arc :: clone ( & logger) ,
1056
+ Arc :: clone ( & wallet) ,
1057
+ Arc :: clone ( & channel_manager) ,
1058
+ Arc :: clone ( & payjoin_scheduler) ,
1059
+ lightning_enrollement,
1060
+ ohttp_keys,
1061
+ ) ;
1062
+ ( Some ( Arc :: new ( payjoin_receiver) ) , Some ( Arc :: new ( payjoin_lightning_receiver) ) )
1063
+ } else {
1064
+ ( None , None )
1065
+ }
1066
+ } else {
1067
+ ( None , None )
1068
+ } ;
1038
1069
1039
1070
let is_listening = Arc :: new ( AtomicBool :: new ( false ) ) ;
1040
1071
let latest_wallet_sync_timestamp = Arc :: new ( RwLock :: new ( None ) ) ;
@@ -1057,6 +1088,7 @@ fn build_with_store_internal(
1057
1088
output_sweeper,
1058
1089
payjoin_receiver,
1059
1090
payjoin_sender,
1091
+ payjoin_lightning_receiver,
1060
1092
peer_manager,
1061
1093
connection_manager,
1062
1094
keys_manager,
0 commit comments