We'll similarly separate blinded path payments code into its own module.
--- /dev/null
+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<PublicKey>,
+}
+
+/// 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<W: Writer>(&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<W: Writer>(&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<T: secp256k1::Signing + secp256k1::Verification>(
+ secp_ctx: &Secp256k1<T>, unblinded_path: &[PublicKey], session_priv: &SecretKey
+) -> Result<Vec<BlindedHop>, 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)
+}
//! Creating blinded paths and related utilities live here.
+pub(crate) mod message;
pub(crate) mod utils;
use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey};
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(|_| ())?,
})
}
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 {
}
}
-/// Construct blinded onion message hops for the given `unblinded_path`.
-fn blinded_message_hops<T: secp256k1::Signing + secp256k1::Verification>(
- secp_ctx: &Secp256k1<T>, unblinded_path: &[PublicKey], session_priv: &SecretKey
-) -> Result<Vec<BlindedHop>, 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<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
self.introduction_node_id.write(w)?;
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<PublicKey>,
-}
-
-/// 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<W: Writer>(&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<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
- // TODO: write padding
- encode_tlv_stream!(writer, {
- (6, self.path_id, option),
- });
- Ok(())
- }
-}
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};
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;
Blinded(Vec<u8>),
/// 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),
}
pub(super) enum ReceiveControlTlvs {
/// See [`ForwardControlTlvs::Blinded`].
Blinded(Vec<u8>),
- /// See [`ForwardControlTlvs::Unblinded`] and [`crate::blinded_path::ReceiveTlvs`].
+ /// See [`ForwardControlTlvs::Unblinded`] and [`crate::blinded_path::message::ReceiveTlvs`].
Unblinded(ReceiveTlvs),
}