Skip to content

Commit a10ed0e

Browse files
committed
Introduce parsing logic for DummyTlvs
1 parent 71918d0 commit a10ed0e

File tree

1 file changed

+63
-44
lines changed

1 file changed

+63
-44
lines changed

lightning/src/onion_message/messenger.rs

Lines changed: 63 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,22 @@ use super::async_payments::AsyncPaymentsMessage;
2020
use super::async_payments::AsyncPaymentsMessageHandler;
2121
use super::dns_resolution::{DNSResolverMessage, DNSResolverMessageHandler};
2222
use super::offers::{OffersMessage, OffersMessageHandler};
23-
use super::packet::OnionMessageContents;
2423
use super::packet::ParsedOnionMessageContents;
24+
use super::packet::{DummyControlTlvs, OnionMessageContents};
2525
use super::packet::{
2626
ForwardControlTlvs, Packet, Payload, ReceiveControlTlvs, BIG_PACKET_HOP_DATA_LEN,
2727
SMALL_PACKET_HOP_DATA_LEN,
2828
};
2929
#[cfg(async_payments)]
3030
use crate::blinded_path::message::AsyncPaymentsContext;
3131
use crate::blinded_path::message::{
32-
BlindedMessagePath, DNSResolverContext, ForwardTlvs, MessageContext, MessageForwardNode,
33-
NextMessageHop, OffersContext, ReceiveTlvs,
32+
BlindedMessagePath, DNSResolverContext, DummyTlv, ForwardTlvs, MessageContext,
33+
MessageForwardNode, NextMessageHop, OffersContext, ReceiveTlvs,
3434
};
3535
use crate::blinded_path::utils;
3636
use crate::blinded_path::{IntroductionNode, NodeIdLookUp};
3737
use crate::events::{Event, EventHandler, EventsProvider, ReplayEvent};
38+
use crate::ln::channelmanager::Verification;
3839
use crate::ln::msgs::{
3940
self, BaseMessageHandler, MessageSendEvent, OnionMessage, OnionMessageHandler, SocketAddress,
4041
};
@@ -1074,6 +1075,44 @@ where
10741075
msg.onion_routing_packet.hmac,
10751076
(control_tlvs_ss, custom_handler.deref(), logger.deref()),
10761077
);
1078+
1079+
// Constructs the next onion message using packet data and blinding logic.
1080+
let compute_onion_message = |packet_pubkey: PublicKey,
1081+
next_hop_hmac: [u8; 32],
1082+
new_packet_bytes: Vec<u8>,
1083+
blinding_point_opt: Option<PublicKey>|
1084+
-> Result<OnionMessage, ()> {
1085+
let new_pubkey =
1086+
match onion_utils::next_hop_pubkey(&secp_ctx, packet_pubkey, &onion_decode_ss) {
1087+
Ok(pk) => pk,
1088+
Err(e) => {
1089+
log_trace!(logger, "Failed to compute next hop packet pubkey: {}", e);
1090+
return Err(());
1091+
},
1092+
};
1093+
let outgoing_packet = Packet {
1094+
version: 0,
1095+
public_key: new_pubkey,
1096+
hop_data: new_packet_bytes,
1097+
hmac: next_hop_hmac,
1098+
};
1099+
let blinding_point = match blinding_point_opt {
1100+
Some(bp) => bp,
1101+
None => match onion_utils::next_hop_pubkey(
1102+
&secp_ctx,
1103+
msg.blinding_point,
1104+
control_tlvs_ss.as_ref(),
1105+
) {
1106+
Ok(bp) => bp,
1107+
Err(e) => {
1108+
log_trace!(logger, "Failed to compute next blinding point: {}", e);
1109+
return Err(());
1110+
},
1111+
},
1112+
};
1113+
Ok(OnionMessage { blinding_point, onion_routing_packet: outgoing_packet })
1114+
};
1115+
10771116
match next_hop {
10781117
Ok((
10791118
Payload::Receive {
@@ -1115,54 +1154,34 @@ where
11151154
Err(())
11161155
},
11171156
},
1157+
Ok((
1158+
Payload::Dummy(DummyControlTlvs::Unblinded(DummyTlv { dummy_tlv, authentication })),
1159+
Some((next_hop_hmac, new_packet_bytes)),
1160+
)) => {
1161+
let expanded_key = node_signer.get_expanded_key();
1162+
dummy_tlv.verify_data(authentication.0, authentication.1, &expanded_key)?;
1163+
1164+
let onion_message = compute_onion_message(
1165+
msg.onion_routing_packet.public_key,
1166+
next_hop_hmac,
1167+
new_packet_bytes,
1168+
None,
1169+
)?;
1170+
peel_onion_message(&onion_message, secp_ctx, node_signer, logger, custom_handler)
1171+
},
11181172
Ok((
11191173
Payload::Forward(ForwardControlTlvs::Unblinded(ForwardTlvs {
11201174
next_hop,
11211175
next_blinding_override,
11221176
})),
11231177
Some((next_hop_hmac, new_packet_bytes)),
11241178
)) => {
1125-
// TODO: we need to check whether `next_hop` is our node, in which case this is a dummy
1126-
// blinded hop and this onion message is destined for us. In this situation, we should keep
1127-
// unwrapping the onion layers to get to the final payload. Since we don't have the option
1128-
// of creating blinded paths with dummy hops currently, we should be ok to not handle this
1129-
// for now.
1130-
let packet_pubkey = msg.onion_routing_packet.public_key;
1131-
let new_pubkey_opt =
1132-
onion_utils::next_hop_pubkey(&secp_ctx, packet_pubkey, &onion_decode_ss);
1133-
let new_pubkey = match new_pubkey_opt {
1134-
Ok(pk) => pk,
1135-
Err(e) => {
1136-
log_trace!(logger, "Failed to compute next hop packet pubkey: {}", e);
1137-
return Err(());
1138-
},
1139-
};
1140-
let outgoing_packet = Packet {
1141-
version: 0,
1142-
public_key: new_pubkey,
1143-
hop_data: new_packet_bytes,
1144-
hmac: next_hop_hmac,
1145-
};
1146-
let onion_message = OnionMessage {
1147-
blinding_point: match next_blinding_override {
1148-
Some(blinding_point) => blinding_point,
1149-
None => {
1150-
match onion_utils::next_hop_pubkey(
1151-
&secp_ctx,
1152-
msg.blinding_point,
1153-
control_tlvs_ss.as_ref(),
1154-
) {
1155-
Ok(bp) => bp,
1156-
Err(e) => {
1157-
log_trace!(logger, "Failed to compute next blinding point: {}", e);
1158-
return Err(());
1159-
},
1160-
}
1161-
},
1162-
},
1163-
onion_routing_packet: outgoing_packet,
1164-
};
1165-
1179+
let onion_message = compute_onion_message(
1180+
msg.onion_routing_packet.public_key,
1181+
next_hop_hmac,
1182+
new_packet_bytes,
1183+
next_blinding_override,
1184+
)?;
11661185
Ok(PeeledOnion::Forward(next_hop, onion_message))
11671186
},
11681187
Err(e) => {

0 commit comments

Comments
 (0)