Skip to content

Commit 71db3ce

Browse files
committed
Add Custom TLVs for payment::ReceiveTlvs
- Building on the previous commit, this update allows users to include their own custom TLVs within the reply path of a sent onion message. - With this, users can attach custom data to the message, which will be returned in the response, providing more flexibility for custom use cases.
1 parent 7baa1ca commit 71db3ce

File tree

7 files changed

+25
-5
lines changed

7 files changed

+25
-5
lines changed

fuzz/src/invoice_request_deser.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ fn build_response<T: secp256k1::Signing + secp256k1::Verification>(
9999
htlc_minimum_msat: 1,
100100
},
101101
payment_context,
102+
custom_tlvs: Vec::new(),
102103
};
103104
let intermediate_nodes = [PaymentForwardNode {
104105
tlvs: ForwardTlvs {

fuzz/src/refund_deser.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ fn build_response<T: secp256k1::Signing + secp256k1::Verification>(
7676
htlc_minimum_msat: 1,
7777
},
7878
payment_context,
79+
custom_tlvs: Vec::new(),
7980
};
8081
let intermediate_nodes = [PaymentForwardNode {
8182
tlvs: ForwardTlvs {

lightning/src/blinded_path/payment.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@ pub struct ReceiveTlvs {
260260
pub payment_constraints: PaymentConstraints,
261261
/// Context for the receiver of this payment.
262262
pub payment_context: PaymentContext,
263+
/// Custom Tlvs
264+
pub custom_tlvs: Vec<u8>,
263265
}
264266

265267
/// Data to construct a [`BlindedHop`] for sending a payment over.
@@ -404,7 +406,8 @@ impl Writeable for ReceiveTlvs {
404406
encode_tlv_stream!(w, {
405407
(12, self.payment_constraints, required),
406408
(65536, self.payment_secret, required),
407-
(65537, self.payment_context, required)
409+
(65537, self.payment_context, required),
410+
(65539, self.custom_tlvs, (default_value, Vec::new())),
408411
});
409412
Ok(())
410413
}
@@ -432,6 +435,7 @@ impl Readable for BlindedPaymentTlvs {
432435
(14, features, (option, encoding: (BlindedHopFeatures, WithoutLength))),
433436
(65536, payment_secret, option),
434437
(65537, payment_context, (default_value, PaymentContext::unknown())),
438+
(65539, custom_tlvs, (default_value, Vec::new()))
435439
});
436440
let _padding: Option<utils::Padding> = _padding;
437441

@@ -452,6 +456,7 @@ impl Readable for BlindedPaymentTlvs {
452456
payment_secret: payment_secret.ok_or(DecodeError::InvalidValue)?,
453457
payment_constraints: payment_constraints.0.unwrap(),
454458
payment_context: payment_context.0.unwrap(),
459+
custom_tlvs: custom_tlvs.0.unwrap(),
455460
}))
456461
}
457462
}
@@ -683,6 +688,7 @@ mod tests {
683688
htlc_minimum_msat: 1,
684689
},
685690
payment_context: PaymentContext::unknown(),
691+
custom_tlvs: Vec::new(),
686692
};
687693
let htlc_maximum_msat = 100_000;
688694
let blinded_payinfo = super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs, htlc_maximum_msat, 12).unwrap();
@@ -702,6 +708,7 @@ mod tests {
702708
htlc_minimum_msat: 1,
703709
},
704710
payment_context: PaymentContext::unknown(),
711+
custom_tlvs: Vec::new(),
705712
};
706713
let blinded_payinfo = super::compute_payinfo(&[], &recv_tlvs, 4242, TEST_FINAL_CLTV as u16).unwrap();
707714
assert_eq!(blinded_payinfo.fee_base_msat, 0);
@@ -758,6 +765,7 @@ mod tests {
758765
htlc_minimum_msat: 3,
759766
},
760767
payment_context: PaymentContext::unknown(),
768+
custom_tlvs: Vec::new(),
761769
};
762770
let htlc_maximum_msat = 100_000;
763771
let blinded_payinfo = super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs, htlc_maximum_msat, TEST_FINAL_CLTV as u16).unwrap();
@@ -811,6 +819,7 @@ mod tests {
811819
htlc_minimum_msat: 1,
812820
},
813821
payment_context: PaymentContext::unknown(),
822+
custom_tlvs: Vec::new(),
814823
};
815824
let htlc_minimum_msat = 3798;
816825
assert!(super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs, htlc_minimum_msat - 1, TEST_FINAL_CLTV as u16).is_err());
@@ -868,6 +877,7 @@ mod tests {
868877
htlc_minimum_msat: 1,
869878
},
870879
payment_context: PaymentContext::unknown(),
880+
custom_tlvs: Vec::new()
871881
};
872882

873883
let blinded_payinfo = super::compute_payinfo(&intermediate_nodes[..], &recv_tlvs, 10_000, TEST_FINAL_CLTV as u16).unwrap();

