Skip to content

Commit

Permalink
Add custom_tlvs support to BlindedPaths::Payment::ReceiveTlvs
Browse files Browse the repository at this point in the history
  • Loading branch information
shaavan committed Jan 17, 2024
1 parent b1ca515 commit c9d57f8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
25 changes: 23 additions & 2 deletions lightning/src/blinded_path/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ use crate::ln::features::BlindedHopFeatures;
use crate::ln::msgs::DecodeError;
use crate::offers::invoice::BlindedPayInfo;
use crate::prelude::*;
use crate::util::ser::{Readable, Writeable, Writer};
use crate::util::ser::{BigSize, FixedLengthReader, Readable, Writeable, Writer};

use core::convert::TryFrom;
use crate::io::Read;

/// An intermediate node, its outbound channel, and relay parameters.
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -53,6 +54,8 @@ pub struct ReceiveTlvs {
pub payment_secret: PaymentSecret,
/// Constraints for the receiver of this payment.
pub payment_constraints: PaymentConstraints,
/// Custom Tlvs
pub custom_tlvs: Vec<(u64, Vec<u8>)>,
}

/// Data to construct a [`BlindedHop`] for sending a payment over.
Expand Down Expand Up @@ -121,6 +124,7 @@ impl Writeable for ForwardTlvs {
impl Writeable for ReceiveTlvs {
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
encode_tlv_stream!(w, {
(1, self.custom_tlvs, optional_vec),
(12, self.payment_constraints, required),
(65536, self.payment_secret, required)
});
Expand All @@ -141,18 +145,29 @@ impl<'a> Writeable for BlindedPaymentTlvsRef<'a> {

impl Readable for BlindedPaymentTlvs {
fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
_init_and_read_tlv_stream!(r, {
let mut custom_tlvs = Vec::new();

let tlv_len = BigSize::read(r)?;
let rd = FixedLengthReader::new(r, tlv_len.0);
_init_and_read_tlv_stream_with_custom_tlv_decode!(rd, {
(1, _padding, option),
(2, scid, option),
(10, payment_relay, option),
(12, payment_constraints, required),
(14, features, option),
(65536, payment_secret, option),
}, |msg_type: u64, msg_reader: &mut FixedLengthReader<_>| -> Result<bool, DecodeError> {
if msg_type < 1 << 16 { return Ok(false) }
let mut value = Vec::new();
msg_reader.read_to_end(&mut value)?;
custom_tlvs.push((msg_type, value));
Ok(true)
});
let _padding: Option<utils::Padding> = _padding;

if let Some(short_channel_id) = scid {
if payment_secret.is_some() { return Err(DecodeError::InvalidValue) }
if !custom_tlvs.is_empty() { return Err(DecodeError::InvalidValue) }
Ok(BlindedPaymentTlvs::Forward(ForwardTlvs {
short_channel_id,
payment_relay: payment_relay.ok_or(DecodeError::InvalidValue)?,
Expand All @@ -164,6 +179,7 @@ impl Readable for BlindedPaymentTlvs {
Ok(BlindedPaymentTlvs::Receive(ReceiveTlvs {
payment_secret: payment_secret.ok_or(DecodeError::InvalidValue)?,
payment_constraints: payment_constraints.0.unwrap(),
custom_tlvs,
}))
}
}
Expand Down Expand Up @@ -325,6 +341,7 @@ mod tests {
max_cltv_expiry: 0,
htlc_minimum_msat: 1,
},
custom_tlvs: Vec::new(),
};
let htlc_maximum_msat = 100_000;
let blinded_payinfo = super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs, htlc_maximum_msat).unwrap();
Expand All @@ -343,6 +360,7 @@ mod tests {
max_cltv_expiry: 0,
htlc_minimum_msat: 1,
},
custom_tlvs: Vec::new(),
};
let blinded_payinfo = super::compute_payinfo(&[], &recv_tlvs, 4242).unwrap();
assert_eq!(blinded_payinfo.fee_base_msat, 0);
Expand Down Expand Up @@ -396,6 +414,7 @@ mod tests {
max_cltv_expiry: 0,
htlc_minimum_msat: 3,
},
custom_tlvs: Vec::new(),
};
let htlc_maximum_msat = 100_000;
let blinded_payinfo = super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs, htlc_maximum_msat).unwrap();
Expand Down Expand Up @@ -446,6 +465,7 @@ mod tests {
max_cltv_expiry: 0,
htlc_minimum_msat: 1,
},
custom_tlvs: Vec::new(),
};
let htlc_minimum_msat = 3798;
assert!(super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs, htlc_minimum_msat - 1).is_err());
Expand Down Expand Up @@ -500,6 +520,7 @@ mod tests {
max_cltv_expiry: 0,
htlc_minimum_msat: 1,
},
custom_tlvs: Vec::new(),
};

let blinded_payinfo = super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs, 10_000).unwrap();
Expand Down
3 changes: 3 additions & 0 deletions lightning/src/ln/blinded_payment_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub fn get_blinded_route_parameters(
max_cltv_expiry: u32::max_value(),
htlc_minimum_msat: channel_upds.last().unwrap().htlc_minimum_msat,
},
custom_tlvs: Vec::new(),
};
let mut secp_ctx = Secp256k1::new();
let blinded_path = BlindedPath::new_for_payment(
Expand Down Expand Up @@ -89,6 +90,7 @@ fn do_one_hop_blinded_path(success: bool) {
max_cltv_expiry: u32::max_value(),
htlc_minimum_msat: chan_upd.htlc_minimum_msat,
},
custom_tlvs: Vec::new(),
};
let mut secp_ctx = Secp256k1::new();
let blinded_path = BlindedPath::one_hop_for_payment(
Expand Down Expand Up @@ -131,6 +133,7 @@ fn mpp_to_one_hop_blinded_path() {
max_cltv_expiry: u32::max_value(),
htlc_minimum_msat: chan_upd_1_3.htlc_minimum_msat,
},
custom_tlvs: Vec::new(),
};
let blinded_path = BlindedPath::one_hop_for_payment(
nodes[3].node.get_our_node_id(), payee_tlvs, &chanmon_cfgs[3].keys_manager, &secp_ctx
Expand Down
1 change: 1 addition & 0 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7939,6 +7939,7 @@ where
max_cltv_expiry,
htlc_minimum_msat: 1,
},
custom_tlvs: Vec::new(),
};
self.router.create_blinded_payment_paths(
payee_node_id, first_hops, payee_tlvs, amount_msats, entropy_source, secp_ctx
Expand Down
4 changes: 2 additions & 2 deletions lightning/src/ln/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2397,8 +2397,8 @@ impl<NS: Deref> ReadableArgs<(Option<PublicKey>, &NS)> for InboundOnionPayload w
})
},
ChaChaPolyReadAdapter { readable: BlindedPaymentTlvs::Receive(ReceiveTlvs {
payment_secret, payment_constraints
})} => {
payment_secret, payment_constraints,
custom_tlvs: _ })} => {
if total_msat.unwrap_or(0) > MAX_VALUE_MSAT { return Err(DecodeError::InvalidValue) }
Ok(Self::BlindedReceive {
amt_msat: amt.ok_or(DecodeError::InvalidValue)?,
Expand Down

0 comments on commit c9d57f8

Please sign in to comment.