Merge pull request #2975 from valentinewallace/2024-03-blinded-path-custom-tlvs
[rust-lightning] / lightning / src / ln / msgs.rs
index f5a36286cfd0d87e577e4c94b29c89b6655eeade..19fbbae31669b0fb9aad4132593c12ed6f261165 100644 (file)
@@ -1711,6 +1711,7 @@ mod fuzzy_internal_msgs {
                        payment_constraints: PaymentConstraints,
                        intro_node_blinding_point: Option<PublicKey>,
                        keysend_preimage: Option<PaymentPreimage>,
+                       custom_tlvs: Vec<(u64, Vec<u8>)>,
                }
        }
 
@@ -1747,6 +1748,7 @@ mod fuzzy_internal_msgs {
                        encrypted_tlvs: Vec<u8>,
                        intro_node_blinding_point: Option<PublicKey>, // Set if the introduction node of the blinded path is the final node
                        keysend_preimage: Option<PaymentPreimage>,
+                       custom_tlvs: Vec<(u64, Vec<u8>)>,
                }
        }
 
@@ -2603,16 +2605,21 @@ impl Writeable for OutboundOnionPayload {
                        },
                        Self::BlindedReceive {
                                sender_intended_htlc_amt_msat, total_msat, cltv_expiry_height, encrypted_tlvs,
-                               intro_node_blinding_point, keysend_preimage,
+                               intro_node_blinding_point, keysend_preimage, ref custom_tlvs,
                        } => {
+                               // We need to update [`ln::outbound_payment::RecipientOnionFields::with_custom_tlvs`]
+                               // to reject any reserved types in the experimental range if new ones are ever
+                               // standardized.
+                               let keysend_tlv = keysend_preimage.map(|preimage| (5482373484, preimage.encode()));
+                               let mut custom_tlvs: Vec<&(u64, Vec<u8>)> = custom_tlvs.iter().chain(keysend_tlv.iter()).collect();
+                               custom_tlvs.sort_unstable_by_key(|(typ, _)| *typ);
                                _encode_varint_length_prefixed_tlv!(w, {
                                        (2, HighZeroBytesDroppedBigSize(*sender_intended_htlc_amt_msat), required),
                                        (4, HighZeroBytesDroppedBigSize(*cltv_expiry_height), required),
                                        (10, *encrypted_tlvs, required_vec),
                                        (12, intro_node_blinding_point, option),
-                                       (18, HighZeroBytesDroppedBigSize(*total_msat), required),
-                                       (5482373484, keysend_preimage, option)
-                               });
+                                       (18, HighZeroBytesDroppedBigSize(*total_msat), required)
+                               }, custom_tlvs.iter());
                        },
                }
                Ok(())
@@ -2715,6 +2722,7 @@ impl<NS: Deref> ReadableArgs<(Option<PublicKey>, &NS)> for InboundOnionPayload w
                                                payment_constraints,
                                                intro_node_blinding_point,
                                                keysend_preimage,
+                                               custom_tlvs,
                                        })
                                },
                        }