Skip to content

Commit a7bdfcf

Browse files
committed
Use variable-length onions for Trampoline.
1 parent ddf5c9d commit a7bdfcf

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

Diff for: lightning/src/ln/msgs.rs

+41-2
Original file line numberDiff line numberDiff line change
@@ -1670,7 +1670,7 @@ mod fuzzy_internal_msgs {
16701670
use crate::prelude::*;
16711671
use crate::ln::{PaymentPreimage, PaymentSecret};
16721672
use crate::ln::features::BlindedHopFeatures;
1673-
use crate::ln::msgs::OnionPacket;
1673+
use crate::ln::msgs::VariableLengthOnionPacket;
16741674

16751675
// These types aren't intended to be pub, but are exposed for direct fuzzing (as we deserialize
16761676
// them from untrusted input):
@@ -1728,7 +1728,7 @@ mod fuzzy_internal_msgs {
17281728
custom_tlvs: Vec<(u64, Vec<u8>)>,
17291729
amt_msat: u64,
17301730
outgoing_cltv_value: u32,
1731-
trampoline_packet: Option<OnionPacket>
1731+
trampoline_packet: Option<VariableLengthOnionPacket>
17321732
},
17331733
BlindedForward {
17341734
encrypted_tlvs: Vec<u8>,
@@ -1789,6 +1789,29 @@ impl fmt::Debug for OnionPacket {
17891789
}
17901790
}
17911791

1792+
/// BOLT 4 onion packet including hop data for the next peer.
1793+
#[derive(Clone, Hash, PartialEq, Eq)]
1794+
pub struct VariableLengthOnionPacket {
1795+
/// BOLT 4 version number.
1796+
pub version: u8,
1797+
/// In order to ensure we always return an error on onion decode in compliance with [BOLT
1798+
/// #4](https://github.com/lightning/bolts/blob/master/04-onion-routing.md), we have to
1799+
/// deserialize `OnionPacket`s contained in [`UpdateAddHTLC`] messages even if the ephemeral
1800+
/// public key (here) is bogus, so we hold a [`Result`] instead of a [`PublicKey`] as we'd
1801+
/// like.
1802+
pub public_key: Result<PublicKey, secp256k1::Error>,
1803+
/// Variable number of bytes encrypted payload for the next hop; 650 by default for Trampoline
1804+
pub(crate) hop_data: Vec<u8>,
1805+
/// HMAC to verify the integrity of hop_data.
1806+
pub hmac: [u8; 32],
1807+
}
1808+
1809+
impl fmt::Debug for VariableLengthOnionPacket {
1810+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1811+
f.write_fmt(format_args!("VariableLengthOnionPacket version {} with hmac {:?}", self.version, &self.hmac[..]))
1812+
}
1813+
}
1814+
17921815
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
17931816
pub(crate) struct OnionErrorPacket {
17941817
// This really should be a constant size slice, but the spec lets these things be up to 128KB?
@@ -2217,6 +2240,22 @@ impl Readable for OnionPacket {
22172240
}
22182241
}
22192242

2243+
// This will be written as the value of a TLV, so when reading, the length of the hop data will be
2244+
// inferred from a ReadableArg containing the total length. The standard hop data for Trampoline
2245+
// onions will prospectively be 650 bytes.
2246+
impl Writeable for VariableLengthOnionPacket {
2247+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
2248+
self.version.write(w)?;
2249+
match self.public_key {
2250+
Ok(pubkey) => pubkey.write(w)?,
2251+
Err(_) => [0u8;33].write(w)?,
2252+
}
2253+
&self.hop_data.write(w)?;
2254+
&self.hmac.write(w)?;
2255+
Ok(())
2256+
}
2257+
}
2258+
22202259
impl_writeable_msg!(UpdateAddHTLC, {
22212260
channel_id,
22222261
htlc_id,

0 commit comments

Comments
 (0)