Skip to content

Commit a81e66e

Browse files
committed
Look up node id from scid in OnionMessenger
When forwarding onion messages, the next node may be represented by a short channel id instead of a node id. Parameterize OnionMessenger with a NodeIdLookUp trait to find which node is the next hop. Implement the trait for ChannelManager for forwarding to channel counterparties. Also use this trait when advancing a blinded path one hop when the sender is the introduction node.
1 parent 206f47e commit a81e66e

File tree

6 files changed

+95
-31
lines changed

6 files changed

+95
-31
lines changed

lightning/src/blinded_path/message.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey};
22

3-
use crate::blinded_path::{BlindedHop, BlindedPath, IntroductionNode};
3+
use crate::blinded_path::{BlindedHop, BlindedPath, IntroductionNode, NodeIdLookUp};
44
use crate::blinded_path::utils;
55
use crate::io;
66
use crate::io::Cursor;
@@ -82,9 +82,14 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
8282

8383
// Advance the blinded onion message path by one hop, so make the second hop into the new
8484
// introduction node.
85-
pub(crate) fn advance_path_by_one<NS: Deref, T: secp256k1::Signing + secp256k1::Verification>(
86-
path: &mut BlindedPath, node_signer: &NS, secp_ctx: &Secp256k1<T>
87-
) -> Result<(), ()> where NS::Target: NodeSigner {
85+
pub(crate) fn advance_path_by_one<NS: Deref, NL: Deref, T>(
86+
path: &mut BlindedPath, node_signer: &NS, node_id_lookup: &NL, secp_ctx: &Secp256k1<T>
87+
) -> Result<(), ()>
88+
where
89+
NS::Target: NodeSigner,
90+
NL::Target: NodeIdLookUp,
91+
T: secp256k1::Signing + secp256k1::Verification,
92+
{
8893
let control_tlvs_ss = node_signer.ecdh(Recipient::Node, &path.blinding_point, None)?;
8994
let rho = onion_utils::gen_rho_from_shared_secret(&control_tlvs_ss.secret_bytes());
9095
let encrypted_control_tlvs = path.blinded_hops.remove(0).encrypted_payload;
@@ -96,7 +101,10 @@ pub(crate) fn advance_path_by_one<NS: Deref, T: secp256k1::Signing + secp256k1::
96101
}) => {
97102
let next_node_id = match next_hop {
98103
NextHop::NodeId(pubkey) => pubkey,
99-
NextHop::ShortChannelId(_) => todo!(),
104+
NextHop::ShortChannelId(scid) => match node_id_lookup.next_node_id(scid) {
105+
Some(pubkey) => pubkey,
106+
None => return Err(()),
107+
},
100108
};
101109
let mut new_blinding_point = match next_blinding_override {
102110
Some(blinding_point) => blinding_point,

lightning/src/blinded_path/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,24 @@ pub enum Direction {
6666
NodeTwo,
6767
}
6868

69+
/// An interface for looking up the node id of a channel counterparty for the purpose of forwarding
70+
/// an [`OnionMessage`].
71+
///
72+
/// [`OnionMessage`]: crate::ln::msgs::OnionMessage
73+
pub trait NodeIdLookUp {
74+
/// Returns the node if of the channel counterparty with `short_channel_id`.
75+
fn next_node_id(&self, short_channel_id: u64) -> Option<PublicKey>;
76+
}
77+
78+
/// A [`NodeIdLookUp`] that always returns `None`.
79+
pub struct EmptyNodeIdLookUp {}
80+
81+
impl NodeIdLookUp for EmptyNodeIdLookUp {
82+
fn next_node_id(&self, _short_channel_id: u64) -> Option<PublicKey> {
83+
None
84+
}
85+
}
86+
6987
/// An encrypted payload and node id corresponding to a hop in a payment or onion message path, to
7088
/// be encoded in the sender's onion packet. These hops cannot be identified by outside observers
7189
/// and thus can be used to hide the identity of the recipient.

lightning/src/ln/channelmanager.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use bitcoin::secp256k1::{SecretKey,PublicKey};
3131
use bitcoin::secp256k1::Secp256k1;
3232
use bitcoin::{secp256k1, Sequence};
3333

34-
use crate::blinded_path::BlindedPath;
34+
use crate::blinded_path::{BlindedPath, NodeIdLookUp};
3535
use crate::blinded_path::payment::{PaymentConstraints, ReceiveTlvs};
3636
use crate::chain;
3737
use crate::chain::{Confirm, ChannelMonitorUpdateStatus, Watch, BestBlock};
@@ -9528,6 +9528,23 @@ where
95289528
}
95299529
}
95309530

