Skip to content

Commit

Permalink
Make Trampoline onion deserializable.
Browse files Browse the repository at this point in the history
  • Loading branch information
arik-so committed Apr 18, 2024
1 parent ac9a2c8 commit 9ee20b9
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion lightning/src/ln/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use crate::sign::{NodeSigner, Recipient};
#[allow(unused_imports)]
use crate::prelude::*;

use core::cmp;
use core::fmt;
use core::fmt::Debug;
use core::ops::Deref;
Expand All @@ -55,7 +56,7 @@ use crate::io_extras::read_to_end;
use crate::events::{EventsProvider, MessageSendEventsProvider};
use crate::crypto::streams::ChaChaPolyReadAdapter;
use crate::util::logger;
use crate::util::ser::{LengthReadable, LengthReadableArgs, Readable, ReadableArgs, Writeable, Writer, WithoutLength, FixedLengthReader, HighZeroBytesDroppedBigSize, Hostname, TransactionU16LenLimited, BigSize};
use crate::util::ser::{BigSize, FixedLengthReader, HighZeroBytesDroppedBigSize, Hostname, LengthRead, LengthReadable, LengthReadableArgs, Readable, ReadableArgs, TransactionU16LenLimited, WithoutLength, Writeable, Writer};
use crate::util::base32;

use crate::routing::gossip::{NodeAlias, NodeId};
Expand Down Expand Up @@ -1856,6 +1857,34 @@ impl Writeable for TrampolineOnionPacket {
}
}

impl LengthReadable for TrampolineOnionPacket {
fn read<R: LengthRead>(r: &mut R) -> Result<Self, DecodeError> {
const READ_BUFFER_SIZE: usize = 4096;

let version = Readable::read(r)?;
let public_key = Readable::read(r)?;

let mut hop_data = Vec::new();
let hop_data_len = r.total_bytes().saturating_sub(66) as usize; // 1 (version) + 33 (pubkey) + 32 (HMAC) = 66
let mut read_idx = 0;
while read_idx < hop_data_len {
let mut read_buffer = [0; READ_BUFFER_SIZE];
let read_amt = cmp::min(hop_data_len - read_idx, READ_BUFFER_SIZE);
r.read_exact(&mut read_buffer[..read_amt])?;
hop_data.extend_from_slice(&read_buffer[..read_amt]);
read_idx += read_amt;
}

let hmac = Readable::read(r)?;
Ok(TrampolineOnionPacket {
version,
public_key,
hop_data,
hmac,
})
}
}

impl Debug for TrampolineOnionPacket {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_fmt(format_args!("TrampolineOnionPacket version {} with hmac {:?}", self.version, &self.hmac[..]))
Expand Down

0 comments on commit 9ee20b9

Please sign in to comment.