2022-05-27 16:25:15 -07:00
|
|
|
// This file is Copyright its original authors, visible in version control
|
|
|
|
// history.
|
|
|
|
//
|
|
|
|
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
|
|
|
|
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
|
|
|
|
// You may not use this file except in accordance with one or both of these
|
|
|
|
// licenses.
|
|
|
|
|
2022-12-14 20:49:53 +00:00
|
|
|
//! Creating blinded paths and related utilities live here.
|
2022-05-27 16:25:15 -07:00
|
|
|
|
2023-06-16 15:43:13 -04:00
|
|
|
pub mod payment;
|
2023-06-16 13:42:57 -04:00
|
|
|
pub(crate) mod message;
|
2023-03-15 21:56:57 -04:00
|
|
|
pub(crate) mod utils;
|
|
|
|
|
2023-06-27 12:35:03 -04:00
|
|
|
use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey};
|
2022-05-27 16:25:15 -07:00
|
|
|
|
2022-10-13 02:35:48 -04:00
|
|
|
use crate::ln::msgs::DecodeError;
|
2023-06-20 20:27:57 -04:00
|
|
|
use crate::offers::invoice::BlindedPayInfo;
|
|
|
|
use crate::sign::EntropySource;
|
2023-06-16 13:59:31 -04:00
|
|
|
use crate::util::ser::{Readable, Writeable, Writer};
|
2022-05-27 16:25:15 -07:00
|
|
|
|
2023-06-16 13:59:31 -04:00
|
|
|
use crate::io;
|
2022-10-13 02:35:48 -04:00
|
|
|
use crate::prelude::*;
|
2022-05-27 16:25:15 -07:00
|
|
|
|
2023-03-15 22:30:41 -04:00
|
|
|
/// Onion messages and payments can be sent and received to blinded paths, which serve to hide the
|
|
|
|
/// identity of the recipient.
|
2023-03-17 17:45:12 -04:00
|
|
|
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
2022-12-14 20:45:37 +00:00
|
|
|
pub struct BlindedPath {
|
2022-12-14 20:49:53 +00:00
|
|
|
/// To send to a blinded path, the sender first finds a route to the unblinded
|
2022-05-27 16:25:15 -07:00
|
|
|
/// `introduction_node_id`, which can unblind its [`encrypted_payload`] to find out the onion
|
2023-03-15 22:30:41 -04:00
|
|
|
/// message or payment's next hop and forward it along.
|
2022-05-27 16:25:15 -07:00
|
|
|
///
|
|
|
|
/// [`encrypted_payload`]: BlindedHop::encrypted_payload
|
2023-06-20 12:59:35 -04:00
|
|
|
pub introduction_node_id: PublicKey,
|
2022-05-27 16:25:15 -07:00
|
|
|
/// Used by the introduction node to decrypt its [`encrypted_payload`] to forward the onion
|
2023-03-15 22:30:41 -04:00
|
|
|
/// message or payment.
|
2022-05-27 16:25:15 -07:00
|
|
|
///
|
|
|
|
/// [`encrypted_payload`]: BlindedHop::encrypted_payload
|
2023-06-20 12:59:35 -04:00
|
|
|
pub blinding_point: PublicKey,
|
2022-12-14 20:49:53 +00:00
|
|
|
/// The hops composing the blinded path.
|
2023-06-20 12:59:35 -04:00
|
|
|
pub blinded_hops: Vec<BlindedHop>,
|
2022-05-27 16:25:15 -07:00
|
|
|
}
|
|
|
|
|
2023-06-16 15:42:38 -04:00
|
|
|
/// An encrypted payload and node id corresponding to a hop in a payment or onion message path, to
|
|
|
|
/// be encoded in the sender's onion packet. These hops cannot be identified by outside observers
|
|
|
|
/// and thus can be used to hide the identity of the recipient.
|
2023-03-17 17:45:12 -04:00
|
|
|
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
|
2022-05-27 16:25:15 -07:00
|
|
|
pub struct BlindedHop {
|
2023-06-16 15:42:38 -04:00
|
|
|
/// The blinded node id of this hop in a [`BlindedPath`].
|
2023-06-20 12:59:35 -04:00
|
|
|
pub blinded_node_id: PublicKey,
|
2023-06-16 15:42:38 -04:00
|
|
|
/// The encrypted payload intended for this hop in a [`BlindedPath`].
|
2022-12-14 20:49:53 +00:00
|
|
|
// The node sending to this blinded path will later encode this payload into the onion packet for
|
2022-05-27 16:25:15 -07:00
|
|
|
// this hop.
|
2023-06-20 12:59:35 -04:00
|
|
|
pub encrypted_payload: Vec<u8>,
|
2022-05-27 16:25:15 -07:00
|
|
|
}
|
|
|
|
|
2022-12-14 20:45:37 +00:00
|
|
|
impl BlindedPath {
|
2023-09-11 17:40:43 -05:00
|
|
|
/// Create a one-hop blinded path for a message.
|
2023-03-03 09:38:45 -06:00
|
|
|
pub fn one_hop_for_message<ES: EntropySource + ?Sized, T: secp256k1::Signing + secp256k1::Verification>(
|
2023-09-11 17:40:43 -05:00
|
|
|
recipient_node_id: PublicKey, entropy_source: &ES, secp_ctx: &Secp256k1<T>
|
|
|
|
) -> Result<Self, ()> {
|
|
|
|
Self::new_for_message(&[recipient_node_id], entropy_source, secp_ctx)
|
|
|
|
}
|
|
|
|
|
2023-03-15 22:30:41 -04:00
|
|
|
/// Create a blinded path for an onion message, to be forwarded along `node_pks`. The last node
|
|
|
|
/// pubkey in `node_pks` will be the destination node.
|
2022-05-27 16:25:15 -07:00
|
|
|
///
|
2023-09-11 17:40:43 -05:00
|
|
|
/// Errors if no hops are provided or if `node_pk`(s) are invalid.
|
2022-05-27 16:25:15 -07:00
|
|
|
// TODO: make all payloads the same size with padding + add dummy hops
|
2023-03-03 09:38:45 -06:00
|
|
|
pub fn new_for_message<ES: EntropySource + ?Sized, T: secp256k1::Signing + secp256k1::Verification>(
|
2023-09-11 17:40:43 -05:00
|
|
|
node_pks: &[PublicKey], entropy_source: &ES, secp_ctx: &Secp256k1<T>
|
|
|
|
) -> Result<Self, ()> {
|
|
|
|
if node_pks.is_empty() { return Err(()) }
|
2022-12-20 14:46:08 -08:00
|
|
|
let blinding_secret_bytes = entropy_source.get_secure_random_bytes();
|
2022-05-27 16:25:15 -07:00
|
|
|
let blinding_secret = SecretKey::from_slice(&blinding_secret_bytes[..]).expect("RNG is busted");
|
|
|
|
let introduction_node_id = node_pks[0];
|
|
|
|
|
2022-12-14 20:45:37 +00:00
|
|
|
Ok(BlindedPath {
|
2022-05-27 16:25:15 -07:00
|
|
|
introduction_node_id,
|
|
|
|
blinding_point: PublicKey::from_secret_key(secp_ctx, &blinding_secret),
|
2023-06-16 13:42:57 -04:00
|
|
|
blinded_hops: message::blinded_hops(secp_ctx, node_pks, &blinding_secret).map_err(|_| ())?,
|
2022-05-27 16:25:15 -07:00
|
|
|
})
|
|
|
|
}
|
2023-06-16 15:43:13 -04:00
|
|
|
|
2023-09-10 00:42:18 -04:00
|
|
|
/// Create a one-hop blinded path for a payment.
|
2023-09-15 13:40:41 -05:00
|
|
|
pub fn one_hop_for_payment<ES: EntropySource + ?Sized, T: secp256k1::Signing + secp256k1::Verification>(
|
2023-09-10 00:42:18 -04:00
|
|
|
payee_node_id: PublicKey, payee_tlvs: payment::ReceiveTlvs, entropy_source: &ES,
|
|
|
|
secp_ctx: &Secp256k1<T>
|
|
|
|
) -> Result<(BlindedPayInfo, Self), ()> {
|
|
|
|
// This value is not considered in pathfinding for 1-hop blinded paths, because it's intended to
|
|
|
|
// be in relation to a specific channel.
|
|
|
|
let htlc_maximum_msat = u64::max_value();
|
|
|
|
Self::new_for_payment(
|
|
|
|
&[], payee_node_id, payee_tlvs, htlc_maximum_msat, entropy_source, secp_ctx
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-09-07 17:11:30 -04:00
|
|
|
/// Create a blinded path for a payment, to be forwarded along `intermediate_nodes`.
|
2023-06-16 15:43:13 -04:00
|
|
|
///
|
2023-06-20 20:27:57 -04:00
|
|
|
/// Errors if:
|
|
|
|
/// * a provided node id is invalid
|
|
|
|
/// * [`BlindedPayInfo`] calculation results in an integer overflow
|
|
|
|
/// * any unknown features are required in the provided [`ForwardTlvs`]
|
|
|
|
///
|
|
|
|
/// [`ForwardTlvs`]: crate::blinded_path::payment::ForwardTlvs
|
2023-06-16 15:43:13 -04:00
|
|
|
// TODO: make all payloads the same size with padding + add dummy hops
|
2023-09-15 13:40:41 -05:00
|
|
|
pub(crate) fn new_for_payment<ES: EntropySource + ?Sized, T: secp256k1::Signing + secp256k1::Verification>(
|
2023-09-07 17:11:30 -04:00
|
|
|
intermediate_nodes: &[payment::ForwardNode], payee_node_id: PublicKey,
|
2023-08-19 17:56:33 -04:00
|
|
|
payee_tlvs: payment::ReceiveTlvs, htlc_maximum_msat: u64, entropy_source: &ES,
|
|
|
|
secp_ctx: &Secp256k1<T>
|
2023-06-20 20:27:57 -04:00
|
|
|
) -> Result<(BlindedPayInfo, Self), ()> {
|
2023-06-16 15:43:13 -04:00
|
|
|
let blinding_secret_bytes = entropy_source.get_secure_random_bytes();
|
|
|
|
let blinding_secret = SecretKey::from_slice(&blinding_secret_bytes[..]).expect("RNG is busted");
|
|
|
|
|
2023-08-19 17:56:33 -04:00
|
|
|
let blinded_payinfo = payment::compute_payinfo(intermediate_nodes, &payee_tlvs, htlc_maximum_msat)?;
|
2023-06-20 20:27:57 -04:00
|
|
|
Ok((blinded_payinfo, BlindedPath {
|
2023-09-07 17:11:30 -04:00
|
|
|
introduction_node_id: intermediate_nodes.first().map_or(payee_node_id, |n| n.node_id),
|
2023-06-16 15:43:13 -04:00
|
|
|
blinding_point: PublicKey::from_secret_key(secp_ctx, &blinding_secret),
|
|
|
|
blinded_hops: payment::blinded_hops(
|
|
|
|
secp_ctx, intermediate_nodes, payee_node_id, payee_tlvs, &blinding_secret
|
|
|
|
).map_err(|_| ())?,
|
2023-06-20 20:27:57 -04:00
|
|
|
}))
|
2023-06-16 15:43:13 -04:00
|
|
|
}
|
2022-05-27 16:25:15 -07:00
|
|
|
}
|
|
|
|
|
2022-12-14 20:45:37 +00:00
|
|
|
impl Writeable for BlindedPath {
|
2022-08-05 18:03:12 -04:00
|
|
|
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
|
|
|
|
self.introduction_node_id.write(w)?;
|
|
|
|
self.blinding_point.write(w)?;
|
|
|
|
(self.blinded_hops.len() as u8).write(w)?;
|
|
|
|
for hop in &self.blinded_hops {
|
|
|
|
hop.write(w)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-14 20:45:37 +00:00
|
|
|
impl Readable for BlindedPath {
|
2022-08-05 18:03:12 -04:00
|
|
|
fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
|
|
|
|
let introduction_node_id = Readable::read(r)?;
|
|
|
|
let blinding_point = Readable::read(r)?;
|
|
|
|
let num_hops: u8 = Readable::read(r)?;
|
|
|
|
if num_hops == 0 { return Err(DecodeError::InvalidValue) }
|
|
|
|
let mut blinded_hops: Vec<BlindedHop> = Vec::with_capacity(num_hops.into());
|
|
|
|
for _ in 0..num_hops {
|
|
|
|
blinded_hops.push(Readable::read(r)?);
|
|
|
|
}
|
2022-12-14 20:45:37 +00:00
|
|
|
Ok(BlindedPath {
|
2022-08-05 18:03:12 -04:00
|
|
|
introduction_node_id,
|
|
|
|
blinding_point,
|
|
|
|
blinded_hops,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_writeable!(BlindedHop, {
|
|
|
|
blinded_node_id,
|
|
|
|
encrypted_payload
|
|
|
|
});
|
|
|
|
|