@@ -14,12 +14,12 @@ use bitcoin::hashes::sha256::Hash as Sha256;
14
14
use bitcoin:: secp256k1:: { self , Secp256k1 , SecretKey } ;
15
15
16
16
use crate :: sign:: { EntropySource , NodeSigner , Recipient } ;
17
- use crate :: events:: { self , PaymentFailureReason } ;
17
+ use crate :: events:: { self , PaymentFailureReason , Event , PaymentPurpose } ;
18
18
use crate :: ln:: { PaymentHash , PaymentPreimage , PaymentSecret } ;
19
19
use crate :: ln:: channelmanager:: { ChannelDetails , EventCompletionAction , HTLCSource , PaymentId } ;
20
20
use crate :: ln:: onion_utils:: { DecodedOnionFailure , HTLCFailReason } ;
21
21
use crate :: offers:: invoice:: Bolt12Invoice ;
22
- use crate :: routing:: router:: { InFlightHtlcs , Path , PaymentParameters , Route , RouteParameters , Router } ;
22
+ use crate :: routing:: router:: { InFlightHtlcs , Path , PaymentParameters , Route , RouteParameters , Router , Payee } ;
23
23
use crate :: util:: errors:: APIError ;
24
24
use crate :: util:: logger:: Logger ;
25
25
use crate :: util:: time:: Time ;
@@ -650,7 +650,7 @@ impl OutboundPayments {
650
650
pub ( super ) fn send_payment_with_route < ES : Deref , NS : Deref , F > (
651
651
& self , route : & Route , payment_hash : PaymentHash , recipient_onion : RecipientOnionFields ,
652
652
payment_id : PaymentId , entropy_source : & ES , node_signer : & NS , best_block_height : u32 ,
653
- send_payment_along_path : F
653
+ pending_events : & Mutex < VecDeque < ( events :: Event , Option < EventCompletionAction > ) > > , send_payment_along_path : F
654
654
) -> Result < ( ) , PaymentSendFailure >
655
655
where
656
656
ES :: Target : EntropySource ,
@@ -659,7 +659,7 @@ impl OutboundPayments {
659
659
{
660
660
let onion_session_privs = self . add_new_pending_payment ( payment_hash, recipient_onion. clone ( ) , payment_id, None , route, None , None , entropy_source, best_block_height) ?;
661
661
self . pay_route_internal ( route, payment_hash, recipient_onion, None , payment_id, None ,
662
- onion_session_privs, node_signer, best_block_height, & send_payment_along_path)
662
+ onion_session_privs, node_signer, best_block_height, pending_events , & send_payment_along_path)
663
663
. map_err ( |e| { self . remove_outbound_if_all_failed ( payment_id, & e) ; e } )
664
664
}
665
665
@@ -690,7 +690,8 @@ impl OutboundPayments {
690
690
pub ( super ) fn send_spontaneous_payment_with_route < ES : Deref , NS : Deref , F > (
691
691
& self , route : & Route , payment_preimage : Option < PaymentPreimage > ,
692
692
recipient_onion : RecipientOnionFields , payment_id : PaymentId , entropy_source : & ES ,
693
- node_signer : & NS , best_block_height : u32 , send_payment_along_path : F
693
+ node_signer : & NS , best_block_height : u32 , pending_events : & Mutex < VecDeque < ( events:: Event , Option < EventCompletionAction > ) > > ,
694
+ send_payment_along_path : F
694
695
) -> Result < PaymentHash , PaymentSendFailure >
695
696
where
696
697
ES :: Target : EntropySource ,
@@ -704,7 +705,7 @@ impl OutboundPayments {
704
705
payment_id, Some ( preimage) , & route, None , None , entropy_source, best_block_height) ?;
705
706
706
707
match self . pay_route_internal ( route, payment_hash, recipient_onion, Some ( preimage) ,
707
- payment_id, None , onion_session_privs, node_signer, best_block_height, & send_payment_along_path
708
+ payment_id, None , onion_session_privs, node_signer, best_block_height, pending_events , & send_payment_along_path
708
709
) {
709
710
Ok ( ( ) ) => Ok ( payment_hash) ,
710
711
Err ( e) => {
@@ -849,8 +850,9 @@ impl OutboundPayments {
849
850
}
850
851
}
851
852
853
+ let payer_pubkey = node_signer. get_node_id ( Recipient :: Node ) . unwrap ( ) ;
852
854
let route = router. find_route_with_id (
853
- & node_signer . get_node_id ( Recipient :: Node ) . unwrap ( ) , & route_params,
855
+ & payer_pubkey , & route_params,
854
856
Some ( & first_hops. iter ( ) . collect :: < Vec < _ > > ( ) ) , inflight_htlcs ( ) ,
855
857
payment_hash, payment_id,
856
858
) . map_err ( |_| {
@@ -869,7 +871,7 @@ impl OutboundPayments {
869
871
} ) ?;
870
872
871
873
let res = self . pay_route_internal ( & route, payment_hash, recipient_onion, keysend_preimage, payment_id, None ,
872
- onion_session_privs, node_signer, best_block_height, & send_payment_along_path) ;
874
+ onion_session_privs, node_signer, best_block_height, pending_events , & send_payment_along_path) ;
873
875
log_info ! ( logger, "Sending payment with id {} and hash {} returned {:?}" ,
874
876
payment_id, payment_hash, res) ;
875
877
if let Err ( e) = res {
@@ -1021,7 +1023,7 @@ impl OutboundPayments {
1021
1023
} ;
1022
1024
let res = self . pay_route_internal ( & route, payment_hash, recipient_onion, keysend_preimage,
1023
1025
payment_id, Some ( total_msat) , onion_session_privs, node_signer, best_block_height,
1024
- & send_payment_along_path) ;
1026
+ pending_events , & send_payment_along_path) ;
1025
1027
log_info ! ( logger, "Result retrying payment id {}: {:?}" , & payment_id, res) ;
1026
1028
if let Err ( e) = res {
1027
1029
self . handle_pay_route_err ( e, payment_id, payment_hash, route, route_params, router, first_hops, inflight_htlcs, entropy_source, node_signer, best_block_height, logger, pending_events, send_payment_along_path) ;
@@ -1107,7 +1109,7 @@ impl OutboundPayments {
1107
1109
1108
1110
pub ( super ) fn send_probe < ES : Deref , NS : Deref , F > (
1109
1111
& self , path : Path , probing_cookie_secret : [ u8 ; 32 ] , entropy_source : & ES , node_signer : & NS ,
1110
- best_block_height : u32 , send_payment_along_path : F
1112
+ best_block_height : u32 , pending_events : & Mutex < VecDeque < ( events :: Event , Option < EventCompletionAction > ) > > , send_payment_along_path : F
1111
1113
) -> Result < ( PaymentHash , PaymentId ) , PaymentSendFailure >
1112
1114
where
1113
1115
ES :: Target : EntropySource ,
@@ -1131,7 +1133,7 @@ impl OutboundPayments {
1131
1133
entropy_source, best_block_height) ?;
1132
1134
1133
1135
match self . pay_route_internal ( & route, payment_hash, RecipientOnionFields :: spontaneous_empty ( ) ,
1134
- None , payment_id, None , onion_session_privs, node_signer, best_block_height, & send_payment_along_path
1136
+ None , payment_id, None , onion_session_privs, node_signer, best_block_height, pending_events , & send_payment_along_path
1135
1137
) {
1136
1138
Ok ( ( ) ) => Ok ( ( payment_hash, payment_id) ) ,
1137
1139
Err ( e) => {
@@ -1238,7 +1240,7 @@ impl OutboundPayments {
1238
1240
& self , route : & Route , payment_hash : PaymentHash , recipient_onion : RecipientOnionFields ,
1239
1241
keysend_preimage : Option < PaymentPreimage > , payment_id : PaymentId , recv_value_msat : Option < u64 > ,
1240
1242
onion_session_privs : Vec < [ u8 ; 32 ] > , node_signer : & NS , best_block_height : u32 ,
1241
- send_payment_along_path : & F
1243
+ pending_events : & Mutex < VecDeque < ( events :: Event , Option < EventCompletionAction > ) > > , send_payment_along_path : & F
1242
1244
) -> Result < ( ) , PaymentSendFailure >
1243
1245
where
1244
1246
NS :: Target : NodeSigner ,
@@ -1252,6 +1254,42 @@ impl OutboundPayments {
1252
1254
{
1253
1255
return Err ( PaymentSendFailure :: ParameterError ( APIError :: APIMisuseError { err : "Payment secret is required for multi-path payments" . to_owned ( ) } ) ) ;
1254
1256
}
1257
+
1258
+ if let Some ( path) = route. paths . get ( 0 ) {
1259
+ // handle self payment if path doesn't have a blinded tail.
1260
+ if path. blinded_tail . is_none ( ) {
1261
+ let last_hop = path. hops . last ( ) . unwrap ( ) ;
1262
+ if node_signer. get_node_id ( Recipient :: Node ) . unwrap ( ) == last_hop. pubkey {
1263
+ let payment_secret = match recipient_onion. payment_secret {
1264
+ Some ( secret) => secret,
1265
+ None => PaymentSecret ( [ 0 ; 32 ] )
1266
+ } ;
1267
+
1268
+ let payment_preimage = PaymentPreimage ( [ 0 ; 32 ] ) ;
1269
+ let payment_purpose = PaymentPurpose :: InvoicePayment {
1270
+ payment_preimage : Some ( payment_preimage) ,
1271
+ payment_secret,
1272
+ } ;
1273
+
1274
+ let mut pending_outbounds_lock = self . pending_outbound_payments . lock ( ) . unwrap ( ) ;
1275
+ let payment = pending_outbounds_lock. get_mut ( & payment_id) . unwrap ( ) ;
1276
+ payment. mark_fulfilled ( ) ;
1277
+
1278
+ let mut pending_events_lock = pending_events. lock ( ) . unwrap ( ) ;
1279
+ pending_events_lock. push_back ( ( Event :: PaymentSent { payment_id : Some ( payment_id) , payment_preimage,
1280
+ payment_hash, fee_paid_msat : None } , None ) ) ;
1281
+ let amt_to_receive = match & route. route_params {
1282
+ Some ( route_params) => route_params. final_value_msat ,
1283
+ None => if recv_value_msat. is_some ( ) { recv_value_msat. unwrap ( ) } else { 0 } ,
1284
+ } ;
1285
+ pending_events_lock. push_back ( ( Event :: PaymentClaimable { receiver_node_id : Some ( last_hop. pubkey ) , payment_hash,
1286
+ onion_fields : Some ( recipient_onion) , amount_msat : amt_to_receive, counterparty_skimmed_fee_msat : 0 ,
1287
+ purpose : payment_purpose, via_channel_id : None , via_user_channel_id : None , claim_deadline : None } , None ) ) ;
1288
+ return Ok ( ( ) ) ;
1289
+ }
1290
+ }
1291
+ }
1292
+
1255
1293
let mut total_value = 0 ;
1256
1294
let our_node_id = node_signer. get_node_id ( Recipient :: Node ) . unwrap ( ) ; // TODO no unwrap
1257
1295
let mut path_errs = Vec :: with_capacity ( route. paths . len ( ) ) ;
@@ -1346,15 +1384,15 @@ impl OutboundPayments {
1346
1384
& self , route : & Route , payment_hash : PaymentHash , recipient_onion : RecipientOnionFields ,
1347
1385
keysend_preimage : Option < PaymentPreimage > , payment_id : PaymentId , recv_value_msat : Option < u64 > ,
1348
1386
onion_session_privs : Vec < [ u8 ; 32 ] > , node_signer : & NS , best_block_height : u32 ,
1349
- send_payment_along_path : F
1387
+ pending_events : & Mutex < VecDeque < ( events :: Event , Option < EventCompletionAction > ) > > , send_payment_along_path : F
1350
1388
) -> Result < ( ) , PaymentSendFailure >
1351
1389
where
1352
1390
NS :: Target : NodeSigner ,
1353
1391
F : Fn ( SendAlongPathArgs ) -> Result < ( ) , APIError > ,
1354
1392
{
1355
1393
self . pay_route_internal ( route, payment_hash, recipient_onion, keysend_preimage, payment_id,
1356
1394
recv_value_msat, onion_session_privs, node_signer, best_block_height,
1357
- & send_payment_along_path)
1395
+ pending_events , & send_payment_along_path)
1358
1396
. map_err ( |e| { self . remove_outbound_if_all_failed ( payment_id, & e) ; e } )
1359
1397
}
1360
1398
0 commit comments