Skip to content

Commit c7ad6b5

Browse files
committed
change make_onion_message to static method, return whole onion
1 parent d14a605 commit c7ad6b5

File tree

3 files changed

+31
-27
lines changed

3 files changed

+31
-27
lines changed

lightning/src/ln/msgs.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,8 @@ pub struct UpdateAddHTLC {
631631
pub struct OnionMessage {
632632
/// Used in decrypting the onion packet's payload.
633633
pub blinding_point: PublicKey,
634-
pub(crate) onion_routing_packet: onion_message::Packet,
634+
/// The full onion packet including hop data, pubkey, and hmac
635+
pub onion_routing_packet: onion_message::Packet,
635636
}
636637

637638
/// An [`update_fulfill_htlc`] message to be sent to or received from a peer.

lightning/src/onion_message/messenger.rs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,14 @@ where
283283
&self, path: OnionMessagePath, message: OnionMessageContents<T>,
284284
reply_path: Option<BlindedPath>
285285
) -> Result<(), SendError> {
286-
let (introduction_node_id, onion_msg) = self.make_onion_message(path, message, reply_path)?;
286+
let (introduction_node_id, onion_msg) = Self::make_onion_message(
287+
&self.entropy_source,
288+
&self.node_signer,
289+
&self.secp_ctx,
290+
path,
291+
message,
292+
reply_path
293+
)?;
287294

288295
let mut pending_per_peer_msgs = self.pending_messages.lock().unwrap();
289296
if outbound_buffer_full(&introduction_node_id, &pending_per_peer_msgs) { return Err(SendError::BufferFull) }
@@ -296,19 +303,15 @@ where
296303
}
297304
}
298305

299-
/// Construct an onion message with contents `message` to the destination of `path`.
300-
pub fn construct_onion_message<T: CustomOnionMessageContents>(
301-
&self, path: OnionMessagePath, message: OnionMessageContents<T>,
302-
reply_path: Option<BlindedPath>
303-
) -> Result<Vec<u8>, SendError> {
304-
let (_, onion_routing_packet) = self.make_onion_message(path, message, reply_path)?;
305-
Ok(onion_routing_packet.onion_routing_packet.hop_data)
306-
}
307-
308-
// returns (introduction_node_id, onion_msg)
309-
fn make_onion_message<T: CustomOnionMessageContents>(
310-
&self, path: OnionMessagePath, message: OnionMessageContents<T>,
311-
reply_path: Option<BlindedPath>
306+
/// Make an onion message with contents `message` to the destination of `path`.
307+
/// Returns (introduction_node_id, onion_msg)
308+
pub fn make_onion_message<T: CustomOnionMessageContents>(
309+
entropy_source: &ES,
310+
node_signer: &NS,
311+
secp_ctx: &Secp256k1<secp256k1::All>,
312+
path: OnionMessagePath,
313+
message: OnionMessageContents<T>,
314+
reply_path: Option<BlindedPath>,
312315
) -> Result<(PublicKey, msgs::OnionMessage), SendError> {
313316
let OnionMessagePath { intermediate_nodes, mut destination } = path;
314317
if let Destination::BlindedPath(BlindedPath { ref blinded_hops, .. }) = destination {
@@ -323,31 +326,31 @@ where
323326
// advance the blinded path by 1 hop so the second hop is the new introduction node.
324327
if intermediate_nodes.len() == 0 {
325328
if let Destination::BlindedPath(ref mut blinded_path) = destination {
326-
let our_node_id = self.node_signer.get_node_id(Recipient::Node)
329+
let our_node_id = node_signer.get_node_id(Recipient::Node)
327330
.map_err(|()| SendError::GetNodeIdFailed)?;
328331
if blinded_path.introduction_node_id == our_node_id {
329-
advance_path_by_one(blinded_path, &self.node_signer, &self.secp_ctx)
332+
advance_path_by_one(blinded_path, node_signer, &secp_ctx)
330333
.map_err(|()| SendError::BlindedPathAdvanceFailed)?;
331334
}
332335
}
333336
}
334337

335-
let blinding_secret_bytes = self.entropy_source.get_secure_random_bytes();
338+
let blinding_secret_bytes = entropy_source.get_secure_random_bytes();
336339
let blinding_secret = SecretKey::from_slice(&blinding_secret_bytes[..]).expect("RNG is busted");
337340
let (introduction_node_id, blinding_point) = if intermediate_nodes.len() != 0 {
338-
(intermediate_nodes[0], PublicKey::from_secret_key(&self.secp_ctx, &blinding_secret))
341+
(intermediate_nodes[0], PublicKey::from_secret_key(&secp_ctx, &blinding_secret))
339342
} else {
340343
match destination {
341-
Destination::Node(pk) => (pk, PublicKey::from_secret_key(&self.secp_ctx, &blinding_secret)),
344+
Destination::Node(pk) => (pk, PublicKey::from_secret_key(&secp_ctx, &blinding_secret)),
342345
Destination::BlindedPath(BlindedPath { introduction_node_id, blinding_point, .. }) =>
343346
(introduction_node_id, blinding_point),
344347
}
345348
};
346349
let (packet_payloads, packet_keys) = packet_payloads_and_keys(
347-
&self.secp_ctx, &intermediate_nodes, destination, message, reply_path, &blinding_secret)
350+
&secp_ctx, &intermediate_nodes, destination, message, reply_path, &blinding_secret)
348351
.map_err(|e| SendError::Secp256k1(e))?;
349352

350-
let prng_seed = self.entropy_source.get_secure_random_bytes();
353+
let prng_seed = entropy_source.get_secure_random_bytes();
351354
let onion_routing_packet = construct_onion_message_packet(
352355
packet_payloads, packet_keys, prng_seed).map_err(|()| SendError::TooBigPacket)?;
353356

lightning/src/onion_message/packet.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ pub(super) const SMALL_PACKET_HOP_DATA_LEN: usize = 1300;
3333
pub(super) const BIG_PACKET_HOP_DATA_LEN: usize = 32768;
3434

3535
#[derive(Clone, Debug, PartialEq, Eq)]
36-
pub(crate) struct Packet {
37-
pub(super) version: u8,
38-
pub(super) public_key: PublicKey,
36+
pub struct Packet {
37+
pub version: u8,
38+
pub public_key: PublicKey,
3939
// Unlike the onion packets used for payments, onion message packets can have payloads greater
4040
// than 1300 bytes.
4141
// TODO: if 1300 ends up being the most common size, optimize this to be:
4242
// enum { ThirteenHundred([u8; 1300]), VarLen(Vec<u8>) }
43-
pub(super) hop_data: Vec<u8>,
44-
pub(super) hmac: [u8; 32],
43+
pub hop_data: Vec<u8>,
44+
pub hmac: [u8; 32],
4545
}
4646

4747
impl onion_utils::Packet for Packet {

0 commit comments

Comments
 (0)