Skip to content

Commit dd91bc9

Browse files
committed
Introduce MessageContext in ReceiveTlvs
1. New Enum for Enhanced Data Handling: - Introduced the `MessageContext` enum, which allows the node to include additional context data along with the `reply_path` sent to the counterparty. - The node anticipates receiving this data back for further processing. 2. Variants in MessageContext: - The `MessageContext` enum includes two variants: "Offers" and "Context" - One of the variants, `Offers`, holds the `payment_id` of the associated Outbound BOLT12 Payment. 3. Future Usage: - This enum will be utilized in a subsequent commit to abandon outbound payments that have failed to complete for various reasons.
1 parent 069ee2a commit dd91bc9

File tree

3 files changed

+76
-17
lines changed

3 files changed

+76
-17
lines changed

lightning/src/blinded_path/message.rs

+50-5
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
//! [`BlindedPath`]: crate::blinded_path::BlindedPath
1313
1414
use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey};
15-
1615
#[allow(unused_imports)]
1716
use crate::prelude::*;
1817

1918
use crate::blinded_path::{BlindedHop, BlindedPath, IntroductionNode, NextMessageHop, NodeIdLookUp};
2019
use crate::blinded_path::utils;
2120
use crate::io;
2221
use crate::io::Cursor;
22+
use crate::ln::channelmanager::PaymentId;
2323
use crate::ln::onion_utils;
2424
use crate::onion_message::packet::ControlTlvs;
2525
use crate::sign::{NodeSigner, Recipient};
@@ -52,10 +52,10 @@ pub(crate) struct ForwardTlvs {
5252

5353
/// Similar to [`ForwardTlvs`], but these TLVs are for the final node.
5454
pub(crate) struct ReceiveTlvs {
55-
/// If `path_id` is `Some`, it is used to identify the blinded path that this onion message is
55+
/// If `context` is `Some`, it is used to identify the blinded path that this onion message is
5656
/// sending to. This is useful for receivers to check that said blinded path is being used in
5757
/// the right context.
58-
pub(crate) path_id: Option<[u8; 32]>,
58+
pub context: Option<MessageContext>
5959
}
6060

6161
impl Writeable for ForwardTlvs {
@@ -78,12 +78,57 @@ impl Writeable for ReceiveTlvs {
7878
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
7979
// TODO: write padding
8080
encode_tlv_stream!(writer, {
81-
(6, self.path_id, option),
81+
(65537, self.context, option),
8282
});
8383
Ok(())
8484
}
8585
}
8686

87+
/// Represents additional data included by the recipient in a [`BlindedPath`].
88+
///
89+
/// This data is encrypted by the recipient and remains invisible to anyone else.
90+
/// It is included in the [BlindedPath], making it accessible again to the recipient
91+
/// whenever the [BlindedPath] is used.
92+
/// The recipient can authenticate the message and utilize it for further processing
93+
/// if needed.
94+
#[derive(Clone, Debug)]
95+
pub enum MessageContext {
96+
/// Represents the data specific to [`OffersMessage`]
97+
///
98+
/// [`OffersMessage`]: crate::onion_message::offers::OffersMessage
99+
Offers(OffersContext),
100+
/// Represents custom data received in a Custom Onion Message.
101+
Custom(Vec<u8>),
102+
}
103+
104+
/// Contains the data specific to [`OffersMessage`]
105+
///
106+
/// [`OffersMessage`]: crate::onion_message::offers::OffersMessage
107+
#[derive(Clone, Debug)]
108+
pub enum OffersContext {
109+
/// Represents an unknown BOLT12 payment context.
110+
/// This variant is used when we are unsure if we are dealing
111+
/// with an Inbound or Outbound BOLT12 payment.
112+
Unknown {},
113+
/// Represents an outbound BOLT12 payment context.
114+
OutboundPayment {
115+
/// Payment ID of the outbound BOLT12 payment.
116+
payment_id: PaymentId
117+
},
118+
}
119+
120+
impl_writeable_tlv_based_enum!(MessageContext, ;
121+
(0, Offers),
122+
(1, Custom),
123+
);
124+
125+
impl_writeable_tlv_based_enum!(OffersContext,
126+
(0, Unknown) => {},
127+
(1, OutboundPayment) => {
128+
(0, payment_id, required),
129+
},
130+
;);
131+
87132
/// Construct blinded onion message hops for the given `intermediate_nodes` and `recipient_node_id`.
88133
pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
89134
secp_ctx: &Secp256k1<T>, intermediate_nodes: &[ForwardNode], recipient_node_id: PublicKey,
@@ -99,7 +144,7 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
99144
None => NextMessageHop::NodeId(*pubkey),
100145
})
101146
.map(|next_hop| ControlTlvs::Forward(ForwardTlvs { next_hop, next_blinding_override: None }))
102-
.chain(core::iter::once(ControlTlvs::Receive(ReceiveTlvs { path_id: None })));
147+
.chain(core::iter::once(ControlTlvs::Receive(ReceiveTlvs { context: None })));
103148

104149
utils::construct_blinded_hops(secp_ctx, pks, tlvs, session_priv)
105150
}

lightning/src/onion_message/messenger.rs

+23-9
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use bitcoin::hashes::sha256::Hash as Sha256;
1616
use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey};
1717

