Update docs and method names for blinded payment paths

This commit is contained in:
Valentine Wallace 2023-03-15 22:30:41 -04:00
parent efed905a4f
commit e691e5077d
No known key found for this signature in database
GPG key ID: FD3E106A2CE099B4
5 changed files with 28 additions and 27 deletions

View file

@ -74,8 +74,8 @@ fn build_response<'a, T: secp256k1::Signing + secp256k1::Verification>(
) -> Result<UnsignedInvoice<'a>, SemanticError> { ) -> Result<UnsignedInvoice<'a>, SemanticError> {
let entropy_source = Randomness {}; let entropy_source = Randomness {};
let paths = vec![ let paths = vec![
BlindedPath::new(&[pubkey(43), pubkey(44), pubkey(42)], &entropy_source, secp_ctx).unwrap(), BlindedPath::new_for_message(&[pubkey(43), pubkey(44), pubkey(42)], &entropy_source, secp_ctx).unwrap(),
BlindedPath::new(&[pubkey(45), pubkey(46), pubkey(42)], &entropy_source, secp_ctx).unwrap(), BlindedPath::new_for_message(&[pubkey(45), pubkey(46), pubkey(42)], &entropy_source, secp_ctx).unwrap(),
]; ];
let payinfo = vec![ let payinfo = vec![

View file

@ -63,8 +63,8 @@ fn build_response<'a, T: secp256k1::Signing + secp256k1::Verification>(
) -> Result<UnsignedInvoice<'a>, SemanticError> { ) -> Result<UnsignedInvoice<'a>, SemanticError> {
let entropy_source = Randomness {}; let entropy_source = Randomness {};
let paths = vec![ let paths = vec![
BlindedPath::new(&[pubkey(43), pubkey(44), pubkey(42)], &entropy_source, secp_ctx).unwrap(), BlindedPath::new_for_message(&[pubkey(43), pubkey(44), pubkey(42)], &entropy_source, secp_ctx).unwrap(),
BlindedPath::new(&[pubkey(45), pubkey(46), pubkey(42)], &entropy_source, secp_ctx).unwrap(), BlindedPath::new_for_message(&[pubkey(45), pubkey(46), pubkey(42)], &entropy_source, secp_ctx).unwrap(),
]; ];
let payinfo = vec![ let payinfo = vec![

View file

@ -27,18 +27,18 @@ use core::ops::Deref;
use crate::io::{self, Cursor}; use crate::io::{self, Cursor};
use crate::prelude::*; use crate::prelude::*;
/// Onion messages can be sent and received to blinded paths, which serve to hide the identity of /// Onion messages and payments can be sent and received to blinded paths, which serve to hide the
/// the recipient. /// identity of the recipient.
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct BlindedPath { pub struct BlindedPath {
/// To send to a blinded path, the sender first finds a route to the unblinded /// To send to a blinded path, the sender first finds a route to the unblinded
/// `introduction_node_id`, which can unblind its [`encrypted_payload`] to find out the onion /// `introduction_node_id`, which can unblind its [`encrypted_payload`] to find out the onion
/// message's next hop and forward it along. /// message or payment's next hop and forward it along.
/// ///
/// [`encrypted_payload`]: BlindedHop::encrypted_payload /// [`encrypted_payload`]: BlindedHop::encrypted_payload
pub(crate) introduction_node_id: PublicKey, pub(crate) introduction_node_id: PublicKey,
/// Used by the introduction node to decrypt its [`encrypted_payload`] to forward the onion /// Used by the introduction node to decrypt its [`encrypted_payload`] to forward the onion
/// message. /// message or payment.
/// ///
/// [`encrypted_payload`]: BlindedHop::encrypted_payload /// [`encrypted_payload`]: BlindedHop::encrypted_payload
pub(crate) blinding_point: PublicKey, pub(crate) blinding_point: PublicKey,
@ -59,12 +59,12 @@ pub struct BlindedHop {
} }
impl BlindedPath { impl BlindedPath {
/// Create a blinded path to be forwarded along `node_pks`. The last node pubkey in `node_pks` /// Create a blinded path for an onion message, to be forwarded along `node_pks`. The last node
/// will be the destination node. /// pubkey in `node_pks` will be the destination node.
/// ///
/// Errors if less than two hops are provided or if `node_pk`(s) are invalid. /// Errors if less than two hops are provided or if `node_pk`(s) are invalid.
// TODO: make all payloads the same size with padding + add dummy hops // TODO: make all payloads the same size with padding + add dummy hops
pub fn new<ES: EntropySource, T: secp256k1::Signing + secp256k1::Verification> pub fn new_for_message<ES: EntropySource, T: secp256k1::Signing + secp256k1::Verification>
(node_pks: &[PublicKey], entropy_source: &ES, secp_ctx: &Secp256k1<T>) -> Result<Self, ()> (node_pks: &[PublicKey], entropy_source: &ES, secp_ctx: &Secp256k1<T>) -> Result<Self, ()>
{ {
if node_pks.len() < 2 { return Err(()) } if node_pks.len() < 2 { return Err(()) }
@ -75,12 +75,13 @@ impl BlindedPath {
Ok(BlindedPath { Ok(BlindedPath {
introduction_node_id, introduction_node_id,
blinding_point: PublicKey::from_secret_key(secp_ctx, &blinding_secret), blinding_point: PublicKey::from_secret_key(secp_ctx, &blinding_secret),
blinded_hops: blinded_hops(secp_ctx, node_pks, &blinding_secret).map_err(|_| ())?, blinded_hops: blinded_message_hops(secp_ctx, node_pks, &blinding_secret).map_err(|_| ())?,
}) })
} }
// Advance the blinded path by one hop, so make the second hop into the new introduction node. // Advance the blinded onion message path by one hop, so make the second hop into the new
pub(super) fn advance_by_one<NS: Deref, T: secp256k1::Signing + secp256k1::Verification> // introduction node.
pub(super) fn advance_message_path_by_one<NS: Deref, T: secp256k1::Signing + secp256k1::Verification>
(&mut self, node_signer: &NS, secp_ctx: &Secp256k1<T>) -> Result<(), ()> (&mut self, node_signer: &NS, secp_ctx: &Secp256k1<T>) -> Result<(), ()>
where NS::Target: NodeSigner where NS::Target: NodeSigner
{ {
@ -115,8 +116,8 @@ impl BlindedPath {
} }
} }
/// Construct blinded hops for the given `unblinded_path`. /// Construct blinded onion message hops for the given `unblinded_path`.
fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>( fn blinded_message_hops<T: secp256k1::Signing + secp256k1::Verification>(
secp_ctx: &Secp256k1<T>, unblinded_path: &[PublicKey], session_priv: &SecretKey secp_ctx: &Secp256k1<T>, unblinded_path: &[PublicKey], session_priv: &SecretKey
) -> Result<Vec<BlindedHop>, secp256k1::Error> { ) -> Result<Vec<BlindedHop>, secp256k1::Error> {
let mut blinded_hops = Vec::with_capacity(unblinded_path.len()); let mut blinded_hops = Vec::with_capacity(unblinded_path.len());

View file

@ -136,7 +136,7 @@ fn two_unblinded_two_blinded() {
let test_msg = OnionMessageContents::Custom(TestCustomMessage {}); let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
let secp_ctx = Secp256k1::new(); let secp_ctx = Secp256k1::new();
let blinded_path = BlindedPath::new(&[nodes[3].get_node_pk(), nodes[4].get_node_pk()], &*nodes[4].keys_manager, &secp_ctx).unwrap(); let blinded_path = BlindedPath::new_for_message(&[nodes[3].get_node_pk(), nodes[4].get_node_pk()], &*nodes[4].keys_manager, &secp_ctx).unwrap();
nodes[0].messenger.send_onion_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk()], Destination::BlindedPath(blinded_path), test_msg, None).unwrap(); nodes[0].messenger.send_onion_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk()], Destination::BlindedPath(blinded_path), test_msg, None).unwrap();
pass_along_path(&nodes, None); pass_along_path(&nodes, None);
@ -148,7 +148,7 @@ fn three_blinded_hops() {
let test_msg = OnionMessageContents::Custom(TestCustomMessage {}); let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
let secp_ctx = Secp256k1::new(); let secp_ctx = Secp256k1::new();
let blinded_path = BlindedPath::new(&[nodes[1].get_node_pk(), nodes[2].get_node_pk(), nodes[3].get_node_pk()], &*nodes[3].keys_manager, &secp_ctx).unwrap(); let blinded_path = BlindedPath::new_for_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk(), nodes[3].get_node_pk()], &*nodes[3].keys_manager, &secp_ctx).unwrap();
nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), test_msg, None).unwrap(); nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), test_msg, None).unwrap();
pass_along_path(&nodes, None); pass_along_path(&nodes, None);
@ -174,13 +174,13 @@ fn we_are_intro_node() {
let test_msg = TestCustomMessage {}; let test_msg = TestCustomMessage {};
let secp_ctx = Secp256k1::new(); let secp_ctx = Secp256k1::new();
let blinded_path = BlindedPath::new(&[nodes[0].get_node_pk(), nodes[1].get_node_pk(), nodes[2].get_node_pk()], &*nodes[2].keys_manager, &secp_ctx).unwrap(); let blinded_path = BlindedPath::new_for_message(&[nodes[0].get_node_pk(), nodes[1].get_node_pk(), nodes[2].get_node_pk()], &*nodes[2].keys_manager, &secp_ctx).unwrap();
nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), OnionMessageContents::Custom(test_msg.clone()), None).unwrap(); nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), OnionMessageContents::Custom(test_msg.clone()), None).unwrap();
pass_along_path(&nodes, None); pass_along_path(&nodes, None);
// Try with a two-hop blinded path where we are the introduction node. // Try with a two-hop blinded path where we are the introduction node.
let blinded_path = BlindedPath::new(&[nodes[0].get_node_pk(), nodes[1].get_node_pk()], &*nodes[1].keys_manager, &secp_ctx).unwrap(); let blinded_path = BlindedPath::new_for_message(&[nodes[0].get_node_pk(), nodes[1].get_node_pk()], &*nodes[1].keys_manager, &secp_ctx).unwrap();
nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), OnionMessageContents::Custom(test_msg), None).unwrap(); nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), OnionMessageContents::Custom(test_msg), None).unwrap();
nodes.remove(2); nodes.remove(2);
pass_along_path(&nodes, None); pass_along_path(&nodes, None);
@ -194,13 +194,13 @@ fn invalid_blinded_path_error() {
// 0 hops // 0 hops
let secp_ctx = Secp256k1::new(); let secp_ctx = Secp256k1::new();
let mut blinded_path = BlindedPath::new(&[nodes[1].get_node_pk(), nodes[2].get_node_pk()], &*nodes[2].keys_manager, &secp_ctx).unwrap(); let mut blinded_path = BlindedPath::new_for_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk()], &*nodes[2].keys_manager, &secp_ctx).unwrap();
blinded_path.blinded_hops.clear(); blinded_path.blinded_hops.clear();
let err = nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), OnionMessageContents::Custom(test_msg.clone()), None).unwrap_err(); let err = nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), OnionMessageContents::Custom(test_msg.clone()), None).unwrap_err();
assert_eq!(err, SendError::TooFewBlindedHops); assert_eq!(err, SendError::TooFewBlindedHops);
// 1 hop // 1 hop
let mut blinded_path = BlindedPath::new(&[nodes[1].get_node_pk(), nodes[2].get_node_pk()], &*nodes[2].keys_manager, &secp_ctx).unwrap(); let mut blinded_path = BlindedPath::new_for_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk()], &*nodes[2].keys_manager, &secp_ctx).unwrap();
blinded_path.blinded_hops.remove(0); blinded_path.blinded_hops.remove(0);
assert_eq!(blinded_path.blinded_hops.len(), 1); assert_eq!(blinded_path.blinded_hops.len(), 1);
let err = nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), OnionMessageContents::Custom(test_msg), None).unwrap_err(); let err = nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), OnionMessageContents::Custom(test_msg), None).unwrap_err();
@ -214,7 +214,7 @@ fn reply_path() {
let secp_ctx = Secp256k1::new(); let secp_ctx = Secp256k1::new();
// Destination::Node // Destination::Node
let reply_path = BlindedPath::new(&[nodes[2].get_node_pk(), nodes[1].get_node_pk(), nodes[0].get_node_pk()], &*nodes[0].keys_manager, &secp_ctx).unwrap(); let reply_path = BlindedPath::new_for_message(&[nodes[2].get_node_pk(), nodes[1].get_node_pk(), nodes[0].get_node_pk()], &*nodes[0].keys_manager, &secp_ctx).unwrap();
nodes[0].messenger.send_onion_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk()], Destination::Node(nodes[3].get_node_pk()), OnionMessageContents::Custom(test_msg.clone()), Some(reply_path)).unwrap(); nodes[0].messenger.send_onion_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk()], Destination::Node(nodes[3].get_node_pk()), OnionMessageContents::Custom(test_msg.clone()), Some(reply_path)).unwrap();
pass_along_path(&nodes, None); pass_along_path(&nodes, None);
// Make sure the last node successfully decoded the reply path. // Make sure the last node successfully decoded the reply path.
@ -223,8 +223,8 @@ fn reply_path() {
&format!("Received an onion message with path_id None and a reply_path"), 1); &format!("Received an onion message with path_id None and a reply_path"), 1);
// Destination::BlindedPath // Destination::BlindedPath
let blinded_path = BlindedPath::new(&[nodes[1].get_node_pk(), nodes[2].get_node_pk(), nodes[3].get_node_pk()], &*nodes[3].keys_manager, &secp_ctx).unwrap(); let blinded_path = BlindedPath::new_for_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk(), nodes[3].get_node_pk()], &*nodes[3].keys_manager, &secp_ctx).unwrap();
let reply_path = BlindedPath::new(&[nodes[2].get_node_pk(), nodes[1].get_node_pk(), nodes[0].get_node_pk()], &*nodes[0].keys_manager, &secp_ctx).unwrap(); let reply_path = BlindedPath::new_for_message(&[nodes[2].get_node_pk(), nodes[1].get_node_pk(), nodes[0].get_node_pk()], &*nodes[0].keys_manager, &secp_ctx).unwrap();
nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), OnionMessageContents::Custom(test_msg), Some(reply_path)).unwrap(); nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), OnionMessageContents::Custom(test_msg), Some(reply_path)).unwrap();
pass_along_path(&nodes, None); pass_along_path(&nodes, None);

