Make advance_path_by_one an associated method.

This commit is contained in:
Valentine Wallace 2024-08-15 18:18:51 -04:00
parent 8eb3344216
commit b204a6a11d
No known key found for this signature in database
GPG key ID: FD3E106A2CE099B4
4 changed files with 84 additions and 84 deletions

View file

@ -138,6 +138,50 @@ impl BlindedMessagePath {
&self.0.blinded_hops
}
/// Advance the blinded onion message path by one hop, making the second hop into the new
/// introduction node.
///
/// Will only modify `self` when returning `Ok`.
pub fn advance_path_by_one<NS: Deref, NL: Deref, T>(
&mut self, node_signer: &NS, node_id_lookup: &NL, secp_ctx: &Secp256k1<T>
) -> Result<(), ()>
where
NS::Target: NodeSigner,
NL::Target: NodeIdLookUp,
T: secp256k1::Signing + secp256k1::Verification,
{
let control_tlvs_ss = node_signer.ecdh(Recipient::Node, &self.0.blinding_point, None)?;
let rho = onion_utils::gen_rho_from_shared_secret(&control_tlvs_ss.secret_bytes());
let encrypted_control_tlvs = &self.0.blinded_hops.get(0).ok_or(())?.encrypted_payload;
let mut s = Cursor::new(encrypted_control_tlvs);
let mut reader = FixedLengthReader::new(&mut s, encrypted_control_tlvs.len() as u64);
match ChaChaPolyReadAdapter::read(&mut reader, rho) {
Ok(ChaChaPolyReadAdapter {
readable: ControlTlvs::Forward(ForwardTlvs { next_hop, next_blinding_override })
}) => {
let next_node_id = match next_hop {
NextMessageHop::NodeId(pubkey) => pubkey,
NextMessageHop::ShortChannelId(scid) => match node_id_lookup.next_node_id(scid) {
Some(pubkey) => pubkey,
None => return Err(()),
},
};
let mut new_blinding_point = match next_blinding_override {
Some(blinding_point) => blinding_point,
None => {
onion_utils::next_hop_pubkey(secp_ctx, self.0.blinding_point,
control_tlvs_ss.as_ref()).map_err(|_| ())?
}
};
mem::swap(&mut self.0.blinding_point, &mut new_blinding_point);
self.0.introduction_node = IntroductionNode::NodeId(next_node_id);
self.0.blinded_hops.remove(0);
Ok(())
},
_ => Err(())
}
}
pub(crate) fn introduction_node_mut(&mut self) -> &mut IntroductionNode {
&mut self.0.introduction_node
}
@ -345,46 +389,3 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
utils::construct_blinded_hops(secp_ctx, pks, tlvs, session_priv)
}
/// Advance the blinded onion message path by one hop, making the second hop into the new
/// introduction node.
///
/// Will only modify `path` when returning `Ok`.
pub fn advance_path_by_one<NS: Deref, NL: Deref, T>(
path: &mut BlindedMessagePath, node_signer: &NS, node_id_lookup: &NL, secp_ctx: &Secp256k1<T>
) -> Result<(), ()>
where
NS::Target: NodeSigner,
NL::Target: NodeIdLookUp,
T: secp256k1::Signing + secp256k1::Verification,
{
let control_tlvs_ss = node_signer.ecdh(Recipient::Node, &path.0.blinding_point, None)?;
let rho = onion_utils::gen_rho_from_shared_secret(&control_tlvs_ss.secret_bytes());
let encrypted_control_tlvs = &path.0.blinded_hops.get(0).ok_or(())?.encrypted_payload;
let mut s = Cursor::new(encrypted_control_tlvs);
let mut reader = FixedLengthReader::new(&mut s, encrypted_control_tlvs.len() as u64);
match ChaChaPolyReadAdapter::read(&mut reader, rho) {
Ok(ChaChaPolyReadAdapter {
readable: ControlTlvs::Forward(ForwardTlvs { next_hop, next_blinding_override })
}) => {
let next_node_id = match next_hop {
NextMessageHop::NodeId(pubkey) => pubkey,
NextMessageHop::ShortChannelId(scid) => match node_id_lookup.next_node_id(scid) {
Some(pubkey) => pubkey,
None => return Err(()),
},
};
let mut new_blinding_point = match next_blinding_override {
Some(blinding_point) => blinding_point,
None => {
onion_utils::next_hop_pubkey(secp_ctx, path.0.blinding_point,
control_tlvs_ss.as_ref()).map_err(|_| ())?
}
};
mem::swap(&mut path.0.blinding_point, &mut new_blinding_point);
path.0.introduction_node = IntroductionNode::NodeId(next_node_id);
path.0.blinded_hops.remove(0);
Ok(())
},
_ => Err(())
}
}

View file

