1 use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey};
3 #[allow(unused_imports)]
6 use crate::blinded_path::{BlindedHop, BlindedPath};
7 use crate::blinded_path::utils;
10 use crate::ln::onion_utils;
11 use crate::onion_message::packet::ControlTlvs;
12 use crate::sign::{NodeSigner, Recipient};
13 use crate::crypto::streams::ChaChaPolyReadAdapter;
14 use crate::util::ser::{FixedLengthReader, LengthReadableArgs, Writeable, Writer};
19 /// TLVs to encode in an intermediate onion message packet's hop data. When provided in a blinded
20 /// route, they are encoded into [`BlindedHop::encrypted_payload`].
21 pub(crate) struct ForwardTlvs {
22 /// The node id of the next hop in the onion message's path.
23 pub(crate) next_node_id: PublicKey,
24 /// Senders to a blinded path use this value to concatenate the route they find to the
25 /// introduction node with the blinded path.
26 pub(crate) next_blinding_override: Option<PublicKey>,
29 /// Similar to [`ForwardTlvs`], but these TLVs are for the final node.
30 pub(crate) struct ReceiveTlvs {
31 /// If `path_id` is `Some`, it is used to identify the blinded path that this onion message is
32 /// sending to. This is useful for receivers to check that said blinded path is being used in
33 /// the right context.
34 pub(crate) path_id: Option<[u8; 32]>,
37 impl Writeable for ForwardTlvs {
38 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
39 // TODO: write padding
40 encode_tlv_stream!(writer, {
41 (4, self.next_node_id, required),
42 (8, self.next_blinding_override, option)
48 impl Writeable for ReceiveTlvs {
49 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
50 // TODO: write padding
51 encode_tlv_stream!(writer, {
52 (6, self.path_id, option),
58 /// Construct blinded onion message hops for the given `unblinded_path`.
59 pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
60 secp_ctx: &Secp256k1<T>, unblinded_path: &[PublicKey], session_priv: &SecretKey
61 ) -> Result<Vec<BlindedHop>, secp256k1::Error> {
62 let blinded_tlvs = unblinded_path.iter()
63 .skip(1) // The first node's TLVs contains the next node's pubkey
65 ControlTlvs::Forward(ForwardTlvs { next_node_id: *pk, next_blinding_override: None })
67 .chain(core::iter::once(ControlTlvs::Receive(ReceiveTlvs { path_id: None })));
69 utils::construct_blinded_hops(secp_ctx, unblinded_path.iter(), blinded_tlvs, session_priv)
72 // Advance the blinded onion message path by one hop, so make the second hop into the new
74 pub(crate) fn advance_path_by_one<NS: Deref, T: secp256k1::Signing + secp256k1::Verification>(
75 path: &mut BlindedPath, node_signer: &NS, secp_ctx: &Secp256k1<T>
76 ) -> Result<(), ()> where NS::Target: NodeSigner {
77 let control_tlvs_ss = node_signer.ecdh(Recipient::Node, &path.blinding_point, None)?;
78 let rho = onion_utils::gen_rho_from_shared_secret(&control_tlvs_ss.secret_bytes());
79 let encrypted_control_tlvs = path.blinded_hops.remove(0).encrypted_payload;
80 let mut s = Cursor::new(&encrypted_control_tlvs);
81 let mut reader = FixedLengthReader::new(&mut s, encrypted_control_tlvs.len() as u64);
82 match ChaChaPolyReadAdapter::read(&mut reader, rho) {
83 Ok(ChaChaPolyReadAdapter { readable: ControlTlvs::Forward(ForwardTlvs {
84 mut next_node_id, next_blinding_override,
86 let mut new_blinding_point = match next_blinding_override {
87 Some(blinding_point) => blinding_point,
89 onion_utils::next_hop_pubkey(secp_ctx, path.blinding_point,
90 control_tlvs_ss.as_ref()).map_err(|_| ())?
93 mem::swap(&mut path.blinding_point, &mut new_blinding_point);
94 mem::swap(&mut path.introduction_node_id, &mut next_node_id);