diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index c4efd895796..9f86095d647 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -3420,12 +3420,10 @@ where let prng_seed = self.entropy_source.get_secure_random_bytes(); let session_priv = SecretKey::from_slice(&session_priv_bytes[..]).expect("RNG is busted"); - let onion_keys = onion_utils::construct_onion_keys(&self.secp_ctx, &path, &session_priv) - .map_err(|_| APIError::InvalidRoute{err: "Pubkey along hop was maliciously selected".to_owned()})?; - let (onion_payloads, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(path, total_value, recipient_onion, cur_height, keysend_preimage)?; - - let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, prng_seed, payment_hash) - .map_err(|_| APIError::InvalidRoute { err: "Route size too large considering onion data".to_owned()})?; + let (onion_packet, htlc_msat, htlc_cltv) = onion_utils::create_payment_onion( + &self.secp_ctx, &path, &session_priv, total_value, recipient_onion, cur_height, + payment_hash, keysend_preimage, prng_seed + )?; let err: Result<(), _> = loop { let (counterparty_node_id, id) = match self.short_to_chan_info.read().unwrap().get(&path.hops.first().unwrap().short_channel_id) { diff --git a/lightning/src/ln/msgs.rs b/lightning/src/ln/msgs.rs index fac1b68d9a3..9b2b95a42fa 100644 --- a/lightning/src/ln/msgs.rs +++ b/lightning/src/ln/msgs.rs @@ -1674,10 +1674,10 @@ 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 +/// Bolt 4 onion packet including hop data for the next peer. #[derive(Clone)] pub struct OnionPacket { - /// Bolt 04 version number + /// Bolt 4 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 @@ -1685,9 +1685,9 @@ pub struct OnionPacket { /// public key (here) is bogus, so we hold a [`Result`] instead of a [`PublicKey`] as we'd /// like. pub public_key: Result, - /// 1300 bytes encrypted payload for the next hop + /// 1300 bytes encrypted payload for the next hop. pub hop_data: [u8; 20*65], - /// HMAC to verify the integrity of hop_data + /// HMAC to verify the integrity of hop_data. pub hmac: [u8; 32], } diff --git a/lightning/src/ln/onion_utils.rs b/lightning/src/ln/onion_utils.rs index e098a541a51..e03cfab6656 100644 --- a/lightning/src/ln/onion_utils.rs +++ b/lightning/src/ln/onion_utils.rs @@ -936,26 +936,23 @@ pub(crate) fn decode_next_payment_hop( } /// Build a payment onion, returning the first hop msat and cltv values as well. -pub fn create_payment_onion( - secp_ctx: &Secp256k1, path: &Path, session_priv: &SecretKey, total_msat: u64, - recipient_onion: RecipientOnionFields, best_block_height: u32, payment_hash: PaymentHash, - keysend_preimage: Option, 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(|_| ())?; +pub fn create_payment_onion( + secp_ctx: &Secp256k1, path: &Path, session_priv: &SecretKey, total_msat: u64, + recipient_onion: RecipientOnionFields, best_block_height: u32, payment_hash: &PaymentHash, + keysend_preimage: &Option, prng_seed: [u8; 32] +) -> Result<(msgs::OnionPacket, u64, u32), APIError> { + let onion_keys = construct_onion_keys(&secp_ctx, &path, &session_priv) + .map_err(|_| APIError::InvalidRoute{ + err: "Pubkey along hop was maliciously selected".to_owned() + })?; 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 + &path, total_msat, recipient_onion, best_block_height, keysend_preimage )?; - Ok((htlc_msat, htlc_cltv, onion_packet)) + let onion_packet = construct_onion_packet(onion_payloads, onion_keys, prng_seed, payment_hash) + .map_err(|_| APIError::InvalidRoute{ + err: "Route size too large considering onion data".to_owned() + })?; + Ok((onion_packet, htlc_msat, htlc_cltv)) } pub(crate) fn decode_next_untagged_hop, N: NextPacketBytes>(shared_secret: [u8; 32], hop_data: &[u8], hmac_bytes: [u8; 32], read_args: T) -> Result<(R, Option<([u8; 32], N)>), OnionDecodeErr> { diff --git a/lightning/src/onion_message/mod.rs b/lightning/src/onion_message/mod.rs index d106a542fd3..ff6e0cd8e5d 100644 --- a/lightning/src/onion_message/mod.rs +++ b/lightning/src/onion_message/mod.rs @@ -28,7 +28,6 @@ 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};