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,