@@ -109,6 +109,7 @@ pub use error::Error as NodeError;
109109use error:: Error ;
110110
111111pub use event:: Event ;
112+ use pj_new_crate:: ChannelScheduler ;
112113pub use types:: { BestBlock , ChannelConfig } ;
113114use payjoin:: Uri ;
114115mod pjoin;
@@ -195,6 +196,7 @@ pub struct Node<K: KVStore + Sync + Send + 'static> {
195196 chain_monitor : Arc < ChainMonitor < K > > ,
196197 output_sweeper : Arc < Sweeper < K > > ,
197198 payjoin : Arc < LDKPayjoin < K > > ,
199+ channel_scheduler : Arc < tokio:: sync:: Mutex < ChannelScheduler > > ,
198200 peer_manager : Arc < PeerManager < K > > ,
199201 keys_manager : Arc < KeysManager > ,
200202 network_graph : Arc < NetworkGraph > ,
@@ -667,6 +669,7 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
667669 Arc :: clone ( & self . runtime ) ,
668670 Arc :: clone ( & self . logger ) ,
669671 Arc :: clone ( & self . config ) ,
672+ Arc :: clone ( & self . channel_scheduler ) ,
670673 ) ) ;
671674
672675 // Setup background processing
@@ -738,18 +741,33 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
738741
739742 /// Request a new channel to be opened with a remote peer.
740743 pub async fn schedule_payjoin_channel (
741- & self , channel_amount_sats : u64 , push_msat : Option < u64 > , announce_channel : bool ,
744+ & self ,
745+ channel_amount_sats : u64 ,
746+ push_msat : Option < u64 > ,
747+ announce_channel : bool ,
742748 node_id : PublicKey ,
749+ address : SocketAddress ,
743750 ) -> Result < String , Error > {
751+ let user_channel_id: u128 = rand:: thread_rng ( ) . gen :: < u128 > ( ) ;
744752 let channel =
745- ScheduledChannel :: new ( channel_amount_sats, push_msat, announce_channel, node_id) ;
746- self . payjoin . schedule ( channel) . await ;
753+ ScheduledChannel :: new ( channel_amount_sats, push_msat, announce_channel, node_id, user_channel_id, None , None ) ;
754+ self . channel_scheduler . lock ( ) . await . schedule ( channel) ;
755+ let announce_channel = true ;
756+ self . connect_open_channel_payjoin (
757+ node_id,
758+ address,
759+ channel_amount_sats,
760+ None ,
761+ None ,
762+ announce_channel,
763+ user_channel_id
764+ ) ?; // this should be stopped after `ACCEPT_CHANNEL`
747765 let bip21 = self . payjoin_bip21 ( channel_amount_sats) ;
748766 bip21
749767 }
750768
751769 /// Generate a BIP21 URI for a payjoin request.
752- pub fn payjoin_bip21 ( & self , amount_sats : u64 ) -> Result < String , Error > {
770+ fn payjoin_bip21 ( & self , amount_sats : u64 ) -> Result < String , Error > {
753771 let address = self . wallet . get_new_address ( ) ?;
754772 let amount = Amount :: from_sat ( amount_sats) ;
755773 let pj = format ! ( "https://0.0.0.0:{}/payjoin" , self . config. payjoin_server_port) ;
@@ -760,7 +778,7 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
760778
761779 /// List all scheduled payjoin channels.
762780 pub async fn list_scheduled_channels ( & self ) -> Result < Vec < ScheduledChannel > , Error > {
763- Ok ( self . payjoin . list_scheduled_channels ( ) . await )
781+ Ok ( self . channel_scheduler . lock ( ) . await . channels . clone ( ) )
764782 }
765783
766784 /// Disconnects all peers, stops all running background tasks, and shuts down [`Node`].
@@ -973,6 +991,76 @@ impl<K: KVStore + Sync + Send + 'static> Node<K> {
973991 Ok ( ( ) )
974992 }
975993
994+ /// included `user_channel_id` in inputs
995+ pub fn connect_open_channel_payjoin (
996+ & self , node_id : PublicKey , address : SocketAddress , channel_amount_sats : u64 ,
997+ push_to_counterparty_msat : Option < u64 > , channel_config : Option < Arc < ChannelConfig > > ,
998+ announce_channel : bool , user_channel_id : u128 ,
999+ ) -> Result < UserChannelId , Error > {
1000+ let rt_lock = self . runtime . read ( ) . unwrap ( ) ;
1001+ if rt_lock. is_none ( ) {
1002+ return Err ( Error :: NotRunning ) ;
1003+ }
1004+ let runtime = rt_lock. as_ref ( ) . unwrap ( ) ;
1005+
1006+ // let cur_balance = self.wallet.get_balance()?;
1007+ // if cur_balance.get_spendable() < channel_amount_sats {
1008+ // log_error!(self.logger, "Unable to create channel due to insufficient funds.");
1009+ // return Err(Error::InsufficientFunds);
1010+ // }
1011+
1012+ let peer_info = PeerInfo { node_id, address } ;
1013+
1014+ let con_node_id = peer_info. node_id ;
1015+ let con_addr = peer_info. address . clone ( ) ;
1016+ let con_logger = Arc :: clone ( & self . logger ) ;
1017+ let con_pm = Arc :: clone ( & self . peer_manager ) ;
1018+
1019+ // We need to use our main runtime here as a local runtime might not be around to poll
1020+ // connection futures going forward.
1021+ tokio:: task:: block_in_place ( move || {
1022+ runtime. block_on ( async move {
1023+ connect_peer_if_necessary ( con_node_id, con_addr, con_pm, con_logger) . await
1024+ } )
1025+ } ) ?;
1026+
1027+ let channel_config = ( * ( channel_config. unwrap_or_default ( ) ) ) . clone ( ) . into ( ) ;
1028+ let user_config = UserConfig {
1029+ channel_handshake_limits : Default :: default ( ) ,
1030+ channel_handshake_config : ChannelHandshakeConfig {
1031+ announced_channel : announce_channel,
1032+ ..Default :: default ( )
1033+ } ,
1034+ channel_config,
1035+ ..Default :: default ( )
1036+ } ;
1037+
1038+ let push_msat = push_to_counterparty_msat. unwrap_or ( 0 ) ;
1039+
1040+ match self . channel_manager . create_channel (
1041+ peer_info. node_id ,
1042+ channel_amount_sats,
1043+ push_msat,
1044+ user_channel_id,
1045+ None ,
1046+ Some ( user_config) ,
1047+ ) {
1048+ Ok ( _) => {
1049+ log_info ! (
1050+ self . logger,
1051+ "Initiated channel creation with peer {}. " ,
1052+ peer_info. node_id
1053+ ) ;
1054+ self . peer_store . add_peer ( peer_info) ?;
1055+ Ok ( UserChannelId ( user_channel_id) )
1056+ } ,
1057+ Err ( e) => {
1058+ log_error ! ( self . logger, "Failed to initiate channel creation: {:?}" , e) ;
1059+ Err ( Error :: ChannelCreationFailed )
1060+ } ,
1061+ }
1062+ }
1063+
9761064 /// Connect to a node and open a new channel. Disconnects and re-connects are handled automatically
9771065 ///
9781066 /// Disconnects and reconnects are handled automatically.
@@ -1964,3 +2052,15 @@ async fn do_connect_peer<K: KVStore + Sync + Send + 'static>(
19642052 } ,
19652053 }
19662054}
2055+
2056+
2057+ // 1. user schedule channel
2058+ // 1.1 qrcode created to scan
2059+ // 2. user scan qrcode
2060+ // 2.1 node receives payjoin request
2061+ // 2.2 http endpoint loops for x amount of time looking for PayjoinProposal
2062+ // 3. node scans if any scheduled channels waiting
2063+ // 3.1 node creates the requested channel
2064+ // 4. node wait for payjoin channel open requests in FundingGenerationReady state
2065+ // 4.1 node creates funding tx with payjoin incoming transaction
2066+ // 4.2 save in channel scheduler
0 commit comments