From: Valentine Wallace Date: Fri, 16 Jun 2023 17:42:57 +0000 (-0400) Subject: Move some blinded path message code into message submodule. X-Git-Tag: v0.0.117-alpha1~45^2~11 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=381cc646c66538f69599ae4ee1224783eb5abe6a;p=rust-lightning Move some blinded path message code into message submodule. We'll similarly separate blinded path payments code into its own module. --- diff --git a/lightning/src/blinded_path/message.rs b/lightning/src/blinded_path/message.rs new file mode 100644 index 000000000..024fb451d --- /dev/null +++ b/lightning/src/blinded_path/message.rs @@ -0,0 +1,79 @@ +use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey}; +use crate::blinded_path::BlindedHop; +use crate::blinded_path::utils; +use crate::io; +use crate::prelude::*; +use crate::util::ser::{Writeable, Writer}; + +/// TLVs to encode in an intermediate onion message packet's hop data. When provided in a blinded +/// route, they are encoded into [`BlindedHop::encrypted_payload`]. +pub(crate) struct ForwardTlvs { + /// The node id of the next hop in the onion message's path. + pub(crate) next_node_id: PublicKey, + /// Senders to a blinded path use this value to concatenate the route they find to the + /// introduction node with the blinded path. + pub(crate) next_blinding_override: Option, +} + +/// Similar to [`ForwardTlvs`], but these TLVs are for the final node. +pub(crate) struct ReceiveTlvs { + /// If `path_id` is `Some`, it is used to identify the blinded path that this onion message is + /// sending to. This is useful for receivers to check that said blinded path is being used in + /// the right context. + pub(crate) path_id: Option<[u8; 32]>, +} + +impl Writeable for ForwardTlvs { + fn write(&self, writer: &mut W) -> Result<(), io::Error> { + // TODO: write padding + encode_tlv_stream!(writer, { + (4, self.next_node_id, required), + (8, self.next_blinding_override, option) + }); + Ok(()) + } +} + +impl Writeable for ReceiveTlvs { + fn write(&self, writer: &mut W) -> Result<(), io::Error> { + // TODO: write padding + encode_tlv_stream!(writer, { + (6, self.path_id, option), + }); + Ok(()) + } +} + +/// Construct blinded onion message hops for the given `unblinded_path`. +pub(super) fn blinded_hops( + secp_ctx: &Secp256k1, unblinded_path: &[PublicKey], session_priv: &SecretKey +) -> Result, secp256k1::Error> { + let mut blinded_hops = Vec::with_capacity(unblinded_path.len()); + + let mut prev_ss_and_blinded_node_id = None; + utils::construct_keys_callback(secp_ctx, unblinded_path, None, session_priv, |blinded_node_id, _, _, encrypted_payload_ss, unblinded_pk, _| { + if let Some((prev_ss, prev_blinded_node_id)) = prev_ss_and_blinded_node_id { + if let Some(pk) = unblinded_pk { + let payload = ForwardTlvs { + next_node_id: pk, + next_blinding_override: None, + }; + blinded_hops.push(BlindedHop { + blinded_node_id: prev_blinded_node_id, + encrypted_payload: utils::encrypt_payload(payload, prev_ss), + }); + } else { debug_assert!(false); } + } + prev_ss_and_blinded_node_id = Some((encrypted_payload_ss, blinded_node_id)); + })?; + + if let Some((final_ss, final_blinded_node_id)) = prev_ss_and_blinded_node_id { + let final_payload = ReceiveTlvs { path_id: None }; + blinded_hops.push(BlindedHop { + blinded_node_id: final_blinded_node_id, + encrypted_payload: utils::encrypt_payload(final_payload, final_ss), + }); + } else { debug_assert!(false) } + + Ok(blinded_hops) +} diff --git a/lightning/src/blinded_path/mod.rs b/lightning/src/blinded_path/mod.rs index 2022e06c9..3ad96869f 100644 --- a/lightning/src/blinded_path/mod.rs +++ b/lightning/src/blinded_path/mod.rs @@ -9,6 +9,7 @@ //! Creating blinded paths and related utilities live here. +pub(crate) mod message; pub(crate) mod utils; use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey}; @@ -73,7 +74,7 @@ impl BlindedPath { Ok(BlindedPath { introduction_node_id, blinding_point: PublicKey::from_secret_key(secp_ctx, &blinding_secret), - blinded_hops: blinded_message_hops(secp_ctx, node_pks, &blinding_secret).map_err(|_| ())?, + blinded_hops: message::blinded_hops(secp_ctx, node_pks, &blinding_secret).map_err(|_| ())?, }) } @@ -89,7 +90,7 @@ impl BlindedPath { let mut s = Cursor::new(&encrypted_control_tlvs); let mut reader = FixedLengthReader::new(&mut s, encrypted_control_tlvs.len() as u64); match ChaChaPolyReadAdapter::read(&mut reader, rho) { - Ok(ChaChaPolyReadAdapter { readable: ControlTlvs::Forward(ForwardTlvs { + Ok(ChaChaPolyReadAdapter { readable: ControlTlvs::Forward(message::ForwardTlvs { mut next_node_id, next_blinding_override, })}) => { let mut new_blinding_point = match next_blinding_override { @@ -108,40 +109,6 @@ impl BlindedPath { } } -/// Construct blinded onion message hops for the given `unblinded_path`. -fn blinded_message_hops( - secp_ctx: &Secp256k1, unblinded_path: &[PublicKey], session_priv: &SecretKey -) -> Result, secp256k1::Error> { - let mut blinded_hops = Vec::with_capacity(unblinded_path.len()); - - let mut prev_ss_and_blinded_node_id = None; - utils::construct_keys_callback(secp_ctx, unblinded_path, None, session_priv, |blinded_node_id, _, _, encrypted_payload_ss, unblinded_pk, _| { - if let Some((prev_ss, prev_blinded_node_id)) = prev_ss_and_blinded_node_id { - if let Some(pk) = unblinded_pk { - let payload = ForwardTlvs { - next_node_id: pk, - next_blinding_override: None, - }; - blinded_hops.push(BlindedHop { - blinded_node_id: prev_blinded_node_id, - encrypted_payload: utils::encrypt_payload(payload, prev_ss), - }); - } else { debug_assert!(false); } - } - prev_ss_and_blinded_node_id = Some((encrypted_payload_ss, blinded_node_id)); - })?; - - if let Some((final_ss, final_blinded_node_id)) = prev_ss_and_blinded_node_id { - let final_payload = ReceiveTlvs { path_id: None }; - blinded_hops.push(BlindedHop { - blinded_node_id: final_blinded_node_id, - encrypted_payload: utils::encrypt_payload(final_payload, final_ss), - }); - } else { debug_assert!(false) } - - Ok(blinded_hops) -} - impl Writeable for BlindedPath { fn write(&self, w: &mut W) -> Result<(), io::Error> { self.introduction_node_id.write(w)?; @@ -177,41 +144,3 @@ impl_writeable!(BlindedHop, { encrypted_payload }); -/// TLVs to encode in an intermediate onion message packet's hop data. When provided in a blinded -/// route, they are encoded into [`BlindedHop::encrypted_payload`]. -pub(crate) struct ForwardTlvs { - /// The node id of the next hop in the onion message's path. - pub(super) next_node_id: PublicKey, - /// Senders to a blinded path use this value to concatenate the route they find to the - /// introduction node with the blinded path. - pub(super) next_blinding_override: Option, -} - -/// Similar to [`ForwardTlvs`], but these TLVs are for the final node. -pub(crate) struct ReceiveTlvs { - /// If `path_id` is `Some`, it is used to identify the blinded path that this onion message is - /// sending to. This is useful for receivers to check that said blinded path is being used in - /// the right context. - pub(super) path_id: Option<[u8; 32]>, -} - -impl Writeable for ForwardTlvs { - fn write(&self, writer: &mut W) -> Result<(), io::Error> { - // TODO: write padding - encode_tlv_stream!(writer, { - (4, self.next_node_id, required), - (8, self.next_blinding_override, option) - }); - Ok(()) - } -} - -impl Writeable for ReceiveTlvs { - fn write(&self, writer: &mut W) -> Result<(), io::Error> { - // TODO: write padding - encode_tlv_stream!(writer, { - (6, self.path_id, option), - }); - Ok(()) - } -} diff --git a/lightning/src/onion_message/messenger.rs b/lightning/src/onion_message/messenger.rs index 7d5f4a22e..b6cdbbd7c 100644 --- a/lightning/src/onion_message/messenger.rs +++ b/lightning/src/onion_message/messenger.rs @@ -15,7 +15,9 @@ use bitcoin::hashes::hmac::{Hmac, HmacEngine}; use bitcoin::hashes::sha256::Hash as Sha256; use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey}; -use crate::blinded_path::{BlindedPath, ForwardTlvs, ReceiveTlvs, utils}; +use crate::blinded_path::BlindedPath; +use crate::blinded_path::message::{ForwardTlvs, ReceiveTlvs}; +use crate::blinded_path::utils; use crate::sign::{EntropySource, KeysManager, NodeSigner, Recipient}; use crate::events::OnionMessageProvider; use crate::ln::features::{InitFeatures, NodeFeatures}; diff --git a/lightning/src/onion_message/packet.rs b/lightning/src/onion_message/packet.rs index 8a5628f16..9eb48a310 100644 --- a/lightning/src/onion_message/packet.rs +++ b/lightning/src/onion_message/packet.rs @@ -12,7 +12,8 @@ use bitcoin::secp256k1::PublicKey; use bitcoin::secp256k1::ecdh::SharedSecret; -use crate::blinded_path::{BlindedPath, ForwardTlvs, ReceiveTlvs}; +use crate::blinded_path::BlindedPath; +use crate::blinded_path::message::{ForwardTlvs, ReceiveTlvs}; use crate::ln::msgs::DecodeError; use crate::ln::onion_utils; use super::messenger::CustomOnionMessageHandler; @@ -151,7 +152,8 @@ pub(super) enum ForwardControlTlvs { Blinded(Vec), /// If we're constructing an onion message hop through an intermediate unblinded node, we'll need /// to construct the intermediate hop's control TLVs in their unblinded state to avoid encoding - /// them into an intermediate Vec. See [`crate::blinded_path::ForwardTlvs`] for more info. + /// them into an intermediate Vec. See [`crate::blinded_path::message::ForwardTlvs`] for more + /// info. Unblinded(ForwardTlvs), } @@ -159,7 +161,7 @@ pub(super) enum ForwardControlTlvs { pub(super) enum ReceiveControlTlvs { /// See [`ForwardControlTlvs::Blinded`]. Blinded(Vec), - /// See [`ForwardControlTlvs::Unblinded`] and [`crate::blinded_path::ReceiveTlvs`]. + /// See [`ForwardControlTlvs::Unblinded`] and [`crate::blinded_path::message::ReceiveTlvs`]. Unblinded(ReceiveTlvs), }