Skip to content

Commit af64091

Browse files
Make advance_path_by_one an associated method.
1 parent b98bc43 commit af64091

File tree

4 files changed

+84
-84
lines changed

4 files changed

+84
-84
lines changed

lightning/src/blinded_path/message.rs

+44-43
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,50 @@ impl BlindedMessagePath {
138138
&self.0.blinded_hops
139139
}
140140

141+
/// Advance the blinded onion message path by one hop, making the second hop into the new
142+
/// introduction node.
143+
///
144+
/// Will only modify `path` when returning `Ok`.
145+
pub fn advance_path_by_one<NS: Deref, NL: Deref, T>(
146+
&mut self, node_signer: &NS, node_id_lookup: &NL, secp_ctx: &Secp256k1<T>
147+
) -> Result<(), ()>
148+
where
149+
NS::Target: NodeSigner,
150+
NL::Target: NodeIdLookUp,
151+
T: secp256k1::Signing + secp256k1::Verification,
152+
{
153+
let control_tlvs_ss = node_signer.ecdh(Recipient::Node, &self.0.blinding_point, None)?;
154+
let rho = onion_utils::gen_rho_from_shared_secret(&control_tlvs_ss.secret_bytes());
155+
let encrypted_control_tlvs = &self.0.blinded_hops.get(0).ok_or(())?.encrypted_payload;
156+
let mut s = Cursor::new(encrypted_control_tlvs);
157+
let mut reader = FixedLengthReader::new(&mut s, encrypted_control_tlvs.len() as u64);
158+
match ChaChaPolyReadAdapter::read(&mut reader, rho) {
159+
Ok(ChaChaPolyReadAdapter {
160+
readable: ControlTlvs::Forward(ForwardTlvs { next_hop, next_blinding_override })
161+
}) => {
162+
let next_node_id = match next_hop {
163+
NextMessageHop::NodeId(pubkey) => pubkey,
164+
NextMessageHop::ShortChannelId(scid) => match node_id_lookup.next_node_id(scid) {
165+
Some(pubkey) => pubkey,
166+
None => return Err(()),
167+
},
168+
};
169+
let mut new_blinding_point = match next_blinding_override {
170+
Some(blinding_point) => blinding_point,
171+
None => {
172+
onion_utils::next_hop_pubkey(secp_ctx, self.0.blinding_point,
173+
control_tlvs_ss.as_ref()).map_err(|_| ())?
174+
}
175+
};
176+
mem::swap(&mut self.0.blinding_point, &mut new_blinding_point);
177+
self.0.introduction_node = IntroductionNode::NodeId(next_node_id);
178+
self.0.blinded_hops.remove(0);
179+
Ok(())
180+
},
181+
_ => Err(())
182+
}
183+
}
184+
141185
pub(crate) fn set_introduction_node_id(&mut self, node_id: PublicKey) {
142186
self.0.introduction_node = IntroductionNode::NodeId(node_id);
143187
}
@@ -345,46 +389,3 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
345389
utils::construct_blinded_hops(secp_ctx, pks, tlvs, session_priv)
346390
}
347391

