From: Matt Corallo Date: Tue, 21 Dec 2021 20:38:24 +0000 (+0000) Subject: Provide users the payment metadata via `Event::PaymentReceived` X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=c98733b20ce474a5b1da9fd30b0eaa2262758da2;p=rust-lightning Provide users the payment metadata via `Event::PaymentReceived` --- diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 3817fcdb7..841eaa6c3 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -3260,6 +3260,7 @@ impl ChannelMana payment_hash, purpose: events::PaymentPurpose::InvoicePayment { payment_preimage: $payment_preimage, + payment_metadata: $payment_data.payment_metadata, payment_secret: $payment_data.payment_secret, }, amt: total_value, diff --git a/lightning/src/ln/functional_tests.rs b/lightning/src/ln/functional_tests.rs index b896e0dbe..cdca9a8ce 100644 --- a/lightning/src/ln/functional_tests.rs +++ b/lightning/src/ln/functional_tests.rs @@ -8295,10 +8295,11 @@ fn test_secret_timeout() { expect_pending_htlcs_forwardable!(nodes[1]); let events = nodes[1].node.get_and_clear_pending_events(); assert_eq!(events.len(), 1); - match events[0] { - Event::PaymentReceived { purpose: PaymentPurpose::InvoicePayment { payment_preimage, payment_secret }, .. } => { + match &events[0] { + Event::PaymentReceived { purpose: PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, payment_metadata }, .. } => { assert!(payment_preimage.is_none()); - assert_eq!(payment_secret, our_payment_secret); + assert!(payment_metadata.is_none()); + assert_eq!(*payment_secret, our_payment_secret); // We don't actually have the payment preimage with which to claim this payment! }, _ => panic!("Unexpected event"), diff --git a/lightning/src/util/events.rs b/lightning/src/util/events.rs index a4a733c8e..e2c862531 100644 --- a/lightning/src/util/events.rs +++ b/lightning/src/util/events.rs @@ -60,6 +60,8 @@ pub enum PaymentPurpose { /// [`ChannelManager::create_inbound_payment`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment /// [`ChannelManager::create_inbound_payment_for_hash`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash payment_secret: PaymentSecret, + ///XXX + payment_metadata: Option>, }, /// Because this is a spontaneous payment, the payer generated their own preimage rather than us /// (the payee) providing a preimage. @@ -417,10 +419,12 @@ impl Writeable for Event { 1u8.write(writer)?; let mut payment_secret = None; let payment_preimage; + let mut payment_metadata = None; match &purpose { - PaymentPurpose::InvoicePayment { payment_preimage: preimage, payment_secret: secret } => { + PaymentPurpose::InvoicePayment { payment_preimage: preimage, payment_secret: secret, payment_metadata: metadata } => { payment_secret = Some(secret); payment_preimage = *preimage; + payment_metadata = metadata.as_ref(); }, PaymentPurpose::SpontaneousPayment(preimage) => { payment_preimage = Some(*preimage); @@ -428,6 +432,7 @@ impl Writeable for Event { } write_tlv_fields!(writer, { (0, payment_hash, required), + (1, payment_metadata, option), (2, payment_secret, option), (4, amt, required), (6, 0u64, required), // user_payment_id required for compatibility with 0.0.103 and earlier @@ -533,10 +538,12 @@ impl MaybeReadable for Event { let mut payment_hash = PaymentHash([0; 32]); let mut payment_preimage = None; let mut payment_secret = None; + let mut payment_metadata = None; let mut amt = 0; let mut _user_payment_id = None::; // For compatibility with 0.0.103 and earlier read_tlv_fields!(reader, { (0, payment_hash, required), + (1, payment_metadata, option), (2, payment_secret, option), (4, amt, required), (6, _user_payment_id, option), @@ -545,8 +552,10 @@ impl MaybeReadable for Event { let purpose = match payment_secret { Some(secret) => PaymentPurpose::InvoicePayment { payment_preimage, + payment_metadata, payment_secret: secret }, + None if payment_metadata.is_some() => return Err(msgs::DecodeError::InvalidValue), None if payment_preimage.is_some() => PaymentPurpose::SpontaneousPayment(payment_preimage.unwrap()), None => return Err(msgs::DecodeError::InvalidValue), };