lightning/src/ln/blinded_payment_tests.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ fn blinded_payment_path(
7777
intro_node_min_htlc_opt.unwrap_or_else(|| channel_upds.last().unwrap().htlc_minimum_msat),
7878
},
7979
payment_context: PaymentContext::unknown(),
80+
custom_tlvs: Vec::new(),
8081
};
8182
let mut secp_ctx = Secp256k1::new();
8283
BlindedPaymentPath::new(
@@ -123,6 +124,7 @@ fn do_one_hop_blinded_path(success: bool) {
123124
htlc_minimum_msat: chan_upd.htlc_minimum_msat,
124125
},
125126
payment_context: PaymentContext::unknown(),
127+
custom_tlvs: Vec::new(),
126128
};
127129
let mut secp_ctx = Secp256k1::new();
128130
let blinded_path = BlindedPaymentPath::new(
@@ -167,6 +169,7 @@ fn mpp_to_one_hop_blinded_path() {
167169
htlc_minimum_msat: chan_upd_1_3.htlc_minimum_msat,
168170
},
169171
payment_context: PaymentContext::unknown(),
172+
custom_tlvs: Vec::new(),
170173
};
171174
let blinded_path = BlindedPaymentPath::new(
172175
&[], nodes[3].node.get_our_node_id(), payee_tlvs, u64::MAX, TEST_FINAL_CLTV as u16,
@@ -1382,6 +1385,7 @@ fn custom_tlvs_to_blinded_path() {
13821385
htlc_minimum_msat: chan_upd.htlc_minimum_msat,
13831386
},
13841387
payment_context: PaymentContext::unknown(),
1388+
custom_tlvs: Vec::new(),
13851389
};
13861390
let mut secp_ctx = Secp256k1::new();
13871391
let blinded_path = BlindedPaymentPath::new(

lightning/src/ln/channelmanager.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10161,7 +10161,7 @@ where
1016110161
Ok((payment_hash, payment_secret)) => {
1016210162
let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
1016310163
let payment_paths = self.create_blinded_payment_paths(
10164-
amount_msats, payment_secret, payment_context
10164+
amount_msats, payment_secret, payment_context, None
1016510165
)
1016610166
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
1016710167

@@ -10478,7 +10478,7 @@ where
1047810478
/// Creates multi-hop blinded payment paths for the given `amount_msats` by delegating to
1047910479
/// [`Router::create_blinded_payment_paths`].
1048010480
fn create_blinded_payment_paths(
10481-
&self, amount_msats: u64, payment_secret: PaymentSecret, payment_context: PaymentContext
10481+
&self, amount_msats: u64, payment_secret: PaymentSecret, payment_context: PaymentContext, custom_tlvs: Option<Vec<u8>>
1048210482
) -> Result<Vec<BlindedPaymentPath>, ()> {
1048310483
let secp_ctx = &self.secp_ctx;
1048410484

@@ -10493,6 +10493,7 @@ where
1049310493
htlc_minimum_msat: 1,
1049410494
},
1049510495
payment_context,
10496+
custom_tlvs: custom_tlvs.unwrap_or_default()
1049610497
};
1049710498
self.router.create_blinded_payment_paths(
1049810499
payee_node_id, first_hops, payee_tlvs, amount_msats, secp_ctx
@@ -12026,7 +12027,7 @@ where
1202612027
invoice_request: invoice_request.fields(),
1202712028
});
1202812029
let payment_paths = match self.create_blinded_payment_paths(
12029-
amount_msats, payment_secret, payment_context
12030+
amount_msats, payment_secret, payment_context, custom_tlvs.clone()
1203012031
) {
1203112032
Ok(payment_paths) => payment_paths,
1203212033
Err(()) => {

lightning/src/ln/max_payment_path_len_tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ fn one_hop_blinded_path_with_custom_tlv() {
167167
htlc_minimum_msat: chan_upd_1_2.htlc_minimum_msat,
168168
},
169169
payment_context: PaymentContext::unknown(),
170+
custom_tlvs: Vec::new(),
170171
};
171172
let mut secp_ctx = Secp256k1::new();
172173
let blinded_path = BlindedPaymentPath::new(

lightning/src/ln/msgs.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2925,9 +2925,11 @@ impl<NS: Deref> ReadableArgs<(Option<PublicKey>, NS)> for InboundOnionPayload wh
29252925
next_blinding_override,
29262926
})
29272927
},
2928+
// Note: The custom tlvs in the receive tlvs is not used here.
29282929
ChaChaPolyReadAdapter { readable: BlindedPaymentTlvs::Receive(ReceiveTlvs {
2929-
payment_secret, payment_constraints, payment_context
2930+
payment_secret, payment_constraints, payment_context, custom_tlvs: user_custom_tlv
29302931
})} => {
2932+
debug_assert_eq!(user_custom_tlv, user_custom_tlvs, "The custom TLVs in ReceiveTlvs must match the ones read from serialization.");
29312933
if total_msat.unwrap_or(0) > MAX_VALUE_MSAT { return Err(DecodeError::InvalidValue) }
29322934
Ok(Self::BlindedReceive {
29332935
sender_intended_htlc_amt_msat: amt.ok_or(DecodeError::InvalidValue)?,

0 commit comments

Comments
 (0)