@@ -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
@@ -834,6 +829,18 @@ impl Node {
834829 ) )
835830 }
836831
832+ /// Returns a payment handler allowing to send spontaneous ("keysend") payments.
833+ pub fn spontaneous_payment ( & self ) -> Arc < SpontaneousPaymentHandler > {
834+ Arc :: new ( SpontaneousPaymentHandler :: new (
835+ Arc :: clone ( & self . runtime ) ,
836+ Arc :: clone ( & self . channel_manager ) ,
837+ Arc :: clone ( & self . keys_manager ) ,
838+ Arc :: clone ( & self . payment_store ) ,
839+ Arc :: clone ( & self . config ) ,
840+ Arc :: clone ( & self . logger ) ,
841+ ) )
842+ }
843+
837844 /// Retrieve a new on-chain/funding address.
838845 pub fn new_onchain_address ( & self ) -> Result < Address , Error > {
839846 let funding_address = self . wallet . get_new_address ( ) ?;
@@ -1120,112 +1127,6 @@ impl Node {
11201127 }
11211128 }
11221129
1123- /// Send a spontaneous, aka. "keysend", payment
1124- pub fn send_spontaneous_payment (
1125- & self , amount_msat : u64 , node_id : PublicKey ,
1126- ) -> Result < PaymentHash , Error > {
1127- let rt_lock = self . runtime . read ( ) . unwrap ( ) ;
1128- if rt_lock. is_none ( ) {
1129- return Err ( Error :: NotRunning ) ;
1130- }
1131-
1132- let payment_preimage = PaymentPreimage ( self . keys_manager . get_secure_random_bytes ( ) ) ;
1133- let payment_hash = PaymentHash :: from ( payment_preimage) ;
1134-
1135- if let Some ( payment) = self . payment_store . get ( & payment_hash) {
1136- if payment. status == PaymentStatus :: Pending
1137- || payment. status == PaymentStatus :: Succeeded
1138- {
1139- log_error ! ( self . logger, "Payment error: must not send duplicate payments." ) ;
1140- return Err ( Error :: DuplicatePayment ) ;
1141- }
1142- }
1143-
1144- let route_params = RouteParameters :: from_payment_params_and_value (
1145- PaymentParameters :: from_node_id ( node_id, self . config . default_cltv_expiry_delta ) ,
1146- amount_msat,
1147- ) ;
1148- let recipient_fields = RecipientOnionFields :: spontaneous_empty ( ) ;
1149-
1150- match self . channel_manager . send_spontaneous_payment_with_retry (
1151- Some ( payment_preimage) ,
1152- recipient_fields,
1153- PaymentId ( payment_hash. 0 ) ,
1154- route_params,
1155- Retry :: Timeout ( LDK_PAYMENT_RETRY_TIMEOUT ) ,
1156- ) {
1157- Ok ( _payment_id) => {
1158- log_info ! ( self . logger, "Initiated sending {}msat to {}." , amount_msat, node_id) ;
1159-
1160- let payment = PaymentDetails {
1161- hash : payment_hash,
1162- preimage : Some ( payment_preimage) ,
1163- secret : None ,
1164- status : PaymentStatus :: Pending ,
1165- direction : PaymentDirection :: Outbound ,
1166- amount_msat : Some ( amount_msat) ,
1167- lsp_fee_limits : None ,
1168- } ;
1169- self . payment_store . insert ( payment) ?;
1170-
1171- Ok ( payment_hash)
1172- } ,
1173- Err ( e) => {
1174- log_error ! ( self . logger, "Failed to send payment: {:?}" , e) ;
1175-
1176- match e {
1177- channelmanager:: RetryableSendFailure :: DuplicatePayment => {
1178- Err ( Error :: DuplicatePayment )
1179- } ,
1180- _ => {
1181- let payment = PaymentDetails {
1182- hash : payment_hash,
1183- preimage : Some ( payment_preimage) ,
1184- secret : None ,
1185- status : PaymentStatus :: Failed ,
1186- direction : PaymentDirection :: Outbound ,
1187- amount_msat : Some ( amount_msat) ,
1188- lsp_fee_limits : None ,
1189- } ;
1190-
1191- self . payment_store . insert ( payment) ?;
1192- Err ( Error :: PaymentSendingFailed )
1193- } ,
1194- }
1195- } ,
1196- }
1197- }
1198-
1199- /// Sends payment probes over all paths of a route that would be used to pay the given
1200- /// amount to the given `node_id`.
1201- ///
1202- /// See [`Bolt11PaymentHandler::send_probes`] for more information.
1203- pub fn send_spontaneous_payment_probes (
1204- & self , amount_msat : u64 , node_id : PublicKey ,
1205- ) -> Result < ( ) , Error > {
1206- let rt_lock = self . runtime . read ( ) . unwrap ( ) ;
1207- if rt_lock. is_none ( ) {
1208- return Err ( Error :: NotRunning ) ;
1209- }
1210-
1211- let liquidity_limit_multiplier = Some ( self . config . probing_liquidity_limit_multiplier ) ;
1212- let cltv_expiry_delta = self . config . default_cltv_expiry_delta ;
1213-
1214- self . channel_manager
1215- . send_spontaneous_preflight_probes (
1216- node_id,
1217- amount_msat,
1218- cltv_expiry_delta,
1219- liquidity_limit_multiplier,
1220- )
1221- . map_err ( |e| {
1222- log_error ! ( self . logger, "Failed to send payment probes: {:?}" , e) ;
1223- Error :: ProbeSendingFailed
1224- } ) ?;
1225-
1226- Ok ( ( ) )
1227- }
1228-
12291130 /// Retrieve the details of a specific payment with the given hash.
12301131 ///
12311132 /// Returns `Some` if the payment was known and `None` otherwise.
0 commit comments