Skip to content

Commit 593d8c4

Browse files
authored
Merge pull request #1413 from ViktorTigerstrom/2022-04-default-to-bolt4-tlv-onions
Drop support for creating BOLT 4 Legacy onion format payloads
2 parents 2a3c99c + 3ecc5ae commit 593d8c4

File tree

6 files changed

+192
-343
lines changed

6 files changed

+192
-343
lines changed

fuzz/src/full_stack.rs

+7-7
Large diffs are not rendered by default.

lightning/src/ln/channelmanager.rs

-8
Original file line numberDiff line numberDiff line change
@@ -2138,13 +2138,6 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
21382138
}
21392139

21402140
let routing = match hop_data.format {
2141-
msgs::OnionHopDataFormat::Legacy { .. } => {
2142-
return Err(ReceiveError {
2143-
err_code: 0x4000|0x2000|3,
2144-
err_data: Vec::new(),
2145-
msg: "We require payment_secrets",
2146-
});
2147-
},
21482141
msgs::OnionHopDataFormat::NonFinalNode { .. } => {
21492142
return Err(ReceiveError {
21502143
err_code: 0x4000|22,
@@ -2280,7 +2273,6 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
22802273
};
22812274

22822275
let short_channel_id = match next_hop_data.format {
2283-
msgs::OnionHopDataFormat::Legacy { short_channel_id } => short_channel_id,
22842276
msgs::OnionHopDataFormat::NonFinalNode { short_channel_id } => short_channel_id,
22852277
msgs::OnionHopDataFormat::FinalNode { .. } => {
22862278
return_err!("Final Node OnionHopData provided for us as an intermediary node", 0x4000 | 22, &[0;0]);

lightning/src/ln/msgs.rs

+32-71
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use crate::io_extras::read_to_end;
4242

4343
use crate::util::events::{MessageSendEventsProvider, OnionMessageProvider};
4444
use crate::util::logger;
45-
use crate::util::ser::{BigSize, LengthReadable, Readable, ReadableArgs, Writeable, Writer, FixedLengthReader, HighZeroBytesDroppedBigSize, Hostname};
45+
use crate::util::ser::{LengthReadable, Readable, ReadableArgs, Writeable, Writer, FixedLengthReader, HighZeroBytesDroppedBigSize, Hostname};
4646

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

@@ -1027,9 +1027,6 @@ mod fuzzy_internal_msgs {
10271027
}
10281028

10291029
pub(crate) enum OnionHopDataFormat {
1030-
Legacy { // aka Realm-0
1031-
short_channel_id: u64,
1032-
},
10331030
NonFinalNode {
10341031
short_channel_id: u64,
10351032
},
@@ -1045,7 +1042,6 @@ mod fuzzy_internal_msgs {
10451042
/// Message serialization may panic if this value is more than 21 million Bitcoin.
10461043
pub(crate) amt_to_forward: u64,
10471044
pub(crate) outgoing_cltv_value: u32,
1048-
// 12 bytes of 0-padding for Legacy format
10491045
}
10501046

10511047
pub struct DecodedOnionErrorPacket {
@@ -1458,13 +1454,6 @@ impl Readable for FinalOnionHopData {
14581454
impl Writeable for OnionHopData {
14591455
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
14601456
match self.format {
1461-
OnionHopDataFormat::Legacy { short_channel_id } => {
1462-
0u8.write(w)?;
1463-
short_channel_id.write(w)?;
1464-
self.amt_to_forward.write(w)?;
1465-
self.outgoing_cltv_value.write(w)?;
1466-
w.write_all(&[0;12])?;
1467-
},
14681457
OnionHopDataFormat::NonFinalNode { short_channel_id } => {
14691458
encode_varint_length_prefixed_tlv!(w, {
14701459
(2, HighZeroBytesDroppedBigSize(self.amt_to_forward), required),
@@ -1487,58 +1476,44 @@ impl Writeable for OnionHopData {
14871476

14881477
impl Readable for OnionHopData {
14891478
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
1490-
let b: BigSize = Readable::read(r)?;
1491-
const LEGACY_ONION_HOP_FLAG: u64 = 0;
1492-
let (format, amt, cltv_value) = if b.0 != LEGACY_ONION_HOP_FLAG {
1493-
let mut rd = FixedLengthReader::new(r, b.0);
1494-
let mut amt = HighZeroBytesDroppedBigSize(0u64);
1495-
let mut cltv_value = HighZeroBytesDroppedBigSize(0u32);
1496-
let mut short_id: Option<u64> = None;
1497-
let mut payment_data: Option<FinalOnionHopData> = None;
1498-
let mut keysend_preimage: Option<PaymentPreimage> = None;
1499-
decode_tlv_stream!(&mut rd, {
1500-
(2, amt, required),
1501-
(4, cltv_value, required),
1502-
(6, short_id, option),
1503-
(8, payment_data, option),
1504-
// See https://github.com/lightning/blips/blob/master/blip-0003.md
1505-
(5482373484, keysend_preimage, option)
1506-
});
1507-
rd.eat_remaining().map_err(|_| DecodeError::ShortRead)?;
1508-
let format = if let Some(short_channel_id) = short_id {
1509-
if payment_data.is_some() { return Err(DecodeError::InvalidValue); }
1510-
OnionHopDataFormat::NonFinalNode {
1511-
short_channel_id,
1512-
}
1513-
} else {
1514-
if let &Some(ref data) = &payment_data {
1515-
if data.total_msat > MAX_VALUE_MSAT {
1516-
return Err(DecodeError::InvalidValue);
1517-
}
1518-
}
1519-
OnionHopDataFormat::FinalNode {
1520-
payment_data,
1521-
keysend_preimage,
1522-
}
1523-
};
1524-
(format, amt.0, cltv_value.0)
1479+
let mut amt = HighZeroBytesDroppedBigSize(0u64);
1480+
let mut cltv_value = HighZeroBytesDroppedBigSize(0u32);
1481+
let mut short_id: Option<u64> = None;
1482+
let mut payment_data: Option<FinalOnionHopData> = None;
1483+
let mut keysend_preimage: Option<PaymentPreimage> = None;
1484+
read_tlv_fields!(r, {
1485+
(2, amt, required),
1486+
(4, cltv_value, required),
1487+
(6, short_id, option),
1488+
(8, payment_data, option),
1489+
// See https://github.com/lightning/blips/blob/master/blip-0003.md
1490+
(5482373484, keysend_preimage, option)
1491+
});
1492+
1493+
let format = if let Some(short_channel_id) = short_id {
1494+
if payment_data.is_some() { return Err(DecodeError::InvalidValue); }
1495+
OnionHopDataFormat::NonFinalNode {
1496+
short_channel_id,
1497+
}
15251498
} else {
1526-
let format = OnionHopDataFormat::Legacy {
1527-
short_channel_id: Readable::read(r)?,
1528-
};
1529-
let amt: u64 = Readable::read(r)?;
1530-
let cltv_value: u32 = Readable::read(r)?;
1531-
r.read_exact(&mut [0; 12])?;
1532-
(format, amt, cltv_value)
1499+
if let &Some(ref data) = &payment_data {
1500+
if data.total_msat > MAX_VALUE_MSAT {
1501+
return Err(DecodeError::InvalidValue);
1502+
}
1503+
}
1504+
OnionHopDataFormat::FinalNode {
1505+
payment_data,
1506+
keysend_preimage,
1507+
}
15331508
};
15341509

1535-
if amt > MAX_VALUE_MSAT {
1510+
if amt.0 > MAX_VALUE_MSAT {
15361511
return Err(DecodeError::InvalidValue);
15371512
}
15381513
Ok(OnionHopData {
15391514
format,
1540-
amt_to_forward: amt,
1541-
outgoing_cltv_value: cltv_value,
1515+
amt_to_forward: amt.0,
1516+
outgoing_cltv_value: cltv_value.0,
15421517
})
15431518
}
15441519
}
@@ -2668,20 +2643,6 @@ mod tests {
26682643
assert_eq!(encoded_value, target_value);
26692644
}
26702645

2671-
#[test]
2672-
fn encoding_legacy_onion_hop_data() {
2673-
let msg = msgs::OnionHopData {
2674-
format: OnionHopDataFormat::Legacy {
2675-
short_channel_id: 0xdeadbeef1bad1dea,
2676-
},
2677-
amt_to_forward: 0x0badf00d01020304,
2678-
outgoing_cltv_value: 0xffffffff,
2679-
};
2680-
let encoded_value = msg.encode();
2681-
let target_value = hex::decode("00deadbeef1bad1dea0badf00d01020304ffffffff000000000000000000000000").unwrap();
2682-
assert_eq!(encoded_value, target_value);
2683-
}
2684-
26852646
#[test]
26862647
fn encoding_nonfinal_onion_hop_data() {
26872648
let mut msg = msgs::OnionHopData {

0 commit comments

Comments
 (0)