Skip to content

Commit d2653d6

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 4eb86d3 commit d2653d6

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);
@@ -2955,7 +2955,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
29552955
let phantom_secret_res = self.keys_manager.get_node_secret(Recipient::PhantomNode);
29562956
if phantom_secret_res.is_ok() && fake_scid::is_valid_phantom(&self.fake_scid_rand_bytes, short_chan_id) {
29572957
let phantom_shared_secret = SharedSecret::new(&onion_packet.public_key.unwrap(), &phantom_secret_res.unwrap()).secret_bytes();
2958-
let next_hop = match onion_utils::decode_next_hop(phantom_shared_secret, &onion_packet.hop_data, onion_packet.hmac, payment_hash) {
2958+
let next_hop = match onion_utils::decode_next_payment_hop(phantom_shared_secret, &onion_packet.hop_data, onion_packet.hmac, payment_hash) {
29592959
Ok(res) => res,
29602960
Err(onion_utils::OnionDecodeErr::Malformed { err_msg, err_code }) => {
29612961
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
@@ -573,7 +573,37 @@ pub(super) fn process_onion_failure<T: secp256k1::Signing, L: Deref>(secp_ctx: &
573573
} else { unreachable!(); }
574574
}
575575

576-
/// Data decrypted from the onion payload.
576+
/// Used in the decoding of inbound payments' and onion messages' routing packets. This enum allows
577+
/// us to use `decode_next_hop` to return the payloads and next hop packet bytes of both payments
578+
/// and onion messages.
579+
enum Payload {
580+
/// This payload was for an incoming payment.
581+
Payment(msgs::OnionHopData),
582+
/// This payload was for an incoming onion message.
583+
Message(onion_message::Payload),
584+
}
585+
586+
enum NextPacketBytes {
587+
Payment([u8; 20*65]),
588+
Message(Vec<u8>),
589+
}
590+
591+
/// Data decrypted from an onion message's onion payload.
592+
pub(crate) enum MessageHop {
593+
/// This onion payload was for us, not for forwarding to a next-hop.
594+
Receive(onion_message::Payload),
595+
/// This onion payload needs to be forwarded to a next-hop.
596+
Forward {
597+
/// Onion payload data used in forwarding the onion message.
598+
next_hop_data: onion_message::Payload,
599+
/// HMAC of the next hop's onion packet.
600+
next_hop_hmac: [u8; 32],
601+
/// Bytes of the onion packet we're forwarding.
602+
new_packet_bytes: Vec<u8>,
603+
},
604+
}
605+
606+
/// Data decrypted from a payment's onion payload.
577607
pub(crate) enum Hop {
578608
/// This onion payload was for us, not for forwarding to a next-hop. Contains information for
579609
/// verifying the incoming payment.
@@ -603,7 +633,13 @@ pub(crate) enum OnionDecodeErr {
603633
},
604634
}
605635

606-
pub(crate) fn decode_next_hop(shared_secret: [u8; 32], hop_data: &[u8], hmac_bytes: [u8; 32], payment_hash: PaymentHash) -> Result<Hop, OnionDecodeErr> {
636+
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> {
637+
}
638+
639+
pub(crate) fn decode_next_payment_hop(shared_secret: [u8; 32], hop_data: &[u8], hmac_bytes: [u8; 32], payment_hash: PaymentHash) -> Result<Hop, OnionDecodeErr> {
640+
}
641+
642+
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> {
607643
let (rho, mu) = gen_rho_mu_from_shared_secret(&shared_secret);
608644
let mut hmac = HmacEngine::<Sha256>::new(&mu);
609645
hmac.input(hop_data);

0 commit comments

Comments
 (0)