Skip to content

Commit 0cdac04

Browse files
committed
Introduce Custom Tlvs more message::ReceiveTlvs
1 parent 206ab82 commit 0cdac04

File tree

9 files changed

+85
-76
lines changed

9 files changed

+85
-76
lines changed

lightning/src/blinded_path/message.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ impl Readable for BlindedMessagePath {
5555
impl BlindedMessagePath {
5656
/// Create a one-hop blinded path for a message.
5757
pub fn one_hop<ES: Deref, T: secp256k1::Signing + secp256k1::Verification>(
58-
recipient_node_id: PublicKey, context: MessageContext, entropy_source: ES, secp_ctx: &Secp256k1<T>
58+
recipient_node_id: PublicKey, context: MessageContext, custom_tlvs: Vec<u8>, entropy_source: ES, secp_ctx: &Secp256k1<T>
5959
) -> Result<Self, ()> where ES::Target: EntropySource {
60-
Self::new(&[], recipient_node_id, context, entropy_source, secp_ctx)
60+
Self::new(&[], recipient_node_id, context, custom_tlvs, entropy_source, secp_ctx)
6161
}
6262

6363
/// Create a path for an onion message, to be forwarded along `node_pks`. The last node
@@ -67,7 +67,7 @@ impl BlindedMessagePath {
6767
// TODO: make all payloads the same size with padding + add dummy hops
6868
pub fn new<ES: Deref, T: secp256k1::Signing + secp256k1::Verification>(
6969
intermediate_nodes: &[MessageForwardNode], recipient_node_id: PublicKey,
70-
context: MessageContext, entropy_source: ES, secp_ctx: &Secp256k1<T>,
70+
context: MessageContext, custom_tlvs: Vec<u8>, entropy_source: ES, secp_ctx: &Secp256k1<T>,
7171
) -> Result<Self, ()> where ES::Target: EntropySource {
7272
let introduction_node = IntroductionNode::NodeId(
7373
intermediate_nodes.first().map_or(recipient_node_id, |n| n.node_id)
@@ -80,7 +80,7 @@ impl BlindedMessagePath {
8080
blinding_point: PublicKey::from_secret_key(secp_ctx, &blinding_secret),
8181
blinded_hops: blinded_hops(
8282
secp_ctx, intermediate_nodes, recipient_node_id,
83-
context, &blinding_secret,
83+
context, custom_tlvs, &blinding_secret,
8484
).map_err(|_| ())?,
8585
}))
8686
}
@@ -241,7 +241,10 @@ pub(crate) struct ReceiveTlvs {
241241
/// If `context` is `Some`, it is used to identify the blinded path that this onion message is
242242
/// sending to. This is useful for receivers to check that said blinded path is being used in
243243
/// the right context.
244-
pub context: Option<MessageContext>
244+
pub context: Option<MessageContext>,
245+
246+
/// Custom Tlvs. A user can use this to send custom tlvs information back to themself.
247+
pub custom_tlvs: Option<Vec<u8>>,
245248
}
246249

247250
impl Writeable for ForwardTlvs {
@@ -265,6 +268,7 @@ impl Writeable for ReceiveTlvs {
265268
// TODO: write padding
266269
encode_tlv_stream!(writer, {
267270
(65537, self.context, option),
271+
(65539, self.custom_tlvs, option),
268272
});
269273
Ok(())
270274
}
@@ -456,7 +460,7 @@ impl_writeable_tlv_based!(DNSResolverContext, {
456460
/// Construct blinded onion message hops for the given `intermediate_nodes` and `recipient_node_id`.
457461
pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
458462
secp_ctx: &Secp256k1<T>, intermediate_nodes: &[MessageForwardNode],
459-
recipient_node_id: PublicKey, context: MessageContext, session_priv: &SecretKey,
463+
recipient_node_id: PublicKey, context: MessageContext, custom_tlvs: Vec<u8>, session_priv: &SecretKey,
460464
) -> Result<Vec<BlindedHop>, secp256k1::Error> {
461465
let pks = intermediate_nodes.iter().map(|node| node.node_id)
462466
.chain(core::iter::once(recipient_node_id));
@@ -468,7 +472,7 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
468472
None => NextMessageHop::NodeId(pubkey),
469473
})
470474
.map(|next_hop| ControlTlvs::Forward(ForwardTlvs { next_hop, next_blinding_override: None }))
471-
.chain(core::iter::once(ControlTlvs::Receive(ReceiveTlvs{ context: Some(context) })));
475+
.chain(core::iter::once(ControlTlvs::Receive(ReceiveTlvs{ context: Some(context), custom_tlvs: Some(custom_tlvs) })));
472476

473477
let path = pks.zip(tlvs);
474478

lightning/src/ln/channelmanager.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9645,7 +9645,7 @@ where
96459645
.collect::<Vec<_>>();
96469646

96479647
self.message_router
9648-
.create_blinded_paths(recipient, context, peers, secp_ctx)
9648+
.create_blinded_paths(recipient, context, Vec::new(), peers, secp_ctx)
96499649
.and_then(|paths| (!paths.is_empty()).then(|| paths).ok_or(()))
96509650
}
96519651

@@ -9673,7 +9673,7 @@ where
96739673
.collect::<Vec<_>>();
96749674

96759675
self.message_router
9676-
.create_compact_blinded_paths(recipient, MessageContext::Offers(context), peers, secp_ctx)
9676+
.create_compact_blinded_paths(recipient, MessageContext::Offers(context), Vec::new(), peers, secp_ctx)
96779677
.and_then(|paths| (!paths.is_empty()).then(|| paths).ok_or(()))
96789678
}
96799679

@@ -11096,7 +11096,7 @@ where
1109611096
L::Target: Logger,
1109711097
{
1109811098
fn handle_message(
11099-
&self, message: OffersMessage, context: Option<OffersContext>, responder: Option<Responder>,
11099+
&self, message: OffersMessage, context: Option<OffersContext>, custom_tlvs: Option<Vec<u8>>, responder: Option<Responder>,
1110011100
) -> Option<(OffersMessage, ResponseInstruction)> {
1110111101
let secp_ctx = &self.secp_ctx;
1110211102
let expanded_key = &self.inbound_payment_key;
@@ -11239,7 +11239,7 @@ where
1123911239
let nonce = Nonce::from_entropy_source(&*self.entropy_source);
1124011240
let hmac = payment_hash.hmac_for_offer_payment(nonce, expanded_key);
1124111241
let context = MessageContext::Offers(OffersContext::InboundPayment { payment_hash, nonce, hmac });
11242-
Some((OffersMessage::Invoice(invoice), responder.respond_with_reply_path(context)))
11242+
Some((OffersMessage::Invoice(invoice), responder.respond_with_reply_path(context, custom_tlvs)))
1124311243
},
1124411244
Err(error) => Some((OffersMessage::InvoiceError(error.into()), responder.respond())),
1124511245
}

lightning/src/ln/offers_tests.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ fn claim_bolt12_payment<'a, 'b, 'c>(
193193

194194
fn extract_offer_nonce<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, message: &OnionMessage) -> Nonce {
195195
match node.onion_messenger.peel_onion_message(message) {
196-
Ok(PeeledOnion::Receive(_, Some(MessageContext::Offers(OffersContext::InvoiceRequest { nonce })), _)) => nonce,
197-
Ok(PeeledOnion::Receive(_, context, _)) => panic!("Unexpected onion message context: {:?}", context),
196+
Ok(PeeledOnion::Receive(_, Some(MessageContext::Offers(OffersContext::InvoiceRequest { nonce })), _, _)) => nonce,
197+
Ok(PeeledOnion::Receive(_, context, _, _)) => panic!("Unexpected onion message context: {:?}", context),
198198
Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
199199
Err(e) => panic!("Failed to process onion message {:?}", e),
200200
}
@@ -204,7 +204,7 @@ fn extract_invoice_request<'a, 'b, 'c>(
204204
node: &Node<'a, 'b, 'c>, message: &OnionMessage
205205
) -> (InvoiceRequest, BlindedMessagePath) {
206206
match node.onion_messenger.peel_onion_message(message) {
207-
Ok(PeeledOnion::Receive(message, _, reply_path)) => match message {
207+
Ok(PeeledOnion::Receive(message, _, _, reply_path)) => match message {
208208
ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
209209
OffersMessage::InvoiceRequest(invoice_request) => (invoice_request, reply_path.unwrap()),
210210
OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
@@ -221,7 +221,7 @@ fn extract_invoice_request<'a, 'b, 'c>(
221221

222222
fn extract_invoice<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, message: &OnionMessage) -> (Bolt12Invoice, BlindedMessagePath) {
223223
match node.onion_messenger.peel_onion_message(message) {
224-
Ok(PeeledOnion::Receive(message, _, reply_path)) => match message {
224+
Ok(PeeledOnion::Receive(message, _, _, reply_path)) => match message {
225225
ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
226226
OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
227227
OffersMessage::Invoice(invoice) => (invoice, reply_path.unwrap()),
@@ -240,7 +240,7 @@ fn extract_invoice_error<'a, 'b, 'c>(
240240
node: &Node<'a, 'b, 'c>, message: &OnionMessage
241241
) -> InvoiceError {
242242
match node.onion_messenger.peel_onion_message(message) {
243-
Ok(PeeledOnion::Receive(message, _, _)) => match message {
243+
Ok(PeeledOnion::Receive(message, _, _, _)) => match message {
244244
ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
245245
OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
246246
OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),

lightning/src/ln/peer_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl OnionMessageHandler for IgnoringMessageHandler {
143143
}
144144

145145
impl OffersMessageHandler for IgnoringMessageHandler {
146-
fn handle_message(&self, _message: OffersMessage, _context: Option<OffersContext>, _responder: Option<Responder>) -> Option<(OffersMessage, ResponseInstruction)> {
146+
fn handle_message(&self, _message: OffersMessage, _context: Option<OffersContext>, _custom_tlvs: Option<Vec<u8>>, _responder: Option<Responder>) -> Option<(OffersMessage, ResponseInstruction)> {
147147
None
148148
}
149149
}

lightning/src/onion_message/functional_tests.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl Drop for MessengerNode {
7676
struct TestOffersMessageHandler {}
7777

7878
impl OffersMessageHandler for TestOffersMessageHandler {
79-
fn handle_message(&self, _message: OffersMessage, _context: Option<OffersContext>, _responder: Option<Responder>) -> Option<(OffersMessage, ResponseInstruction)> {
79+
fn handle_message(&self, _message: OffersMessage, _context: Option<OffersContext>, _custom_tlvs: Option<Vec<u8>>, _responder: Option<Responder>) -> Option<(OffersMessage, ResponseInstruction)> {
8080
None
8181
}
8282
}
@@ -204,7 +204,7 @@ impl CustomOnionMessageHandler for TestCustomMessageHandler {
204204

205205
match responder {
206206
Some(responder) if expectation.include_reply_path => {
207-
Some((response, responder.respond_with_reply_path(MessageContext::Custom(context.unwrap_or_else(Vec::new)))))
207+
Some((response, responder.respond_with_reply_path(MessageContext::Custom(context.unwrap_or_else(Vec::new)), None)))
208208
},
209209
Some(responder) => Some((response, responder.respond())),
210210
None => None
@@ -391,7 +391,7 @@ fn one_blinded_hop() {
391391

392392
let secp_ctx = Secp256k1::new();
393393
let context = MessageContext::Custom(Vec::new());
394-
let blinded_path = BlindedMessagePath::new(&[], nodes[1].node_id, context, &*nodes[1].entropy_source, &secp_ctx).unwrap();
394+
let blinded_path = BlindedMessagePath::new(&[], nodes[1].node_id, context, Vec::new(), &*nodes[1].entropy_source, &secp_ctx).unwrap();
395395
let destination = Destination::BlindedPath(blinded_path);
396396
let instructions = MessageSendInstructions::WithoutReplyPath { destination };
397397
nodes[0].messenger.send_onion_message(test_msg, instructions).unwrap();
@@ -407,7 +407,7 @@ fn two_unblinded_two_blinded() {
407407
let secp_ctx = Secp256k1::new();
408408
let intermediate_nodes = [MessageForwardNode { node_id: nodes[3].node_id, short_channel_id: None }];
409409
let context = MessageContext::Custom(Vec::new());
410-
let blinded_path = BlindedMessagePath::new(&intermediate_nodes, nodes[4].node_id, context, &*nodes[4].entropy_source, &secp_ctx).unwrap();
410+
let blinded_path = BlindedMessagePath::new(&intermediate_nodes, nodes[4].node_id, context, Vec::new(), &*nodes[4].entropy_source, &secp_ctx).unwrap();
411411
let path = OnionMessagePath {
412412
intermediate_nodes: vec![nodes[1].node_id, nodes[2].node_id],
413413
destination: Destination::BlindedPath(blinded_path),
@@ -430,7 +430,7 @@ fn three_blinded_hops() {
430430
MessageForwardNode { node_id: nodes[2].node_id, short_channel_id: None },
431431
];
432432
let context = MessageContext::Custom(Vec::new());
433-
let blinded_path = BlindedMessagePath::new(&intermediate_nodes, nodes[3].node_id, context, &*nodes[3].entropy_source, &secp_ctx).unwrap();
433+
let blinded_path = BlindedMessagePath::new(&intermediate_nodes, nodes[3].node_id, context, Vec::new(), &*nodes[3].entropy_source, &secp_ctx).unwrap();
434434
let destination = Destination::BlindedPath(blinded_path);
435435
let instructions = MessageSendInstructions::WithoutReplyPath { destination };
436436

@@ -454,7 +454,7 @@ fn async_response_over_one_blinded_hop() {
454454
// 3. Simulate the creation of a Blinded Reply path provided by Bob.
455455
let secp_ctx = Secp256k1::new();
456456
let context = MessageContext::Custom(Vec::new());
457-
let reply_path = BlindedMessagePath::new(&[], nodes[1].node_id, context, &*nodes[1].entropy_source, &secp_ctx).unwrap();
457+
let reply_path = BlindedMessagePath::new(&[], nodes[1].node_id, context, Vec::new(), &*nodes[1].entropy_source, &secp_ctx).unwrap();
458458

459459
// 4. Create a responder using the reply path for Alice.
460460
let responder = Some(Responder::new(reply_path));
@@ -491,7 +491,7 @@ fn async_response_with_reply_path_succeeds() {
491491
// Alice receives a message from Bob with an added reply_path for responding back.
492492
let message = TestCustomMessage::Ping;
493493
let context = MessageContext::Custom(Vec::new());
494-
let reply_path = BlindedMessagePath::new(&[], bob.node_id, context, &*bob.entropy_source, &secp_ctx).unwrap();
494+
let reply_path = BlindedMessagePath::new(&[], bob.node_id, context, Vec::new(), &*bob.entropy_source, &secp_ctx).unwrap();
495495

496496
// Alice asynchronously responds to Bob, expecting a response back from him.
497497
let responder = Responder::new(reply_path);
@@ -529,7 +529,7 @@ fn async_response_with_reply_path_fails() {
529529
// Alice receives a message from Bob with an added reply_path for responding back.
530530
let message = TestCustomMessage::Ping;
531531
let context = MessageContext::Custom(Vec::new());
532-
let reply_path = BlindedMessagePath::new(&[], bob.node_id, context, &*bob.entropy_source, &secp_ctx).unwrap();
532+
let reply_path = BlindedMessagePath::new(&[], bob.node_id, context, Vec::new(), &*bob.entropy_source, &secp_ctx).unwrap();
533533

534534
// Alice tries to asynchronously respond to Bob, but fails because the nodes are unannounced and
535535
// disconnected. Thus, a reply path could no be created for the response.
@@ -575,7 +575,7 @@ fn we_are_intro_node() {
575575
MessageForwardNode { node_id: nodes[1].node_id, short_channel_id: None },
576576
];
577577
let context = MessageContext::Custom(Vec::new());
578-
let blinded_path = BlindedMessagePath::new(&intermediate_nodes, nodes[2].node_id, context, &*nodes[2].entropy_source, &secp_ctx).unwrap();
578+
let blinded_path = BlindedMessagePath::new(&intermediate_nodes, nodes[2].node_id, context, Vec::new(), &*nodes[2].entropy_source, &secp_ctx).unwrap();
579579
let destination = Destination::BlindedPath(blinded_path);
580580
let instructions = MessageSendInstructions::WithoutReplyPath { destination };
581581

@@ -586,7 +586,7 @@ fn we_are_intro_node() {
586586
// Try with a two-hop blinded path where we are the introduction node.
587587
let intermediate_nodes = [MessageForwardNode { node_id: nodes[0].node_id, short_channel_id: None }];
588588
let context = MessageContext::Custom(Vec::new());
589-
let blinded_path = BlindedMessagePath::new(&intermediate_nodes, nodes[1].node_id, context, &*nodes[1].entropy_source, &secp_ctx).unwrap();
589+
let blinded_path = BlindedMessagePath::new(&intermediate_nodes, nodes[1].node_id, context, Vec::new(), &*nodes[1].entropy_source, &secp_ctx).unwrap();
590590
let destination = Destination::BlindedPath(blinded_path);
591591
let instructions = MessageSendInstructions::WithoutReplyPath { destination };
592592

@@ -605,7 +605,7 @@ fn invalid_blinded_path_error() {
605605
let secp_ctx = Secp256k1::new();
606606
let intermediate_nodes = [MessageForwardNode { node_id: nodes[1].node_id, short_channel_id: None }];
607607
let context = MessageContext::Custom(Vec::new());
608-
let mut blinded_path = BlindedMessagePath::new(&intermediate_nodes, nodes[2].node_id, context, &*nodes[2].entropy_source, &secp_ctx).unwrap();
608+
let mut blinded_path = BlindedMessagePath::new(&intermediate_nodes, nodes[2].node_id, context, Vec::new(), &*nodes[2].entropy_source, &secp_ctx).unwrap();
609609
blinded_path.clear_blinded_hops();
610610
let destination = Destination::BlindedPath(blinded_path);
611611
let instructions = MessageSendInstructions::WithoutReplyPath { destination };
@@ -631,7 +631,7 @@ fn reply_path() {
631631
MessageForwardNode { node_id: nodes[1].node_id, short_channel_id: None },
632632
];
633633
let context = MessageContext::Custom(Vec::new());
634-
let reply_path = BlindedMessagePath::new(&intermediate_nodes, nodes[0].node_id, context, &*nodes[0].entropy_source, &secp_ctx).unwrap();
634+
let reply_path = BlindedMessagePath::new(&intermediate_nodes, nodes[0].node_id, context, Vec::new(), &*nodes[0].entropy_source, &secp_ctx).unwrap();
635635
nodes[0].messenger.send_onion_message_using_path(path, test_msg.clone(), Some(reply_path)).unwrap();
636636
nodes[3].custom_message_handler.expect_message(TestCustomMessage::Ping);
637637
pass_along_path(&nodes);
@@ -646,14 +646,14 @@ fn reply_path() {
646646
MessageForwardNode { node_id: nodes[2].node_id, short_channel_id: None },
647647
];
648648
let context = MessageContext::Custom(Vec::new());
649-
let blinded_path = BlindedMessagePath::new(&intermediate_nodes, nodes[3].node_id, context, &*nodes[3].entropy_source, &secp_ctx).unwrap();
649+
let blinded_path = BlindedMessagePath::new(&intermediate_nodes, nodes[3].node_id, context, Vec::new(), &*nodes[3].entropy_source, &secp_ctx).unwrap();
650650
let destination = Destination::BlindedPath(blinded_path);
651651
let intermediate_nodes = [
652652
MessageForwardNode { node_id: nodes[2].node_id, short_channel_id: None },
653653
MessageForwardNode { node_id: nodes[1].node_id, short_channel_id: None },
654654
];
655655
let context = MessageContext::Custom(Vec::new());
656-
let reply_path = BlindedMessagePath::new(&intermediate_nodes, nodes[0].node_id, context, &*nodes[0].entropy_source, &secp_ctx).unwrap();
656+
let reply_path = BlindedMessagePath::new(&intermediate_nodes, nodes[0].node_id, context, Vec::new(), &*nodes[0].entropy_source, &secp_ctx).unwrap();
657657
let instructions = MessageSendInstructions::WithSpecifiedReplyPath { destination, reply_path };
658658

659659
nodes[0].messenger.send_onion_message(test_msg, instructions).unwrap();
@@ -746,7 +746,7 @@ fn requests_peer_connection_for_buffered_messages() {
746746
let intermediate_nodes = [MessageForwardNode { node_id: nodes[1].node_id, short_channel_id: None }];
747747
let context = MessageContext::Custom(Vec::new());
748748
let blinded_path = BlindedMessagePath::new(
749-
&intermediate_nodes, nodes[2].node_id, context, &*nodes[0].entropy_source, &secp_ctx
749+
&intermediate_nodes, nodes[2].node_id, context, Vec::new(), &*nodes[0].entropy_source, &secp_ctx
750750
).unwrap();
751751
let destination = Destination::BlindedPath(blinded_path);
752752
let instructions = MessageSendInstructions::WithoutReplyPath { destination };
@@ -786,7 +786,7 @@ fn drops_buffered_messages_waiting_for_peer_connection() {
786786
let intermediate_nodes = [MessageForwardNode { node_id: nodes[1].node_id, short_channel_id: None }];
787787
let context = MessageContext::Custom(Vec::new());
788788
let blinded_path = BlindedMessagePath::new(
789-
&intermediate_nodes, nodes[2].node_id, context, &*nodes[0].entropy_source, &secp_ctx
789+
&intermediate_nodes, nodes[2].node_id, context, Vec::new(), &*nodes[0].entropy_source, &secp_ctx
790790
).unwrap();
791791
let destination = Destination::BlindedPath(blinded_path);
792792
let instructions = MessageSendInstructions::WithoutReplyPath { destination };
@@ -838,7 +838,7 @@ fn intercept_offline_peer_oms() {
838838
let intermediate_nodes = [MessageForwardNode { node_id: nodes[1].node_id, short_channel_id: None }];
839839
let context = MessageContext::Custom(Vec::new());
840840
let blinded_path = BlindedMessagePath::new(
841-
&intermediate_nodes, nodes[2].node_id, context, &*nodes[2].entropy_source, &secp_ctx
841+
&intermediate_nodes, nodes[2].node_id, context, Vec::new(), &*nodes[2].entropy_source, &secp_ctx
842842
).unwrap();
843843
let destination = Destination::BlindedPath(blinded_path);
844844
let instructions = MessageSendInstructions::WithoutReplyPath { destination };

0 commit comments

Comments
 (0)