]> git.bitcoin.ninja Git - rust-lightning/commitdiff
demo 2025-06-3085-alt-demo
authorMatt Corallo <git@bluematt.me>
Tue, 25 Jun 2024 22:04:46 +0000 (22:04 +0000)
committerMatt Corallo <git@bluematt.me>
Tue, 25 Jun 2024 22:42:04 +0000 (22:42 +0000)
lightning/src/blinded_path/message.rs
lightning/src/onion_message/packet.rs

index 9a30d91bd8009cf81dca0da511af9bb207549d69..ca8215224d42f88d5697297752cd2aeb3c98bddf 100644 (file)
@@ -52,12 +52,21 @@ pub(crate) struct ForwardTlvs {
        pub(crate) next_blinding_override: Option<PublicKey>,
 }
 
-/// Similar to [`ForwardTlvs`], but these TLVs are for the final node.
-pub(crate) struct ReceiveTlvs {
-       /// If `path_id` is `Some`, it is used to identify the blinded path that this onion message is
-       /// sending to. This is useful for receivers to check that said blinded path is being used in
-       /// the right context.
-       pub path_id: Option<MessageContext>
+
+/// Represents additional data included by the recipient in a [`BlindedPath`].
+///
+/// This data is encrypted and will be included when sending through
+/// [`BlindedPath`]. The recipient can authenticate the message and
+/// utilize it for further processing if needed.
+#[derive(Clone, Debug)]
+pub enum ReceiveTlvs {
+       /// Represents the data specific to [`OffersMessage`]
+       ///
+       /// [`OffersMessage`]: crate::onion_message::offers::OffersMessage
+       Offers(OffersContext),
+       /// Represents the data appended with Custom Onion Message.
+       Custom(Vec<u8>),
+       Empty,
 }
 
 impl Writeable for ForwardTlvs {
@@ -78,38 +87,46 @@ impl Writeable for ForwardTlvs {
 
 impl Writeable for ReceiveTlvs {
        fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
-               // TODO: write padding
-               encode_tlv_stream!(writer, {
-                       (6, self.path_id, option),
-               });
+               match self {
+                       Self::Offers(o) => {
+                               encode_tlv_stream!(writer, {
+                                       (6, (0u8, &o), required),
+                               });
+                       },
+                       Self::Custom(v) => {
+                               encode_tlv_stream!(writer, {
+                                       (6, (1u8, &v), required),
+                               });
+                       },
+                       Self::Empty => {},
+               }
                Ok(())
        }
 }
 
 impl Readable for ReceiveTlvs {
        fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
-               _init_and_read_tlv_stream!(r, {
-                       (6, path_id, option),
-               });
-               Ok(ReceiveTlvs { path_id })
+               let mut res = Self::Empty;
+               decode_tlv_stream_with_custom_tlv_decode!(r, {},
+                       |ty, reader| {
+                               if ty == 6 {
+                                       let ty: u8 = Readable::read(reader)?;
+                                       match ty {
+                                               0 => { res = Self::Offers(Readable::read(reader)?); }
+                                               1 => { res = Self::Custom(Readable::read(reader)?); }
+                                               _ if ty % 2 == 0 => { return Err(DecodeError::UnknownRequiredFeature); }
+                                               _ => {},
+                                       }
+                                       Ok(true)
+                               } else {
+                                       Ok(false)
+                               }
+                       }
+               );
+               Ok(res)
        }
 }
 
-/// Represents additional data included by the recipient in a [`BlindedPath`].
-///
-/// This data is encrypted and will be included when sending through
-/// [`BlindedPath`]. The recipient can authenticate the message and
-/// utilize it for further processing if needed.
-#[derive(Clone, Debug)]
-pub enum MessageContext {
-       /// Represents the data specific to [`OffersMessage`]
-       ///
-       /// [`OffersMessage`]: crate::onion_message::offers::OffersMessage
-       Offers(OffersContext),
-       /// Represents the data appended with Custom Onion Message.
-       Custom(Vec<u8>),
-}
-
 /// Contains the data specific to [`OffersMessage`]
 ///
 /// [`OffersMessage`]: crate::onion_message::offers::OffersMessage
@@ -125,11 +142,6 @@ pub enum OffersContext {
 
 }
 
-impl_writeable_tlv_based_enum!(MessageContext, ;
-       (0, Offers),
-       (1, Custom),
-);
-
 impl_writeable_tlv_based_enum!(OffersContext,
        (0, Unknown) => {},
        (1, OutboundPayment) => {
index fca7dd6a91ac6a1eda39e5519822c1c9101df78c..3bd2f56fd8bcd04730ed1655297034b2f76e23ad 100644 (file)
@@ -341,7 +341,7 @@ impl Readable for ControlTlvs {
                        })
                } else if valid_recv_fmt {
                        ControlTlvs::Receive(ReceiveTlvs {
-                               path_id,
+                               path_id, // this would be unwrap_or(::Empty)
                        })
                } else {
                        return Err(DecodeError::InvalidValue)