From e7e1afac2961a4313e3c7ba027eb8f8ad8e16d65 Mon Sep 17 00:00:00 2001 From: Arik Sosman Date: Tue, 26 Mar 2024 23:48:05 -0700 Subject: [PATCH] Trampoline onion construction. --- lightning/src/ln/msgs.rs | 27 +++++++++++++++++++++++++++ lightning/src/ln/onion_utils.rs | 18 ++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/lightning/src/ln/msgs.rs b/lightning/src/ln/msgs.rs index e454be89b..19c92e574 100644 --- a/lightning/src/ln/msgs.rs +++ b/lightning/src/ln/msgs.rs @@ -1740,6 +1740,17 @@ mod fuzzy_internal_msgs { } } + pub(crate) enum OutboundTrampolinePayload { + #[allow(unused)] + Forward { + /// The value, in msat, of the payment after this hop's fee is deducted. + amt_to_forward: u64, + outgoing_cltv_value: u32, + /// The node id to which the trampoline node must find a route + outgoing_node_id: PublicKey, + } + } + pub struct DecodedOnionErrorPacket { pub(crate) hmac: [u8; 32], pub(crate) failuremsg: Vec, @@ -2597,6 +2608,22 @@ impl Writeable for OutboundOnionPayload { } } +impl Writeable for OutboundTrampolinePayload { + fn write(&self, w: &mut W) -> Result<(), io::Error> { + match self { + Self::Forward { amt_to_forward, outgoing_cltv_value, outgoing_node_id } => { + _encode_varint_length_prefixed_tlv!(w, { + (2, HighZeroBytesDroppedBigSize(*amt_to_forward), required), + (4, HighZeroBytesDroppedBigSize(*outgoing_cltv_value), required), + (14, outgoing_node_id, required) + }); + } + } + Ok(()) + } +} + + impl ReadableArgs<(Option, &NS)> for InboundOnionPayload where NS::Target: NodeSigner { fn read(r: &mut R, args: (Option, &NS)) -> Result { let (update_add_blinding_point, node_signer) = args; diff --git a/lightning/src/ln/onion_utils.rs b/lightning/src/ln/onion_utils.rs index 53ef0729a..74de4a923 100644 --- a/lightning/src/ln/onion_utils.rs +++ b/lightning/src/ln/onion_utils.rs @@ -291,6 +291,24 @@ pub(super) fn construct_onion_packet( ) } +#[allow(unused)] +pub(super) fn construct_trampoline_onion_packet( + payloads: Vec, onion_keys: Vec, + prng_seed: [u8; 32], associated_data: &PaymentHash, length: u16, +) -> Result { + let mut packet_data = vec![0u8; length as usize]; + + let mut chacha = ChaCha20::new(&prng_seed, &[0; 8]); + chacha.process(&vec![0u8; length as usize], &mut packet_data); + + construct_onion_packet_with_init_noise::<_, _>( + payloads, + onion_keys, + packet_data, + Some(associated_data), + ) +} + #[cfg(test)] /// Used in testing to write bogus `BogusOnionHopData` as well as `RawOnionHopData`, which is /// otherwise not representable in `msgs::OnionHopData`. -- 2.39.5