Skip to content

Commit 6e7faea

Browse files
committed
rebase to main
1 parent 1fd127b commit 6e7faea

File tree

1 file changed

+60
-64
lines changed

1 file changed

+60
-64
lines changed

lightning/src/onion_message/messenger.rs

Lines changed: 60 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,63 @@ pub enum PeeledOnion<CMH: Deref> where
261261
#[derive(Debug, PartialEq, Eq)]
262262
pub struct ReceiveError { }
263263

264+
/// Create an onion message with contents `message` to the destination of `path`.
265+
/// Returns (introduction_node_id, onion_msg)
266+
pub fn create_onion_message<ES: Deref, NS: Deref, T: CustomOnionMessageContents>(
267+
entropy_source: &ES, node_signer: &NS, secp_ctx: &Secp256k1<secp256k1::All>,
268+
path: OnionMessagePath, message: OnionMessageContents<T>, reply_path: Option<BlindedPath>,
269+
) -> Result<(PublicKey, msgs::OnionMessage), SendError>
270+
where
271+
ES::Target: EntropySource,
272+
NS::Target: NodeSigner,
273+
{
274+
let OnionMessagePath { intermediate_nodes, mut destination } = path;
275+
if let Destination::BlindedPath(BlindedPath { ref blinded_hops, .. }) = destination {
276+
if blinded_hops.len() < 2 {
277+
return Err(SendError::TooFewBlindedHops);
278+
}
279+
}
280+
281+
if message.tlv_type() < 64 { return Err(SendError::InvalidMessage) }
282+
283+
// If we are sending straight to a blinded path and we are the introduction node, we need to
284+
// advance the blinded path by 1 hop so the second hop is the new introduction node.
285+
if intermediate_nodes.len() == 0 {
286+
if let Destination::BlindedPath(ref mut blinded_path) = destination {
287+
let our_node_id = node_signer.get_node_id(Recipient::Node)
288+
.map_err(|()| SendError::GetNodeIdFailed)?;
289+
if blinded_path.introduction_node_id == our_node_id {
290+
advance_path_by_one(blinded_path, node_signer, &secp_ctx)
291+
.map_err(|()| SendError::BlindedPathAdvanceFailed)?;
292+
}
293+
}
294+
}
295+
296+
let blinding_secret_bytes = entropy_source.get_secure_random_bytes();
297+
let blinding_secret = SecretKey::from_slice(&blinding_secret_bytes[..]).expect("RNG is busted");
298+
let (introduction_node_id, blinding_point) = if intermediate_nodes.len() != 0 {
299+
(intermediate_nodes[0], PublicKey::from_secret_key(&secp_ctx, &blinding_secret))
300+
} else {
301+
match destination {
302+
Destination::Node(pk) => (pk, PublicKey::from_secret_key(&secp_ctx, &blinding_secret)),
303+
Destination::BlindedPath(BlindedPath { introduction_node_id, blinding_point, .. }) =>
304+
(introduction_node_id, blinding_point),
305+
}
306+
};
307+
let (packet_payloads, packet_keys) = packet_payloads_and_keys(
308+
&secp_ctx, &intermediate_nodes, destination, message, reply_path, &blinding_secret)
309+
.map_err(|e| SendError::Secp256k1(e))?;
310+
311+
let prng_seed = entropy_source.get_secure_random_bytes();
312+
let onion_routing_packet = construct_onion_message_packet(
313+
packet_payloads, packet_keys, prng_seed).map_err(|()| SendError::TooBigPacket)?;
314+
315+
Ok((introduction_node_id, msgs::OnionMessage {
316+
blinding_point,
317+
onion_routing_packet
318+
}))
319+
}
320+
264321
impl<ES: Deref, NS: Deref, L: Deref, MR: Deref, OMH: Deref, CMH: Deref>
265322
OnionMessenger<ES, NS, L, MR, OMH, CMH>
266323
where
@@ -298,13 +355,9 @@ where
298355
&self, path: OnionMessagePath, message: OnionMessageContents<T>,
299356
reply_path: Option<BlindedPath>
300357
) -> Result<(), SendError> {
301-
let (introduction_node_id, onion_msg) = Self::create_onion_message(
302-
&self.entropy_source,
303-
&self.node_signer,
304-
&self.secp_ctx,
305-
path,
306-
message,
307-
reply_path
358+
let (introduction_node_id, onion_msg) = create_onion_message(
359+
&self.entropy_source, &self.node_signer, &self.secp_ctx,
360+
path, message, reply_path
308361
)?;
309362

310363
let mut pending_per_peer_msgs = self.pending_messages.lock().unwrap();
@@ -318,63 +371,6 @@ where
318371
}
319372
}
320373

