From a90a35bcbb10b7e53340fba85222867499e84a0f Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 24 Mar 2023 17:07:07 +0000 Subject: [PATCH] Deserialize payment metadata fields in the onion final hop data --- lightning/src/ln/channelmanager.rs | 2 +- lightning/src/ln/msgs.rs | 15 ++++++++++++--- lightning/src/ln/onion_utils.rs | 1 + 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 28b279aac..7590d6f2f 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -2163,7 +2163,7 @@ where msg: "Got non final data with an HMAC of 0", }); }, - msgs::OnionHopDataFormat::FinalNode { payment_data, keysend_preimage } => { + msgs::OnionHopDataFormat::FinalNode { payment_data, keysend_preimage, .. } => { // TODO: expose the payment_metadata to the user if payment_data.is_some() && keysend_preimage.is_some() { return Err(ReceiveError { err_code: 0x4000|22, diff --git a/lightning/src/ln/msgs.rs b/lightning/src/ln/msgs.rs index 2557454b4..c5177828f 100644 --- a/lightning/src/ln/msgs.rs +++ b/lightning/src/ln/msgs.rs @@ -42,7 +42,7 @@ use crate::io_extras::read_to_end; use crate::events::{MessageSendEventsProvider, OnionMessageProvider}; use crate::util::logger; -use crate::util::ser::{LengthReadable, Readable, ReadableArgs, Writeable, Writer, FixedLengthReader, HighZeroBytesDroppedBigSize, Hostname}; +use crate::util::ser::{LengthReadable, Readable, ReadableArgs, Writeable, Writer, WithoutLength, FixedLengthReader, HighZeroBytesDroppedBigSize, Hostname}; use crate::ln::{PaymentPreimage, PaymentHash, PaymentSecret}; @@ -1168,6 +1168,7 @@ mod fuzzy_internal_msgs { }, FinalNode { payment_data: Option, + payment_metadata: Option>, keysend_preimage: Option, }, } @@ -1661,11 +1662,12 @@ impl Writeable for OnionHopData { (6, short_channel_id, required) }); }, - OnionHopDataFormat::FinalNode { ref payment_data, ref keysend_preimage } => { + OnionHopDataFormat::FinalNode { ref payment_data, ref payment_metadata, ref keysend_preimage } => { _encode_varint_length_prefixed_tlv!(w, { (2, HighZeroBytesDroppedBigSize(self.amt_to_forward), required), (4, HighZeroBytesDroppedBigSize(self.outgoing_cltv_value), required), (8, payment_data, option), + (16, payment_metadata.as_ref().map(|m| WithoutLength(m)), option), (5482373484, keysend_preimage, option) }); }, @@ -1680,29 +1682,33 @@ impl Readable for OnionHopData { let mut cltv_value = HighZeroBytesDroppedBigSize(0u32); let mut short_id: Option = None; let mut payment_data: Option = None; + let mut payment_metadata: Option>> = None; let mut keysend_preimage: Option = None; read_tlv_fields!(r, { (2, amt, required), (4, cltv_value, required), (6, short_id, option), (8, payment_data, option), + (16, payment_metadata, option), // See https://github.com/lightning/blips/blob/master/blip-0003.md (5482373484, keysend_preimage, option) }); let format = if let Some(short_channel_id) = short_id { if payment_data.is_some() { return Err(DecodeError::InvalidValue); } + if payment_metadata.is_some() { return Err(DecodeError::InvalidValue); } OnionHopDataFormat::NonFinalNode { short_channel_id, } } else { - if let &Some(ref data) = &payment_data { + if let Some(data) = &payment_data { if data.total_msat > MAX_VALUE_MSAT { return Err(DecodeError::InvalidValue); } } OnionHopDataFormat::FinalNode { payment_data, + payment_metadata: payment_metadata.map(|w| w.0), keysend_preimage, } }; @@ -2880,6 +2886,7 @@ mod tests { let mut msg = msgs::OnionHopData { format: OnionHopDataFormat::FinalNode { payment_data: None, + payment_metadata: None, keysend_preimage: None, }, amt_to_forward: 0x0badf00d01020304, @@ -2903,6 +2910,7 @@ mod tests { payment_secret: expected_payment_secret, total_msat: 0x1badca1f }), + payment_metadata: None, keysend_preimage: None, }, amt_to_forward: 0x0badf00d01020304, @@ -2917,6 +2925,7 @@ mod tests { payment_secret, total_msat: 0x1badca1f }), + payment_metadata: None, keysend_preimage: None, } = msg.format { assert_eq!(payment_secret, expected_payment_secret); diff --git a/lightning/src/ln/onion_utils.rs b/lightning/src/ln/onion_utils.rs index 2c20fb177..a751cb092 100644 --- a/lightning/src/ln/onion_utils.rs +++ b/lightning/src/ln/onion_utils.rs @@ -170,6 +170,7 @@ pub(super) fn build_onion_payloads(path: &Vec, total_msat: u64, mut re total_msat, }) } else { None }, + payment_metadata: None, keysend_preimage: *keysend_preimage, } } else { -- 2.39.5