Skip to content

Commit 0eddcbc

Browse files
committed
public create_payment_onion in onion_utils
1 parent d2242f6 commit 0eddcbc

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

lightning/src/ln/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub(crate) mod onion_utils;
3939
mod outbound_payment;
4040
pub mod wire;
4141

42+
pub use onion_utils::create_payment_onion;
4243
// Older rustc (which we support) refuses to let us call the get_payment_preimage_hash!() macro
4344
// without the node parameter being mut. This is incorrect, and thus newer rustcs will complain
4445
// about an unnecessary mut. Thus, we silence the unused_mut warning in two test modules below.

lightning/src/ln/msgs.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -1674,17 +1674,21 @@ 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
16771678
#[derive(Clone)]
1678-
pub(crate) struct OnionPacket {
1679-
pub(crate) version: u8,
1679+
pub struct OnionPacket {
1680+
/// Bolt 04 version number
1681+
pub version: u8,
16801682
/// In order to ensure we always return an error on onion decode in compliance with [BOLT
16811683
/// #4](https://github.com/lightning/bolts/blob/master/04-onion-routing.md), we have to
16821684
/// deserialize `OnionPacket`s contained in [`UpdateAddHTLC`] messages even if the ephemeral
16831685
/// public key (here) is bogus, so we hold a [`Result`] instead of a [`PublicKey`] as we'd
16841686
/// like.
1685-
pub(crate) public_key: Result<PublicKey, secp256k1::Error>,
1686-
pub(crate) hop_data: [u8; 20*65],
1687-
pub(crate) hmac: [u8; 32],
1687+
pub public_key: Result<PublicKey, secp256k1::Error>,
1688+
/// 1300 bytes encrypted payload for the next hop
1689+
pub hop_data: [u8; 20*65],
1690+
/// HMAC to verify the integrity of hop_data
1691+
pub hmac: [u8; 32],
16881692
}
16891693

16901694
impl onion_utils::Packet for OnionPacket {

lightning/src/ln/onion_utils.rs

+23
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,29 @@ pub(crate) fn decode_next_payment_hop<NS: Deref>(
935935
}
936936
}
937937

938+
/// 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(|_| ())?;
948+
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
957+
)?;
958+
Ok((htlc_msat, htlc_cltv, onion_packet))
959+
}
960+
938961
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> {
939962
decode_next_hop(shared_secret, hop_data, hmac_bytes, None, read_args)
940963
}

lightning/src/onion_message/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ 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};
3132
#[cfg(not(c_bindings))]
3233
pub use self::messenger::{SimpleArcOnionMessenger, SimpleRefOnionMessenger};
3334
pub use self::offers::{OffersMessage, OffersMessageHandler};

0 commit comments

Comments
 (0)