Skip to content

Commit b8d62fc

Browse files
committed
Introduce parsing logic for DummyTlvs
1 parent 4f5b367 commit b8d62fc

File tree

1 file changed

+61
-40
lines changed

1 file changed

+61
-40
lines changed

lightning/src/onion_message/messenger.rs

Lines changed: 61 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,44 @@ where
11441144
msg.onion_routing_packet.hmac,
11451145
(control_tlvs_ss, custom_handler.deref(), receiving_context_auth_key, logger.deref()),
11461146
);
1147+
1148+
// Constructs the next onion message using packet data and blinding logic.
1149+
let build_outbound_onion_message = |packet_pubkey: PublicKey,
1150+
next_hop_hmac: [u8; 32],
1151+
new_packet_bytes: Vec<u8>,
1152+
blinding_point_opt: Option<PublicKey>|
1153+
-> Result<OnionMessage, ()> {
1154+
let new_pubkey =
1155+
match onion_utils::next_hop_pubkey(&secp_ctx, packet_pubkey, &onion_decode_ss) {
1156+
Ok(pk) => pk,
1157+
Err(e) => {
1158+
log_trace!(logger, "Failed to compute next hop packet pubkey: {}", e);
1159+
return Err(());
1160+
},
1161+
};
1162+
let outgoing_packet = Packet {
1163+
version: 0,
1164+
public_key: new_pubkey,
1165+
hop_data: new_packet_bytes,
1166+
hmac: next_hop_hmac,
1167+
};
1168+
let blinding_point = match blinding_point_opt {
1169+
Some(bp) => bp,
1170+
None => match onion_utils::next_hop_pubkey(
1171+
&secp_ctx,
1172+
msg.blinding_point,
1173+
control_tlvs_ss.as_ref(),
1174+
) {
1175+
Ok(bp) => bp,
1176+
Err(e) => {
1177+
log_trace!(logger, "Failed to compute next blinding point: {}", e);
1178+
return Err(());
1179+
},
1180+
},
1181+
};
1182+
Ok(OnionMessage { blinding_point, onion_routing_packet: outgoing_packet })
1183+
};
1184+
11471185
match next_hop {
11481186
Ok((
11491187
Payload::Receive {
@@ -1216,53 +1254,36 @@ where
12161254
Err(())
12171255
},
12181256
},
1257+
Ok((
1258+
Payload::Dummy { control_tlvs_authenticated },
1259+
Some((next_hop_hmac, new_packet_bytes)),
1260+
)) => {
1261+
if !control_tlvs_authenticated {
1262+
log_trace!(logger, "Received an unauthenticated dummy onion message");
1263+
return Err(());
1264+
}
1265+
1266+
let onion_message = build_outbound_onion_message(
1267+
msg.onion_routing_packet.public_key,
1268+
next_hop_hmac,
1269+
new_packet_bytes,
1270+
None,
1271+
)?;
1272+
peel_onion_message(&onion_message, secp_ctx, node_signer, logger, custom_handler)
1273+
},
12191274
Ok((
12201275
Payload::Forward(ForwardControlTlvs::Unblinded(ForwardTlvs {
12211276
next_hop,
12221277
next_blinding_override,
12231278
})),
12241279
Some((next_hop_hmac, new_packet_bytes)),
12251280
)) => {
1226-
// TODO: we need to check whether `next_hop` is our node, in which case this is a dummy
1227-
// blinded hop and this onion message is destined for us. In this situation, we should keep
1228-
// unwrapping the onion layers to get to the final payload. Since we don't have the option
1229-
// of creating blinded paths with dummy hops currently, we should be ok to not handle this
1230-
// for now.
1231-
let packet_pubkey = msg.onion_routing_packet.public_key;
1232-
let new_pubkey_opt =
1233-
onion_utils::next_hop_pubkey(&secp_ctx, packet_pubkey, &onion_decode_ss);
1234-
let new_pubkey = match new_pubkey_opt {
1235-
Ok(pk) => pk,
1236-
Err(e) => {
1237-
log_trace!(logger, "Failed to compute next hop packet pubkey: {}", e);
1238-
return Err(());
1239-
},
1240-
};
1241-
let outgoing_packet = Packet {
1242-
version: 0,
1243-
public_key: new_pubkey,
1244-
hop_data: new_packet_bytes,
1245-
hmac: next_hop_hmac,
1246-
};
1247-
let onion_message = OnionMessage {
1248-
blinding_point: match next_blinding_override {
1249-
Some(blinding_point) => blinding_point,
1250-
None => {
1251-
match onion_utils::next_hop_pubkey(
1252-
&secp_ctx,
1253-
msg.blinding_point,
1254-
control_tlvs_ss.as_ref(),
1255-
) {
1256-
Ok(bp) => bp,
1257-
Err(e) => {
1258-
log_trace!(logger, "Failed to compute next blinding point: {}", e);
1259-
return Err(());
1260-
},
1261-
}
1262-
},
1263-
},
1264-
onion_routing_packet: outgoing_packet,
1265-
};
1281+
let onion_message = build_outbound_onion_message(
1282+
msg.onion_routing_packet.public_key,
1283+
next_hop_hmac,
1284+
new_packet_bytes,
1285+
next_blinding_override,
1286+
)?;
12661287

12671288
Ok(PeeledOnion::Forward(next_hop, onion_message))
12681289
},

0 commit comments

Comments
 (0)