348-
/// Advance the blinded onion message path by one hop, making the second hop into the new
349-
/// introduction node.
350-
///
351-
/// Will only modify `path` when returning `Ok`.
352-
pub fn advance_path_by_one<NS: Deref, NL: Deref, T>(
353-
path: &mut BlindedMessagePath, node_signer: &NS, node_id_lookup: &NL, secp_ctx: &Secp256k1<T>
354-
) -> Result<(), ()>
355-
where
356-
NS::Target: NodeSigner,
357-
NL::Target: NodeIdLookUp,
358-
T: secp256k1::Signing + secp256k1::Verification,
359-
{
360-
let control_tlvs_ss = node_signer.ecdh(Recipient::Node, &path.0.blinding_point, None)?;
361-
let rho = onion_utils::gen_rho_from_shared_secret(&control_tlvs_ss.secret_bytes());
362-
let encrypted_control_tlvs = &path.0.blinded_hops.get(0).ok_or(())?.encrypted_payload;
363-
let mut s = Cursor::new(encrypted_control_tlvs);
364-
let mut reader = FixedLengthReader::new(&mut s, encrypted_control_tlvs.len() as u64);
365-
match ChaChaPolyReadAdapter::read(&mut reader, rho) {
366-
Ok(ChaChaPolyReadAdapter {
367-
readable: ControlTlvs::Forward(ForwardTlvs { next_hop, next_blinding_override })
368-
}) => {
369-
let next_node_id = match next_hop {
370-
NextMessageHop::NodeId(pubkey) => pubkey,
371-
NextMessageHop::ShortChannelId(scid) => match node_id_lookup.next_node_id(scid) {
372-
Some(pubkey) => pubkey,
373-
None => return Err(()),
374-
},
375-
};
376-
let mut new_blinding_point = match next_blinding_override {
377-
Some(blinding_point) => blinding_point,
378-
None => {
379-
onion_utils::next_hop_pubkey(secp_ctx, path.0.blinding_point,
380-
control_tlvs_ss.as_ref()).map_err(|_| ())?
381-
}
382-
};
383-
mem::swap(&mut path.0.blinding_point, &mut new_blinding_point);
384-
path.0.introduction_node = IntroductionNode::NodeId(next_node_id);
385-
path.0.blinded_hops.remove(0);
386-
Ok(())
387-
},
388-
_ => Err(())
389-
}
390-
}

lightning/src/blinded_path/payment.rs

+37-37
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,43 @@ impl BlindedPaymentPath {
121121
&self.0.blinded_hops
122122
}
123123

