Skip to content

Commit 6916934

Browse files
Implement receiving and forwarding onion messages
This required adapting `onion_utils::decode_next_hop` to work for both payments and onion messages. Currently we just print out the path_id of any onion messages we receive. In the future, these received onion messages will be redirected to their respective handlers: i.e. an invoice_request will go to an InvoiceHandler, custom onion messages will go to a custom handler, etc.
1 parent e3ececc commit 6916934

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

lightning/src/ln/channelmanager.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2139,7 +2139,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
21392139
}
21402140
}
21412141

2142-
let next_hop = match onion_utils::decode_next_hop(shared_secret, &msg.onion_routing_packet.hop_data[..], msg.onion_routing_packet.hmac, msg.payment_hash) {
2142+
let next_hop = match onion_utils::decode_next_payment_hop(shared_secret, &msg.onion_routing_packet.hop_data[..], msg.onion_routing_packet.hmac, msg.payment_hash) {
21432143
Ok(res) => res,
21442144
Err(onion_utils::OnionDecodeErr::Malformed { err_msg, err_code }) => {
21452145
return_malformed_err!(err_msg, err_code);
@@ -2967,7 +2967,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
29672967
let phantom_secret_res = self.keys_manager.get_node_secret(Recipient::PhantomNode);
29682968
if phantom_secret_res.is_ok() && fake_scid::is_valid_phantom(&self.fake_scid_rand_bytes, short_chan_id) {
29692969
let phantom_shared_secret = SharedSecret::new(&onion_packet.public_key.unwrap(), &phantom_secret_res.unwrap()).secret_bytes();
2970-
let next_hop = match onion_utils::decode_next_hop(phantom_shared_secret, &onion_packet.hop_data, onion_packet.hmac, payment_hash) {
2970+
let next_hop = match onion_utils::decode_next_payment_hop(phantom_shared_secret, &onion_packet.hop_data, onion_packet.hmac, payment_hash) {
29712971
Ok(res) => res,
29722972
Err(onion_utils::OnionDecodeErr::Malformed { err_msg, err_code }) => {
29732973
let sha256_of_onion = Sha256::hash(&onion_packet.hop_data).into_inner();

lightning/src/ln/onion_message.rs

+7
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ impl Writeable for (Payload, SharedSecret) {
6464
}
6565
}
6666

67+
/// Reads of `Payload`s are parameterized by the `rho` of a `SharedSecret`, which is used to decrypt
68+
/// the onion message payload's `encrypted_data` field.
69+
impl ReadableArgs<SharedSecret> for Payload {
70+
fn read<R: Read>(mut r: &mut R, encrypted_tlvs_ss: SharedSecret) -> Result<Self, DecodeError> {
71+
}
72+
}
73+
6774
/// Onion messages contain an encrypted TLV stream. This can be supplied by someone else, in the
6875
/// case that we're sending to a blinded route, or created by us if we're constructing payloads for
6976
/// unblinded hops in the onion message's path.

lightning/src/ln/onion_utils.rs

+38-2
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,37 @@ pub(super) fn process_onion_failure<T: secp256k1::Signing, L: Deref>(secp_ctx: &
558558
} else { unreachable!(); }
559559
}
560560

561-
/// Data decrypted from the onion payload.
561+
/// Used in the decoding of inbound payments' and onion messages' routing packets. This enum allows
562+
/// us to use `decode_next_hop` to return the payloads and next hop packet bytes of both payments
563+
/// and onion messages.
564+
enum Payload {
565+
/// This payload was for an incoming payment.
566+
Payment(msgs::OnionHopData),
567+
/// This payload was for an incoming onion message.
568+
Message(onion_message::Payload),
569+
}
570+
571+
enum NextPacketBytes {
572+
Payment([u8; 20*65]),
573+
Message(Vec<u8>),
574+
}
575+
576+
/// Data decrypted from an onion message's onion payload.
577+
pub(crate) enum MessageHop {
578+
/// This onion payload was for us, not for forwarding to a next-hop.
579+
Receive(onion_message::Payload),
580+
/// This onion payload needs to be forwarded to a next-hop.
581+
Forward {
582+
/// Onion payload data used in forwarding the onion message.
583+
next_hop_data: onion_message::Payload,
584+
/// HMAC of the next hop's onion packet.
585+
next_hop_hmac: [u8; 32],
586+
/// Bytes of the onion packet we're forwarding.
587+
new_packet_bytes: Vec<u8>,
588+
},
589+
}
590+
591+
/// Data decrypted from a payment's onion payload.
562592
pub(crate) enum Hop {
563593
/// This onion payload was for us, not for forwarding to a next-hop. Contains information for
564594
/// verifying the incoming payment.
@@ -588,7 +618,13 @@ pub(crate) enum OnionDecodeErr {
588618
},
589619
}
590620

591-
pub(crate) fn decode_next_hop(shared_secret: [u8; 32], hop_data: &[u8], hmac_bytes: [u8; 32], payment_hash: PaymentHash) -> Result<Hop, OnionDecodeErr> {
621+
pub(crate) fn decode_next_message_hop(shared_secret: [u8; 32], hop_data: &[u8], hmac_bytes: [u8; 32], encrypted_tlvs_ss: SharedSecret) -> Result<MessageHop, OnionDecodeErr> {
622+
}
623+
624+
pub(crate) fn decode_next_payment_hop(shared_secret: [u8; 32], hop_data: &[u8], hmac_bytes: [u8; 32], payment_hash: PaymentHash) -> Result<Hop, OnionDecodeErr> {
625+
}
626+
627+
fn decode_next_hop(shared_secret: [u8; 32], hop_data: &[u8], hmac_bytes: [u8; 32], payment_hash: Option<PaymentHash>, encrypted_tlv_ss: Option<SharedSecret>) -> Result<(Payload, Option<([u8; 32], NextPacketBytes)>), OnionDecodeErr> {
592628
let (rho, mu) = gen_rho_mu_from_shared_secret(&shared_secret);
593629
let mut hmac = HmacEngine::<Sha256>::new(&mu);
594630
hmac.input(hop_data);

0 commit comments

Comments
 (0)