Skip to content

Commit c9d57f8

Browse files
committed
Add custom_tlvs support to BlindedPaths::Payment::ReceiveTlvs
1 parent b1ca515 commit c9d57f8

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

lightning/src/blinded_path/payment.rs

+23-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ use crate::ln::features::BlindedHopFeatures;
1313
use crate::ln::msgs::DecodeError;
1414
use crate::offers::invoice::BlindedPayInfo;
1515
use crate::prelude::*;
16-
use crate::util::ser::{Readable, Writeable, Writer};
16+
use crate::util::ser::{BigSize, FixedLengthReader, Readable, Writeable, Writer};
1717

1818
use core::convert::TryFrom;
19+
use crate::io::Read;
1920

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

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

142146
impl Readable for BlindedPaymentTlvs {
143147
fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
144-
_init_and_read_tlv_stream!(r, {
148+
let mut custom_tlvs = Vec::new();
149+
150+
let tlv_len = BigSize::read(r)?;
151+
let rd = FixedLengthReader::new(r, tlv_len.0);
152+
_init_and_read_tlv_stream_with_custom_tlv_decode!(rd, {
145153
(1, _padding, option),
146154
(2, scid, option),
147155
(10, payment_relay, option),
148156
(12, payment_constraints, required),
149157
(14, features, option),
150158
(65536, payment_secret, option),
159+
}, |msg_type: u64, msg_reader: &mut FixedLengthReader<_>| -> Result<bool, DecodeError> {
160+
if msg_type < 1 << 16 { return Ok(false) }
161+
let mut value = Vec::new();
162+
msg_reader.read_to_end(&mut value)?;
163+
custom_tlvs.push((msg_type, value));
164+
Ok(true)
151165
});
152166
let _padding: Option<utils::Padding> = _padding;
153167

154168
if let Some(short_channel_id) = scid {
155169
if payment_secret.is_some() { return Err(DecodeError::InvalidValue) }
170+
if !custom_tlvs.is_empty() { return Err(DecodeError::InvalidValue) }
156171
Ok(BlindedPaymentTlvs::Forward(ForwardTlvs {
157172
short_channel_id,
158173
payment_relay: payment_relay.ok_or(DecodeError::InvalidValue)?,
@@ -164,6 +179,7 @@ impl Readable for BlindedPaymentTlvs {
164179
Ok(BlindedPaymentTlvs::Receive(ReceiveTlvs {
165180
payment_secret: payment_secret.ok_or(DecodeError::InvalidValue)?,
166181
payment_constraints: payment_constraints.0.unwrap(),
182+
custom_tlvs,
167183
}))
168184
}
169185
}
@@ -325,6 +341,7 @@ mod tests {
325341
max_cltv_expiry: 0,
326342
htlc_minimum_msat: 1,
327343
},
344+
custom_tlvs: Vec::new(),
328345
};
329346
let htlc_maximum_msat = 100_000;
330347
let blinded_payinfo = super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs, htlc_maximum_msat).unwrap();
@@ -343,6 +360,7 @@ mod tests {
343360
max_cltv_expiry: 0,
344361
htlc_minimum_msat: 1,
345362
},
363+
custom_tlvs: Vec::new(),
346364
};
347365
let blinded_payinfo = super::compute_payinfo(&[], &recv_tlvs, 4242).unwrap();
348366
assert_eq!(blinded_payinfo.fee_base_msat, 0);
@@ -396,6 +414,7 @@ mod tests {
396414
max_cltv_expiry: 0,
397415
htlc_minimum_msat: 3,
398416
},
417+
custom_tlvs: Vec::new(),
399418
};
400419
let htlc_maximum_msat = 100_000;
401420
let blinded_payinfo = super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs, htlc_maximum_msat).unwrap();
@@ -446,6 +465,7 @@ mod tests {
446465
max_cltv_expiry: 0,
447466
htlc_minimum_msat: 1,
448467
},
468+
custom_tlvs: Vec::new(),
449469
};
450470
let htlc_minimum_msat = 3798;
451471
assert!(super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs, htlc_minimum_msat - 1).is_err());
@@ -500,6 +520,7 @@ mod tests {
500520
max_cltv_expiry: 0,
501521
htlc_minimum_msat: 1,
502522
},
523+
custom_tlvs: Vec::new(),
503524
};
504525

505526
let blinded_payinfo = super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs, 10_000).unwrap();

lightning/src/ln/blinded_payment_tests.rs

+3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ pub fn get_blinded_route_parameters(
5656
max_cltv_expiry: u32::max_value(),
5757
htlc_minimum_msat: channel_upds.last().unwrap().htlc_minimum_msat,
5858
},
59+
custom_tlvs: Vec::new(),
5960
};
6061
let mut secp_ctx = Secp256k1::new();
6162
let blinded_path = BlindedPath::new_for_payment(
@@ -89,6 +90,7 @@ fn do_one_hop_blinded_path(success: bool) {
8990
max_cltv_expiry: u32::max_value(),
9091
htlc_minimum_msat: chan_upd.htlc_minimum_msat,
9192
},
93+
custom_tlvs: Vec::new(),
9294
};
9395
let mut secp_ctx = Secp256k1::new();
9496
let blinded_path = BlindedPath::one_hop_for_payment(
@@ -131,6 +133,7 @@ fn mpp_to_one_hop_blinded_path() {
131133
max_cltv_expiry: u32::max_value(),
132134
htlc_minimum_msat: chan_upd_1_3.htlc_minimum_msat,
133135
},
136+
custom_tlvs: Vec::new(),
134137
};
135138
let blinded_path = BlindedPath::one_hop_for_payment(
136139
nodes[3].node.get_our_node_id(), payee_tlvs, &chanmon_cfgs[3].keys_manager, &secp_ctx

lightning/src/ln/channelmanager.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7939,6 +7939,7 @@ where
79397939
max_cltv_expiry,
79407940
htlc_minimum_msat: 1,
79417941
},
7942+
custom_tlvs: Vec::new(),
79427943
};
79437944
self.router.create_blinded_payment_paths(
79447945
payee_node_id, first_hops, payee_tlvs, amount_msats, entropy_source, secp_ctx

lightning/src/ln/msgs.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2397,8 +2397,8 @@ impl<NS: Deref> ReadableArgs<(Option<PublicKey>, &NS)> for InboundOnionPayload w
23972397
})
23982398
},
23992399
ChaChaPolyReadAdapter { readable: BlindedPaymentTlvs::Receive(ReceiveTlvs {
2400-
payment_secret, payment_constraints
2401-
})} => {
2400+
payment_secret, payment_constraints,
2401+
custom_tlvs: _ })} => {
24022402
if total_msat.unwrap_or(0) > MAX_VALUE_MSAT { return Err(DecodeError::InvalidValue) }
24032403
Ok(Self::BlindedReceive {
24042404
amt_msat: amt.ok_or(DecodeError::InvalidValue)?,

0 commit comments

Comments
 (0)