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