Skip to content

Commit

Permalink
Calculate Trampoline onion size dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
arik-so committed Dec 4, 2024
1 parent c23b18e commit 2056f92
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions lightning/src/ln/onion_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,12 +541,31 @@ pub(super) fn construct_onion_packet(
#[allow(unused)]
pub(super) fn construct_trampoline_onion_packet(
payloads: Vec<msgs::OutboundTrampolinePayload>, onion_keys: Vec<OnionKeys>,
prng_seed: [u8; 32], associated_data: &PaymentHash, length: u16,
prng_seed: [u8; 32], associated_data: &PaymentHash, length: Option<u16>,
) -> Result<msgs::TrampolineOnionPacket, ()> {
let mut packet_data = vec![0u8; length as usize];
let minimum_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();

assert!(
minimum_packet_length < ONION_DATA_LEN,
"Trampoline onion packet must be smaller than outer onion"
);

let packet_length = length.unwrap_or(minimum_packet_length as u16) as usize;
assert!(
packet_length >= minimum_packet_length,
"Packet length cannot be smaller than the payloads require."
);

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,
Expand Down

0 comments on commit 2056f92

Please sign in to comment.