@ -121,6 +121,43 @@ impl BlindedPaymentPath {
&self.0.blinded_hops
}
/// Advance the blinded onion payment path by one hop, making the second hop into the new
/// introduction node.
///
/// Will only modify `self` when returning `Ok`.
pub fn advance_path_by_one<NS: Deref, NL: Deref, T>(
&mut self, node_signer: &NS, node_id_lookup: &NL, secp_ctx: &Secp256k1<T>
) -> Result<(), ()>
where
NS::Target: NodeSigner,
NL::Target: NodeIdLookUp,
T: secp256k1::Signing + secp256k1::Verification,
{
let control_tlvs_ss = node_signer.ecdh(Recipient::Node, &self.0.blinding_point, None)?;
let rho = onion_utils::gen_rho_from_shared_secret(&control_tlvs_ss.secret_bytes());
let encrypted_control_tlvs = &self.0.blinded_hops.get(0).ok_or(())?.encrypted_payload;
let mut s = Cursor::new(encrypted_control_tlvs);
let mut reader = FixedLengthReader::new(&mut s, encrypted_control_tlvs.len() as u64);
match ChaChaPolyReadAdapter::read(&mut reader, rho) {
Ok(ChaChaPolyReadAdapter {
readable: BlindedPaymentTlvs::Forward(ForwardTlvs { short_channel_id, .. })
}) => {
let next_node_id = match node_id_lookup.next_node_id(short_channel_id) {
Some(node_id) => node_id,
None => return Err(()),
};
let mut new_blinding_point = onion_utils::next_hop_pubkey(
secp_ctx, self.0.blinding_point, control_tlvs_ss.as_ref()
).map_err(|_| ())?;
mem::swap(&mut self.0.blinding_point, &mut new_blinding_point);
self.0.introduction_node = IntroductionNode::NodeId(next_node_id);
self.0.blinded_hops.remove(0);
Ok(())
},
_ => Err(())
}
}
#[cfg(any(test, fuzzing))]
pub fn from_raw(
introduction_node_id: PublicKey, blinding_point: PublicKey, blinded_hops: Vec<BlindedHop>
@ -383,43 +420,6 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
utils::construct_blinded_hops(secp_ctx, pks, tlvs, session_priv)
}
/// Advance the blinded onion payment path by one hop, making the second hop into the new
/// introduction node.
///
/// Will only modify `path` when returning `Ok`.
pub fn advance_path_by_one<NS: Deref, NL: Deref, T>(
path: &mut BlindedPaymentPath, node_signer: &NS, node_id_lookup: &NL, secp_ctx: &Secp256k1<T>
) -> Result<(), ()>
where
NS::Target: NodeSigner,
NL::Target: NodeIdLookUp,
T: secp256k1::Signing + secp256k1::Verification,
{
let control_tlvs_ss = node_signer.ecdh(Recipient::Node, &path.0.blinding_point, None)?;
let rho = onion_utils::gen_rho_from_shared_secret(&control_tlvs_ss.secret_bytes());
let encrypted_control_tlvs = &path.0.blinded_hops.get(0).ok_or(())?.encrypted_payload;
let mut s = Cursor::new(encrypted_control_tlvs);
let mut reader = FixedLengthReader::new(&mut s, encrypted_control_tlvs.len() as u64);
match ChaChaPolyReadAdapter::read(&mut reader, rho) {
Ok(ChaChaPolyReadAdapter {
readable: BlindedPaymentTlvs::Forward(ForwardTlvs { short_channel_id, .. })
}) => {
let next_node_id = match node_id_lookup.next_node_id(short_channel_id) {
Some(node_id) => node_id,
None => return Err(()),
};
let mut new_blinding_point = onion_utils::next_hop_pubkey(
secp_ctx, path.0.blinding_point, control_tlvs_ss.as_ref()
).map_err(|_| ())?;
mem::swap(&mut path.0.blinding_point, &mut new_blinding_point);
path.0.introduction_node = IntroductionNode::NodeId(next_node_id);
path.0.blinded_hops.remove(0);
Ok(())
},
_ => Err(())
}
}
/// `None` if underflow occurs.
pub(crate) fn amt_to_forward_msat(inbound_amt_msat: u64, payment_relay: &PaymentRelay) -> Option<u64> {
let inbound_amt = inbound_amt_msat as u128;

View file

@ -14,7 +14,6 @@ use bitcoin::hashes::sha256::Hash as Sha256;
use bitcoin::secp256k1::{self, Secp256k1, SecretKey};
use crate::blinded_path::{IntroductionNode, NodeIdLookUp};
use crate::blinded_path::payment::advance_path_by_one;
use crate::events::{self, PaymentFailureReason};
use crate::ln::types::{PaymentHash, PaymentPreimage, PaymentSecret};
use crate::ln::channel_state::ChannelDetails;
@ -845,7 +844,7 @@ impl OutboundPayments {
},
};
if introduction_node_id == our_node_id {
let _ = advance_path_by_one(path, node_signer, node_id_lookup, secp_ctx);
let _ = path.advance_path_by_one(node_signer, node_id_lookup, secp_ctx);
}
}
}

View file

@ -16,7 +16,7 @@ use bitcoin::hashes::sha256::Hash as Sha256;
use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey};
use crate::blinded_path::{IntroductionNode, NodeIdLookUp};
use crate::blinded_path::message::{advance_path_by_one, BlindedMessagePath, ForwardNode, ForwardTlvs, MessageContext, NextMessageHop, ReceiveTlvs};
use crate::blinded_path::message::{BlindedMessagePath, ForwardNode, ForwardTlvs, MessageContext, NextMessageHop, ReceiveTlvs};
use crate::blinded_path::utils;
use crate::events::{Event, EventHandler, EventsProvider, ReplayEvent};
use crate::sign::{EntropySource, NodeSigner, Recipient};
@ -901,7 +901,7 @@ where
},
};
if introduction_node_id == our_node_id {
advance_path_by_one(blinded_path, node_signer, node_id_lookup, &secp_ctx)
blinded_path.advance_path_by_one(node_signer, node_id_lookup, &secp_ctx)
.map_err(|()| SendError::BlindedPathAdvanceFailed)?;
}
}