Skip to content

Commit 3b56c36

Browse files
committed
Introduce parsing logic for DummyTlvs
1 parent f8ad4ba commit 3b56c36

File tree

1 file changed

+67
-42
lines changed

1 file changed

+67
-42
lines changed

lightning/src/onion_message/messenger.rs

Lines changed: 67 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ use super::packet::{
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};
@@ -39,6 +39,7 @@ use crate::ln::msgs::{
3939
self, BaseMessageHandler, MessageSendEvent, OnionMessage, OnionMessageHandler, SocketAddress,
4040
};
4141
use crate::ln::onion_utils;
42+
use crate::onion_message::packet::DummyControlTlvs;
4243
use crate::routing::gossip::{NetworkGraph, NodeId, ReadOnlyNetworkGraph};
4344
use crate::sign::{EntropySource, NodeSigner, ReceiveAuthKey, Recipient};
4445
use crate::types::features::{InitFeatures, NodeFeatures};
@@ -1111,6 +1112,44 @@ where
11111112
msg.onion_routing_packet.hmac,
11121113
(control_tlvs_ss, custom_handler.deref(), receiving_context_auth_key, logger.deref()),
11131114
);
1115+
1116+
// Constructs the next onion message using packet data and blinding logic.
1117+
let compute_onion_message = |packet_pubkey: PublicKey,
1118+
next_hop_hmac: [u8; 32],
1119+
new_packet_bytes: Vec<u8>,
1120+
blinding_point_opt: Option<PublicKey>|
1121+
-> Result<OnionMessage, ()> {
1122+
let new_pubkey =
1123+
match onion_utils::next_hop_pubkey(&secp_ctx, packet_pubkey, &onion_decode_ss) {
1124+
Ok(pk) => pk,
1125+
Err(e) => {
1126+
log_trace!(logger, "Failed to compute next hop packet pubkey: {}", e);
1127+
return Err(());
1128+
},
1129+
};
1130+
let outgoing_packet = Packet {
1131+
version: 0,
1132+
public_key: new_pubkey,
1133+
hop_data: new_packet_bytes,
1134+
hmac: next_hop_hmac,
1135+
};
1136+
let blinding_point = match blinding_point_opt {
1137+
Some(bp) => bp,
1138+
None => match onion_utils::next_hop_pubkey(
1139+
&secp_ctx,
1140+
msg.blinding_point,
1141+
control_tlvs_ss.as_ref(),
1142+
) {
1143+
Ok(bp) => bp,
1144+
Err(e) => {
1145+
log_trace!(logger, "Failed to compute next blinding point: {}", e);
1146+
return Err(());
1147+
},
1148+
},
1149+
};
1150+
Ok(OnionMessage { blinding_point, onion_routing_packet: outgoing_packet })
1151+
};
1152+
11141153
match next_hop {
11151154
Ok((
11161155
Payload::Receive {
@@ -1184,53 +1223,39 @@ where
11841223
Err(())
11851224
},
11861225
},
1226+
Ok((
1227+
Payload::Dummy {
1228+
control_tlvs_authenticated,
1229+
control_tlvs: DummyControlTlvs::Unblinded(DummyTlv {}),
1230+
},
1231+
Some((next_hop_hmac, new_packet_bytes)),
1232+
)) => {
1233+
if !control_tlvs_authenticated {
1234+
log_trace!(logger, "Received an unauthenticated dummy onion message");
1235+
return Err(());
1236+
}
1237+
1238+
let onion_message = compute_onion_message(
1239+
msg.onion_routing_packet.public_key,
1240+
next_hop_hmac,
1241+
new_packet_bytes,
1242+
None,
1243+
)?;
1244+
peel_onion_message(&onion_message, secp_ctx, node_signer, logger, custom_handler)
1245+
},
11871246
Ok((
11881247
Payload::Forward(ForwardControlTlvs::Unblinded(ForwardTlvs {
11891248
next_hop,
11901249
next_blinding_override,
11911250
})),
11921251
Some((next_hop_hmac, new_packet_bytes)),
11931252
)) => {
1194-
// TODO: we need to check whether `next_hop` is our node, in which case this is a dummy
1195-
// blinded hop and this onion message is destined for us. In this situation, we should keep
1196-
// unwrapping the onion layers to get to the final payload. Since we don't have the option
1197-
// of creating blinded paths with dummy hops currently, we should be ok to not handle this
1198-
// for now.
1199-
let packet_pubkey = msg.onion_routing_packet.public_key;
1200-
let new_pubkey_opt =
1201-
onion_utils::next_hop_pubkey(&secp_ctx, packet_pubkey, &onion_decode_ss);
1202-
let new_pubkey = match new_pubkey_opt {
1203-
Ok(pk) => pk,
1204-
Err(e) => {
1205-
log_trace!(logger, "Failed to compute next hop packet pubkey: {}", e);
1206-
return Err(());
1207-
},
1208-
};
1209-
let outgoing_packet = Packet {
1210-
version: 0,
1211-
public_key: new_pubkey,
1212-
hop_data: new_packet_bytes,
1213-
hmac: next_hop_hmac,
1214-
};
1215-
let onion_message = OnionMessage {
1216-
blinding_point: match next_blinding_override {
1217-
Some(blinding_point) => blinding_point,
1218-
None => {
1219-
match onion_utils::next_hop_pubkey(
1220-
&secp_ctx,
1221-
msg.blinding_point,
1222-
control_tlvs_ss.as_ref(),
1223-
) {
1224-
Ok(bp) => bp,
1225-
Err(e) => {
1226-
log_trace!(logger, "Failed to compute next blinding point: {}", e);
1227-
return Err(());
1228-
},
1229-
}
1230-
},
1231-
},
1232-
onion_routing_packet: outgoing_packet,
1233-
};
1253+
let onion_message = compute_onion_message(
1254+
msg.onion_routing_packet.public_key,
1255+
next_hop_hmac,
1256+
new_packet_bytes,
1257+
next_blinding_override,
1258+
)?;
12341259

12351260
Ok(PeeledOnion::Forward(next_hop, onion_message))
12361261
},

0 commit comments

Comments
 (0)