From: Valentine Wallace Date: Thu, 13 May 2021 19:38:31 +0000 (-0400) Subject: Parse keysend TLV field in onion. X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=f8a36e5af40a79b58395aa05ec8c242c9250b01c;p=rust-lightning Parse keysend TLV field in onion. This doesn't yet use the field, but it will be used in upcoming commits. --- diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index e7b1ff487..52ac049ab 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -1436,7 +1436,7 @@ impl ChannelMana let payment_data = match next_hop_data.format { msgs::OnionHopDataFormat::Legacy { .. } => None, msgs::OnionHopDataFormat::NonFinalNode { .. } => return_err!("Got non final data with an HMAC of 0", 0x4000 | 22, &[0;0]), - msgs::OnionHopDataFormat::FinalNode { payment_data } => payment_data, + msgs::OnionHopDataFormat::FinalNode { payment_data, .. } => payment_data, }; if payment_data.is_none() { diff --git a/lightning/src/ln/msgs.rs b/lightning/src/ln/msgs.rs index a64bbc01c..c1404db2f 100644 --- a/lightning/src/ln/msgs.rs +++ b/lightning/src/ln/msgs.rs @@ -880,7 +880,7 @@ pub trait RoutingMessageHandler : MessageSendEventsProvider { mod fuzzy_internal_msgs { use prelude::*; - use ln::PaymentSecret; + use ln::{PaymentPreimage, PaymentSecret}; // These types aren't intended to be pub, but are exposed for direct fuzzing (as we deserialize // them from untrusted input): @@ -901,6 +901,7 @@ mod fuzzy_internal_msgs { }, FinalNode { payment_data: Option, + keysend_preimage: Option }, } @@ -1300,14 +1301,15 @@ impl Writeable for OnionHopData { (6, short_channel_id, required) }); }, - OnionHopDataFormat::FinalNode { ref payment_data } => { + OnionHopDataFormat::FinalNode { ref payment_data, ref keysend_preimage } => { if let Some(final_data) = payment_data { if final_data.total_msat > MAX_VALUE_MSAT { panic!("We should never be sending infinite/overflow onion payments"); } } encode_varint_length_prefixed_tlv!(w, { (2, HighZeroBytesDroppedVarInt(self.amt_to_forward), required), (4, HighZeroBytesDroppedVarInt(self.outgoing_cltv_value), required), - (8, payment_data, option) + (8, payment_data, option), + (5482373484, keysend_preimage, option) }); }, } @@ -1330,11 +1332,14 @@ impl Readable for OnionHopData { let mut cltv_value = HighZeroBytesDroppedVarInt(0u32); let mut short_id: Option = None; let mut payment_data: Option = None; + let mut keysend_preimage: Option = None; + // The TLV type is chosen to be compatible with lnd and c-lightning. decode_tlv_stream!(&mut rd, { (2, amt, required), (4, cltv_value, required), (6, short_id, option), (8, payment_data, option), + (5482373484, keysend_preimage, option) }); rd.eat_remaining().map_err(|_| DecodeError::ShortRead)?; let format = if let Some(short_channel_id) = short_id { @@ -1349,7 +1354,8 @@ impl Readable for OnionHopData { } } OnionHopDataFormat::FinalNode { - payment_data + payment_data, + keysend_preimage, } }; (format, amt.0, cltv_value.0) @@ -2529,6 +2535,7 @@ mod tests { let mut msg = msgs::OnionHopData { format: OnionHopDataFormat::FinalNode { payment_data: None, + keysend_preimage: None, }, amt_to_forward: 0x0badf00d01020304, outgoing_cltv_value: 0xffffffff, @@ -2537,7 +2544,7 @@ mod tests { let target_value = hex::decode("1002080badf00d010203040404ffffffff").unwrap(); assert_eq!(encoded_value, target_value); msg = Readable::read(&mut Cursor::new(&target_value[..])).unwrap(); - if let OnionHopDataFormat::FinalNode { payment_data: None } = msg.format { } else { panic!(); } + if let OnionHopDataFormat::FinalNode { payment_data: None, .. } = msg.format { } else { panic!(); } assert_eq!(msg.amt_to_forward, 0x0badf00d01020304); assert_eq!(msg.outgoing_cltv_value, 0xffffffff); } @@ -2551,6 +2558,7 @@ mod tests { payment_secret: expected_payment_secret, total_msat: 0x1badca1f }), + keysend_preimage: None, }, amt_to_forward: 0x0badf00d01020304, outgoing_cltv_value: 0xffffffff, @@ -2563,7 +2571,8 @@ mod tests { payment_data: Some(FinalOnionHopData { payment_secret, total_msat: 0x1badca1f - }) + }), + keysend_preimage: None, } = msg.format { assert_eq!(payment_secret, expected_payment_secret); } else { panic!(); } diff --git a/lightning/src/ln/onion_utils.rs b/lightning/src/ln/onion_utils.rs index 0deb8b74f..bd739d86e 100644 --- a/lightning/src/ln/onion_utils.rs +++ b/lightning/src/ln/onion_utils.rs @@ -141,6 +141,7 @@ pub(super) fn build_onion_payloads(path: &Vec, total_msat: u64, paymen total_msat, }) } else { None }, + keysend_preimage: None, } } else { msgs::OnionHopDataFormat::NonFinalNode {