Skip to content

Commit 0931752

Browse files
committed
Give users who use get_payment_secret_preimage the PaymentPreimage
For users who get PaymentPreimages via `get_payment_secret_preimage`, they need to provide the PaymentPreimage back in `claim_funds` but they aren't actually given the preimage anywhere. This commit gives users the PaymentPreimage in the `PaymentReceived` event.
1 parent 9d8ce63 commit 0931752

File tree

5 files changed

+33
-10
lines changed

5 files changed

+33
-10
lines changed

lightning/src/ln/chanmon_update_fail_tests.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,9 @@ fn do_test_simple_monitor_temporary_update_fail(disconnect: bool, persister_fail
206206
let events_3 = nodes[1].node.get_and_clear_pending_events();
207207
assert_eq!(events_3.len(), 1);
208208
match events_3[0] {
209-
Event::PaymentReceived { ref payment_hash, ref payment_secret, amt, user_payment_id: _ } => {
209+
Event::PaymentReceived { ref payment_hash, ref payment_preimage, ref payment_secret, amt, user_payment_id: _ } => {
210210
assert_eq!(payment_hash_1, *payment_hash);
211+
assert!(payment_preimage.is_none());
211212
assert_eq!(payment_secret_1, *payment_secret);
212213
assert_eq!(amt, 1000000);
213214
},
@@ -574,8 +575,9 @@ fn do_test_monitor_temporary_update_fail(disconnect_count: usize) {
574575
let events_5 = nodes[1].node.get_and_clear_pending_events();
575576
assert_eq!(events_5.len(), 1);
576577
match events_5[0] {
577-
Event::PaymentReceived { ref payment_hash, ref payment_secret, amt, user_payment_id: _ } => {
578+
Event::PaymentReceived { ref payment_hash, ref payment_preimage, ref payment_secret, amt, user_payment_id: _ } => {
578579
assert_eq!(payment_hash_2, *payment_hash);
580+
assert!(payment_preimage.is_none());
579581
assert_eq!(payment_secret_2, *payment_secret);
580582
assert_eq!(amt, 1000000);
581583
},
@@ -688,8 +690,9 @@ fn test_monitor_update_fail_cs() {
688690
let events = nodes[1].node.get_and_clear_pending_events();
689691
assert_eq!(events.len(), 1);
690692
match events[0] {
691-
Event::PaymentReceived { payment_hash, payment_secret, amt, user_payment_id: _ } => {
693+
Event::PaymentReceived { payment_hash, payment_preimage, payment_secret, amt, user_payment_id: _ } => {
692694
assert_eq!(payment_hash, our_payment_hash);
695+
assert!(payment_preimage.is_none());
693696
assert_eq!(our_payment_secret, payment_secret);
694697
assert_eq!(amt, 1000000);
695698
},

lightning/src/ln/channelmanager.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2019,6 +2019,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
20192019
} else if total_value == payment_data.total_msat {
20202020
new_events.push(events::Event::PaymentReceived {
20212021
payment_hash,
2022+
payment_preimage: inbound_payment.get().payment_preimage,
20222023
payment_secret: payment_data.payment_secret,
20232024
amt: total_value,
20242025
user_payment_id: inbound_payment.get().user_payment_id,
@@ -3394,6 +3395,10 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
33943395
/// This differs from [`create_inbound_payment_for_hash`] only in that it generates the
33953396
/// [`PaymentHash`] and [`PaymentPreimage`] for you, returning the first and storing the second.
33963397
///
3398+
/// The [`PaymentPreimage`] will ultimately be returned to you in the [`PaymentReceived`], which
3399+
/// will have the [`PaymentReceived::payment_preimage`] field filled in. That should then be
3400+
/// passed directly to [`claim_funds`].
3401+
///
33973402
/// See [`create_inbound_payment_for_hash`] for detailed documentation on behavior and requirements.
33983403
///
33993404
/// [`create_inbound_payment_for_hash`]: Self::create_inbound_payment_for_hash

lightning/src/ln/functional_test_utils.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -941,8 +941,9 @@ macro_rules! expect_payment_received {
941941
let events = $node.node.get_and_clear_pending_events();
942942
assert_eq!(events.len(), 1);
943943
match events[0] {
944-
Event::PaymentReceived { ref payment_hash, ref payment_secret, amt, user_payment_id: _ } => {
944+
Event::PaymentReceived { ref payment_hash, ref payment_preimage, ref payment_secret, amt, user_payment_id: _ } => {
945945
assert_eq!($expected_payment_hash, *payment_hash);
946+
assert!(payment_preimage.is_none());
946947
assert_eq!($expected_payment_secret, *payment_secret);
947948
assert_eq!($expected_recv_value, amt);
948949
},
@@ -1009,8 +1010,9 @@ pub fn pass_along_path<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_path
10091010
if payment_received_expected {
10101011
assert_eq!(events_2.len(), 1);
10111012
match events_2[0] {
1012-
Event::PaymentReceived { ref payment_hash, ref payment_secret, amt, user_payment_id: _ } => {
1013+
Event::PaymentReceived { ref payment_hash, ref payment_preimage, ref payment_secret, amt, user_payment_id: _ } => {
10131014
assert_eq!(our_payment_hash, *payment_hash);
1015+
assert!(payment_preimage.is_none());
10141016
assert_eq!(our_payment_secret, *payment_secret);
10151017
assert_eq!(amt, recv_value);
10161018
},

lightning/src/ln/functional_tests.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2070,16 +2070,18 @@ fn test_channel_reserve_holding_cell_htlcs() {
20702070
let events = nodes[2].node.get_and_clear_pending_events();
20712071
assert_eq!(events.len(), 2);
20722072
match events[0] {
2073-
Event::PaymentReceived { ref payment_hash, ref payment_secret, amt, user_payment_id: _ } => {
2073+
Event::PaymentReceived { ref payment_hash, ref payment_preimage, ref payment_secret, amt, user_payment_id: _ } => {
20742074
assert_eq!(our_payment_hash_21, *payment_hash);
2075+
assert!(payment_preimage.is_none());
20752076
assert_eq!(our_payment_secret_21, *payment_secret);
20762077
assert_eq!(recv_value_21, amt);
20772078
},
20782079
_ => panic!("Unexpected event"),
20792080
}
20802081
match events[1] {
2081-
Event::PaymentReceived { ref payment_hash, ref payment_secret, amt, user_payment_id: _ } => {
2082+
Event::PaymentReceived { ref payment_hash, ref payment_preimage, ref payment_secret, amt, user_payment_id: _ } => {
20822083
assert_eq!(our_payment_hash_22, *payment_hash);
2084+
assert!(payment_preimage.is_none());
20832085
assert_eq!(our_payment_secret_22, *payment_secret);
20842086
assert_eq!(recv_value_22, amt);
20852087
},
@@ -3646,8 +3648,9 @@ fn do_test_drop_messages_peer_disconnect(messages_delivered: u8) {
36463648
let events_2 = nodes[1].node.get_and_clear_pending_events();
36473649
assert_eq!(events_2.len(), 1);
36483650
match events_2[0] {
3649-
Event::PaymentReceived { ref payment_hash, ref payment_secret, amt, user_payment_id: _ } => {
3651+
Event::PaymentReceived { ref payment_hash, ref payment_preimage, ref payment_secret, amt, user_payment_id: _ } => {
36503652
assert_eq!(payment_hash_1, *payment_hash);
3653+
assert!(payment_preimage.is_none());
36513654
assert_eq!(payment_secret_1, *payment_secret);
36523655
assert_eq!(amt, 1000000);
36533656
},
@@ -3983,8 +3986,9 @@ fn test_drop_messages_peer_disconnect_dual_htlc() {
39833986
let events_5 = nodes[1].node.get_and_clear_pending_events();
39843987
assert_eq!(events_5.len(), 1);
39853988
match events_5[0] {
3986-
Event::PaymentReceived { ref payment_hash, ref payment_secret, amt: _, user_payment_id: _ } => {
3989+
Event::PaymentReceived { ref payment_hash, ref payment_preimage, ref payment_secret, amt: _, user_payment_id: _ } => {
39873990
assert_eq!(payment_hash_2, *payment_hash);
3991+
assert!(payment_preimage.is_none());
39883992
assert_eq!(payment_secret_2, *payment_secret);
39893993
},
39903994
_ => panic!("Unexpected event"),

lightning/src/util/events.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ pub enum Event {
6161
PaymentReceived {
6262
/// The hash for which the preimage should be handed to the ChannelManager.
6363
payment_hash: PaymentHash,
64+
/// The preimage to the payment_hash, if the payment hash (and secret) were fetched via
65+
/// [`ChannelManager::get_payment_secret_preimage`]. If provided, this can be handed
66+
/// directly to [`ChannelManager::claim_funds`].
67+
///
68+
/// [`ChannelManager::get_payment_secret_preimage`]: crate::ln::channelmanager::ChannelManager::get_payment_secret_preimage
69+
/// [`ChannelManager::claim_funds`]: crate::ln::channelmanager::ChannelManager::claim_funds
70+
payment_preimage: Option<PaymentPreimage>,
6471
/// The "payment secret". This authenticates the sender to the recipient, preventing a
6572
/// number of deanonymization attacks during the routing process.
6673
/// It is provided here for your reference, however its accuracy is enforced directly by
@@ -139,9 +146,10 @@ impl Writeable for Event {
139146
// We never write out FundingGenerationReady events as, upon disconnection, peers
140147
// drop any channels which have not yet exchanged funding_signed.
141148
},
142-
&Event::PaymentReceived { ref payment_hash, ref payment_secret, ref amt, ref user_payment_id } => {
149+
&Event::PaymentReceived { ref payment_hash, ref payment_preimage, ref payment_secret, ref amt, ref user_payment_id } => {
143150
1u8.write(writer)?;
144151
payment_hash.write(writer)?;
152+
payment_preimage.write(writer)?;
145153
payment_secret.write(writer)?;
146154
amt.write(writer)?;
147155
user_payment_id.write(writer)?;
@@ -186,6 +194,7 @@ impl MaybeReadable for Event {
186194
0u8 => Ok(None),
187195
1u8 => Ok(Some(Event::PaymentReceived {
188196
payment_hash: Readable::read(reader)?,
197+
payment_preimage: Readable::read(reader)?,
189198
payment_secret: Readable::read(reader)?,
190199
amt: Readable::read(reader)?,
191200
user_payment_id: Readable::read(reader)?,

0 commit comments

Comments
 (0)