@@ -100,15 +100,18 @@ mod wallet;
100
100
pub use bip39;
101
101
pub use bitcoin;
102
102
pub use lightning;
103
+ use lightning:: routing:: gossip:: NodeId ;
103
104
pub use lightning_invoice;
104
105
105
106
pub use balance:: { BalanceDetails , LightningBalance , PendingSweepBalance } ;
106
107
pub use config:: { default_config, Config } ;
107
108
pub use error:: Error as NodeError ;
108
109
use error:: Error ;
109
110
110
- use :: payjoin:: Uri ;
111
+ use payjoin:: Uri ;
111
112
pub use event:: Event ;
113
+ use pj:: { PayjoinExecuter , PayjoinScheduler , PendingChannels } ;
114
+ use tokio:: sync:: mpsc;
112
115
pub use types:: ChannelConfig ;
113
116
114
117
pub use io:: utils:: generate_entropy_mnemonic;
@@ -172,6 +175,8 @@ use std::sync::{Arc, Mutex, RwLock};
172
175
use std:: time:: { Duration , Instant , SystemTime } ;
173
176
use std:: { default:: Default , str:: FromStr } ;
174
177
178
+ use crate :: pj:: { PayjoinRequest , PayjoinResponse , ScheduledChannel } ;
179
+
175
180
#[ cfg( feature = "uniffi" ) ]
176
181
uniffi:: include_scaffolding!( "ldk_node" ) ;
177
182
@@ -191,6 +196,8 @@ pub struct Node<K: KVStore + Sync + Send + 'static> {
191
196
channel_manager : Arc < ChannelManager < K > > ,
192
197
chain_monitor : Arc < ChainMonitor < K > > ,
193
198
output_sweeper : Arc < Sweeper < K > > ,
199
+ pending_channels : Arc < Mutex < PendingChannels > > ,
200
+ payjoin_executer : Arc < PayjoinExecuter < K > > ,
194
201
peer_manager : Arc < PeerManager < K > > ,
195
202
keys_manager : Arc < KeysManager > ,
196
203
network_graph : Arc < NetworkGraph > ,
@@ -470,21 +477,29 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
470
477
use tokio:: net:: TcpListener ;
471
478
472
479
// Start the HTTP server for payjoin
473
- let wallet = Arc :: clone ( & self . wallet ) ;
480
+ let ( payjoin_queue_sender, mut payjoin_queue_receiver) = mpsc:: channel :: < PayjoinRequest > ( 1 ) ;
481
+ let executor = Arc :: clone ( & self . payjoin_executer ) ;
482
+ // listen for payjoin_queue_receiver
483
+ runtime. spawn ( async move {
484
+ loop {
485
+ let payjoin_request = payjoin_queue_receiver. recv ( ) . await . unwrap ( ) ;
486
+ Self :: create_channel_from_pj ( payjoin_request, executor. clone ( ) ) . await ;
487
+ }
488
+ } ) ;
489
+
490
+ let pj_port = self . config . payjoin_server_port ;
474
491
runtime. spawn ( async move {
475
- let addr = SocketAddr :: from ( ( [ 127 , 0 , 0 , 1 ] , PAYJOIN_HTTP_SERVER_PORT ) ) ;
492
+ let addr = SocketAddr :: from ( ( [ 127 , 0 , 0 , 1 ] , pj_port ) ) ;
476
493
let listener = TcpListener :: bind ( addr) . await . unwrap ( ) ;
477
494
dbg ! ( "Started HTTP server on http://{}" , addr) ;
478
- // let our_pubkey= wallet.get_new_address().unwrap().script_pubkey().into_bytes();
479
- let create_channel_request = pj:: CreateChannelRequest :: init ( wallet) ;
495
+ let pj_scheduler = pj:: PayjoinScheduler :: new ( payjoin_queue_sender) ;
480
496
loop {
481
497
let ( stream, _) = listener. accept ( ) . await . unwrap ( ) ;
482
498
let io = TokioIo :: new ( stream) ;
483
- let clone_ccr = create_channel_request . clone ( ) ;
499
+ let clone_pj_scheduler = pj_scheduler . clone ( ) ;
484
500
tokio:: task:: spawn ( async move {
485
- if let Err ( err) = http1:: Builder :: new ( )
486
- . serve_connection ( io, clone_ccr)
487
- . await
501
+ if let Err ( err) =
502
+ http1:: Builder :: new ( ) . serve_connection ( io, clone_pj_scheduler) . await
488
503
{
489
504
println ! ( "Failed to serve connection: {:?}" , err) ;
490
505
}
@@ -705,11 +720,36 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
705
720
self . runtime . read ( ) . unwrap ( ) . is_some ( )
706
721
}
707
722
708
- /// Request Payjoin Payment
709
- pub fn payjoin_uri ( & self ) -> Result < String , Error > {
723
+ async fn create_channel_from_pj (
724
+ pj_req : PayjoinRequest , _payjoin_executer : Arc < PayjoinExecuter < K > > ,
725
+ ) {
726
+ let psbt = pj_req. clone ( ) . original_psbt ( ) ;
727
+ let pj_response = PayjoinResponse :: new ( psbt) ;
728
+ // Send OpenChannel message
729
+ // Wait for AcceptChannel message
730
+ // Send FundingCreated message
731
+ // Wait for FundingSigned message
732
+ // Build PayjoinResponse
733
+ // Send PayjoinResponse to queue
734
+ pj_req. clone ( ) . queue ( pj_response) ;
735
+ }
736
+
737
+ /// Request a new channel to be opened with a remote peer.
738
+ pub fn payjoin_channel (
739
+ & self , channel_amount_sats : u64 , push_msat : Option < u64 > , announce_channel : bool ,
740
+ node_id : PublicKey ,
741
+ ) -> Result < String , Error > {
742
+ let user_channel_id: u128 = rand:: thread_rng ( ) . gen :: < u128 > ( ) ;
743
+ self . pending_channels . lock ( ) . unwrap ( ) . push ( ScheduledChannel :: new (
744
+ channel_amount_sats,
745
+ push_msat,
746
+ user_channel_id,
747
+ announce_channel,
748
+ node_id,
749
+ ) ) ;
710
750
let address = self . wallet . get_new_address ( ) ?;
711
- let amount = Amount :: from_sat ( 1000 ) ;
712
- let pj = "https://0.0.0.0:3227 /payjoin" ;
751
+ let amount = Amount :: from_sat ( channel_amount_sats ) ;
752
+ let pj = format ! ( "https://0.0.0.0:{} /payjoin" , self . config . payjoin_server_port ) ;
713
753
let pj_uri_string = format ! ( "{}?amount={}&pj={}" , address. to_qr_uri( ) , amount. to_btc( ) , pj) ;
714
754
assert ! ( Uri :: from_str( & pj_uri_string) . is_ok( ) ) ;
715
755
Ok ( pj_uri_string)
0 commit comments