@@ -123,14 +123,14 @@ pub use builder::BuildError;
123123pub use builder:: NodeBuilder as Builder ;
124124
125125use config:: {
126- LDK_PAYMENT_RETRY_TIMEOUT , NODE_ANN_BCAST_INTERVAL , PEER_RECONNECTION_INTERVAL ,
127- RGS_SYNC_INTERVAL , WALLET_SYNC_INTERVAL_MINIMUM_SECS ,
126+ NODE_ANN_BCAST_INTERVAL , PEER_RECONNECTION_INTERVAL , RGS_SYNC_INTERVAL ,
127+ WALLET_SYNC_INTERVAL_MINIMUM_SECS ,
128128} ;
129129use connection:: ConnectionManager ;
130130use event:: { EventHandler , EventQueue } ;
131131use gossip:: GossipSource ;
132132use liquidity:: LiquiditySource ;
133- use payment:: Bolt11PaymentHandler ;
133+ use payment:: { Bolt11PaymentHandler , SpontaneousPaymentHandler } ;
134134use payment_store:: PaymentStore ;
135135pub use payment_store:: { LSPFeeLimits , PaymentDetails , PaymentDirection , PaymentStatus } ;
136136use peer_store:: { PeerInfo , PeerStore } ;
@@ -143,11 +143,8 @@ pub use types::{ChannelDetails, PeerDetails, UserChannelId};
143143use logger:: { log_error, log_info, log_trace, FilesystemLogger , Logger } ;
144144
145145use lightning:: chain:: Confirm ;
146- use lightning:: ln:: channelmanager:: { self , PaymentId , RecipientOnionFields , Retry } ;
147146use lightning:: ln:: msgs:: SocketAddress ;
148- use lightning:: ln:: { PaymentHash , PaymentPreimage } ;
149-
150- use lightning:: sign:: EntropySource ;
147+ use lightning:: ln:: PaymentHash ;
151148
152149use lightning:: util:: config:: { ChannelHandshakeConfig , UserConfig } ;
153150pub use lightning:: util:: logger:: Level as LogLevel ;
@@ -156,8 +153,6 @@ use lightning_background_processor::process_events_async;
156153
157154use lightning_transaction_sync:: EsploraSyncClient ;
158155
159- use lightning:: routing:: router:: { PaymentParameters , RouteParameters } ;
160-
161156use bitcoin:: secp256k1:: PublicKey ;
162157use bitcoin:: { Address , Txid } ;
163158
@@ -763,6 +758,18 @@ impl Node {
763758 ) )
764759 }
765760
761+ /// Returns a payment handler allowing to send spontaneous ("keysend") payments.
762+ pub fn spontaneous_payment ( & self ) -> Arc < SpontaneousPaymentHandler > {
763+ Arc :: new ( SpontaneousPaymentHandler :: new (
764+ Arc :: clone ( & self . runtime ) ,
765+ Arc :: clone ( & self . channel_manager ) ,
766+ Arc :: clone ( & self . keys_manager ) ,
767+ Arc :: clone ( & self . payment_store ) ,
768+ Arc :: clone ( & self . config ) ,
769+ Arc :: clone ( & self . logger ) ,
770+ ) )
771+ }
772+
766773 /// Retrieve a new on-chain/funding address.
767774 pub fn new_onchain_address ( & self ) -> Result < Address , Error > {
768775 let funding_address = self . wallet . get_new_address ( ) ?;
@@ -1049,112 +1056,6 @@ impl Node {
10491056 }
10501057 }
10511058
1052- /// Send a spontaneous, aka. "keysend", payment
1053- pub fn send_spontaneous_payment (
1054- & self , amount_msat : u64 , node_id : PublicKey ,
1055- ) -> Result < PaymentHash , Error > {
1056- let rt_lock = self . runtime . read ( ) . unwrap ( ) ;
1057- if rt_lock. is_none ( ) {
1058- return Err ( Error :: NotRunning ) ;
1059- }
1060-
1061- let payment_preimage = PaymentPreimage ( self . keys_manager . get_secure_random_bytes ( ) ) ;
1062- let payment_hash = PaymentHash :: from ( payment_preimage) ;
1063-
1064- if let Some ( payment) = self . payment_store . get ( & payment_hash) {
1065- if payment. status == PaymentStatus :: Pending
1066- || payment. status == PaymentStatus :: Succeeded
1067- {
1068- log_error ! ( self . logger, "Payment error: must not send duplicate payments." ) ;
1069- return Err ( Error :: DuplicatePayment ) ;
1070- }
1071- }
1072-
1073- let route_params = RouteParameters :: from_payment_params_and_value (
1074- PaymentParameters :: from_node_id ( node_id, self . config . default_cltv_expiry_delta ) ,
1075- amount_msat,
1076- ) ;
1077- let recipient_fields = RecipientOnionFields :: spontaneous_empty ( ) ;
1078-
1079- match self . channel_manager . send_spontaneous_payment_with_retry (
1080- Some ( payment_preimage) ,
1081- recipient_fields,
1082- PaymentId ( payment_hash. 0 ) ,
1083- route_params,
1084- Retry :: Timeout ( LDK_PAYMENT_RETRY_TIMEOUT ) ,
1085- ) {
1086- Ok ( _payment_id) => {
1087- log_info ! ( self . logger, "Initiated sending {}msat to {}." , amount_msat, node_id) ;
1088-
1089- let payment = PaymentDetails {
1090- hash : payment_hash,
1091- preimage : Some ( payment_preimage) ,
1092- secret : None ,
1093- status : PaymentStatus :: Pending ,
1094- direction : PaymentDirection :: Outbound ,
1095- amount_msat : Some ( amount_msat) ,
1096- lsp_fee_limits : None ,
1097- } ;
1098- self . payment_store . insert ( payment) ?;
1099-
1100- Ok ( payment_hash)
1101- } ,
1102- Err ( e) => {
1103- log_error ! ( self . logger, "Failed to send payment: {:?}" , e) ;
1104-
1105- match e {
1106- channelmanager:: RetryableSendFailure :: DuplicatePayment => {
1107- Err ( Error :: DuplicatePayment )
1108- } ,
1109- _ => {
1110- let payment = PaymentDetails {
1111- hash : payment_hash,
1112- preimage : Some ( payment_preimage) ,
1113- secret : None ,
1114- status : PaymentStatus :: Failed ,
1115- direction : PaymentDirection :: Outbound ,
1116- amount_msat : Some ( amount_msat) ,
1117- lsp_fee_limits : None ,
1118- } ;
1119-
1120- self . payment_store . insert ( payment) ?;
1121- Err ( Error :: PaymentSendingFailed )
1122- } ,
1123- }
1124- } ,
1125- }
1126- }
1127-
1128- /// Sends payment probes over all paths of a route that would be used to pay the given
1129- /// amount to the given `node_id`.
1130- ///
1131- /// See [`Bolt11PaymentHandler::send_probes`] for more information.
1132- pub fn send_spontaneous_payment_probes (
1133- & self , amount_msat : u64 , node_id : PublicKey ,
1134- ) -> Result < ( ) , Error > {
1135- let rt_lock = self . runtime . read ( ) . unwrap ( ) ;
1136- if rt_lock. is_none ( ) {
1137- return Err ( Error :: NotRunning ) ;
1138- }
1139-
1140- let liquidity_limit_multiplier = Some ( self . config . probing_liquidity_limit_multiplier ) ;
1141- let cltv_expiry_delta = self . config . default_cltv_expiry_delta ;
1142-
1143- self . channel_manager
1144- . send_spontaneous_preflight_probes (
1145- node_id,
1146- amount_msat,
1147- cltv_expiry_delta,
1148- liquidity_limit_multiplier,
1149- )
1150- . map_err ( |e| {
1151- log_error ! ( self . logger, "Failed to send payment probes: {:?}" , e) ;
1152- Error :: ProbeSendingFailed
1153- } ) ?;
1154-
1155- Ok ( ( ) )
1156- }
1157-
11581059 /// Retrieve the details of a specific payment with the given hash.
11591060 ///
11601061 /// Returns `Some` if the payment was known and `None` otherwise.
0 commit comments