9531+
impl<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref>
9532+
NodeIdLookUp for ChannelManager<M, T, ES, NS, SP, F, R, L>
9533+
where
9534+
M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
9535+
T::Target: BroadcasterInterface,
9536+
ES::Target: EntropySource,
9537+
NS::Target: NodeSigner,
9538+
SP::Target: SignerProvider,
9539+
F::Target: FeeEstimator,
9540+
R::Target: Router,
9541+
L::Target: Logger,
9542+
{
9543+
fn next_node_id(&self, short_channel_id: u64) -> Option<PublicKey> {
9544+
self.short_to_chan_info.read().unwrap().get(&short_channel_id).map(|(pubkey, _)| *pubkey)
9545+
}
9546+
}
9547+
95319548
/// Fetches the set of [`NodeFeatures`] flags that are provided by or required by
95329549
/// [`ChannelManager`].
95339550
pub(crate) fn provided_node_features(config: &UserConfig) -> NodeFeatures {

lightning/src/ln/functional_test_utils.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ type TestOnionMessenger<'chan_man, 'node_cfg, 'chan_mon_cfg> = OnionMessenger<
415415
DedicatedEntropy,
416416
&'node_cfg test_utils::TestKeysInterface,
417417
&'chan_mon_cfg test_utils::TestLogger,
418+
&'chan_man TestChannelManager<'node_cfg, 'chan_mon_cfg>,
418419
&'node_cfg test_utils::TestMessageRouter<'chan_mon_cfg>,
419420
&'chan_man TestChannelManager<'node_cfg, 'chan_mon_cfg>,
420421
IgnoringMessageHandler,
@@ -3058,8 +3059,8 @@ pub fn create_network<'a, 'b: 'a, 'c: 'b>(node_count: usize, cfgs: &'b Vec<NodeC
30583059
for i in 0..node_count {
30593060
let dedicated_entropy = DedicatedEntropy(RandomBytes::new([i as u8; 32]));
30603061
let onion_messenger = OnionMessenger::new(
3061-
dedicated_entropy, cfgs[i].keys_manager, cfgs[i].logger, &cfgs[i].message_router,
3062-
&chan_mgrs[i], IgnoringMessageHandler {},
3062+
dedicated_entropy, cfgs[i].keys_manager, cfgs[i].logger, &chan_mgrs[i],
3063+
&cfgs[i].message_router, &chan_mgrs[i], IgnoringMessageHandler {},
30633064
);
30643065
let gossip_sync = P2PGossipSync::new(cfgs[i].network_graph.as_ref(), None, cfgs[i].logger);
30653066
let wallet_source = Arc::new(test_utils::TestWalletSource::new(SecretKey::from_slice(&[i as u8 + 1; 32]).unwrap()));

lightning/src/onion_message/functional_tests.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
//! Onion message testing and test utilities live here.
1111
12-
use crate::blinded_path::BlindedPath;
12+
use crate::blinded_path::{BlindedPath, EmptyNodeIdLookUp};
1313
use crate::events::{Event, EventsProvider};
1414
use crate::ln::features::{ChannelFeatures, InitFeatures};
1515
use crate::ln::msgs::{self, DecodeError, OnionMessageHandler};
@@ -42,6 +42,7 @@ struct MessengerNode {
4242
Arc<test_utils::TestKeysInterface>,
4343
Arc<test_utils::TestNodeSigner>,
4444
Arc<test_utils::TestLogger>,
45+
Arc<EmptyNodeIdLookUp>,
4546
Arc<DefaultMessageRouter<
4647
Arc<NetworkGraph<Arc<test_utils::TestLogger>>>,
4748
Arc<test_utils::TestLogger>,
@@ -175,6 +176,7 @@ fn create_nodes_using_secrets(secrets: Vec<SecretKey>) -> Vec<MessengerNode> {
175176
let entropy_source = Arc::new(test_utils::TestKeysInterface::new(&seed, Network::Testnet));
176177
let node_signer = Arc::new(test_utils::TestNodeSigner::new(secret_key));
177178

179+
let node_id_lookup = Arc::new(EmptyNodeIdLookUp {});
178180
let message_router = Arc::new(
179181
DefaultMessageRouter::new(network_graph.clone(), entropy_source.clone())
180182
);
@@ -185,7 +187,7 @@ fn create_nodes_using_secrets(secrets: Vec<SecretKey>) -> Vec<MessengerNode> {
185187
node_id: node_signer.get_node_id(Recipient::Node).unwrap(),
186188
entropy_source: entropy_source.clone(),
187189
messenger: OnionMessenger::new(
188-
entropy_source, node_signer, logger.clone(), message_router,
190+
entropy_source, node_signer, logger.clone(), node_id_lookup, message_router,
189191
offers_message_handler, custom_message_handler.clone()
190192
),
191193
custom_message_handler,

lightning/src/onion_message/messenger.rs

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use bitcoin::hashes::hmac::{Hmac, HmacEngine};
1515
use bitcoin::hashes::sha256::Hash as Sha256;
1616
use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey};
1717

18-
use crate::blinded_path::{BlindedPath, Direction, IntroductionNode};
18+
use crate::blinded_path::{BlindedPath, Direction, IntroductionNode, NodeIdLookUp};
1919
use crate::blinded_path::message::{advance_path_by_one, ForwardTlvs, NextHop, ReceiveTlvs};
2020
use crate::blinded_path::utils;
2121
use crate::events::{Event, EventHandler, EventsProvider};
@@ -70,7 +70,7 @@ pub(super) const MAX_TIMER_TICKS: usize = 2;
7070
/// # use bitcoin::hashes::_export::_core::time::Duration;
7171
/// # use bitcoin::hashes::hex::FromHex;
7272
/// # use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey, self};
73-
/// # use lightning::blinded_path::BlindedPath;
73+
/// # use lightning::blinded_path::{BlindedPath, EmptyNodeIdLookUp};
7474
/// # use lightning::sign::{EntropySource, KeysManager};
7575
/// # use lightning::ln::peer_handler::IgnoringMessageHandler;
7676
/// # use lightning::onion_message::messenger::{Destination, MessageRouter, OnionMessagePath, OnionMessenger};
@@ -111,14 +111,15 @@ pub(super) const MAX_TIMER_TICKS: usize = 2;
111111
/// # let hop_node_id1 = PublicKey::from_secret_key(&secp_ctx, &node_secret);
112112
/// # let (hop_node_id3, hop_node_id4) = (hop_node_id1, hop_node_id1);
113113
/// # let destination_node_id = hop_node_id1;
114+
/// # let node_id_lookup = EmptyNodeIdLookUp {};
114115
/// # let message_router = Arc::new(FakeMessageRouter {});
115116
/// # let custom_message_handler = IgnoringMessageHandler {};
116117
/// # let offers_message_handler = IgnoringMessageHandler {};
117118
/// // Create the onion messenger. This must use the same `keys_manager` as is passed to your
118119
/// // ChannelManager.
119120
/// let onion_messenger = OnionMessenger::new(
120-
/// &keys_manager, &keys_manager, logger, message_router, &offers_message_handler,
121-
/// &custom_message_handler
121+
/// &keys_manager, &keys_manager, logger, &node_id_lookup, message_router,
122+
/// &offers_message_handler, &custom_message_handler
122123
/// );
123124
124125
/// # #[derive(Debug)]
@@ -155,11 +156,12 @@ pub(super) const MAX_TIMER_TICKS: usize = 2;
155156
///
156157
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
157158
/// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
158-
pub struct OnionMessenger<ES: Deref, NS: Deref, L: Deref, MR: Deref, OMH: Deref, CMH: Deref>
159+
pub struct OnionMessenger<ES: Deref, NS: Deref, L: Deref, NL: Deref, MR: Deref, OMH: Deref, CMH: Deref>
159160
where
160161
ES::Target: EntropySource,
161162
NS::Target: NodeSigner,
162163
L::Target: Logger,
164+
NL::Target: NodeIdLookUp,
163165
MR::Target: MessageRouter,
164166
OMH::Target: OffersMessageHandler,
165167
CMH::Target: CustomOnionMessageHandler,
@@ -169,6 +171,7 @@ where
169171
logger: L,
170172
message_recipients: Mutex<HashMap<PublicKey, OnionMessageRecipient>>,
171173
secp_ctx: Secp256k1<secp256k1::All>,
174+
node_id_lookup: NL,
172175
message_router: MR,
173176
offers_handler: OMH,
174177
custom_handler: CMH,
@@ -584,13 +587,15 @@ pub enum PeeledOnion<T: OnionMessageContents> {
584587
///
585588
/// Returns the node id of the peer to send the message to, the message itself, and any addresses
586589
/// need to connect to the first node.
587-
pub fn create_onion_message<ES: Deref, NS: Deref, T: OnionMessageContents>(
588-
entropy_source: &ES, node_signer: &NS, secp_ctx: &Secp256k1<secp256k1::All>,
589-
path: OnionMessagePath, contents: T, reply_path: Option<BlindedPath>,
590+
pub fn create_onion_message<ES: Deref, NS: Deref, NL: Deref, T: OnionMessageContents>(
591+
entropy_source: &ES, node_signer: &NS, node_id_lookup: &NL,
592+
secp_ctx: &Secp256k1<secp256k1::All>, path: OnionMessagePath, contents: T,
593+
reply_path: Option<BlindedPath>,
590594
) -> Result<(PublicKey, OnionMessage, Option<Vec<SocketAddress>>), SendError>
591595
where
592596
ES::Target: EntropySource,
593597
NS::Target: NodeSigner,
598+
NL::Target: NodeIdLookUp,
594599
{
595600
let OnionMessagePath { intermediate_nodes, mut destination, first_node_addresses } = path;
596601
if let Destination::BlindedPath(BlindedPath { ref blinded_hops, .. }) = destination {
@@ -614,7 +619,7 @@ where
614619
let our_node_id = node_signer.get_node_id(Recipient::Node)
615620
.map_err(|()| SendError::GetNodeIdFailed)?;
616621
if introduction_node_id == our_node_id {
617-
advance_path_by_one(blinded_path, node_signer, &secp_ctx)
622+
advance_path_by_one(blinded_path, node_signer, node_id_lookup, &secp_ctx)
618623
.map_err(|()| SendError::BlindedPathAdvanceFailed)?;
619624
}
620625
}
@@ -746,21 +751,22 @@ where
746751
}
747752
}
748753

749-
impl<ES: Deref, NS: Deref, L: Deref, MR: Deref, OMH: Deref, CMH: Deref>
750-
OnionMessenger<ES, NS, L, MR, OMH, CMH>
754+
impl<ES: Deref, NS: Deref, L: Deref, NL: Deref, MR: Deref, OMH: Deref, CMH: Deref>
755+
OnionMessenger<ES, NS, L, NL, MR, OMH, CMH>
751756
where
752757
ES::Target: EntropySource,
753758
NS::Target: NodeSigner,
754759
L::Target: Logger,
760+
NL::Target: NodeIdLookUp,
755761
MR::Target: MessageRouter,
756762
OMH::Target: OffersMessageHandler,
757763
CMH::Target: CustomOnionMessageHandler,
758764
{
759765
/// Constructs a new `OnionMessenger` to send, forward, and delegate received onion messages to
760766
/// their respective handlers.
761767
pub fn new(
762-
entropy_source: ES, node_signer: NS, logger: L, message_router: MR, offers_handler: OMH,
763-
custom_handler: CMH
768+
entropy_source: ES, node_signer: NS, logger: L, node_id_lookup: NL, message_router: MR,
769+
offers_handler: OMH, custom_handler: CMH
764770
) -> Self {
765771
let mut secp_ctx = Secp256k1::new();
766772
secp_ctx.seeded_randomize(&entropy_source.get_secure_random_bytes());
@@ -770,6 +776,7 @@ where
770776
message_recipients: Mutex::new(new_hash_map()),
771777
secp_ctx,
772778
logger,
779+
node_id_lookup,
773780
message_router,
774781
offers_handler,
775782
custom_handler,
@@ -846,7 +853,8 @@ where
846853
log_trace!(self.logger, "Constructing onion message {}: {:?}", log_suffix, contents);
847854

848855
let (first_node_id, onion_message, addresses) = create_onion_message(
849-
&self.entropy_source, &self.node_signer, &self.secp_ctx, path, contents, reply_path
856+
&self.entropy_source, &self.node_signer, &self.node_id_lookup, &self.secp_ctx, path,
857+
contents, reply_path,
850858
)?;
851859

852860
let mut message_recipients = self.message_recipients.lock().unwrap();
@@ -942,12 +950,13 @@ fn outbound_buffer_full(peer_node_id: &PublicKey, buffer: &HashMap<PublicKey, On
942950
false
943951
}
944952

945-
impl<ES: Deref, NS: Deref, L: Deref, MR: Deref, OMH: Deref, CMH: Deref> EventsProvider
946-
for OnionMessenger<ES, NS, L, MR, OMH, CMH>
953+
impl<ES: Deref, NS: Deref, L: Deref, NL: Deref, MR: Deref, OMH: Deref, CMH: Deref> EventsProvider
954+
for OnionMessenger<ES, NS, L, NL, MR, OMH, CMH>
947955
where
948956
ES::Target: EntropySource,
949957
NS::Target: NodeSigner,
950958
L::Target: Logger,
959+
NL::Target: NodeIdLookUp,
951960
MR::Target: MessageRouter,
952961
OMH::Target: OffersMessageHandler,
953962
CMH::Target: CustomOnionMessageHandler,
@@ -963,12 +972,13 @@ where
963972
}
964973
}
965974

966-
impl<ES: Deref, NS: Deref, L: Deref, MR: Deref, OMH: Deref, CMH: Deref> OnionMessageHandler
967-
for OnionMessenger<ES, NS, L, MR, OMH, CMH>
975+
impl<ES: Deref, NS: Deref, L: Deref, NL: Deref, MR: Deref, OMH: Deref, CMH: Deref> OnionMessageHandler
976+
for OnionMessenger<ES, NS, L, NL, MR, OMH, CMH>
968977
where
969978
ES::Target: EntropySource,
970979
NS::Target: NodeSigner,
971980
L::Target: Logger,
981+
NL::Target: NodeIdLookUp,
972982
MR::Target: MessageRouter,
973983
OMH::Target: OffersMessageHandler,
974984
CMH::Target: CustomOnionMessageHandler,
@@ -1005,7 +1015,13 @@ where
10051015
Ok(PeeledOnion::Forward(next_hop, onion_message)) => {
10061016
let next_node_id = match next_hop {
10071017
NextHop::NodeId(pubkey) => pubkey,
1008-
NextHop::ShortChannelId(_) => todo!(),
1018+
NextHop::ShortChannelId(scid) => match self.node_id_lookup.next_node_id(scid) {
1019+
Some(pubkey) => pubkey,
1020+
None => {
1021+
log_trace!(self.logger, "Dropping forwarded onion message to unresolved peer using SCID {}", scid);
1022+
return
1023+
},
1024+
},
10091025
};
10101026

10111027
let mut message_recipients = self.message_recipients.lock().unwrap();
@@ -1137,6 +1153,7 @@ pub type SimpleArcOnionMessenger<M, T, F, L> = OnionMessenger<
11371153
Arc<KeysManager>,
11381154
Arc<KeysManager>,
11391155
Arc<L>,
1156+
Arc<SimpleArcChannelManager<M, T, F, L>>,
11401157
Arc<DefaultMessageRouter<Arc<NetworkGraph<Arc<L>>>, Arc<L>, Arc<KeysManager>>>,
11411158
Arc<SimpleArcChannelManager<M, T, F, L>>,
11421159
IgnoringMessageHandler
@@ -1156,8 +1173,9 @@ pub type SimpleRefOnionMessenger<
11561173
&'a KeysManager,
11571174
&'a KeysManager,
11581175
&'b L,
1159-
&'i DefaultMessageRouter<&'g NetworkGraph<&'b L>, &'b L, &'a KeysManager>,
1160-
&'j SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, M, T, F, L>,
1176+
&'i SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, M, T, F, L>,
1177+
&'j DefaultMessageRouter<&'g NetworkGraph<&'b L>, &'b L, &'a KeysManager>,
1178+
&'i SimpleRefChannelManager<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, M, T, F, L>,
11611179
IgnoringMessageHandler
11621180
>;
11631181

0 commit comments

Comments
 (0)