1818
use crate::blinded_path::{BlindedPath, IntroductionNode, NextMessageHop, NodeIdLookUp};
19-
use crate::blinded_path::message::{advance_path_by_one, ForwardNode, ForwardTlvs, ReceiveTlvs};
19+
use crate::blinded_path::message::{advance_path_by_one, ForwardNode, ForwardTlvs, ReceiveTlvs, MessageContext};
2020
use crate::blinded_path::utils;
2121
use crate::events::{Event, EventHandler, EventsProvider};
2222
use crate::sign::{EntropySource, NodeSigner, Recipient};
@@ -795,8 +795,8 @@ pub trait CustomOnionMessageHandler {
795795
pub enum PeeledOnion<T: OnionMessageContents> {
796796
/// Forwarded onion, with the next node id and a new onion
797797
Forward(NextMessageHop, OnionMessage),
798-
/// Received onion message, with decrypted contents, path_id, and reply path
799-
Receive(ParsedOnionMessageContents<T>, Option<[u8; 32]>, Option<BlindedPath>)
798+
/// Received onion message, with decrypted contents, context, and reply path
799+
Receive(ParsedOnionMessageContents<T>, Option<MessageContext>, Option<BlindedPath>)
800800
}
801801

802802

@@ -946,9 +946,23 @@ where
946946
(control_tlvs_ss, custom_handler.deref(), logger.deref())
947947
) {
948948
Ok((Payload::Receive::<ParsedOnionMessageContents<<<CMH as Deref>::Target as CustomOnionMessageHandler>::CustomMessage>> {
949-
message, control_tlvs: ReceiveControlTlvs::Unblinded(ReceiveTlvs { path_id }), reply_path,
949+
message, control_tlvs: ReceiveControlTlvs::Unblinded(ReceiveTlvs { context }), reply_path,
950950
}, None)) => {
951-
Ok(PeeledOnion::Receive(message, path_id, reply_path))
951+
match (&message, &context) {
952+
(_, None) => {
953+
Ok(PeeledOnion::Receive(message, None, reply_path))
954+
}
955+
(ParsedOnionMessageContents::Offers(_), Some(MessageContext::Offers(_))) => {
956+
Ok(PeeledOnion::Receive(message, context, reply_path))
957+
}
958+
(ParsedOnionMessageContents::Custom(_), Some(MessageContext::Custom(_))) => {
959+
Ok(PeeledOnion::Receive(message, context, reply_path))
960+
}
961+
_ => {
962+
log_trace!(logger, "Received message was sent on a blinded path with the wrong context.");
963+
Err(())
964+
}
965+
}
952966
},
953967
Ok((Payload::Forward(ForwardControlTlvs::Unblinded(ForwardTlvs {
954968
next_hop, next_blinding_override
@@ -1432,11 +1446,11 @@ where
14321446
fn handle_onion_message(&self, peer_node_id: &PublicKey, msg: &OnionMessage) {
14331447
let logger = WithContext::from(&self.logger, Some(*peer_node_id), None, None);
14341448
match self.peel_onion_message(msg) {
1435-
Ok(PeeledOnion::Receive(message, path_id, reply_path)) => {
1449+
Ok(PeeledOnion::Receive(message, _context, reply_path)) => {
14361450
log_trace!(
14371451
logger,
1438-
"Received an onion message with path_id {:02x?} and {} reply_path: {:?}",
1439-
path_id, if reply_path.is_some() { "a" } else { "no" }, message);
1452+
"Received an onion message with {} reply_path: {:?}",
1453+
if reply_path.is_some() { "a" } else { "no" }, message);
14401454

14411455
let responder = reply_path.map(Responder::new);
14421456
match message {
@@ -1727,7 +1741,7 @@ fn packet_payloads_and_keys<T: OnionMessageContents, S: secp256k1::Signing + sec
17271741
}, prev_control_tlvs_ss.unwrap()));
17281742
} else {
17291743
payloads.push((Payload::Receive {
1730-
control_tlvs: ReceiveControlTlvs::Unblinded(ReceiveTlvs { path_id: None, }),
1744+
control_tlvs: ReceiveControlTlvs::Unblinded(ReceiveTlvs { context: None }),
17311745
reply_path: reply_path.take(),
17321746
message,
17331747
}, prev_control_tlvs_ss.unwrap()));

lightning/src/onion_message/packet.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,8 @@ impl Readable for ControlTlvs {
319319
(1, _padding, option),
320320
(2, short_channel_id, option),
321321
(4, next_node_id, option),
322-
(6, path_id, option),
323322
(8, next_blinding_override, option),
323+
(65537, context, option),
324324
});
325325
let _padding: Option<Padding> = _padding;
326326

@@ -331,7 +331,7 @@ impl Readable for ControlTlvs {
331331
(None, None) => None,
332332
};
333333

334-
let valid_fwd_fmt = next_hop.is_some() && path_id.is_none();
334+
let valid_fwd_fmt = next_hop.is_some();
335335
let valid_recv_fmt = next_hop.is_none() && next_blinding_override.is_none();
336336

337337
let payload_fmt = if valid_fwd_fmt {
@@ -341,7 +341,7 @@ impl Readable for ControlTlvs {
341341
})
342342
} else if valid_recv_fmt {
343343
ControlTlvs::Receive(ReceiveTlvs {
344-
path_id,
344+
context,
345345
})
346346
} else {
347347
return Err(DecodeError::InvalidValue)

0 commit comments

Comments
 (0)