Skip to content

Commit

Permalink
public create_payment_onion in onion_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Evanfeenstra committed Nov 2, 2023
1 parent d2242f6 commit 0eddcbc
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
1 change: 1 addition & 0 deletions lightning/src/ln/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub(crate) mod onion_utils;
mod outbound_payment;
pub mod wire;

pub use onion_utils::create_payment_onion;
// Older rustc (which we support) refuses to let us call the get_payment_preimage_hash!() macro
// without the node parameter being mut. This is incorrect, and thus newer rustcs will complain
// about an unnecessary mut. Thus, we silence the unused_mut warning in two test modules below.
Expand Down
14 changes: 9 additions & 5 deletions lightning/src/ln/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1674,17 +1674,21 @@ pub use self::fuzzy_internal_msgs::*;
#[cfg(not(fuzzing))]
pub(crate) use self::fuzzy_internal_msgs::*;

/// Bolt04 OnionPacket including hop data for the next peer
#[derive(Clone)]
pub(crate) struct OnionPacket {
pub(crate) version: u8,
pub struct OnionPacket {
/// Bolt 04 version number
pub version: u8,
/// In order to ensure we always return an error on onion decode in compliance with [BOLT
/// #4](https://github.com/lightning/bolts/blob/master/04-onion-routing.md), we have to
/// deserialize `OnionPacket`s contained in [`UpdateAddHTLC`] messages even if the ephemeral
/// public key (here) is bogus, so we hold a [`Result`] instead of a [`PublicKey`] as we'd
/// like.
pub(crate) public_key: Result<PublicKey, secp256k1::Error>,
pub(crate) hop_data: [u8; 20*65],
pub(crate) hmac: [u8; 32],
pub public_key: Result<PublicKey, secp256k1::Error>,
/// 1300 bytes encrypted payload for the next hop
pub hop_data: [u8; 20*65],
/// HMAC to verify the integrity of hop_data
pub hmac: [u8; 32],
}

impl onion_utils::Packet for OnionPacket {
Expand Down
23 changes: 23 additions & 0 deletions lightning/src/ln/onion_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,29 @@ pub(crate) fn decode_next_payment_hop<NS: Deref>(
}
}

/// Build a payment onion, returning the first hop msat and cltv values as well.
pub fn create_payment_onion<T>(
secp_ctx: &Secp256k1<T>, path: &Path, session_priv: &SecretKey, total_msat: u64,
recipient_onion: RecipientOnionFields, best_block_height: u32, payment_hash: PaymentHash,
keysend_preimage: Option<PaymentPreimage>, prng_seed: [u8; 32]
) -> Result<(u64, u32, msgs::OnionPacket), ()>
where
T: secp256k1::Signing
{
let onion_keys = construct_onion_keys(&secp_ctx, &path, &session_priv).map_err(|_| ())?;
let (onion_payloads, htlc_msat, htlc_cltv) = build_onion_payloads(
&path,
total_msat,
recipient_onion,
best_block_height + 1,
&keysend_preimage,
).map_err(|_| ())?;
let onion_packet = construct_onion_packet(
onion_payloads, onion_keys, prng_seed, &payment_hash
)?;
Ok((htlc_msat, htlc_cltv, onion_packet))
}

pub(crate) fn decode_next_untagged_hop<T, R: ReadableArgs<T>, N: NextPacketBytes>(shared_secret: [u8; 32], hop_data: &[u8], hmac_bytes: [u8; 32], read_args: T) -> Result<(R, Option<([u8; 32], N)>), OnionDecodeErr> {
decode_next_hop(shared_secret, hop_data, hmac_bytes, None, read_args)
}
Expand Down
1 change: 1 addition & 0 deletions lightning/src/onion_message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ mod functional_tests;

// Re-export structs so they can be imported with just the `onion_message::` module prefix.
pub use self::messenger::{CustomOnionMessageHandler, DefaultMessageRouter, Destination, MessageRouter, OnionMessageContents, OnionMessagePath, OnionMessenger, PeeledOnion, PendingOnionMessage, SendError};
pub use self::messenger::{create_onion_message, peel_onion_message};
#[cfg(not(c_bindings))]
pub use self::messenger::{SimpleArcOnionMessenger, SimpleRefOnionMessenger};
pub use self::offers::{OffersMessage, OffersMessageHandler};
Expand Down

0 comments on commit 0eddcbc

Please sign in to comment.