321-
/// Create an onion message with contents `message` to the destination of `path`.
322-
/// Returns (introduction_node_id, onion_msg)
323-
pub fn create_onion_message<T: CustomOnionMessageContents>(
324-
entropy_source: &ES,
325-
node_signer: &NS,
326-
secp_ctx: &Secp256k1<secp256k1::All>,
327-
path: OnionMessagePath,
328-
message: OnionMessageContents<T>,
329-
reply_path: Option<BlindedPath>,
330-
) -> Result<(PublicKey, msgs::OnionMessage), SendError> {
331-
let OnionMessagePath { intermediate_nodes, mut destination } = path;
332-
if let Destination::BlindedPath(BlindedPath { ref blinded_hops, .. }) = destination {
333-
if blinded_hops.len() < 2 {
334-
return Err(SendError::TooFewBlindedHops);
335-
}
336-
}
337-
338-
if message.tlv_type() < 64 { return Err(SendError::InvalidMessage) }
339-
340-
// If we are sending straight to a blinded path and we are the introduction node, we need to
341-
// advance the blinded path by 1 hop so the second hop is the new introduction node.
342-
if intermediate_nodes.len() == 0 {
343-
if let Destination::BlindedPath(ref mut blinded_path) = destination {
344-
let our_node_id = node_signer.get_node_id(Recipient::Node)
345-
.map_err(|()| SendError::GetNodeIdFailed)?;
346-
if blinded_path.introduction_node_id == our_node_id {
347-
advance_path_by_one(blinded_path, node_signer, &secp_ctx)
348-
.map_err(|()| SendError::BlindedPathAdvanceFailed)?;
349-
}
350-
}
351-
}
352-
353-
let blinding_secret_bytes = entropy_source.get_secure_random_bytes();
354-
let blinding_secret = SecretKey::from_slice(&blinding_secret_bytes[..]).expect("RNG is busted");
355-
let (introduction_node_id, blinding_point) = if intermediate_nodes.len() != 0 {
356-
(intermediate_nodes[0], PublicKey::from_secret_key(&secp_ctx, &blinding_secret))
357-
} else {
358-
match destination {
359-
Destination::Node(pk) => (pk, PublicKey::from_secret_key(&secp_ctx, &blinding_secret)),
360-
Destination::BlindedPath(BlindedPath { introduction_node_id, blinding_point, .. }) =>
361-
(introduction_node_id, blinding_point),
362-
}
363-
};
364-
let (packet_payloads, packet_keys) = packet_payloads_and_keys(
365-
&secp_ctx, &intermediate_nodes, destination, message, reply_path, &blinding_secret)
366-
.map_err(|e| SendError::Secp256k1(e))?;
367-
368-
let prng_seed = entropy_source.get_secure_random_bytes();
369-
let onion_routing_packet = construct_onion_message_packet(
370-
packet_payloads, packet_keys, prng_seed).map_err(|()| SendError::TooBigPacket)?;
371-
372-
Ok((introduction_node_id, msgs::OnionMessage {
373-
blinding_point,
374-
onion_routing_packet
375-
}))
376-
}
377-
378374
/// Decode one layer of an incoming onion message
379375
/// Returns either a Forward (another onion message), or Receive (decrypted content)
380376
pub fn peel_onion<T: CustomOnionMessageContents>(

0 commit comments

Comments
 (0)