]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Make Trampoline onion deserializable.
authorArik Sosman <git@arik.io>
Thu, 18 Apr 2024 05:59:21 +0000 (22:59 -0700)
committerArik Sosman <git@arik.io>
Tue, 21 May 2024 00:32:21 +0000 (17:32 -0700)
lightning/src/ln/msgs.rs

index cbdb29bfc7c735478c89f3064f95d21d3fcf0e1a..9af5ec527fa9ae06519fe69b9bea8dc401381762 100644 (file)
@@ -55,7 +55,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};
@@ -1856,6 +1856,26 @@ impl Writeable for TrampolineOnionPacket {
        }
 }
 
+impl LengthReadable for TrampolineOnionPacket {
+       fn read<R: LengthRead>(r: &mut R) -> Result<Self, DecodeError> {
+               let version = Readable::read(r)?;
+               let public_key = Readable::read(r)?;
+
+               let hop_data_len = r.total_bytes().saturating_sub(66); // 1 (version) + 33 (pubkey) + 32 (HMAC) = 66
+               let mut rd = FixedLengthReader::new(r, hop_data_len);
+               let hop_data = WithoutLength::<Vec<u8>>::read(&mut rd)?.0;
+
+               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[..]))