Skip to content

Commit 6dc4223

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 8b442fe commit 6dc4223

File tree

4 files changed

+32
-33
lines changed

4 files changed

+32
-33
lines changed

fuzz/src/onion_message.rs

-13
Original file line numberDiff line numberDiff line change
@@ -211,19 +211,6 @@ mod tests {
211211

212212
#[test]
213213
fn test_no_onion_message_breakage() {
214-
let one_hop_om = "020000000000000000000000000000000000000000000000000000000000000e01055600020000000000000000000000000000000000000000000000000000000000000e01ae0276020000000000000000000000000000000000000000000000000000000000000002020000000000000000000000000000000000000000000000000000000000000e0101022a0000000000000000000000000000014551231950b75fc4402da1732fc9bebf00109500000000000000000000000000000004106d000000000000000000000000000000fd1092202a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005600000000000000000000000000000000000000000000000000000000000000";
215-
let logger = TrackingLogger { lines: Mutex::new(HashMap::new()) };
216-
super::do_test(&::hex::decode(one_hop_om).unwrap(), &logger);
217-
{
218-
let log_entries = logger.lines.lock().unwrap();
219-
assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(),
220-
"Received an onion message with path_id None and a reply_path".to_string())), Some(&1));
221-
assert_eq!(log_entries.get(&("lightning::onion_message::messenger".to_string(),
222-
"Sending onion message when responding to Custom onion message with path_id None".to_string())), Some(&1));
223-
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));
225-
}
226-
227214
let two_unblinded_hops_om = "020000000000000000000000000000000000000000000000000000000000000e01055600020000000000000000000000000000000000000000000000000000000000000e0135043304210202020202020202020202020202020202020202020202020202020202020202026d000000000000000000000000000000eb0000000000000000000000000000000000000000000000000000000000000036041096000000000000000000000000000000fd1092202a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004800000000000000000000000000000000000000000000000000000000000000";
228215
let logger = TrackingLogger { lines: Mutex::new(HashMap::new()) };
229216
super::do_test(&::hex::decode(two_unblinded_hops_om).unwrap(), &logger);

lightning/src/blinded_path/mod.rs

+12-5
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

+17-12
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

+3-3
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,8 @@ pub enum SendError {
225225
/// Because implementations such as Eclair will drop onion messages where the message packet
226226
/// exceeds 32834 bytes, we refuse to send messages where the packet exceeds this size.
227227
TooBigPacket,
228-
/// The provided [`Destination`] was an invalid [`BlindedPath`], due to having fewer than two
229-
/// blinded hops.
228+
/// The provided [`Destination`] was an invalid [`BlindedPath`] due to not having any blinded
229+
/// hops.
230230
TooFewBlindedHops,
231231
/// Our next-hop peer was offline or does not support onion message forwarding.
232232
InvalidFirstHop,
@@ -299,7 +299,7 @@ where
299299
{
300300
let OnionMessagePath { intermediate_nodes, mut destination } = path;
301301
if let Destination::BlindedPath(BlindedPath { ref blinded_hops, .. }) = destination {
302-
if blinded_hops.len() < 2 {
302+
if blinded_hops.is_empty() {
303303
return Err(SendError::TooFewBlindedHops);
304304
}
305305
}

0 commit comments

Comments
 (0)