1 use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey};
3 #[allow(unused_imports)]
6 use crate::blinded_path::{BlindedHop, BlindedPath, IntroductionNode, NextMessageHop, NodeIdLookUp};
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 next hop in the onion message's path.
23 pub(crate) next_hop: NextMessageHop,
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 let (next_node_id, short_channel_id) = match self.next_hop {
40 NextMessageHop::NodeId(pubkey) => (Some(pubkey), None),
41 NextMessageHop::ShortChannelId(scid) => (None, Some(scid)),
43 // TODO: write padding
44 encode_tlv_stream!(writer, {
45 (2, short_channel_id, option),
46 (4, next_node_id, option),
47 (8, self.next_blinding_override, option)
53 impl Writeable for ReceiveTlvs {
54 fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
55 // TODO: write padding
56 encode_tlv_stream!(writer, {
57 (6, self.path_id, option),
63 /// Construct blinded onion message hops for the given `unblinded_path`.
64 pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
65 secp_ctx: &Secp256k1<T>, unblinded_path: &[PublicKey], session_priv: &SecretKey
66 ) -> Result<Vec<BlindedHop>, secp256k1::Error> {
67 let blinded_tlvs = unblinded_path.iter()
68 .skip(1) // The first node's TLVs contains the next node's pubkey
69 .map(|pk| ForwardTlvs { next_hop: NextMessageHop::NodeId(*pk), next_blinding_override: None })
70 .map(|tlvs| ControlTlvs::Forward(tlvs))
71 .chain(core::iter::once(ControlTlvs::Receive(ReceiveTlvs { path_id: None })));
73 utils::construct_blinded_hops(secp_ctx, unblinded_path.iter(), blinded_tlvs, session_priv)
76 // Advance the blinded onion message path by one hop, so make the second hop into the new
78 pub(crate) fn advance_path_by_one<NS: Deref, NL: Deref, T>(
79 path: &mut BlindedPath, node_signer: &NS, node_id_lookup: &NL, secp_ctx: &Secp256k1<T>
82 NS::Target: NodeSigner,
83 NL::Target: NodeIdLookUp,
84 T: secp256k1::Signing + secp256k1::Verification,
86 let control_tlvs_ss = node_signer.ecdh(Recipient::Node, &path.blinding_point, None)?;
87 let rho = onion_utils::gen_rho_from_shared_secret(&control_tlvs_ss.secret_bytes());
88 let encrypted_control_tlvs = path.blinded_hops.remove(0).encrypted_payload;
89 let mut s = Cursor::new(&encrypted_control_tlvs);
90 let mut reader = FixedLengthReader::new(&mut s, encrypted_control_tlvs.len() as u64);
91 match ChaChaPolyReadAdapter::read(&mut reader, rho) {
92 Ok(ChaChaPolyReadAdapter {
93 readable: ControlTlvs::Forward(ForwardTlvs { next_hop, next_blinding_override })
95 let next_node_id = match next_hop {
96 NextMessageHop::NodeId(pubkey) => pubkey,
97 NextMessageHop::ShortChannelId(scid) => match node_id_lookup.next_node_id(scid) {
98 Some(pubkey) => pubkey,
99 None => return Err(()),
102 let mut new_blinding_point = match next_blinding_override {
103 Some(blinding_point) => blinding_point,
105 onion_utils::next_hop_pubkey(secp_ctx, path.blinding_point,
106 control_tlvs_ss.as_ref()).map_err(|_| ())?
109 mem::swap(&mut path.blinding_point, &mut new_blinding_point);
110 path.introduction_node = IntroductionNode::NodeId(next_node_id);