Skip to content

Commit 2255665

Browse files
committed
Introduce parsing logic for DummyTlvs
1 parent 688d782 commit 2255665

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};
@@ -1150,6 +1151,44 @@ where
11501151
msg.onion_routing_packet.hmac,
11511152
(control_tlvs_ss, custom_handler.deref(), receiving_context_auth_key, logger.deref()),
11521153
);
1154+
1155+
// Constructs the next onion message using packet data and blinding logic.
1156+
let compute_onion_message = |packet_pubkey: PublicKey,
1157+
next_hop_hmac: [u8; 32],
1158+
new_packet_bytes: Vec<u8>,
1159+
blinding_point_opt: Option<PublicKey>|
1160+
-> Result<OnionMessage, ()> {
1161+
let new_pubkey =
1162+
match onion_utils::next_hop_pubkey(&secp_ctx, packet_pubkey, &onion_decode_ss) {
1163+
Ok(pk) => pk,
1164+
Err(e) => {
1165+
log_trace!(logger, "Failed to compute next hop packet pubkey: {}", e);
1166+
return Err(());
1167+
},
1168+
};
1169+
let outgoing_packet = Packet {
1170+
version: 0,
1171+
public_key: new_pubkey,
1172+
hop_data: new_packet_bytes,
1173+
hmac: next_hop_hmac,
1174+
};
1175+
let blinding_point = match blinding_point_opt {
1176+
Some(bp) => bp,
1177+
None => match onion_utils::next_hop_pubkey(
1178+
&secp_ctx,
1179+
msg.blinding_point,
1180+
control_tlvs_ss.as_ref(),
1181+
) {
1182+
Ok(bp) => bp,
1183+
Err(e) => {
1184+
log_trace!(logger, "Failed to compute next blinding point: {}", e);
1185+
return Err(());
1186+
},
1187+
},
1188+
};
1189+
Ok(OnionMessage { blinding_point, onion_routing_packet: outgoing_packet })
1190+
};
1191+
11531192
match next_hop {
11541193
Ok((
11551194
Payload::Receive {
@@ -1223,53 +1262,39 @@ where
12231262
Err(())
12241263
},
12251264
},
1265+
Ok((
1266+
Payload::Dummy {
1267+
control_tlvs_authenticated,
1268+
control_tlvs: DummyControlTlvs(DummyTlv {}),
1269+
},
1270+
Some((next_hop_hmac, new_packet_bytes)),
1271+
)) => {
1272+
if !control_tlvs_authenticated {
1273+
log_trace!(logger, "Received an unauthenticated dummy onion message");
1274+
return Err(());
1275+
}
1276+
1277+
let onion_message = compute_onion_message(
1278+
msg.onion_routing_packet.public_key,
1279+
next_hop_hmac,
1280+
new_packet_bytes,
1281+
None,
1282+
)?;
1283+
peel_onion_message(&onion_message, secp_ctx, node_signer, logger, custom_handler)
1284+
},
12261285
Ok((
12271286
Payload::Forward(ForwardControlTlvs::Unblinded(ForwardTlvs {
12281287
next_hop,
12291288
next_blinding_override,
12301289
})),
12311290
Some((next_hop_hmac, new_packet_bytes)),
12321291
)) => {
1233-
// TODO: we need to check whether `next_hop` is our node, in which case this is a dummy
1234-
// blinded hop and this onion message is destined for us. In this situation, we should keep
1235-
// unwrapping the onion layers to get to the final payload. Since we don't have the option
1236-
// of creating blinded paths with dummy hops currently, we should be ok to not handle this
1237-
// for now.
1238-
let packet_pubkey = msg.onion_routing_packet.public_key;
1239-
let new_pubkey_opt =
1240-
onion_utils::next_hop_pubkey(&secp_ctx, packet_pubkey, &onion_decode_ss);
1241-
let new_pubkey = match new_pubkey_opt {
1242-
Ok(pk) => pk,
1243-
Err(e) => {
1244-
log_trace!(logger, "Failed to compute next hop packet pubkey: {}", e);
1245-
return Err(());
1246-
},
1247-
};
1248-
let outgoing_packet = Packet {
1249-
version: 0,
1250-
public_key: new_pubkey,
1251-
hop_data: new_packet_bytes,
1252-
hmac: next_hop_hmac,
1253-
};
1254-
let onion_message = OnionMessage {
1255-
blinding_point: match next_blinding_override {
1256-
Some(blinding_point) => blinding_point,
1257-
None => {
1258-
match onion_utils::next_hop_pubkey(
1259-
&secp_ctx,
1260-
msg.blinding_point,
1261-
control_tlvs_ss.as_ref(),
1262-
) {
1263-
Ok(bp) => bp,
1264-
Err(e) => {
1265-
log_trace!(logger, "Failed to compute next blinding point: {}", e);
1266-
return Err(());
1267-
},
1268-
}
1269-
},
1270-
},
1271-
onion_routing_packet: outgoing_packet,
1272-
};
1292+
let onion_message = compute_onion_message(
1293+
msg.onion_routing_packet.public_key,
1294+
next_hop_hmac,
1295+
new_packet_bytes,
1296+
next_blinding_override,
1297+
)?;
12731298

12741299
Ok(PeeledOnion::Forward(next_hop, onion_message))
12751300
},

0 commit comments

Comments
 (0)