@@ -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:: Bolt11Payment ;
133+ use payment:: { Bolt11Payment , SpontaneousPayment } ;
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:: { BestBlock , 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
@@ -853,6 +848,32 @@ impl Node {
853848 ) )
854849 }
855850
851+ /// Returns a payment handler allowing to send spontaneous ("keysend") payments.
852+ #[ cfg( not( feature = "uniffi" ) ) ]
853+ pub fn spontaneous_payment ( & self ) -> SpontaneousPayment {
854+ SpontaneousPayment :: new (
855+ Arc :: clone ( & self . runtime ) ,
856+ Arc :: clone ( & self . channel_manager ) ,
857+ Arc :: clone ( & self . keys_manager ) ,
858+ Arc :: clone ( & self . payment_store ) ,
859+ Arc :: clone ( & self . config ) ,
860+ Arc :: clone ( & self . logger ) ,
861+ )
862+ }
863+
864+ /// Returns a payment handler allowing to send spontaneous ("keysend") payments.
865+ #[ cfg( feature = "uniffi" ) ]
866+ pub fn spontaneous_payment ( & self ) -> Arc < SpontaneousPayment > {
867+ Arc :: new ( SpontaneousPayment :: new (
868+ Arc :: clone ( & self . runtime ) ,
869+ Arc :: clone ( & self . channel_manager ) ,
870+ Arc :: clone ( & self . keys_manager ) ,
871+ Arc :: clone ( & self . payment_store ) ,
872+ Arc :: clone ( & self . config ) ,
873+ Arc :: clone ( & self . logger ) ,
874+ ) )
875+ }
876+
856877 /// Retrieve a new on-chain/funding address.
857878 pub fn new_onchain_address ( & self ) -> Result < Address , Error > {
858879 let funding_address = self . wallet . get_new_address ( ) ?;
@@ -1139,112 +1160,6 @@ impl Node {
11391160 }
11401161 }
11411162
1142- /// Send a spontaneous, aka. "keysend", payment
1143- pub fn send_spontaneous_payment (
1144- & self , amount_msat : u64 , node_id : PublicKey ,
1145- ) -> Result < PaymentHash , Error > {
1146- let rt_lock = self . runtime . read ( ) . unwrap ( ) ;
1147- if rt_lock. is_none ( ) {
1148- return Err ( Error :: NotRunning ) ;
1149- }
1150-
1151- let payment_preimage = PaymentPreimage ( self . keys_manager . get_secure_random_bytes ( ) ) ;
1152- let payment_hash = PaymentHash :: from ( payment_preimage) ;
1153-
1154- if let Some ( payment) = self . payment_store . get ( & payment_hash) {
1155- if payment. status == PaymentStatus :: Pending
1156- || payment. status == PaymentStatus :: Succeeded
1157- {
1158- log_error ! ( self . logger, "Payment error: must not send duplicate payments." ) ;
1159- return Err ( Error :: DuplicatePayment ) ;
1160- }
1161- }
1162-
1163- let route_params = RouteParameters :: from_payment_params_and_value (
1164- PaymentParameters :: from_node_id ( node_id, self . config . default_cltv_expiry_delta ) ,
1165- amount_msat,
1166- ) ;
1167- let recipient_fields = RecipientOnionFields :: spontaneous_empty ( ) ;
1168-
1169- match self . channel_manager . send_spontaneous_payment_with_retry (
1170- Some ( payment_preimage) ,
1171- recipient_fields,
1172- PaymentId ( payment_hash. 0 ) ,
1173- route_params,
1174- Retry :: Timeout ( LDK_PAYMENT_RETRY_TIMEOUT ) ,
1175- ) {
1176- Ok ( _payment_id) => {
1177- log_info ! ( self . logger, "Initiated sending {}msat to {}." , amount_msat, node_id) ;
1178-
1179- let payment = PaymentDetails {
1180- hash : payment_hash,
1181- preimage : Some ( payment_preimage) ,
1182- secret : None ,
1183- status : PaymentStatus :: Pending ,
1184- direction : PaymentDirection :: Outbound ,
1185- amount_msat : Some ( amount_msat) ,
1186- lsp_fee_limits : None ,
1187- } ;
1188- self . payment_store . insert ( payment) ?;
1189-
1190- Ok ( payment_hash)
1191- } ,
1192- Err ( e) => {
1193- log_error ! ( self . logger, "Failed to send payment: {:?}" , e) ;
1194-
1195- match e {
1196- channelmanager:: RetryableSendFailure :: DuplicatePayment => {
1197- Err ( Error :: DuplicatePayment )
1198- } ,
1199- _ => {
1200- let payment = PaymentDetails {
1201- hash : payment_hash,
1202- preimage : Some ( payment_preimage) ,
1203- secret : None ,
1204- status : PaymentStatus :: Failed ,
1205- direction : PaymentDirection :: Outbound ,
1206- amount_msat : Some ( amount_msat) ,
1207- lsp_fee_limits : None ,
1208- } ;
1209-
1210- self . payment_store . insert ( payment) ?;
1211- Err ( Error :: PaymentSendingFailed )
1212- } ,
1213- }
1214- } ,
1215- }
1216- }
1217-
1218- /// Sends payment probes over all paths of a route that would be used to pay the given
1219- /// amount to the given `node_id`.
1220- ///
1221- /// See [`Bolt11Payment::send_probes`] for more information.
1222- pub fn send_spontaneous_payment_probes (
1223- & self , amount_msat : u64 , node_id : PublicKey ,
1224- ) -> Result < ( ) , Error > {
1225- let rt_lock = self . runtime . read ( ) . unwrap ( ) ;
1226- if rt_lock. is_none ( ) {
1227- return Err ( Error :: NotRunning ) ;
1228- }
1229-
1230- let liquidity_limit_multiplier = Some ( self . config . probing_liquidity_limit_multiplier ) ;
1231- let cltv_expiry_delta = self . config . default_cltv_expiry_delta ;
1232-
1233- self . channel_manager
1234- . send_spontaneous_preflight_probes (
1235- node_id,
1236- amount_msat,
1237- cltv_expiry_delta,
1238- liquidity_limit_multiplier,
1239- )
1240- . map_err ( |e| {
1241- log_error ! ( self . logger, "Failed to send payment probes: {:?}" , e) ;
1242- Error :: ProbeSendingFailed
1243- } ) ?;
1244-
1245- Ok ( ( ) )
1246- }
1247-
12481163 /// Retrieve the details of a specific payment with the given hash.
12491164 ///
12501165 /// Returns `Some` if the payment was known and `None` otherwise.
0 commit comments