Skip to content

Commit

Permalink
Error when attempting to send an OM to a blinded route with 0 hops
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinewallace committed Jul 7, 2022
1 parent 059e39c commit cdca641
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lightning/src/onion_message/functional_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use util::test_utils;
use bitcoin::network::constants::Network;
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};

use core::mem;
use sync::Arc;

struct MessengerNode {
Expand Down Expand Up @@ -123,3 +124,18 @@ fn too_big_packet_error() {
let err = nodes[0].messenger.send_onion_message(hops, Destination::Node(hop_node_id)).unwrap_err();
assert_eq!(err, SendError::TooBigPacket);
}

#[test]
fn invalid_blinded_route_error() {
// Make sure we error as expected if a provided blinded route has 0 hops.
let mut nodes = create_nodes(3);
let (node1, node2, node3) = (nodes.remove(0), nodes.remove(0), nodes.remove(0));

let secp_ctx = Secp256k1::new();
let mut blinded_route = BlindedRoute::new(vec![node2.get_node_pk(), node3.get_node_pk()], &node3.keys_manager, &secp_ctx).unwrap();
let mut empty_hops = Vec::new();
mem::swap(&mut empty_hops, &mut blinded_route.blinded_hops);

let err = node1.messenger.send_onion_message(vec![], Destination::BlindedRoute(blinded_route)).unwrap_err();
assert_eq!(err, SendError::MissingBlindedHops);
}
10 changes: 10 additions & 0 deletions lightning/src/onion_message/messenger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ pub enum SendError {
/// Because implementations such as Eclair will drop onion messages where the message packet
/// exceeds 32834 bytes, we refuse to send messages where the packet exceeds this size.
TooBigPacket,
/// The provided [destination] was an invalid [blinded route], due to having 0 blinded hops.
///
/// [destination]: Destination
/// [blinded route]: super::blinded_route::BlindedRoute
MissingBlindedHops,
}

impl<Signer: Sign, K: Deref, L: Deref> OnionMessenger<Signer, K, L>
Expand All @@ -139,6 +144,11 @@ impl<Signer: Sign, K: Deref, L: Deref> OnionMessenger<Signer, K, L>

/// Send an empty onion message to `destination`, routing it through `intermediate_nodes`.
pub fn send_onion_message(&self, intermediate_nodes: Vec<PublicKey>, destination: Destination) -> Result<(), SendError> {
if let Destination::BlindedRoute(BlindedRoute { ref blinded_hops, .. }) = destination {
if blinded_hops.len() == 0 {
return Err(SendError::MissingBlindedHops);
}
}
let blinding_secret_bytes = self.keys_manager.get_secure_random_bytes();
let blinding_secret = SecretKey::from_slice(&blinding_secret_bytes[..]).expect("RNG is busted");
let (introduction_node_id, blinding_point) = if intermediate_nodes.len() != 0 {
Expand Down

0 comments on commit cdca641

Please sign in to comment.