@@ -23,7 +23,7 @@ use bitcoin::constants::ChainHash;
23
23
use bitcoin::key::constants::SECRET_KEY_SIZE;
24
24
use bitcoin::network::Network;
25
25
26
- use bitcoin::hashes::Hash;
26
+ use bitcoin::hashes::{ Hash, HashEngine, HmacEngine} ;
27
27
use bitcoin::hashes::hmac::Hmac;
28
28
use bitcoin::hashes::sha256::Hash as Sha256;
29
29
use bitcoin::hash_types::{BlockHash, Txid};
@@ -366,6 +366,7 @@ pub(crate) struct HTLCPreviousHopData {
366
366
counterparty_node_id: Option<PublicKey>,
367
367
}
368
368
369
+ #[derive(PartialEq, Eq)]
369
370
enum OnionPayload {
370
371
/// Indicates this incoming onion payload is for the purpose of paying an invoice.
371
372
Invoice {
@@ -378,6 +379,7 @@ enum OnionPayload {
378
379
}
379
380
380
381
/// HTLCs that are to us and can be failed/claimed by the user
382
+ #[derive(PartialEq, Eq)]
381
383
struct ClaimableHTLC {
382
384
prev_hop: HTLCPreviousHopData,
383
385
cltv_expiry: u32,
@@ -409,6 +411,23 @@ impl From<&ClaimableHTLC> for events::ClaimedHTLC {
409
411
}
410
412
}
411
413
414
+ impl PartialOrd for ClaimableHTLC {
415
+ fn partial_cmp(&self, other: &ClaimableHTLC) -> Option<cmp::Ordering> {
416
+ Some(self.cmp(other))
417
+ }
418
+ }
419
+ impl Ord for ClaimableHTLC {
420
+ fn cmp(&self, other: &ClaimableHTLC) -> cmp::Ordering {
421
+ let res = (self.prev_hop.channel_id, self.prev_hop.htlc_id).cmp(
422
+ &(other.prev_hop.channel_id, other.prev_hop.htlc_id)
423
+ );
424
+ if res.is_eq() {
425
+ debug_assert!(self == other, "ClaimableHTLCs from the same source should be identical");
426
+ }
427
+ res
428
+ }
429
+ }
430
+
412
431
/// A trait defining behavior for creating and verifing the HMAC for authenticating a given data.
413
432
pub trait Verification {
414
433
/// Constructs an HMAC to include in [`OffersContext`] for the data along with the given
@@ -469,6 +488,20 @@ impl Verification for PaymentId {
469
488
) -> Result<(), ()> {
470
489
signer::verify_payment_id(*self, hmac, nonce, expanded_key)
471
490
}
491
+
492
+ fn for_inbound_from_htlcs<I: Iterator<Item=(ChannelId, u64)>>(key: &[u8; 32], htlcs: I) -> PaymentId {
493
+ let mut prev_pair = None;
494
+ let mut hasher = HmacEngine::new(key);
495
+ for (channel_id, htlc_id) in htlcs {
496
+ hasher.input(&channel_id.0);
497
+ hasher.input(&htlc_id.to_le_bytes());
498
+ if let Some(prev) = prev_pair {
499
+ debug_assert!(prev < (channel_id, htlc_id), "HTLCs should be sorted");
500
+ }
501
+ prev_pair = Some((channel_id, htlc_id));
502
+ }
503
+ PaymentId(Hmac::<Sha256>::from_engine(hasher).to_byte_array())
504
+ }
472
505
}
473
506
474
507
impl Writeable for PaymentId {
@@ -744,6 +777,7 @@ struct ClaimingPayment {
744
777
htlcs: Vec<events::ClaimedHTLC>,
745
778
sender_intended_value: Option<u64>,
746
779
onion_fields: Option<RecipientOnionFields>,
780
+ payment_id: Option<PaymentId>,
747
781
}
748
782
impl_writeable_tlv_based!(ClaimingPayment, {
749
783
(0, amount_msat, required),
@@ -752,6 +786,7 @@ impl_writeable_tlv_based!(ClaimingPayment, {
752
786
(5, htlcs, optional_vec),
753
787
(7, sender_intended_value, option),
754
788
(9, onion_fields, option),
789
+ (11, payment_id, option),
755
790
});
756
791
757
792
struct ClaimablePayment {
@@ -760,6 +795,15 @@ struct ClaimablePayment {
760
795
htlcs: Vec<ClaimableHTLC>,
761
796
}
762
797
798
+ impl ClaimablePayment {
799
+ fn inbound_payment_id(&self, secret: &[u8; 32]) -> PaymentId {
800
+ PaymentId::for_inbound_from_htlcs(
801
+ secret,
802
+ self.htlcs.iter().map(|htlc| (htlc.prev_hop.channel_id, htlc.prev_hop.htlc_id))
803
+ )
804
+ }
805
+ }
806
+
763
807
/// Represent the channel funding transaction type.
764
808
enum FundingType {
765
809
/// This variant is useful when we want LDK to validate the funding transaction and
@@ -5627,10 +5671,9 @@ where
5627
5671
} else {
5628
5672
claimable_payment.onion_fields = Some(onion_fields);
5629
5673
}
5630
- let ref mut htlcs = &mut claimable_payment.htlcs;
5631
5674
let mut total_value = claimable_htlc.sender_intended_value;
5632
5675
let mut earliest_expiry = claimable_htlc.cltv_expiry;
5633
- for htlc in htlcs.iter() {
5676
+ for htlc in claimable_payment. htlcs.iter() {
5634
5677
total_value += htlc.sender_intended_value;
5635
5678
earliest_expiry = cmp::min(earliest_expiry, htlc.cltv_expiry);
5636
5679
if htlc.total_msat != claimable_htlc.total_msat {
@@ -5652,13 +5695,18 @@ where
5652
5695
#[allow(unused_assignments)] {
5653
5696
committed_to_claimable = true;
5654
5697
}
5655
- htlcs.push(claimable_htlc);
5656
- let amount_msat = htlcs.iter().map(|htlc| htlc.value).sum();
5657
- htlcs.iter_mut().for_each(|htlc| htlc.total_value_received = Some(amount_msat));
5658
- let counterparty_skimmed_fee_msat = htlcs.iter()
5698
+ claimable_payment.htlcs.push(claimable_htlc);
5699
+ let amount_msat =
5700
+ claimable_payment.htlcs.iter().map(|htlc| htlc.value).sum();
5701
+ claimable_payment.htlcs.iter_mut()
5702
+ .for_each(|htlc| htlc.total_value_received = Some(amount_msat));
5703
+ let counterparty_skimmed_fee_msat = claimable_payment.htlcs.iter()
5659
5704
.map(|htlc| htlc.counterparty_skimmed_fee_msat.unwrap_or(0)).sum();
5660
5705
debug_assert!(total_value.saturating_sub(amount_msat) <=
5661
5706
counterparty_skimmed_fee_msat);
5707
+ claimable_payment.htlcs.sort();
5708
+ let payment_id =
5709
+ claimable_payment.inbound_payment_id(&self.inbound_payment_id_secret);
5662
5710
new_events.push_back((events::Event::PaymentClaimable {
5663
5711
receiver_node_id: Some(receiver_node_id),
5664
5712
payment_hash,
@@ -5669,13 +5717,14 @@ where
5669
5717
via_user_channel_id: Some(prev_user_channel_id),
5670
5718
claim_deadline: Some(earliest_expiry - HTLC_FAIL_BACK_BUFFER),
5671
5719
onion_fields: claimable_payment.onion_fields.clone(),
5720
+ payment_id: Some(payment_id),
5672
5721
}, None));
5673
5722
payment_claimable_generated = true;
5674
5723
} else {
5675
5724
// Nothing to do - we haven't reached the total
5676
5725
// payment value yet, wait until we receive more
5677
5726
// MPP parts.
5678
- htlcs.push(claimable_htlc);
5727
+ claimable_payment. htlcs.push(claimable_htlc);
5679
5728
#[allow(unused_assignments)] {
5680
5729
committed_to_claimable = true;
5681
5730
}
@@ -6472,6 +6521,7 @@ where
6472
6521
}
6473
6522
}
6474
6523
6524
+ let payment_id = payment.inbound_payment_id(&self.inbound_payment_id_secret);
6475
6525
let claiming_payment = claimable_payments.pending_claiming_payments
6476
6526
.entry(payment_hash)
6477
6527
.and_modify(|_| {
@@ -6489,6 +6539,7 @@ where
6489
6539
htlcs,
6490
6540
sender_intended_value,
6491
6541
onion_fields: payment.onion_fields,
6542
+ payment_id: Some(payment_id),
6492
6543
}
6493
6544
});
6494
6545
@@ -7006,6 +7057,7 @@ where
7006
7057
htlcs,
7007
7058
sender_intended_value: sender_intended_total_msat,
7008
7059
onion_fields,
7060
+ payment_id,
7009
7061
}) = payment {
7010
7062
self.pending_events.lock().unwrap().push_back((events::Event::PaymentClaimed {
7011
7063
payment_hash,
@@ -7015,6 +7067,7 @@ where
7015
7067
htlcs,
7016
7068
sender_intended_total_msat,
7017
7069
onion_fields,
7070
+ payment_id,
7018
7071
}, None));
7019
7072
}
7020
7073
},
@@ -12740,6 +12793,7 @@ where
12740
12793
previous_hop_monitor.provide_payment_preimage(&payment_hash, &payment_preimage, &args.tx_broadcaster, &bounded_fee_estimator, &args.logger);
12741
12794
}
12742
12795
}
12796
+ let payment_id = payment.inbound_payment_id(&inbound_payment_id_secret.unwrap());
12743
12797
pending_events_read.push_back((events::Event::PaymentClaimed {
12744
12798
receiver_node_id,
12745
12799
payment_hash,
@@ -12748,6 +12802,7 @@ where
12748
12802
htlcs: payment.htlcs.iter().map(events::ClaimedHTLC::from).collect(),
12749
12803
sender_intended_total_msat: payment.htlcs.first().map(|htlc| htlc.total_msat),
12750
12804
onion_fields: payment.onion_fields,
12805
+ payment_id: Some(payment_id),
12751
12806
}, None));
12752
12807
}
12753
12808
}
0 commit comments