Skip to content

Drop support for creating BOLT 4 Legacy onion format payloads #1413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions fuzz/src/full_stack.rs

Large diffs are not rendered by default.

8 changes: 0 additions & 8 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2103,13 +2103,6 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
}

let routing = match hop_data.format {
msgs::OnionHopDataFormat::Legacy { .. } => {
return Err(ReceiveError {
err_code: 0x4000|0x2000|3,
err_data: Vec::new(),
msg: "We require payment_secrets",
});
},
msgs::OnionHopDataFormat::NonFinalNode { .. } => {
return Err(ReceiveError {
err_code: 0x4000|22,
Expand Down Expand Up @@ -2244,7 +2237,6 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
};

let short_channel_id = match next_hop_data.format {
msgs::OnionHopDataFormat::Legacy { short_channel_id } => short_channel_id,
msgs::OnionHopDataFormat::NonFinalNode { short_channel_id } => short_channel_id,
msgs::OnionHopDataFormat::FinalNode { .. } => {
return_err!("Final Node OnionHopData provided for us as an intermediary node", 0x4000 | 22, &[0;0]);
Expand Down
103 changes: 32 additions & 71 deletions lightning/src/ln/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use crate::io_extras::read_to_end;

use crate::util::events::{MessageSendEventsProvider, OnionMessageProvider};
use crate::util::logger;
use crate::util::ser::{BigSize, LengthReadable, Readable, ReadableArgs, Writeable, Writer, FixedLengthReader, HighZeroBytesDroppedBigSize, Hostname};
use crate::util::ser::{LengthReadable, Readable, ReadableArgs, Writeable, Writer, FixedLengthReader, HighZeroBytesDroppedBigSize, Hostname};

use crate::ln::{PaymentPreimage, PaymentHash, PaymentSecret};

Expand Down Expand Up @@ -1028,9 +1028,6 @@ mod fuzzy_internal_msgs {
}

pub(crate) enum OnionHopDataFormat {
Legacy { // aka Realm-0
short_channel_id: u64,
},
NonFinalNode {
short_channel_id: u64,
},
Expand All @@ -1046,7 +1043,6 @@ mod fuzzy_internal_msgs {
/// Message serialization may panic if this value is more than 21 million Bitcoin.
pub(crate) amt_to_forward: u64,
pub(crate) outgoing_cltv_value: u32,
// 12 bytes of 0-padding for Legacy format
}

pub struct DecodedOnionErrorPacket {
Expand Down Expand Up @@ -1459,13 +1455,6 @@ impl Readable for FinalOnionHopData {
impl Writeable for OnionHopData {
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
match self.format {
OnionHopDataFormat::Legacy { short_channel_id } => {
0u8.write(w)?;
short_channel_id.write(w)?;
self.amt_to_forward.write(w)?;
self.outgoing_cltv_value.write(w)?;
w.write_all(&[0;12])?;
},
OnionHopDataFormat::NonFinalNode { short_channel_id } => {
encode_varint_length_prefixed_tlv!(w, {
(2, HighZeroBytesDroppedBigSize(self.amt_to_forward), required),
Expand All @@ -1488,58 +1477,44 @@ impl Writeable for OnionHopData {

impl Readable for OnionHopData {
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
let b: BigSize = Readable::read(r)?;
const LEGACY_ONION_HOP_FLAG: u64 = 0;
let (format, amt, cltv_value) = if b.0 != LEGACY_ONION_HOP_FLAG {
let mut rd = FixedLengthReader::new(r, b.0);
let mut amt = HighZeroBytesDroppedBigSize(0u64);
let mut cltv_value = HighZeroBytesDroppedBigSize(0u32);
let mut short_id: Option<u64> = None;
let mut payment_data: Option<FinalOnionHopData> = None;
let mut keysend_preimage: Option<PaymentPreimage> = None;
decode_tlv_stream!(&mut rd, {
(2, amt, required),
(4, cltv_value, required),
(6, short_id, option),
(8, payment_data, option),
// See https://github.com/lightning/blips/blob/master/blip-0003.md
(5482373484, keysend_preimage, option)
});
rd.eat_remaining().map_err(|_| DecodeError::ShortRead)?;
let format = if let Some(short_channel_id) = short_id {
if payment_data.is_some() { return Err(DecodeError::InvalidValue); }
OnionHopDataFormat::NonFinalNode {
short_channel_id,
}
} else {
if let &Some(ref data) = &payment_data {
if data.total_msat > MAX_VALUE_MSAT {
return Err(DecodeError::InvalidValue);
}
}
OnionHopDataFormat::FinalNode {
payment_data,
keysend_preimage,
}
};
(format, amt.0, cltv_value.0)
let mut amt = HighZeroBytesDroppedBigSize(0u64);
let mut cltv_value = HighZeroBytesDroppedBigSize(0u32);
let mut short_id: Option<u64> = None;
let mut payment_data: Option<FinalOnionHopData> = None;
let mut keysend_preimage: Option<PaymentPreimage> = None;
read_tlv_fields!(r, {
(2, amt, required),
(4, cltv_value, required),
(6, short_id, option),
(8, payment_data, option),
// See https://github.com/lightning/blips/blob/master/blip-0003.md
(5482373484, keysend_preimage, option)
});

let format = if let Some(short_channel_id) = short_id {
if payment_data.is_some() { return Err(DecodeError::InvalidValue); }
OnionHopDataFormat::NonFinalNode {
short_channel_id,
}
} else {
let format = OnionHopDataFormat::Legacy {
short_channel_id: Readable::read(r)?,
};
let amt: u64 = Readable::read(r)?;
let cltv_value: u32 = Readable::read(r)?;
r.read_exact(&mut [0; 12])?;
(format, amt, cltv_value)
if let &Some(ref data) = &payment_data {
if data.total_msat > MAX_VALUE_MSAT {
return Err(DecodeError::InvalidValue);
}
}
OnionHopDataFormat::FinalNode {
payment_data,
keysend_preimage,
}
};

if amt > MAX_VALUE_MSAT {
if amt.0 > MAX_VALUE_MSAT {
return Err(DecodeError::InvalidValue);
}
Ok(OnionHopData {
format,
amt_to_forward: amt,
outgoing_cltv_value: cltv_value,
amt_to_forward: amt.0,
outgoing_cltv_value: cltv_value.0,
})
}
}
Expand Down Expand Up @@ -2669,20 +2644,6 @@ mod tests {
assert_eq!(encoded_value, target_value);
}

#[test]
fn encoding_legacy_onion_hop_data() {
let msg = msgs::OnionHopData {
format: OnionHopDataFormat::Legacy {
short_channel_id: 0xdeadbeef1bad1dea,
},
amt_to_forward: 0x0badf00d01020304,
outgoing_cltv_value: 0xffffffff,
};
let encoded_value = msg.encode();
let target_value = hex::decode("00deadbeef1bad1dea0badf00d01020304ffffffff000000000000000000000000").unwrap();
assert_eq!(encoded_value, target_value);
}

#[test]
fn encoding_nonfinal_onion_hop_data() {
let mut msg = msgs::OnionHopData {
Expand Down
Loading