Skip to content

Commit 0e184cb

Browse files
Add onion_messages::Packet and adapt relevant onion util
We need to add a new Packet struct because onion message packet hop_data fields can be of variable length, whereas regular payment packets are always 1366 bytes.
1 parent a79d741 commit 0e184cb

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

lightning/src/ln/onion_message.rs

+19
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,25 @@
99

1010
//! Onion Messages: sending, receiving, forwarding, and ancillary utilities live here
1111
12+
#[derive(Clone, Debug, PartialEq)]
13+
pub(crate) struct Packet {
14+
pub(crate) version: u8,
15+
/// We don't want to disconnect a peer just because they provide a bogus public key, so we hold a
16+
/// Result instead of a PublicKey as we'd like.
17+
pub(crate) public_key: Result<PublicKey, secp256k1::Error>,
18+
// Unlike the onion packets used for payments, onion message packets can have payloads greater than 1300 bytes.
19+
pub(crate) hop_data: Vec<u8>,
20+
pub(crate) hmac: [u8; 32],
21+
}
22+
23+
impl Writeable for Packet {
24+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {}
25+
}
26+
27+
impl ReadableArgs<u16> for Packet {
28+
fn read<R: Read>(r: &mut R, len: u16) -> Result<Self, DecodeError> {}
29+
}
30+
1231
/// Onion messages have "control" TLVs and "data" TLVs. Control TLVs are used to control the
1332
/// direction and routing of an onion message from hop to hop, whereas data TLVs contain the onion
1433
/// message content itself.

lightning/src/ln/onion_utils.rs

+35-3
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ fn shift_arr_right(arr: &mut [u8; ONION_DATA_LEN], amt: usize) {
185185
}
186186
}
187187

188+
#[inline]
189+
fn shift_vec_right(vec: &mut Vec<u8>, amt: usize) {
190+
}
191+
188192
pub(super) fn route_size_insane(payloads: &Vec<msgs::OnionHopData>) -> bool {
189193
let mut len = 0;
190194
for payload in payloads.iter() {
@@ -206,7 +210,8 @@ pub(super) fn construct_onion_packet(payloads: Vec<msgs::OnionHopData>, onion_ke
206210
let mut chacha = ChaCha20::new(&prng_seed, &[0; 8]);
207211
chacha.process(&[0; ONION_DATA_LEN], &mut packet_data);
208212

209-
construct_onion_packet_with_init_noise(payloads, onion_keys, packet_data, associated_data)
213+
construct_onion_packet_with_init_noise(
214+
payloads, onion_keys, PacketData::Payment(packet_data), Some(associated_data)).try_into().unwrap()
210215
}
211216

212217
#[cfg(test)]
@@ -218,11 +223,38 @@ pub(super) fn construct_onion_packet_bogus_hopdata<HD: Writeable>(payloads: Vec<
218223
let mut chacha = ChaCha20::new(&prng_seed, &[0; 8]);
219224
chacha.process(&[0; ONION_DATA_LEN], &mut packet_data);
220225

221-
construct_onion_packet_with_init_noise(payloads, onion_keys, packet_data, associated_data)
226+
construct_onion_packet_with_init_noise(
227+
payloads, onion_keys, PacketData::Payment(packet_data), Some(associated_data)).try_into().unwrap()
228+
}
229+
230+
enum PacketData {
231+
Payment([u8; ONION_DATA_LEN]),
232+
Message(Vec<u8>),
233+
}
234+
235+
impl PacketData {
236+
fn len(&self) -> usize {
237+
}
238+
}
239+
240+
impl TryFrom<Packet> for onion_message::Packet {
241+
type Error = ();
242+
fn try_from(packet: Packet) -> Result<Self, Self::Error> {
243+
}
244+
}
245+
impl TryFrom<Packet> for msgs::OnionPacket {
246+
type Error = ();
247+
fn try_from(packet: Packet) -> Result<Self, Self::Error> {
248+
}
249+
}
250+
251+
enum Packet {
252+
Payment(msgs::OnionPacket),
253+
Message(onion_message::Packet),
222254
}
223255

224256
/// panics if route_size_insane(paylods)
225-
fn construct_onion_packet_with_init_noise<HD: Writeable>(mut payloads: Vec<HD>, onion_keys: Vec<OnionKeys>, mut packet_data: [u8; ONION_DATA_LEN], associated_data: &PaymentHash) -> msgs::OnionPacket {
257+
fn construct_onion_packet_with_init_noise<HD: Writeable>(mut payloads: Vec<HD>, onion_keys: Vec<OnionKeys>, mut packet_data: PacketData, associated_data: Option<&PaymentHash>) -> Packet {
226258
let filler = {
227259
const ONION_HOP_DATA_LEN: usize = 65; // We may decrease this eventually after TLV is common
228260
let mut res = Vec::with_capacity(ONION_HOP_DATA_LEN * (payloads.len() - 1));

0 commit comments

Comments
 (0)