Skip to content

Commit 82e433b

Browse files
Receiving/forwarding onion messages -- internal api changes
This commit covers the internal refactors needed for receiving and forwarding onion messages, and docs updates. Note that we support receiving custom TLVs, just not sending them.
1 parent 16dfab0 commit 82e433b

File tree

6 files changed

+52
-19
lines changed

6 files changed

+52
-19
lines changed

lightning/src/ln/channelmanager.rs

+1-13
Original file line numberDiff line numberDiff line change
@@ -2097,21 +2097,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
20972097
},
20982098
onion_utils::Hop::Forward { next_hop_data, next_hop_hmac, new_packet_bytes } => {
20992099
let mut new_pubkey = msg.onion_routing_packet.public_key.unwrap();
2100-
2101-
let blinding_factor = {
2102-
let mut sha = Sha256::engine();
2103-
sha.input(&new_pubkey.serialize()[..]);
2104-
sha.input(&shared_secret);
2105-
Sha256::from_engine(sha).into_inner()
2106-
};
2107-
2108-
let public_key = if let Err(e) = new_pubkey.mul_assign(&self.secp_ctx, &blinding_factor[..]) {
2109-
Err(e)
2110-
} else { Ok(new_pubkey) };
2111-
21122100
let outgoing_packet = msgs::OnionPacket {
21132101
version: 0,
2114-
public_key,
2102+
public_key: onion_utils::next_hop_packet_pubkey(&new_pubkey, &shared_secret),
21152103
hop_data: new_packet_bytes,
21162104
hmac: next_hop_hmac.clone(),
21172105
};

lightning/src/ln/msgs.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,15 @@ impl Writeable for (OnionMsgPayload, [u8; 32]) {
14571457
}
14581458
}
14591459

1460+
/// Reads of `OnionMsgPayload`s are parameterized by the `rho` of a `SharedSecret`, which is used to
1461+
/// decrypt the onion message's `encrypted_data` field.
1462+
impl ReadableArgs<[u8; 32]> for OnionMsgPayload {
1463+
fn read<R: Read>(mut r: &mut R, encrypted_data_ss: [u8; 32]) -> Result<Self, DecodeError> {
1464+
// calls:
1465+
// * ChaCha20Poly1305RFC::decrypt_in_place
1466+
}
1467+
}
1468+
14601469
impl Writeable for Ping {
14611470
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
14621471
self.ponglen.write(w)?;

lightning/src/ln/onion_message.rs

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ impl<Signer: Sign, K: Deref> OnionMessager<Signer, K>
5555

5656
impl OnionMessageHandler for OnionMessager {
5757
fn handle_onion_message(&self, peer_node_id: &PublicKey, msg: &msgs::OnionMessage) {
58+
// calls:
59+
// * onion_utils::decode_next_hop
60+
// * onion_utils::next_hop_packet_pubkey
5861
}
5962
}
6063

lightning/src/ln/onion_utils.rs

+29-6
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ pub(super) fn gen_ammag_from_shared_secret(shared_secret: &[u8]) -> [u8; 32] {
7373
Hmac::from_engine(hmac).into_inner()
7474
}
7575

76+
pub(super) fn next_hop_packet_pubkey(packet_pubkey: &PublicKey, packet_shared_secret: &SharedSecret) -> Result<PublicKey, secp256k1::Error> {}
77+
7678
/// Used in the construction of keys to build the onion routing packet for payments and onion
7779
/// messages, in `construct_onion_keys_callback`.
7880
///
@@ -547,15 +549,25 @@ pub(super) fn process_onion_failure<T: secp256k1::Signing, L: Deref>(secp_ctx: &
547549
} else { unreachable!(); }
548550
}
549551