View file

@ -91,7 +91,7 @@ use crate::prelude::*;
/// // Create a blinded path to yourself, for someone to send an onion message to. /// // Create a blinded path to yourself, for someone to send an onion message to.
/// # let your_node_id = hop_node_id1; /// # let your_node_id = hop_node_id1;
/// let hops = [hop_node_id3, hop_node_id4, your_node_id]; /// let hops = [hop_node_id3, hop_node_id4, your_node_id];
/// let blinded_path = BlindedPath::new(&hops, &keys_manager, &secp_ctx).unwrap(); /// let blinded_path = BlindedPath::new_for_message(&hops, &keys_manager, &secp_ctx).unwrap();
/// ///
/// // Send a custom onion message to a blinded path. /// // Send a custom onion message to a blinded path.
/// # let intermediate_hops = [hop_node_id1, hop_node_id2]; /// # let intermediate_hops = [hop_node_id1, hop_node_id2];
@ -226,7 +226,7 @@ impl<ES: Deref, NS: Deref, L: Deref, CMH: Deref> OnionMessenger<ES, NS, L, CMH>
let our_node_id = self.node_signer.get_node_id(Recipient::Node) let our_node_id = self.node_signer.get_node_id(Recipient::Node)
.map_err(|()| SendError::GetNodeIdFailed)?; .map_err(|()| SendError::GetNodeIdFailed)?;
if blinded_path.introduction_node_id == our_node_id { if blinded_path.introduction_node_id == our_node_id {
blinded_path.advance_by_one(&self.node_signer, &self.secp_ctx) blinded_path.advance_message_path_by_one(&self.node_signer, &self.secp_ctx)
.map_err(|()| SendError::BlindedPathAdvanceFailed)?; .map_err(|()| SendError::BlindedPathAdvanceFailed)?;
} }
} }