@@ -212,6 +212,7 @@ pub(crate) enum HTLCSource {
212
212
first_hop_htlc_msat : u64 ,
213
213
payment_id : PaymentId ,
214
214
payment_secret : Option < PaymentSecret > ,
215
+ payment_metadata : Option < Vec < u8 > > ,
215
216
payment_params : Option < PaymentParameters > ,
216
217
} ,
217
218
}
@@ -223,12 +224,13 @@ impl core::hash::Hash for HTLCSource {
223
224
0u8 . hash ( hasher) ;
224
225
prev_hop_data. hash ( hasher) ;
225
226
} ,
226
- HTLCSource :: OutboundRoute { path, session_priv, payment_id, payment_secret, first_hop_htlc_msat, payment_params } => {
227
+ HTLCSource :: OutboundRoute { path, session_priv, payment_id, payment_secret, payment_metadata , first_hop_htlc_msat, payment_params } => {
227
228
1u8 . hash ( hasher) ;
228
229
path. hash ( hasher) ;
229
230
session_priv[ ..] . hash ( hasher) ;
230
231
payment_id. hash ( hasher) ;
231
232
payment_secret. hash ( hasher) ;
233
+ payment_metadata. hash ( hasher) ;
232
234
first_hop_htlc_msat. hash ( hasher) ;
233
235
payment_params. hash ( hasher) ;
234
236
} ,
@@ -245,6 +247,7 @@ impl HTLCSource {
245
247
first_hop_htlc_msat : 0 ,
246
248
payment_id : PaymentId ( [ 2 ; 32 ] ) ,
247
249
payment_secret : None ,
250
+ payment_metadata : None ,
248
251
payment_params : None ,
249
252
}
250
253
}
@@ -470,6 +473,7 @@ pub(crate) enum PendingOutboundPayment {
470
473
session_privs : HashSet < [ u8 ; 32 ] > ,
471
474
payment_hash : PaymentHash ,
472
475
payment_secret : Option < PaymentSecret > ,
476
+ payment_metadata : Option < Vec < u8 > > ,
473
477
pending_amt_msat : u64 ,
474
478
/// Used to track the fee paid. Only present if the payment was serialized on 0.0.103+.
475
479
pending_fee_msat : Option < u64 > ,
@@ -2331,15 +2335,15 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2331
2335
}
2332
2336
2333
2337
// Only public for testing, this should otherwise never be called direcly
2334
- pub ( crate ) fn send_payment_along_path ( & self , path : & Vec < RouteHop > , payment_params : & Option < PaymentParameters > , payment_hash : & PaymentHash , payment_secret : & Option < PaymentSecret > , total_value : u64 , cur_height : u32 , payment_id : PaymentId , keysend_preimage : & Option < PaymentPreimage > ) -> Result < ( ) , APIError > {
2338
+ pub ( crate ) fn send_payment_along_path ( & self , path : & Vec < RouteHop > , payment_params : & Option < PaymentParameters > , payment_hash : & PaymentHash , payment_secret : & Option < PaymentSecret > , payment_metadata : & Option < Vec < u8 > > , total_value : u64 , cur_height : u32 , payment_id : PaymentId , keysend_preimage : & Option < PaymentPreimage > ) -> Result < ( ) , APIError > {
2335
2339
log_trace ! ( self . logger, "Attempting to send payment for path with next hop {}" , path. first( ) . unwrap( ) . short_channel_id) ;
2336
2340
let prng_seed = self . keys_manager . get_secure_random_bytes ( ) ;
2337
2341
let session_priv_bytes = self . keys_manager . get_secure_random_bytes ( ) ;
2338
2342
let session_priv = SecretKey :: from_slice ( & session_priv_bytes[ ..] ) . expect ( "RNG is busted" ) ;
2339
2343
2340
2344
let onion_keys = onion_utils:: construct_onion_keys ( & self . secp_ctx , & path, & session_priv)
2341
2345
. map_err ( |_| APIError :: RouteError { err : "Pubkey along hop was maliciously selected" } ) ?;
2342
- let ( onion_payloads, htlc_msat, htlc_cltv) = onion_utils:: build_onion_payloads ( path, total_value, payment_secret, cur_height, keysend_preimage) ?;
2346
+ let ( onion_payloads, htlc_msat, htlc_cltv) = onion_utils:: build_onion_payloads ( path, total_value, payment_secret, payment_metadata . clone ( ) , cur_height, keysend_preimage) ?;
2343
2347
if onion_utils:: route_size_insane ( & onion_payloads) {
2344
2348
return Err ( APIError :: RouteError { err : "Route size too large considering onion data" } ) ;
2345
2349
}
@@ -2373,6 +2377,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2373
2377
pending_fee_msat: Some ( 0 ) ,
2374
2378
payment_hash: * payment_hash,
2375
2379
payment_secret: * payment_secret,
2380
+ payment_metadata: payment_metadata. clone( ) ,
2376
2381
starting_block_height: self . best_block. read( ) . unwrap( ) . height( ) ,
2377
2382
total_msat: total_value,
2378
2383
} ) ;
@@ -2396,6 +2401,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2396
2401
first_hop_htlc_msat: htlc_msat,
2397
2402
payment_id,
2398
2403
payment_secret: payment_secret. clone( ) ,
2404
+ payment_metadata: payment_metadata. clone( ) ,
2399
2405
payment_params: payment_params. clone( ) ,
2400
2406
} , onion_packet, & self . logger) ,
2401
2407
channel_state, chan)
@@ -2480,10 +2486,10 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2480
2486
/// bit set (either as required or as available). If multiple paths are present in the Route,
2481
2487
/// we assume the invoice had the basic_mpp feature set.
2482
2488
pub fn send_payment ( & self , route : & Route , payment_hash : PaymentHash , payment_secret : & Option < PaymentSecret > ) -> Result < PaymentId , PaymentSendFailure > {
2483
- self . send_payment_internal ( route, payment_hash, payment_secret, None , None , None )
2489
+ self . send_payment_internal ( route, payment_hash, payment_secret, None , None , None , None )
2484
2490
}
2485
2491
2486
- fn send_payment_internal ( & self , route : & Route , payment_hash : PaymentHash , payment_secret : & Option < PaymentSecret > , keysend_preimage : Option < PaymentPreimage > , payment_id : Option < PaymentId > , recv_value_msat : Option < u64 > ) -> Result < PaymentId , PaymentSendFailure > {
2492
+ fn send_payment_internal ( & self , route : & Route , payment_hash : PaymentHash , payment_secret : & Option < PaymentSecret > , payment_metadata : Option < Vec < u8 > > , keysend_preimage : Option < PaymentPreimage > , payment_id : Option < PaymentId > , recv_value_msat : Option < u64 > ) -> Result < PaymentId , PaymentSendFailure > {
2487
2493
if route. paths . len ( ) < 1 {
2488
2494
return Err ( PaymentSendFailure :: ParameterError ( APIError :: RouteError { err : "There must be at least one path to send over" } ) ) ;
2489
2495
}
@@ -2525,7 +2531,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2525
2531
let cur_height = self . best_block . read ( ) . unwrap ( ) . height ( ) + 1 ;
2526
2532
let mut results = Vec :: new ( ) ;
2527
2533
for path in route. paths . iter ( ) {
2528
- results. push ( self . send_payment_along_path ( & path, & route. payment_params , & payment_hash, payment_secret, total_value, cur_height, payment_id, & keysend_preimage) ) ;
2534
+ results. push ( self . send_payment_along_path ( & path, & route. payment_params , & payment_hash, payment_secret, & payment_metadata , total_value, cur_height, payment_id, & keysend_preimage) ) ;
2529
2535
}
2530
2536
let mut has_ok = false ;
2531
2537
let mut has_err = false ;
@@ -2588,20 +2594,20 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2588
2594
}
2589
2595
}
2590
2596
2591
- let ( total_msat, payment_hash, payment_secret) = {
2597
+ let ( total_msat, payment_hash, payment_secret, payment_metadata ) = {
2592
2598
let outbounds = self . pending_outbound_payments . lock ( ) . unwrap ( ) ;
2593
2599
if let Some ( payment) = outbounds. get ( & payment_id) {
2594
2600
match payment {
2595
2601
PendingOutboundPayment :: Retryable {
2596
- total_msat, payment_hash, payment_secret, pending_amt_msat, ..
2602
+ total_msat, payment_hash, payment_secret, pending_amt_msat, payment_metadata , ..
2597
2603
} => {
2598
2604
let retry_amt_msat: u64 = route. paths . iter ( ) . map ( |path| path. last ( ) . unwrap ( ) . fee_msat ) . sum ( ) ;
2599
2605
if retry_amt_msat + * pending_amt_msat > * total_msat * ( 100 + RETRY_OVERFLOW_PERCENTAGE ) / 100 {
2600
2606
return Err ( PaymentSendFailure :: ParameterError ( APIError :: APIMisuseError {
2601
2607
err : format ! ( "retry_amt_msat of {} will put pending_amt_msat (currently: {}) more than 10% over total_payment_amt_msat of {}" , retry_amt_msat, pending_amt_msat, total_msat) . to_string ( )
2602
2608
} ) )
2603
2609
}
2604
- ( * total_msat, * payment_hash, * payment_secret)
2610
+ ( * total_msat, * payment_hash, * payment_secret, payment_metadata . clone ( ) )
2605
2611
} ,
2606
2612
PendingOutboundPayment :: Legacy { .. } => {
2607
2613
return Err ( PaymentSendFailure :: ParameterError ( APIError :: APIMisuseError {
@@ -2625,7 +2631,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2625
2631
} ) )
2626
2632
}
2627
2633
} ;
2628
- return self . send_payment_internal ( route, payment_hash, & payment_secret, None , Some ( payment_id) , Some ( total_msat) ) . map ( |_| ( ) )
2634
+ return self . send_payment_internal ( route, payment_hash, & payment_secret, payment_metadata , None , Some ( payment_id) , Some ( total_msat) ) . map ( |_| ( ) )
2629
2635
}
2630
2636
2631
2637
/// Signals that no further retries for the given payment will occur.
@@ -2679,7 +2685,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2679
2685
None => PaymentPreimage ( self . keys_manager . get_secure_random_bytes ( ) ) ,
2680
2686
} ;
2681
2687
let payment_hash = PaymentHash ( Sha256 :: hash ( & preimage. 0 ) . into_inner ( ) ) ;
2682
- match self . send_payment_internal ( route, payment_hash, & None , Some ( preimage) , None , None ) {
2688
+ match self . send_payment_internal ( route, payment_hash, & None , None , Some ( preimage) , None , None ) {
2683
2689
Ok ( payment_id) => Ok ( ( payment_hash, payment_id) ) ,
2684
2690
Err ( e) => Err ( e)
2685
2691
}
@@ -6152,13 +6158,15 @@ impl Readable for HTLCSource {
6152
6158
let mut payment_id = None ;
6153
6159
let mut payment_secret = None ;
6154
6160
let mut payment_params = None ;
6161
+ let mut payment_metadata = None ;
6155
6162
read_tlv_fields ! ( reader, {
6156
6163
( 0 , session_priv, required) ,
6157
6164
( 1 , payment_id, option) ,
6158
6165
( 2 , first_hop_htlc_msat, required) ,
6159
6166
( 3 , payment_secret, option) ,
6160
6167
( 4 , path, vec_type) ,
6161
6168
( 5 , payment_params, option) ,
6169
+ ( 7 , payment_metadata, option) ,
6162
6170
} ) ;
6163
6171
if payment_id. is_none ( ) {
6164
6172
// For backwards compat, if there was no payment_id written, use the session_priv bytes
@@ -6172,6 +6180,7 @@ impl Readable for HTLCSource {
6172
6180
payment_id : payment_id. unwrap ( ) ,
6173
6181
payment_secret,
6174
6182
payment_params,
6183
+ payment_metadata,
6175
6184
} )
6176
6185
}
6177
6186
1 => Ok ( HTLCSource :: PreviousHopData ( Readable :: read ( reader) ?) ) ,
@@ -6183,7 +6192,7 @@ impl Readable for HTLCSource {
6183
6192
impl Writeable for HTLCSource {
6184
6193
fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , :: io:: Error > {
6185
6194
match self {
6186
- HTLCSource :: OutboundRoute { ref session_priv, ref first_hop_htlc_msat, ref path, payment_id, payment_secret, payment_params } => {
6195
+ HTLCSource :: OutboundRoute { ref session_priv, ref first_hop_htlc_msat, ref path, payment_id, payment_secret, ref payment_metadata , payment_params } => {
6187
6196
0u8 . write ( writer) ?;
6188
6197
let payment_id_opt = Some ( payment_id) ;
6189
6198
write_tlv_fields ! ( writer, {
@@ -6193,6 +6202,7 @@ impl Writeable for HTLCSource {
6193
6202
( 3 , payment_secret, option) ,
6194
6203
( 4 , path, vec_type) ,
6195
6204
( 5 , payment_params, option) ,
6205
+ ( 7 , payment_metadata, option) ,
6196
6206
} ) ;
6197
6207
}
6198
6208
HTLCSource :: PreviousHopData ( ref field) => {
@@ -6247,6 +6257,7 @@ impl_writeable_tlv_based_enum_upgradable!(PendingOutboundPayment,
6247
6257
( 0 , session_privs, required) ,
6248
6258
( 1 , pending_fee_msat, option) ,
6249
6259
( 2 , payment_hash, required) ,
6260
+ ( 3 , payment_metadata, option) ,
6250
6261
( 4 , payment_secret, option) ,
6251
6262
( 6 , total_msat, required) ,
6252
6263
( 8 , pending_amt_msat, required) ,
@@ -6709,7 +6720,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
6709
6720
for ( _, monitor) in args. channel_monitors {
6710
6721
if by_id. get ( & monitor. get_funding_txo ( ) . 0 . to_channel_id ( ) ) . is_none ( ) {
6711
6722
for ( htlc_source, htlc) in monitor. get_pending_outbound_htlcs ( ) {
6712
- if let HTLCSource :: OutboundRoute { payment_id, session_priv, path, payment_secret, .. } = htlc_source {
6723
+ if let HTLCSource :: OutboundRoute { payment_id, session_priv, path, payment_secret, payment_metadata , .. } = htlc_source {
6713
6724
if path. is_empty ( ) {
6714
6725
log_error ! ( args. logger, "Got an empty path for a pending payment" ) ;
6715
6726
return Err ( DecodeError :: InvalidValue ) ;
@@ -6729,6 +6740,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
6729
6740
session_privs : [ session_priv_bytes] . iter ( ) . map ( |a| * a) . collect ( ) ,
6730
6741
payment_hash : htlc. payment_hash ,
6731
6742
payment_secret,
6743
+ payment_metadata,
6732
6744
pending_amt_msat : path_amt,
6733
6745
pending_fee_msat : Some ( path_fee) ,
6734
6746
total_msat : path_amt,
@@ -7004,7 +7016,7 @@ mod tests {
7004
7016
// Use the utility function send_payment_along_path to send the payment with MPP data which
7005
7017
// indicates there are more HTLCs coming.
7006
7018
let cur_height = CHAN_CONFIRM_DEPTH + 1 ; // route_payment calls send_payment, which adds 1 to the current height. So we do the same here to match.
7007
- nodes[ 0 ] . node . send_payment_along_path ( & route. paths [ 0 ] , & route. payment_params , & our_payment_hash, & Some ( payment_secret) , 200_000 , cur_height, payment_id, & None ) . unwrap ( ) ;
7019
+ nodes[ 0 ] . node . send_payment_along_path ( & route. paths [ 0 ] , & route. payment_params , & our_payment_hash, & Some ( payment_secret) , & None , 200_000 , cur_height, payment_id, & None ) . unwrap ( ) ;
7008
7020
check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
7009
7021
let mut events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
7010
7022
assert_eq ! ( events. len( ) , 1 ) ;
@@ -7034,7 +7046,7 @@ mod tests {
7034
7046
expect_payment_failed ! ( nodes[ 0 ] , our_payment_hash, true ) ;
7035
7047
7036
7048
// Send the second half of the original MPP payment.
7037
- nodes[ 0 ] . node . send_payment_along_path ( & route. paths [ 0 ] , & route. payment_params , & our_payment_hash, & Some ( payment_secret) , 200_000 , cur_height, payment_id, & None ) . unwrap ( ) ;
7049
+ nodes[ 0 ] . node . send_payment_along_path ( & route. paths [ 0 ] , & route. payment_params , & our_payment_hash, & Some ( payment_secret) , & None , 200_000 , cur_height, payment_id, & None ) . unwrap ( ) ;
7038
7050
check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
7039
7051
let mut events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
7040
7052
assert_eq ! ( events. len( ) , 1 ) ;
@@ -7228,7 +7240,7 @@ mod tests {
7228
7240
7229
7241
let test_preimage = PaymentPreimage ( [ 42 ; 32 ] ) ;
7230
7242
let mismatch_payment_hash = PaymentHash ( [ 43 ; 32 ] ) ;
7231
- let _ = nodes[ 0 ] . node . send_payment_internal ( & route, mismatch_payment_hash, & None , Some ( test_preimage) , None , None ) . unwrap ( ) ;
7243
+ let _ = nodes[ 0 ] . node . send_payment_internal ( & route, mismatch_payment_hash, & None , None , Some ( test_preimage) , None , None ) . unwrap ( ) ;
7232
7244
check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
7233
7245
7234
7246
let updates = get_htlc_update_msgs ! ( nodes[ 0 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
@@ -7273,7 +7285,7 @@ mod tests {
7273
7285
let test_preimage = PaymentPreimage ( [ 42 ; 32 ] ) ;
7274
7286
let test_secret = PaymentSecret ( [ 43 ; 32 ] ) ;
7275
7287
let payment_hash = PaymentHash ( Sha256 :: hash ( & test_preimage. 0 ) . into_inner ( ) ) ;
7276
- let _ = nodes[ 0 ] . node . send_payment_internal ( & route, payment_hash, & Some ( test_secret) , Some ( test_preimage) , None , None ) . unwrap ( ) ;
7288
+ let _ = nodes[ 0 ] . node . send_payment_internal ( & route, payment_hash, & Some ( test_secret) , None , Some ( test_preimage) , None , None ) . unwrap ( ) ;
7277
7289
check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
7278
7290
7279
7291
let updates = get_htlc_update_msgs ! ( nodes[ 0 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
0 commit comments