552+
/// Used in the decoding of inbound payments' and onion messages' routing packets. This enum allows
553+
/// us to use `decode_next_hop` for returning the payloads and next hop packet bytes of both
554+
/// payments and onion messages.
555+
pub(crate) enum Payload {
556+
/// This payload was for an incoming payment.
557+
Payment(msgs::OnionPayload),
558+
/// This payload was for an incoming onion message.
559+
Message(msgs::OnionMsgPayload),
560+
}
561+
550562
/// Data decrypted from the onion payload.
551563
pub(crate) enum HopPayload {
552-
/// This onion payload was for us, not for forwarding to a next-hop. Contains information for
553-
/// verifying the incoming payment.
554-
Receive(msgs::OnionHopData),
564+
/// This onion payload was for us, not for forwarding to a next-hop. If we're receiving a payment,
565+
/// this contains information for verifying the incoming payment.
566+
Receive(Payload),
555567
/// This onion payload needs to be forwarded to a next-hop.
556568
Forward {
557-
/// Onion payload data used in forwarding the payment.
558-
next_hop_data: msgs::OnionHopData,
569+
/// Onion payload data used in forwarding the payment or onion message.
570+
next_hop_data: Payload,
559571
/// HMAC of the next hop's onion packet.
560572
next_hop_hmac: [u8; 32],
561573
/// Bytes of the onion packet we're forwarding.
@@ -577,7 +589,18 @@ pub(crate) enum OnionDecodeErr {
577589
},
578590
}
579591

580-
pub(crate) fn decode_next_hop(shared_secret: [u8; 32], hop_data: &[u8], hmac_bytes: [u8; 32], payment_hash: PaymentHash) -> Result<Hop, OnionDecodeErr> {
592+
/// Used in the decoding of inbound payments' and onion messages' routing packets. This enum
593+
/// indicates whether the incoming packet corresponds to a payment or an onion message.
594+
pub(crate) enum Onion {
595+
/// We're receiving an inbound payment, so the payment hash is provided as associated data for
596+
/// calculating the packet hmac.
597+
Payment(PaymentHash),
598+
/// We're receiving an inbound onion message, so the `rho` is provided for decrypting the onion
599+
/// message's `encrypted_data` field.
600+
Message([u8; 32],
601+
}
602+
603+
pub(crate) fn decode_next_hop(shared_secret: [u8; 32], hop_data: &[u8], hmac_bytes: [u8; 32], onion_type: Onion) -> Result<Hop, OnionDecodeErr> {
581604
let (rho, mu) = gen_rho_mu_from_shared_secret(&shared_secret);
582605
let mut hmac = HmacEngine::<Sha256>::new(&mu);
583606
hmac.input(hop_data);

lightning/src/util/chacha20poly1305rfc.rs

+4
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ mod real_chachapoly {
9696
false
9797
}
9898
}
99+
100+
pub fn decrypt_in_place(&mut self, input_output: &mut [u8], tag: &[u8]) -> bool {}
101+
102+
fn decrypt_inner(&mut self, input: &mut [u8], output: Option<&mut [u8]>, tag: &[u8]) -> bool {}
99103
}
100104
}
101105
#[cfg(not(fuzzing))]

lightning/src/util/ser_macros.rs

+6
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ macro_rules! decode_tlv {
165165
}};
166166
}
167167

168+
/// Decode a TLV stream that contains custom TLVs that are unknown to LDK but may be known to the
169+
/// user.
170+
macro_rules! decode_tlv_stream_with_custom {}
171+
168172
macro_rules! decode_tlv_stream {
169173
($stream: expr, {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}) => { {
170174
use ln::msgs::DecodeError;
@@ -231,6 +235,8 @@ macro_rules! decode_tlv_stream {
231235
} }
232236
}
233237

238+
macro_rules! decode_tlv_stream_inner {}
239+
234240
macro_rules! impl_writeable_msg {
235241
($st:ident, {$($field:ident),* $(,)*}, {$(($type: expr, $tlvfield: ident, $fieldty: tt)),* $(,)*}) => {
236242
impl ::util::ser::Writeable for $st {

0 commit comments

Comments
 (0)