From 85c7d766560d4a5641d81b29efdf31fb2594e168 Mon Sep 17 00:00:00 2001 From: Arik Sosman Date: Tue, 24 Sep 2024 11:08:58 +0900 Subject: [PATCH] Calculate Trampoline onion packet sizes dynamically. --- lightning/src/ln/onion_utils.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lightning/src/ln/onion_utils.rs b/lightning/src/ln/onion_utils.rs index 5ad68407631..0a730ef950b 100644 --- a/lightning/src/ln/onion_utils.rs +++ b/lightning/src/ln/onion_utils.rs @@ -418,12 +418,20 @@ 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, + prng_seed: [u8; 32], associated_data: &PaymentHash, ) -> Result { - let mut packet_data = vec![0u8; length as usize]; + let packet_length: usize = payloads + .iter() + .map(|p| { + let mut payload_len = LengthCalculatingWriter(0); + p.write(&mut payload_len).expect("Failed to calculate length"); + payload_len.0 + 32 + }) + .sum(); + let mut packet_data = vec![0u8; packet_length]; let mut chacha = ChaCha20::new(&prng_seed, &[0; 8]); - chacha.process(&vec![0u8; length as usize], &mut packet_data); + chacha.process(&vec![0u8; packet_length], &mut packet_data); construct_onion_packet_with_init_noise::<_, _>( payloads,