@@ -14,7 +14,7 @@ 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 } ;
@@ -698,7 +698,7 @@ impl OutboundPayments {
698
698
pub ( super ) fn send_payment_with_route < ES : Deref , NS : Deref , F > (
699
699
& self , route : & Route , payment_hash : PaymentHash , recipient_onion : RecipientOnionFields ,
700
700
payment_id : PaymentId , entropy_source : & ES , node_signer : & NS , best_block_height : u32 ,
701
- send_payment_along_path : F
701
+ pending_events : & Mutex < VecDeque < ( events :: Event , Option < EventCompletionAction > ) > > , send_payment_along_path : F
702
702
) -> Result < ( ) , PaymentSendFailure >
703
703
where
704
704
ES :: Target : EntropySource ,
@@ -707,7 +707,7 @@ impl OutboundPayments {
707
707
{
708
708
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) ?;
709
709
self . pay_route_internal ( route, payment_hash, recipient_onion, None , payment_id, None ,
710
- onion_session_privs, node_signer, best_block_height, & send_payment_along_path)
710
+ onion_session_privs, node_signer, best_block_height, pending_events , & send_payment_along_path)
711
711
. map_err ( |e| { self . remove_outbound_if_all_failed ( payment_id, & e) ; e } )
712
712
}
713
713
@@ -738,7 +738,8 @@ impl OutboundPayments {
738
738
pub ( super ) fn send_spontaneous_payment_with_route < ES : Deref , NS : Deref , F > (
739
739
& self , route : & Route , payment_preimage : Option < PaymentPreimage > ,
740
740
recipient_onion : RecipientOnionFields , payment_id : PaymentId , entropy_source : & ES ,
741
- node_signer : & NS , best_block_height : u32 , send_payment_along_path : F
741
+ node_signer : & NS , best_block_height : u32 , pending_events : & Mutex < VecDeque < ( events:: Event , Option < EventCompletionAction > ) > > ,
742
+ send_payment_along_path : F
742
743
) -> Result < PaymentHash , PaymentSendFailure >
743
744
where
744
745
ES :: Target : EntropySource ,
@@ -752,7 +753,7 @@ impl OutboundPayments {
752
753
payment_id, Some ( preimage) , & route, None , None , entropy_source, best_block_height) ?;
753
754
754
755
match self . pay_route_internal ( route, payment_hash, recipient_onion, Some ( preimage) ,
755
- payment_id, None , onion_session_privs, node_signer, best_block_height, & send_payment_along_path
756
+ payment_id, None , onion_session_privs, node_signer, best_block_height, pending_events , & send_payment_along_path
756
757
) {
757
758
Ok ( ( ) ) => Ok ( payment_hash) ,
758
759
Err ( e) => {
@@ -903,7 +904,7 @@ impl OutboundPayments {
903
904
}
904
905
905
906
let mut route = router. find_route_with_id (
906
- & node_signer. get_node_id ( Recipient :: Node ) . unwrap ( ) , & route_params,
907
+ & node_signer. get_node_id ( Recipient :: Node ) . unwrap ( ) , & route_params,
907
908
Some ( & first_hops. iter ( ) . collect :: < Vec < _ > > ( ) ) , inflight_htlcs ( ) ,
908
909
payment_hash, payment_id,
909
910
) . map_err ( |_| {
@@ -928,7 +929,7 @@ impl OutboundPayments {
928
929
} ) ?;
929
930
930
931
let res = self . pay_route_internal ( & route, payment_hash, recipient_onion, keysend_preimage, payment_id, None ,
931
- onion_session_privs, node_signer, best_block_height, & send_payment_along_path) ;
932
+ onion_session_privs, node_signer, best_block_height, pending_events , & send_payment_along_path) ;
932
933
log_info ! ( logger, "Sending payment with id {} and hash {} returned {:?}" ,
933
934
payment_id, payment_hash, res) ;
934
935
if let Err ( e) = res {
@@ -1087,7 +1088,7 @@ impl OutboundPayments {
1087
1088
} ;
1088
1089
let res = self . pay_route_internal ( & route, payment_hash, recipient_onion, keysend_preimage,
1089
1090
payment_id, Some ( total_msat) , onion_session_privs, node_signer, best_block_height,
1090
- & send_payment_along_path) ;
1091
+ pending_events , & send_payment_along_path) ;
1091
1092
log_info ! ( logger, "Result retrying payment id {}: {:?}" , & payment_id, res) ;
1092
1093
if let Err ( e) = res {
1093
1094
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) ;
@@ -1173,7 +1174,7 @@ impl OutboundPayments {
1173
1174
1174
1175
pub ( super ) fn send_probe < ES : Deref , NS : Deref , F > (
1175
1176
& self , path : Path , probing_cookie_secret : [ u8 ; 32 ] , entropy_source : & ES , node_signer : & NS ,
1176
- best_block_height : u32 , send_payment_along_path : F
1177
+ best_block_height : u32 , pending_events : & Mutex < VecDeque < ( events :: Event , Option < EventCompletionAction > ) > > , send_payment_along_path : F
1177
1178
) -> Result < ( PaymentHash , PaymentId ) , PaymentSendFailure >
1178
1179
where
1179
1180
ES :: Target : EntropySource ,
@@ -1197,7 +1198,7 @@ impl OutboundPayments {
1197
1198
entropy_source, best_block_height) ?;
1198
1199
1199
1200
match self . pay_route_internal ( & route, payment_hash, RecipientOnionFields :: spontaneous_empty ( ) ,
1200
- None , payment_id, None , onion_session_privs, node_signer, best_block_height, & send_payment_along_path
1201
+ None , payment_id, None , onion_session_privs, node_signer, best_block_height, pending_events , & send_payment_along_path
1201
1202
) {
1202
1203
Ok ( ( ) ) => Ok ( ( payment_hash, payment_id) ) ,
1203
1204
Err ( e) => {
@@ -1307,7 +1308,7 @@ impl OutboundPayments {
1307
1308
& self , route : & Route , payment_hash : PaymentHash , recipient_onion : RecipientOnionFields ,
1308
1309
keysend_preimage : Option < PaymentPreimage > , payment_id : PaymentId , recv_value_msat : Option < u64 > ,
1309
1310
onion_session_privs : Vec < [ u8 ; 32 ] > , node_signer : & NS , best_block_height : u32 ,
1310
- send_payment_along_path : & F
1311
+ pending_events : & Mutex < VecDeque < ( events :: Event , Option < EventCompletionAction > ) > > , send_payment_along_path : & F
1311
1312
) -> Result < ( ) , PaymentSendFailure >
1312
1313
where
1313
1314
NS :: Target : NodeSigner ,
@@ -1321,6 +1322,42 @@ impl OutboundPayments {
1321
1322
{
1322
1323
return Err ( PaymentSendFailure :: ParameterError ( APIError :: APIMisuseError { err : "Payment secret is required for multi-path payments" . to_owned ( ) } ) ) ;
1323
1324
}
1325
+
1326
+ if let Some ( path) = route. paths . get ( 0 ) {
1327
+ // handle self payment if path doesn't have a blinded tail.
1328
+ if path. blinded_tail . is_none ( ) {
1329
+ let last_hop = path. hops . last ( ) . unwrap ( ) ;
1330
+ if node_signer. get_node_id ( Recipient :: Node ) . unwrap ( ) == last_hop. pubkey {
1331
+ let payment_secret = match recipient_onion. payment_secret {
1332
+ Some ( secret) => secret,
1333
+ None => PaymentSecret ( [ 0 ; 32 ] )
1334
+ } ;
1335
+
1336
+ let payment_preimage = PaymentPreimage ( [ 0 ; 32 ] ) ;
1337
+ let payment_purpose = PaymentPurpose :: InvoicePayment {
1338
+ payment_preimage : Some ( payment_preimage) ,
1339
+ payment_secret,
1340
+ } ;
1341
+
1342
+ let mut pending_outbounds_lock = self . pending_outbound_payments . lock ( ) . unwrap ( ) ;
1343
+ let payment = pending_outbounds_lock. get_mut ( & payment_id) . unwrap ( ) ;
1344
+ payment. mark_fulfilled ( ) ;
1345
+
1346
+ let mut pending_events_lock = pending_events. lock ( ) . unwrap ( ) ;
1347
+ pending_events_lock. push_back ( ( Event :: PaymentSent { payment_id : Some ( payment_id) , payment_preimage,
1348
+ payment_hash, fee_paid_msat : None } , None ) ) ;
1349
+ let amt_to_receive = match & route. route_params {
1350
+ Some ( route_params) => route_params. final_value_msat ,
1351
+ None => if recv_value_msat. is_some ( ) { recv_value_msat. unwrap ( ) } else { 0 } ,
1352
+ } ;
1353
+ pending_events_lock. push_back ( ( Event :: PaymentClaimable { receiver_node_id : Some ( last_hop. pubkey ) , payment_hash,
1354
+ onion_fields : Some ( recipient_onion) , amount_msat : amt_to_receive, counterparty_skimmed_fee_msat : 0 ,
1355
+ purpose : payment_purpose, via_channel_id : None , via_user_channel_id : None , claim_deadline : None } , None ) ) ;
1356
+ return Ok ( ( ) ) ;
1357
+ }
1358
+ }
1359
+ }
1360
+
1324
1361
let mut total_value = 0 ;
1325
1362
let our_node_id = node_signer. get_node_id ( Recipient :: Node ) . unwrap ( ) ; // TODO no unwrap
1326
1363
let mut path_errs = Vec :: with_capacity ( route. paths . len ( ) ) ;
@@ -1430,15 +1467,15 @@ impl OutboundPayments {
1430
1467
& self , route : & Route , payment_hash : PaymentHash , recipient_onion : RecipientOnionFields ,
1431
1468
keysend_preimage : Option < PaymentPreimage > , payment_id : PaymentId , recv_value_msat : Option < u64 > ,
1432
1469
onion_session_privs : Vec < [ u8 ; 32 ] > , node_signer : & NS , best_block_height : u32 ,
1433
- send_payment_along_path : F
1470
+ pending_events : & Mutex < VecDeque < ( events :: Event , Option < EventCompletionAction > ) > > , send_payment_along_path : F
1434
1471
) -> Result < ( ) , PaymentSendFailure >
1435
1472
where
1436
1473
NS :: Target : NodeSigner ,
1437
1474
F : Fn ( SendAlongPathArgs ) -> Result < ( ) , APIError > ,
1438
1475
{
1439
1476
self . pay_route_internal ( route, payment_hash, recipient_onion, keysend_preimage, payment_id,
1440
1477
recv_value_msat, onion_session_privs, node_signer, best_block_height,
1441
- & send_payment_along_path)
1478
+ pending_events , & send_payment_along_path)
1442
1479
. map_err ( |e| { self . remove_outbound_if_all_failed ( payment_id, & e) ; e } )
1443
1480
}
1444
1481
0 commit comments