@@ -100,15 +100,18 @@ mod wallet;
100100pub use bip39;
101101pub use bitcoin;
102102pub use lightning;
103+ use lightning:: routing:: gossip:: NodeId ;
103104pub use lightning_invoice;
104105
105106pub use balance:: { BalanceDetails , LightningBalance , PendingSweepBalance } ;
106107pub use config:: { default_config, Config } ;
107108pub use error:: Error as NodeError ;
108109use error:: Error ;
109110
110- use :: payjoin:: Uri ;
111+ use payjoin:: Uri ;
111112pub use event:: Event ;
113+ use pj:: { PayjoinExecuter , PayjoinScheduler , PendingChannels } ;
114+ use tokio:: sync:: mpsc;
112115pub use types:: ChannelConfig ;
113116
114117pub use io:: utils:: generate_entropy_mnemonic;
@@ -172,6 +175,8 @@ use std::sync::{Arc, Mutex, RwLock};
172175use std:: time:: { Duration , Instant , SystemTime } ;
173176use std:: { default:: Default , str:: FromStr } ;
174177
178+ use crate :: pj:: { PayjoinRequest , PayjoinResponse , ScheduledChannel } ;
179+
175180#[ cfg( feature = "uniffi" ) ]
176181uniffi:: include_scaffolding!( "ldk_node" ) ;
177182
@@ -191,6 +196,8 @@ pub struct Node<K: KVStore + Sync + Send + 'static> {
191196 channel_manager : Arc < ChannelManager < K > > ,
192197 chain_monitor : Arc < ChainMonitor < K > > ,
193198 output_sweeper : Arc < Sweeper < K > > ,
199+ pending_channels : Arc < Mutex < PendingChannels > > ,
200+ payjoin_executer : Arc < PayjoinExecuter < K > > ,
194201 peer_manager : Arc < PeerManager < K > > ,
195202 keys_manager : Arc < KeysManager > ,
196203 network_graph : Arc < NetworkGraph > ,
@@ -470,21 +477,29 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
470477 use tokio:: net:: TcpListener ;
471478
472479 // 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 ;
474491 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 ) ) ;
476493 let listener = TcpListener :: bind ( addr) . await . unwrap ( ) ;
477494 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) ;
480496 loop {
481497 let ( stream, _) = listener. accept ( ) . await . unwrap ( ) ;
482498 let io = TokioIo :: new ( stream) ;
483- let clone_ccr = create_channel_request . clone ( ) ;
499+ let clone_pj_scheduler = pj_scheduler . clone ( ) ;
484500 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
488503 {
489504 println ! ( "Failed to serve connection: {:?}" , err) ;
490505 }
@@ -705,11 +720,36 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
705720 self . runtime . read ( ) . unwrap ( ) . is_some ( )
706721 }
707722
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+ ) ) ;
710750 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 ) ;
713753 let pj_uri_string = format ! ( "{}?amount={}&pj={}" , address. to_qr_uri( ) , amount. to_btc( ) , pj) ;
714754 assert ! ( Uri :: from_str( & pj_uri_string) . is_ok( ) ) ;
715755 Ok ( pj_uri_string)
0 commit comments