@@ -288,11 +288,14 @@ pub(super) struct ChannelHolder<ChanSigner: ChannelKeys> {
288
288
/// guarantees are made about the existence of a channel with the short id here, nor the short
289
289
/// ids in the PendingHTLCInfo!
290
290
pub ( super ) forward_htlcs : HashMap < u64 , Vec < HTLCForwardInfo > > ,
291
- /// Tracks HTLCs that were to us and can be failed/claimed by the user
291
+ /// (payment_hash, payment_secret) -> Vec<HTLCs> for tracking HTLCs that
292
+ /// were to us and can be failed/claimed by the user
292
293
/// Note that while this is held in the same mutex as the channels themselves, no consistency
293
294
/// guarantees are made about the channels given here actually existing anymore by the time you
294
295
/// go to read them!
295
- claimable_htlcs : HashMap < PaymentHash , Vec < ClaimableHTLC > > ,
296
+ /// TODO: We need to time out HTLCs sitting here which are waiting on other AMP HTLCs to
297
+ /// arrive.
298
+ claimable_htlcs : HashMap < ( PaymentHash , Option < [ u8 ; 32 ] > ) , Vec < ClaimableHTLC > > ,
296
299
/// Messages to send to peers - pushed to in the same lock that they are generated in (except
297
300
/// for broadcast messages, where ordering isn't as strict).
298
301
pub ( super ) pending_msg_events : Vec < events:: MessageSendEvent > ,
@@ -1195,7 +1198,14 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1195
1198
/// In case of APIError::MonitorUpdateFailed, the commitment update has been irrevocably
1196
1199
/// committed on our end and we're just waiting for a monitor update to send it. Do NOT retry
1197
1200
/// the payment via a different route unless you intend to pay twice!
1198
- pub fn send_payment ( & self , route : Route , payment_hash : PaymentHash ) -> Result < ( ) , APIError > {
1201
+ ///
1202
+ /// payment_secret is unrelated to payment_hash (or PaymentPreimage) and exists to authenticate
1203
+ /// the sender to the recipient and prevent payment-probing (deanonymization) attacks. For
1204
+ /// newer nodes, it will be provided to you in the invoice. If you do not have one, the Route
1205
+ /// must not contain multiple paths as otherwise the multipath data cannot be sent.
1206
+ /// If a payment_secret *is* provided, we assume that the invoice had the basic_mpp feature bit
1207
+ /// set (either as required or as available).
1208
+ pub fn send_payment ( & self , route : Route , payment_hash : PaymentHash , payment_secret : Option < & [ u8 ; 32 ] > ) -> Result < ( ) , APIError > {
1199
1209
if route. hops . len ( ) < 1 || route. hops . len ( ) > 20 {
1200
1210
return Err ( APIError :: RouteError { err : "Route didn't go anywhere/had bogus size" } ) ;
1201
1211
}
@@ -1212,7 +1222,7 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1212
1222
1213
1223
let onion_keys = secp_call ! ( onion_utils:: construct_onion_keys( & self . secp_ctx, & route, & session_priv) ,
1214
1224
APIError :: RouteError { err: "Pubkey along hop was maliciously selected" } ) ;
1215
- let ( onion_payloads, htlc_msat, htlc_cltv) = onion_utils:: build_onion_payloads ( & route, cur_height) ?;
1225
+ let ( onion_payloads, htlc_msat, htlc_cltv) = onion_utils:: build_onion_payloads ( & route, payment_secret , cur_height) ?;
1216
1226
if onion_utils:: route_size_insane ( & onion_payloads) {
1217
1227
return Err ( APIError :: RouteError { err : "Route size too large considering onion data" } ) ;
1218
1228
}
@@ -1446,7 +1456,9 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1446
1456
htlc_id : prev_htlc_id,
1447
1457
incoming_packet_shared_secret : forward_info. incoming_shared_secret ,
1448
1458
} ) ;
1449
- failed_forwards. push ( ( htlc_source, forward_info. payment_hash , 0x4000 | 10 , None ) ) ;
1459
+ failed_forwards. push ( ( htlc_source, forward_info. payment_hash ,
1460
+ HTLCFailReason :: Reason { failure_code : 0x1000 | 7 , data : Vec :: new ( ) }
1461
+ ) ) ;
1450
1462
} ,
1451
1463
HTLCForwardInfo :: FailHTLC { .. } => {
1452
1464
// Channel went away before we could fail it. This implies
@@ -1482,7 +1494,9 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1482
1494
panic ! ( "Stated return value requirements in send_htlc() were not met" ) ;
1483
1495
}
1484
1496
let chan_update = self . get_channel_update ( chan. get ( ) ) . unwrap ( ) ;
1485
- failed_forwards. push ( ( htlc_source, payment_hash, 0x1000 | 7 , Some ( chan_update) ) ) ;
1497
+ failed_forwards. push ( ( htlc_source, payment_hash,
1498
+ HTLCFailReason :: Reason { failure_code : 0x1000 | 7 , data : chan_update. encode_with_len ( ) }
1499
+ ) ) ;
1486
1500
continue ;
1487
1501
} ,
1488
1502
Ok ( update_add) => {
@@ -1591,15 +1605,48 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1591
1605
htlc_id : prev_htlc_id,
1592
1606
incoming_packet_shared_secret : incoming_shared_secret,
1593
1607
} ;
1594
- channel_state. claimable_htlcs . entry ( payment_hash) . or_insert ( Vec :: new ( ) ) . push ( ClaimableHTLC {
1608
+
1609
+ let mut total_value = 0 ;
1610
+ let htlcs = channel_state. claimable_htlcs . entry ( ( payment_hash, if let & Some ( ref data) = & payment_data {
1611
+ Some ( data. payment_secret . clone ( ) ) } else { None } ) )
1612
+ . or_insert ( Vec :: new ( ) ) ;
1613
+ htlcs. push ( ClaimableHTLC {
1595
1614
prev_hop,
1596
1615
value : amt_to_forward,
1597
- payment_data,
1598
- } ) ;
1599
- new_events. push ( events:: Event :: PaymentReceived {
1600
- payment_hash : payment_hash,
1601
- amt : amt_to_forward,
1616
+ payment_data : payment_data. clone ( ) ,
1602
1617
} ) ;
1618
+ if let & Some ( ref data) = & payment_data {
1619
+ for htlc in htlcs. iter ( ) {
1620
+ total_value += htlc. value ;
1621
+ if htlc. payment_data . as_ref ( ) . unwrap ( ) . total_msat != data. total_msat {
1622
+ total_value = msgs:: MAX_VALUE_MSAT ;
1623
+ }
1624
+ if total_value >= msgs:: MAX_VALUE_MSAT { break ; }
1625
+ }
1626
+ if total_value >= msgs:: MAX_VALUE_MSAT {
1627
+ for htlc in htlcs. iter ( ) {
1628
+ failed_forwards. push ( ( HTLCSource :: PreviousHopData ( HTLCPreviousHopData {
1629
+ short_channel_id : htlc. prev_hop . short_channel_id ,
1630
+ htlc_id : htlc. prev_hop . htlc_id ,
1631
+ incoming_packet_shared_secret : htlc. prev_hop . incoming_packet_shared_secret ,
1632
+ } ) , payment_hash,
1633
+ HTLCFailReason :: Reason { failure_code : 0x4000 | 15 , data : byte_utils:: be64_to_array ( htlc. value ) . to_vec ( ) }
1634
+ ) ) ;
1635
+ }
1636
+ } else if total_value >= data. total_msat {
1637
+ new_events. push ( events:: Event :: PaymentReceived {
1638
+ payment_hash : payment_hash,
1639
+ payment_secret : Some ( data. payment_secret ) ,
1640
+ amt : total_value,
1641
+ } ) ;
1642
+ }
1643
+ } else {
1644
+ new_events. push ( events:: Event :: PaymentReceived {
1645
+ payment_hash : payment_hash,
1646
+ payment_secret : None ,
1647
+ amt : amt_to_forward,
1648
+ } ) ;
1649
+ }
1603
1650
} ,
1604
1651
HTLCForwardInfo :: AddHTLC { .. } => {
1605
1652
panic ! ( "short_channel_id == 0 should imply any pending_forward entries are of type Receive" ) ;
@@ -1613,11 +1660,8 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1613
1660
}
1614
1661
}
1615
1662
1616
- for ( htlc_source, payment_hash, failure_code, update) in failed_forwards. drain ( ..) {
1617
- match update {
1618
- None => self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , htlc_source, & payment_hash, HTLCFailReason :: Reason { failure_code, data : Vec :: new ( ) } ) ,
1619
- Some ( chan_update) => self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , htlc_source, & payment_hash, HTLCFailReason :: Reason { failure_code, data : chan_update. encode_with_len ( ) } ) ,
1620
- } ;
1663
+ for ( htlc_source, payment_hash, failure_reason) in failed_forwards. drain ( ..) {
1664
+ self . fail_htlc_backwards_internal ( self . channel_state . lock ( ) . unwrap ( ) , htlc_source, & payment_hash, failure_reason) ;
1621
1665
}
1622
1666
1623
1667
if handle_errors. len ( ) > 0 {
@@ -1662,11 +1706,11 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1662
1706
/// along the path (including in our own channel on which we received it).
1663
1707
/// Returns false if no payment was found to fail backwards, true if the process of failing the
1664
1708
/// HTLC backwards has been started.
1665
- pub fn fail_htlc_backwards ( & self , payment_hash : & PaymentHash ) -> bool {
1709
+ pub fn fail_htlc_backwards ( & self , payment_hash : & PaymentHash , payment_secret : & Option < [ u8 ; 32 ] > ) -> bool {
1666
1710
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
1667
1711
1668
1712
let mut channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
1669
- let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( payment_hash) ;
1713
+ let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( & ( * payment_hash, * payment_secret ) ) ;
1670
1714
if let Some ( mut sources) = removed_source {
1671
1715
for htlc in sources. drain ( ..) {
1672
1716
if channel_state. is_none ( ) { channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ; }
@@ -1788,13 +1832,13 @@ impl<ChanSigner: ChannelKeys, M: Deref, T: Deref, K: Deref, F: Deref> ChannelMan
1788
1832
/// motivated attackers.
1789
1833
///
1790
1834
/// May panic if called except in response to a PaymentReceived event.
1791
- pub fn claim_funds ( & self , payment_preimage : PaymentPreimage , expected_amount : u64 ) -> bool {
1835
+ pub fn claim_funds ( & self , payment_preimage : PaymentPreimage , payment_secret : & Option < [ u8 ; 32 ] > , expected_amount : u64 ) -> bool {
1792
1836
let payment_hash = PaymentHash ( Sha256 :: hash ( & payment_preimage. 0 ) . into_inner ( ) ) ;
1793
1837
1794
1838
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
1795
1839
1796
1840
let mut channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ;
1797
- let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( & payment_hash) ;
1841
+ let removed_source = channel_state. as_mut ( ) . unwrap ( ) . claimable_htlcs . remove ( & ( payment_hash, * payment_secret ) ) ;
1798
1842
if let Some ( mut sources) = removed_source {
1799
1843
for htlc in sources. drain ( ..) {
1800
1844
if channel_state. is_none ( ) { channel_state = Some ( self . channel_state . lock ( ) . unwrap ( ) ) ; }
0 commit comments