Skip to content

Commit 43a6da1

Browse files
committed
Allow sending onion messages to 1-hop blinded path
This allows for specifying the introduction node as the message recipient.
1 parent 524ede6 commit 43a6da1

File tree

4 files changed

+33
-19
lines changed

4 files changed

+33
-19
lines changed

fuzz/src/onion_message.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ mod tests {
221221
assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(),
222222
"Sending onion message when responding to Custom onion message with path_id None".to_string())), Some(&1));
223223
assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(),
224-
"Failed sending onion message when responding to Custom onion message with path_id None: TooFewBlindedHops".to_string())), Some(&1));
224+
"Failed sending onion message when responding to Custom onion message with path_id None: InvalidFirstHop".to_string())), Some(&1));
225225
}
226226

227227
let two_unblinded_hops_om = "020000000000000000000000000000000000000000000000000000000000000e01055600020000000000000000000000000000000000000000000000000000000000000e0135043304210202020202020202020202020202020202020202020202020202020202020202026d000000000000000000000000000000eb0000000000000000000000000000000000000000000000000000000000000036041096000000000000000000000000000000fd1092202a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004800000000000000000000000000000000000000000000000000000000000000";

lightning/src/blinded_path/mod.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,22 @@ pub struct BlindedHop {
5656
}
5757

5858
impl BlindedPath {
59+
/// Create a one-hop blinded path for a message.
60+
pub fn one_hop_for_message<ES: EntropySource, T: secp256k1::Signing + secp256k1::Verification>(
61+
recipient_node_id: PublicKey, entropy_source: &ES, secp_ctx: &Secp256k1<T>
62+
) -> Result<Self, ()> {
63+
Self::new_for_message(&[recipient_node_id], entropy_source, secp_ctx)
64+
}
65+
5966
/// Create a blinded path for an onion message, to be forwarded along `node_pks`. The last node
6067
/// pubkey in `node_pks` will be the destination node.
6168
///
62-
/// Errors if less than two hops are provided or if `node_pk`(s) are invalid.
69+
/// Errors if no hops are provided or if `node_pk`(s) are invalid.
6370
// TODO: make all payloads the same size with padding + add dummy hops
64-
pub fn new_for_message<ES: EntropySource, T: secp256k1::Signing + secp256k1::Verification>
65-
(node_pks: &[PublicKey], entropy_source: &ES, secp_ctx: &Secp256k1<T>) -> Result<Self, ()>
66-
{
67-
if node_pks.len() < 2 { return Err(()) }
71+
pub fn new_for_message<ES: EntropySource, T: secp256k1::Signing + secp256k1::Verification>(
72+
node_pks: &[PublicKey], entropy_source: &ES, secp_ctx: &Secp256k1<T>
73+
) -> Result<Self, ()> {
74+
if node_pks.is_empty() { return Err(()) }
6875
let blinding_secret_bytes = entropy_source.get_secure_random_bytes();
6976
let blinding_secret = SecretKey::from_slice(&blinding_secret_bytes[..]).expect("RNG is busted");
7077
let introduction_node_id = node_pks[0];

lightning/src/onion_message/functional_tests.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ fn pass_along_path(path: &Vec<MessengerNode>) {
197197
}
198198

199199
#[test]
200-
fn one_hop() {
200+
fn one_unblinded_hop() {
201201
let nodes = create_nodes(2);
202202
let test_msg = TestCustomMessage::Response;
203203

@@ -224,6 +224,22 @@ fn two_unblinded_hops() {
224224
pass_along_path(&nodes);
225225
}
226226

227+
#[test]
228+
fn one_blinded_hop() {
229+
let nodes = create_nodes(2);
230+
let test_msg = TestCustomMessage::Response;
231+
232+
let secp_ctx = Secp256k1::new();
233+
let blinded_path = BlindedPath::new_for_message(&[nodes[1].get_node_pk()], &*nodes[1].keys_manager, &secp_ctx).unwrap();
234+
let path = OnionMessagePath {
235+
intermediate_nodes: vec![],
236+
destination: Destination::BlindedPath(blinded_path),
237+
};
238+
nodes[0].messenger.send_onion_message(path, test_msg, None).unwrap();
239+
nodes[1].custom_message_handler.expect_message(TestCustomMessage::Response);
240+
pass_along_path(&nodes);
241+
}
242+
227243
#[test]
228244
fn two_unblinded_two_blinded() {
229245
let nodes = create_nodes(5);
@@ -320,17 +336,6 @@ fn invalid_blinded_path_error() {
320336
};
321337
let err = nodes[0].messenger.send_onion_message(path, test_msg.clone(), None).unwrap_err();
322338
assert_eq!(err, SendError::TooFewBlindedHops);
323-
324-
// 1 hop
325-
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();
326-
blinded_path.blinded_hops.remove(0);
327-
assert_eq!(blinded_path.blinded_hops.len(), 1);
328-
let path = OnionMessagePath {
329-
intermediate_nodes: vec![],
330-
destination: Destination::BlindedPath(blinded_path),
331-
};
332-
let err = nodes[0].messenger.send_onion_message(path, test_msg, None).unwrap_err();
333-
assert_eq!(err, SendError::TooFewBlindedHops);
334339
}
335340

336341
#[test]

lightning/src/onion_message/messenger.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ where
293293
{
294294
let OnionMessagePath { intermediate_nodes, mut destination } = path;
295295
if let Destination::BlindedPath(BlindedPath { ref blinded_hops, .. }) = destination {
296-
if blinded_hops.len() < 2 {
296+
if blinded_hops.is_empty() {
297297
return Err(SendError::TooFewBlindedHops);
298298
}
299299
}
@@ -734,6 +734,8 @@ fn packet_payloads_and_keys<T: OnionMessageContents, S: secp256k1::Signing + sec
734734
next_node_id: intro_node_id,
735735
next_blinding_override: Some(blinding_pt),
736736
})), control_tlvs_ss));
737+
} else {
738+
prev_control_tlvs_ss = Some(control_tlvs_ss);
737739
}
738740
}
739741
if blinded_path_idx < num_blinded_hops.saturating_sub(1) && enc_payload_opt.is_some() {

0 commit comments

Comments
 (0)