124+
/// Advance the blinded onion payment path by one hop, making the second hop into the new
125+
/// introduction node.
126+
///
127+
/// Will only modify `path` when returning `Ok`.
128+
pub fn advance_path_by_one<NS: Deref, NL: Deref, T>(
129+
&mut self, node_signer: &NS, node_id_lookup: &NL, secp_ctx: &Secp256k1<T>
130+
) -> Result<(), ()>
131+
where
132+
NS::Target: NodeSigner,
133+
NL::Target: NodeIdLookUp,
134+
T: secp256k1::Signing + secp256k1::Verification,
135+
{
136+
let control_tlvs_ss = node_signer.ecdh(Recipient::Node, &self.0.blinding_point, None)?;
137+
let rho = onion_utils::gen_rho_from_shared_secret(&control_tlvs_ss.secret_bytes());
138+
let encrypted_control_tlvs = &self.0.blinded_hops.get(0).ok_or(())?.encrypted_payload;
139+
let mut s = Cursor::new(encrypted_control_tlvs);
140+
let mut reader = FixedLengthReader::new(&mut s, encrypted_control_tlvs.len() as u64);
141+
match ChaChaPolyReadAdapter::read(&mut reader, rho) {
142+
Ok(ChaChaPolyReadAdapter {
143+
readable: BlindedPaymentTlvs::Forward(ForwardTlvs { short_channel_id, .. })
144+
}) => {
145+
let next_node_id = match node_id_lookup.next_node_id(short_channel_id) {
146+
Some(node_id) => node_id,
147+
None => return Err(()),
148+
};
149+
let mut new_blinding_point = onion_utils::next_hop_pubkey(
150+
secp_ctx, self.0.blinding_point, control_tlvs_ss.as_ref()
151+
).map_err(|_| ())?;
152+
mem::swap(&mut self.0.blinding_point, &mut new_blinding_point);
153+
self.0.introduction_node = IntroductionNode::NodeId(next_node_id);
154+
self.0.blinded_hops.remove(0);
155+
Ok(())
156+
},
157+
_ => Err(())
158+
}
159+
}
160+
124161
#[cfg(any(test, fuzzing))]
125162
pub fn from_raw(
126163
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>(
383420
utils::construct_blinded_hops(secp_ctx, pks, tlvs, session_priv)
384421
}
385422

386-
/// Advance the blinded onion payment path by one hop, making the second hop into the new
387-
/// introduction node.
388-
///
389-
/// Will only modify `path` when returning `Ok`.
390-
pub fn advance_path_by_one<NS: Deref, NL: Deref, T>(
391-
path: &mut BlindedPaymentPath, node_signer: &NS, node_id_lookup: &NL, secp_ctx: &Secp256k1<T>
392-
) -> Result<(), ()>
393-
where
394-
NS::Target: NodeSigner,
395-
NL::Target: NodeIdLookUp,
396-
T: secp256k1::Signing + secp256k1::Verification,
397-
{
398-
let control_tlvs_ss = node_signer.ecdh(Recipient::Node, &path.0.blinding_point, None)?;
399-
let rho = onion_utils::gen_rho_from_shared_secret(&control_tlvs_ss.secret_bytes());
400-
let encrypted_control_tlvs = &path.0.blinded_hops.get(0).ok_or(())?.encrypted_payload;
401-
let mut s = Cursor::new(encrypted_control_tlvs);
402-
let mut reader = FixedLengthReader::new(&mut s, encrypted_control_tlvs.len() as u64);
403-
match ChaChaPolyReadAdapter::read(&mut reader, rho) {
404-
Ok(ChaChaPolyReadAdapter {
405-
readable: BlindedPaymentTlvs::Forward(ForwardTlvs { short_channel_id, .. })
406-
}) => {
407-
let next_node_id = match node_id_lookup.next_node_id(short_channel_id) {
408-
Some(node_id) => node_id,
409-
None => return Err(()),
410-
};
411-
let mut new_blinding_point = onion_utils::next_hop_pubkey(
412-
secp_ctx, path.0.blinding_point, control_tlvs_ss.as_ref()
413-
).map_err(|_| ())?;
414-
mem::swap(&mut path.0.blinding_point, &mut new_blinding_point);
415-
path.0.introduction_node = IntroductionNode::NodeId(next_node_id);
416-
path.0.blinded_hops.remove(0);
417-
Ok(())
418-
},
419-
_ => Err(())
420-
}
421-
}
422-
423423
/// `None` if underflow occurs.
424424
pub(crate) fn amt_to_forward_msat(inbound_amt_msat: u64, payment_relay: &PaymentRelay) -> Option<u64> {
425425
let inbound_amt = inbound_amt_msat as u128;

lightning/src/ln/outbound_payment.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use bitcoin::hashes::sha256::Hash as Sha256;
1414
use bitcoin::secp256k1::{self, Secp256k1, SecretKey};
1515

1616
use crate::blinded_path::{IntroductionNode, NodeIdLookUp};
17-
use crate::blinded_path::payment::advance_path_by_one;
1817
use crate::events::{self, PaymentFailureReason};
1918
use crate::ln::types::{PaymentHash, PaymentPreimage, PaymentSecret};
2019
use crate::ln::channel_state::ChannelDetails;
@@ -845,7 +844,7 @@ impl OutboundPayments {
845844
},
846845
};
847846
if introduction_node_id == our_node_id {
848-
let _ = advance_path_by_one(path, node_signer, node_id_lookup, secp_ctx);
847+
let _ = path.advance_path_by_one(node_signer, node_id_lookup, secp_ctx);
849848
}
850849
}
851850
}

lightning/src/onion_message/messenger.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use bitcoin::hashes::sha256::Hash as Sha256;
1616
use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey};
1717

1818
use crate::blinded_path::{IntroductionNode, NodeIdLookUp};
19-
use crate::blinded_path::message::{advance_path_by_one, BlindedMessagePath, ForwardNode, ForwardTlvs, MessageContext, NextMessageHop, ReceiveTlvs};
19+
use crate::blinded_path::message::{BlindedMessagePath, ForwardNode, ForwardTlvs, MessageContext, NextMessageHop, ReceiveTlvs};
2020
use crate::blinded_path::utils;
2121
use crate::events::{Event, EventHandler, EventsProvider, ReplayEvent};
2222
use crate::sign::{EntropySource, NodeSigner, Recipient};
@@ -901,7 +901,7 @@ where
901901
},
902902
};
903903
if introduction_node_id == our_node_id {
904-
advance_path_by_one(blinded_path, node_signer, node_id_lookup, &secp_ctx)
904+
blinded_path.advance_path_by_one(node_signer, node_id_lookup, &secp_ctx)
905905
.map_err(|()| SendError::BlindedPathAdvanceFailed)?;
906906
}
907907
}

0 commit comments

Comments
 (0)