From 3b8ac139ba4dc97ee02feb9a8b0787827bff4e11 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 23 Apr 2021 22:24:47 +0000 Subject: [PATCH] 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. --- lightning/src/ln/chanmon_update_fail_tests.rs | 9 ++++++--- lightning/src/ln/channelmanager.rs | 8 ++++++++ lightning/src/ln/functional_test_utils.rs | 6 ++++-- lightning/src/ln/functional_tests.rs | 12 ++++++++---- lightning/src/util/events.rs | 11 ++++++++++- 5 files changed, 36 insertions(+), 10 deletions(-) diff --git a/lightning/src/ln/chanmon_update_fail_tests.rs b/lightning/src/ln/chanmon_update_fail_tests.rs index bcc115ae9..266f5b0af 100644 --- a/lightning/src/ln/chanmon_update_fail_tests.rs +++ b/lightning/src/ln/chanmon_update_fail_tests.rs @@ -206,8 +206,9 @@ fn do_test_simple_monitor_temporary_update_fail(disconnect: bool, persister_fail let events_3 = nodes[1].node.get_and_clear_pending_events(); assert_eq!(events_3.len(), 1); match events_3[0] { - Event::PaymentReceived { ref payment_hash, ref payment_secret, amt, user_payment_id: _ } => { + Event::PaymentReceived { ref payment_hash, ref payment_preimage, ref payment_secret, amt, user_payment_id: _ } => { assert_eq!(payment_hash_1, *payment_hash); + assert!(payment_preimage.is_none()); assert_eq!(payment_secret_1, *payment_secret); assert_eq!(amt, 1000000); }, @@ -574,8 +575,9 @@ fn do_test_monitor_temporary_update_fail(disconnect_count: usize) { let events_5 = nodes[1].node.get_and_clear_pending_events(); assert_eq!(events_5.len(), 1); match events_5[0] { - Event::PaymentReceived { ref payment_hash, ref payment_secret, amt, user_payment_id: _ } => { + Event::PaymentReceived { ref payment_hash, ref payment_preimage, ref payment_secret, amt, user_payment_id: _ } => { assert_eq!(payment_hash_2, *payment_hash); + assert!(payment_preimage.is_none()); assert_eq!(payment_secret_2, *payment_secret); assert_eq!(amt, 1000000); }, @@ -688,8 +690,9 @@ fn test_monitor_update_fail_cs() { let events = nodes[1].node.get_and_clear_pending_events(); assert_eq!(events.len(), 1); match events[0] { - Event::PaymentReceived { payment_hash, payment_secret, amt, user_payment_id: _ } => { + Event::PaymentReceived { payment_hash, payment_preimage, payment_secret, amt, user_payment_id: _ } => { assert_eq!(payment_hash, our_payment_hash); + assert!(payment_preimage.is_none()); assert_eq!(our_payment_secret, payment_secret); assert_eq!(amt, 1000000); }, diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index b1890853e..f147a8162 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -2024,6 +2024,7 @@ impl ChannelMana } else if total_value == payment_data.total_msat { new_events.push(events::Event::PaymentReceived { payment_hash, + payment_preimage: inbound_payment.get().payment_preimage, payment_secret: payment_data.payment_secret, amt: total_value, user_payment_id: inbound_payment.get().user_payment_id, @@ -3410,8 +3411,15 @@ impl ChannelMana /// This differs from [`create_inbound_payment_for_hash`] only in that it generates the /// [`PaymentHash`] and [`PaymentPreimage`] for you, returning the first and storing the second. /// + /// The [`PaymentPreimage`] will ultimately be returned to you in the [`PaymentReceived`], which + /// will have the [`PaymentReceived::payment_preimage`] field filled in. That should then be + /// passed directly to [`claim_funds`]. + /// /// See [`create_inbound_payment_for_hash`] for detailed documentation on behavior and requirements. /// + /// [`claim_funds`]: Self::claim_funds + /// [`PaymentReceived`]: events::Event::PaymentReceived + /// [`PaymentReceived::payment_preimage`]: events::Event::PaymentReceived::payment_preimage /// [`create_inbound_payment_for_hash`]: Self::create_inbound_payment_for_hash pub fn create_inbound_payment(&self, min_value_msat: Option, invoice_expiry_delta_secs: u32, user_payment_id: u64) -> (PaymentHash, PaymentSecret) { let payment_preimage = PaymentPreimage(self.keys_manager.get_secure_random_bytes()); diff --git a/lightning/src/ln/functional_test_utils.rs b/lightning/src/ln/functional_test_utils.rs index 1b0567ab3..cd4a478d8 100644 --- a/lightning/src/ln/functional_test_utils.rs +++ b/lightning/src/ln/functional_test_utils.rs @@ -941,8 +941,9 @@ macro_rules! expect_payment_received { let events = $node.node.get_and_clear_pending_events(); assert_eq!(events.len(), 1); match events[0] { - Event::PaymentReceived { ref payment_hash, ref payment_secret, amt, user_payment_id: _ } => { + Event::PaymentReceived { ref payment_hash, ref payment_preimage, ref payment_secret, amt, user_payment_id: _ } => { assert_eq!($expected_payment_hash, *payment_hash); + assert!(payment_preimage.is_none()); assert_eq!($expected_payment_secret, *payment_secret); assert_eq!($expected_recv_value, amt); }, @@ -1009,8 +1010,9 @@ pub fn pass_along_path<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_path if payment_received_expected { assert_eq!(events_2.len(), 1); match events_2[0] { - Event::PaymentReceived { ref payment_hash, ref payment_secret, amt, user_payment_id: _ } => { + Event::PaymentReceived { ref payment_hash, ref payment_preimage, ref payment_secret, amt, user_payment_id: _ } => { assert_eq!(our_payment_hash, *payment_hash); + assert!(payment_preimage.is_none()); assert_eq!(our_payment_secret, *payment_secret); assert_eq!(amt, recv_value); }, diff --git a/lightning/src/ln/functional_tests.rs b/lightning/src/ln/functional_tests.rs index 56c795d8e..7c3dce522 100644 --- a/lightning/src/ln/functional_tests.rs +++ b/lightning/src/ln/functional_tests.rs @@ -2070,16 +2070,18 @@ fn test_channel_reserve_holding_cell_htlcs() { let events = nodes[2].node.get_and_clear_pending_events(); assert_eq!(events.len(), 2); match events[0] { - Event::PaymentReceived { ref payment_hash, ref payment_secret, amt, user_payment_id: _ } => { + Event::PaymentReceived { ref payment_hash, ref payment_preimage, ref payment_secret, amt, user_payment_id: _ } => { assert_eq!(our_payment_hash_21, *payment_hash); + assert!(payment_preimage.is_none()); assert_eq!(our_payment_secret_21, *payment_secret); assert_eq!(recv_value_21, amt); }, _ => panic!("Unexpected event"), } match events[1] { - Event::PaymentReceived { ref payment_hash, ref payment_secret, amt, user_payment_id: _ } => { + Event::PaymentReceived { ref payment_hash, ref payment_preimage, ref payment_secret, amt, user_payment_id: _ } => { assert_eq!(our_payment_hash_22, *payment_hash); + assert!(payment_preimage.is_none()); assert_eq!(our_payment_secret_22, *payment_secret); assert_eq!(recv_value_22, amt); }, @@ -3646,8 +3648,9 @@ fn do_test_drop_messages_peer_disconnect(messages_delivered: u8) { let events_2 = nodes[1].node.get_and_clear_pending_events(); assert_eq!(events_2.len(), 1); match events_2[0] { - Event::PaymentReceived { ref payment_hash, ref payment_secret, amt, user_payment_id: _ } => { + Event::PaymentReceived { ref payment_hash, ref payment_preimage, ref payment_secret, amt, user_payment_id: _ } => { assert_eq!(payment_hash_1, *payment_hash); + assert!(payment_preimage.is_none()); assert_eq!(payment_secret_1, *payment_secret); assert_eq!(amt, 1000000); }, @@ -3983,8 +3986,9 @@ fn test_drop_messages_peer_disconnect_dual_htlc() { let events_5 = nodes[1].node.get_and_clear_pending_events(); assert_eq!(events_5.len(), 1); match events_5[0] { - Event::PaymentReceived { ref payment_hash, ref payment_secret, amt: _, user_payment_id: _ } => { + Event::PaymentReceived { ref payment_hash, ref payment_preimage, ref payment_secret, amt: _, user_payment_id: _ } => { assert_eq!(payment_hash_2, *payment_hash); + assert!(payment_preimage.is_none()); assert_eq!(payment_secret_2, *payment_secret); }, _ => panic!("Unexpected event"), diff --git a/lightning/src/util/events.rs b/lightning/src/util/events.rs index ad7e61177..ac0fc3c89 100644 --- a/lightning/src/util/events.rs +++ b/lightning/src/util/events.rs @@ -61,6 +61,13 @@ pub enum Event { PaymentReceived { /// The hash for which the preimage should be handed to the ChannelManager. payment_hash: PaymentHash, + /// The preimage to the payment_hash, if the payment hash (and secret) were fetched via + /// [`ChannelManager::create_inbound_payment`]. If provided, this can be handed directly to + /// [`ChannelManager::claim_funds`]. + /// + /// [`ChannelManager::create_inbound_payment`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment + /// [`ChannelManager::claim_funds`]: crate::ln::channelmanager::ChannelManager::claim_funds + payment_preimage: Option, /// The "payment secret". This authenticates the sender to the recipient, preventing a /// number of deanonymization attacks during the routing process. /// It is provided here for your reference, however its accuracy is enforced directly by @@ -139,9 +146,10 @@ impl Writeable for Event { // We never write out FundingGenerationReady events as, upon disconnection, peers // drop any channels which have not yet exchanged funding_signed. }, - &Event::PaymentReceived { ref payment_hash, ref payment_secret, ref amt, ref user_payment_id } => { + &Event::PaymentReceived { ref payment_hash, ref payment_preimage, ref payment_secret, ref amt, ref user_payment_id } => { 1u8.write(writer)?; payment_hash.write(writer)?; + payment_preimage.write(writer)?; payment_secret.write(writer)?; amt.write(writer)?; user_payment_id.write(writer)?; @@ -186,6 +194,7 @@ impl MaybeReadable for Event { 0u8 => Ok(None), 1u8 => Ok(Some(Event::PaymentReceived { payment_hash: Readable::read(reader)?, + payment_preimage: Readable::read(reader)?, payment_secret: Readable::read(reader)?, amt: Readable::read(reader)?, user_payment_id: Readable::read(reader)?, -- 2.39.5