Minor BlindedHop docs update
[rust-lightning] / lightning / src / blinded_path / mod.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
8 // licenses.
9
10 //! Creating blinded paths and related utilities live here.
11
12 pub(crate) mod message;
13 pub(crate) mod utils;
14
15 use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey};
16
17 use crate::sign::EntropySource;
18 use crate::ln::msgs::DecodeError;
19 use crate::util::ser::{Readable, Writeable, Writer};
20
21 use crate::io;
22 use crate::prelude::*;
23
24 /// Onion messages and payments can be sent and received to blinded paths, which serve to hide the
25 /// identity of the recipient.
26 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
27 pub struct BlindedPath {
28         /// To send to a blinded path, the sender first finds a route to the unblinded
29         /// `introduction_node_id`, which can unblind its [`encrypted_payload`] to find out the onion
30         /// message or payment's next hop and forward it along.
31         ///
32         /// [`encrypted_payload`]: BlindedHop::encrypted_payload
33         pub introduction_node_id: PublicKey,
34         /// Used by the introduction node to decrypt its [`encrypted_payload`] to forward the onion
35         /// message or payment.
36         ///
37         /// [`encrypted_payload`]: BlindedHop::encrypted_payload
38         pub blinding_point: PublicKey,
39         /// The hops composing the blinded path.
40         pub blinded_hops: Vec<BlindedHop>,
41 }
42
43 /// An encrypted payload and node id corresponding to a hop in a payment or onion message path, to
44 /// be encoded in the sender's onion packet. These hops cannot be identified by outside observers
45 /// and thus can be used to hide the identity of the recipient.
46 #[derive(Clone, Debug, Hash, PartialEq, Eq)]
47 pub struct BlindedHop {
48         /// The blinded node id of this hop in a [`BlindedPath`].
49         pub blinded_node_id: PublicKey,
50         /// The encrypted payload intended for this hop in a [`BlindedPath`].
51         // The node sending to this blinded path will later encode this payload into the onion packet for
52         // this hop.
53         pub encrypted_payload: Vec<u8>,
54 }
55
56 impl BlindedPath {
57         /// Create a blinded path for an onion message, to be forwarded along `node_pks`. The last node
58         /// pubkey in `node_pks` will be the destination node.
59         ///
60         /// Errors if less than two hops are provided or if `node_pk`(s) are invalid.
61         //  TODO: make all payloads the same size with padding + add dummy hops
62         pub fn new_for_message<ES: EntropySource, T: secp256k1::Signing + secp256k1::Verification>
63                 (node_pks: &[PublicKey], entropy_source: &ES, secp_ctx: &Secp256k1<T>) -> Result<Self, ()>
64         {
65                 if node_pks.len() < 2 { return Err(()) }
66                 let blinding_secret_bytes = entropy_source.get_secure_random_bytes();
67                 let blinding_secret = SecretKey::from_slice(&blinding_secret_bytes[..]).expect("RNG is busted");
68                 let introduction_node_id = node_pks[0];
69
70                 Ok(BlindedPath {
71                         introduction_node_id,
72                         blinding_point: PublicKey::from_secret_key(secp_ctx, &blinding_secret),
73                         blinded_hops: message::blinded_hops(secp_ctx, node_pks, &blinding_secret).map_err(|_| ())?,
74                 })
75         }
76 }
77
78 impl Writeable for BlindedPath {
79         fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
80                 self.introduction_node_id.write(w)?;
81                 self.blinding_point.write(w)?;
82                 (self.blinded_hops.len() as u8).write(w)?;
83                 for hop in &self.blinded_hops {
84                         hop.write(w)?;
85                 }
86                 Ok(())
87         }
88 }
89
90 impl Readable for BlindedPath {
91         fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
92                 let introduction_node_id = Readable::read(r)?;
93                 let blinding_point = Readable::read(r)?;
94                 let num_hops: u8 = Readable::read(r)?;
95                 if num_hops == 0 { return Err(DecodeError::InvalidValue) }
96                 let mut blinded_hops: Vec<BlindedHop> = Vec::with_capacity(num_hops.into());
97                 for _ in 0..num_hops {
98                         blinded_hops.push(Readable::read(r)?);
99                 }
100                 Ok(BlindedPath {
101                         introduction_node_id,
102                         blinding_point,
103                         blinded_hops,
104                 })
105         }
106 }
107
108 impl_writeable!(BlindedHop, {
109         blinded_node_id,
110         encrypted_payload
111 });
112