Skip to content

Commit 0d2b14e

Browse files
committed
create_payment_onion returns ApiError and is used in send_payment_along_path
1 parent 0eddcbc commit 0d2b14e

File tree

4 files changed

+23
-29
lines changed

4 files changed

+23
-29
lines changed

lightning/src/ln/channelmanager.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -3420,12 +3420,10 @@ where
34203420
let prng_seed = self.entropy_source.get_secure_random_bytes();
34213421
let session_priv = SecretKey::from_slice(&session_priv_bytes[..]).expect("RNG is busted");
34223422

3423-
let onion_keys = onion_utils::construct_onion_keys(&self.secp_ctx, &path, &session_priv)
3424-
.map_err(|_| APIError::InvalidRoute{err: "Pubkey along hop was maliciously selected".to_owned()})?;
3425-
let (onion_payloads, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(path, total_value, recipient_onion, cur_height, keysend_preimage)?;
3426-
3427-
let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, prng_seed, payment_hash)
3428-
.map_err(|_| APIError::InvalidRoute { err: "Route size too large considering onion data".to_owned()})?;
3423+
let (onion_packet, htlc_msat, htlc_cltv) = onion_utils::create_payment_onion(
3424+
&self.secp_ctx, &path, &session_priv, total_value, recipient_onion, cur_height,
3425+
payment_hash, keysend_preimage, prng_seed
3426+
)?;
34293427

34303428
let err: Result<(), _> = loop {
34313429
let (counterparty_node_id, id) = match self.short_to_chan_info.read().unwrap().get(&path.hops.first().unwrap().short_channel_id) {

lightning/src/ln/msgs.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1674,20 +1674,20 @@ pub use self::fuzzy_internal_msgs::*;
16741674
#[cfg(not(fuzzing))]
16751675
pub(crate) use self::fuzzy_internal_msgs::*;
16761676

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

lightning/src/ln/onion_utils.rs

+15-18
Original file line numberDiff line numberDiff line change
@@ -936,26 +936,23 @@ pub(crate) fn decode_next_payment_hop<NS: Deref>(
936936
}
937937

938938
/// Build a payment onion, returning the first hop msat and cltv values as well.
939-
pub fn create_payment_onion<T>(
940-
secp_ctx: &Secp256k1<T>, path: &Path, session_priv: &SecretKey, total_msat: u64,
941-
recipient_onion: RecipientOnionFields, best_block_height: u32, payment_hash: PaymentHash,
942-
keysend_preimage: Option<PaymentPreimage>, prng_seed: [u8; 32]
943-
) -> Result<(u64, u32, msgs::OnionPacket), ()>
944-
where
945-
T: secp256k1::Signing
946-
{
947-
let onion_keys = construct_onion_keys(&secp_ctx, &path, &session_priv).map_err(|_| ())?;
939+
pub fn create_payment_onion<T: secp256k1::Signing>(
940+
secp_ctx: &Secp256k1<T>, path: &Path, session_priv: &SecretKey, total_msat: u64,
941+
recipient_onion: RecipientOnionFields, best_block_height: u32, payment_hash: &PaymentHash,
942+
keysend_preimage: &Option<PaymentPreimage>, prng_seed: [u8; 32]
943+
) -> Result<(msgs::OnionPacket, u64, u32), APIError> {
944+
let onion_keys = construct_onion_keys(&secp_ctx, &path, &session_priv)
945+
.map_err(|_| APIError::InvalidRoute{
946+
err: "Pubkey along hop was maliciously selected".to_owned()
947+
})?;
948948
let (onion_payloads, htlc_msat, htlc_cltv) = build_onion_payloads(
949-
&path,
950-
total_msat,
951-
recipient_onion,
952-
best_block_height + 1,
953-
&keysend_preimage,
954-
).map_err(|_| ())?;
955-
let onion_packet = construct_onion_packet(
956-
onion_payloads, onion_keys, prng_seed, &payment_hash
949+
&path, total_msat, recipient_onion, best_block_height, keysend_preimage
957950
)?;
958-
Ok((htlc_msat, htlc_cltv, onion_packet))
951+
let onion_packet = construct_onion_packet(onion_payloads, onion_keys, prng_seed, payment_hash)
952+
.map_err(|_| APIError::InvalidRoute{
953+
err: "Route size too large considering onion data".to_owned()
954+
})?;
955+
Ok((onion_packet, htlc_msat, htlc_cltv))
959956
}
960957

961958
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> {

lightning/src/onion_message/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ mod functional_tests;
2828

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

0 commit comments

Comments
 (0)