use crate::sign::{NodeSigner, Recipient};
use crate::util::ser::{FixedLengthReader, LengthReadable, Writeable, Writer};
use crate::util::test_utils;
-use super::{CustomOnionMessageContents, CustomOnionMessageHandler, Destination, MessageRouter, OffersMessage, OffersMessageHandler, OnionMessageContents, OnionMessagePath, OnionMessenger, SendError};
+use super::{CustomOnionMessageContents, CustomOnionMessageHandler, Destination, MessageRouter, OffersMessage, OffersMessageHandler, ParsedOnionMessageContents, OnionMessagePath, OnionMessenger, SendError};
use bitcoin::network::constants::Network;
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
#[test]
fn one_hop() {
let nodes = create_nodes(2);
- let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
+ let test_msg = ParsedOnionMessageContents::Custom(TestCustomMessage::Response);
let path = OnionMessagePath {
intermediate_nodes: vec![],
#[test]
fn two_unblinded_hops() {
let nodes = create_nodes(3);
- let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
+ let test_msg = ParsedOnionMessageContents::Custom(TestCustomMessage::Response);
let path = OnionMessagePath {
intermediate_nodes: vec![nodes[1].get_node_pk()],
#[test]
fn two_unblinded_two_blinded() {
let nodes = create_nodes(5);
- let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
+ let test_msg = ParsedOnionMessageContents::Custom(TestCustomMessage::Response);
let secp_ctx = Secp256k1::new();
let blinded_path = BlindedPath::new_for_message(&[nodes[3].get_node_pk(), nodes[4].get_node_pk()], &*nodes[4].keys_manager, &secp_ctx).unwrap();
#[test]
fn three_blinded_hops() {
let nodes = create_nodes(4);
- let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
+ let test_msg = ParsedOnionMessageContents::Custom(TestCustomMessage::Response);
let secp_ctx = Secp256k1::new();
let blinded_path = BlindedPath::new_for_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk(), nodes[3].get_node_pk()], &*nodes[3].keys_manager, &secp_ctx).unwrap();
fn too_big_packet_error() {
// Make sure we error as expected if a packet is too big to send.
let nodes = create_nodes(2);
- let test_msg = OnionMessageContents::Custom(TestCustomMessage::Response);
+ let test_msg = ParsedOnionMessageContents::Custom(TestCustomMessage::Response);
let hop_node_id = nodes[1].get_node_pk();
let hops = vec![hop_node_id; 400];
destination: Destination::BlindedPath(blinded_path),
};
- nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg.clone()), None).unwrap();
+ nodes[0].messenger.send_onion_message(path, ParsedOnionMessageContents::Custom(test_msg.clone()), None).unwrap();
nodes[2].custom_message_handler.expect_message(TestCustomMessage::Response);
pass_along_path(&nodes);
intermediate_nodes: vec![],
destination: Destination::BlindedPath(blinded_path),
};
- nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg), None).unwrap();
+ nodes[0].messenger.send_onion_message(path, ParsedOnionMessageContents::Custom(test_msg), None).unwrap();
nodes[1].custom_message_handler.expect_message(TestCustomMessage::Response);
nodes.remove(2);
pass_along_path(&nodes);
intermediate_nodes: vec![],
destination: Destination::BlindedPath(blinded_path),
};
- let err = nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg.clone()), None).unwrap_err();
+ let err = nodes[0].messenger.send_onion_message(path, ParsedOnionMessageContents::Custom(test_msg.clone()), None).unwrap_err();
assert_eq!(err, SendError::TooFewBlindedHops);
// 1 hop
intermediate_nodes: vec![],
destination: Destination::BlindedPath(blinded_path),
};
- let err = nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg), None).unwrap_err();
+ let err = nodes[0].messenger.send_onion_message(path, ParsedOnionMessageContents::Custom(test_msg), None).unwrap_err();
assert_eq!(err, SendError::TooFewBlindedHops);
}
destination: Destination::Node(nodes[3].get_node_pk()),
};
let reply_path = BlindedPath::new_for_message(&[nodes[2].get_node_pk(), nodes[1].get_node_pk(), nodes[0].get_node_pk()], &*nodes[0].keys_manager, &secp_ctx).unwrap();
- nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg.clone()), Some(reply_path)).unwrap();
+ nodes[0].messenger.send_onion_message(path, ParsedOnionMessageContents::Custom(test_msg.clone()), Some(reply_path)).unwrap();
nodes[3].custom_message_handler.expect_message(TestCustomMessage::Request);
pass_along_path(&nodes);
// Make sure the last node successfully decoded the reply path.
};
let reply_path = BlindedPath::new_for_message(&[nodes[2].get_node_pk(), nodes[1].get_node_pk(), nodes[0].get_node_pk()], &*nodes[0].keys_manager, &secp_ctx).unwrap();
- nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg), Some(reply_path)).unwrap();
+ nodes[0].messenger.send_onion_message(path, ParsedOnionMessageContents::Custom(test_msg), Some(reply_path)).unwrap();
nodes[3].custom_message_handler.expect_message(TestCustomMessage::Request);
pass_along_path(&nodes);
fn write<W: Writer>(&self, _w: &mut W) -> Result<(), io::Error> { unreachable!() }
}
- let test_msg = OnionMessageContents::Custom(InvalidCustomMessage {});
+ let test_msg = ParsedOnionMessageContents::Custom(InvalidCustomMessage {});
let path = OnionMessagePath {
intermediate_nodes: vec![],
destination: Destination::Node(nodes[1].get_node_pk()),
destination: Destination::Node(nodes[1].get_node_pk()),
};
for _ in 0..188 { // Based on MAX_PER_PEER_BUFFER_SIZE in OnionMessenger
- nodes[0].messenger.send_onion_message(path.clone(), OnionMessageContents::Custom(test_msg.clone()), None).unwrap();
+ nodes[0].messenger.send_onion_message(path.clone(), ParsedOnionMessageContents::Custom(test_msg.clone()), None).unwrap();
}
- let err = nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg), None).unwrap_err();
+ let err = nodes[0].messenger.send_onion_message(path, ParsedOnionMessageContents::Custom(test_msg), None).unwrap_err();
assert_eq!(err, SendError::BufferFull);
}
intermediate_nodes,
destination: Destination::Node(nodes[num_nodes-1].get_node_pk()),
};
- nodes[0].messenger.send_onion_message(path, OnionMessageContents::Custom(test_msg), None).unwrap();
+ nodes[0].messenger.send_onion_message(path, ParsedOnionMessageContents::Custom(test_msg), None).unwrap();
nodes[num_nodes-1].custom_message_handler.expect_message(TestCustomMessage::Response);
pass_along_path(&nodes);
}
use crate::ln::msgs::{self, OnionMessage, OnionMessageHandler};
use crate::ln::onion_utils;
use crate::ln::peer_handler::IgnoringMessageHandler;
-pub use super::packet::{CustomOnionMessageContents, OnionMessageContents};
+pub use super::packet::{CustomOnionMessageContents, ParsedOnionMessageContents};
use super::offers::OffersMessageHandler;
use super::packet::{BIG_PACKET_HOP_DATA_LEN, ForwardControlTlvs, Packet, Payload, ReceiveControlTlvs, SMALL_PACKET_HOP_DATA_LEN};
use crate::util::logger::Logger;
/// # use lightning::blinded_path::BlindedPath;
/// # use lightning::sign::KeysManager;
/// # use lightning::ln::peer_handler::IgnoringMessageHandler;
-/// # use lightning::onion_message::{CustomOnionMessageContents, Destination, MessageRouter, OnionMessageContents, OnionMessagePath, OnionMessenger};
+/// # use lightning::onion_message::{CustomOnionMessageContents, Destination, MessageRouter, ParsedOnionMessageContents, OnionMessagePath, OnionMessenger};
/// # use lightning::util::logger::{Logger, Record};
/// # use lightning::util::ser::{Writeable, Writer};
/// # use lightning::io;
/// };
/// let reply_path = None;
/// # let your_custom_message = YourCustomMessage {};
-/// let message = OnionMessageContents::Custom(your_custom_message);
+/// let message = ParsedOnionMessageContents::Custom(your_custom_message);
/// onion_messenger.send_onion_message(path, message, reply_path);
///
/// // Create a blinded path to yourself, for someone to send an onion message to.
/// };
/// let reply_path = None;
/// # let your_custom_message = YourCustomMessage {};
-/// let message = OnionMessageContents::Custom(your_custom_message);
+/// let message = ParsedOnionMessageContents::Custom(your_custom_message);
/// onion_messenger.send_onion_message(path, message, reply_path);
/// ```
///
/// Forwarded onion, with the next node id and a new onion
Forward(PublicKey, OnionMessage),
/// Received onion message, with decrypted contents, path_id, and reply path
- Receive(OnionMessageContents<CM>, Option<[u8; 32]>, Option<BlindedPath>)
+ Receive(ParsedOnionMessageContents<CM>, Option<[u8; 32]>, Option<BlindedPath>)
}
/// Creates an [`OnionMessage`] with the given `contents` for sending to the destination of
/// Returns both the node id of the peer to send the message to and the message itself.
pub fn create_onion_message<ES: Deref, NS: Deref, T: CustomOnionMessageContents>(
entropy_source: &ES, node_signer: &NS, secp_ctx: &Secp256k1<secp256k1::All>,
- path: OnionMessagePath, contents: OnionMessageContents<T>, reply_path: Option<BlindedPath>,
+ path: OnionMessagePath, contents: ParsedOnionMessageContents<T>,
+ reply_path: Option<BlindedPath>,
) -> Result<(PublicKey, OnionMessage), SendError>
where
ES::Target: EntropySource,
///
/// See [`OnionMessenger`] for example usage.
pub fn send_onion_message<T: CustomOnionMessageContents>(
- &self, path: OnionMessagePath, contents: OnionMessageContents<T>,
+ &self, path: OnionMessagePath, contents: ParsedOnionMessageContents<T>,
reply_path: Option<BlindedPath>
) -> Result<(), SendError> {
let (first_node_id, onion_msg) = create_onion_message(
}
fn find_path_and_enqueue_onion_message<T: CustomOnionMessageContents>(
- &self, contents: OnionMessageContents<T>, destination: Destination,
+ &self, contents: ParsedOnionMessageContents<T>, destination: Destination,
log_suffix: fmt::Arguments
) {
let sender = match self.node_signer.get_node_id(Recipient::Node) {
"Received an onion message with path_id {:02x?} and {} reply_path",
path_id, if reply_path.is_some() { "a" } else { "no" });
let response = match message {
- OnionMessageContents::Offers(msg) => {
+ ParsedOnionMessageContents::Offers(msg) => {
self.offers_handler.handle_message(msg)
- .map(|msg| OnionMessageContents::Offers(msg))
+ .map(|msg| ParsedOnionMessageContents::Offers(msg))
},
- OnionMessageContents::Custom(msg) => {
+ ParsedOnionMessageContents::Custom(msg) => {
self.custom_handler.handle_custom_message(msg)
- .map(|msg| OnionMessageContents::Custom(msg))
+ .map(|msg| ParsedOnionMessageContents::Custom(msg))
},
};
if let Some(response) = response {
/// `unblinded_path` to the given `destination`.
fn packet_payloads_and_keys<T: CustomOnionMessageContents, S: secp256k1::Signing + secp256k1::Verification>(
secp_ctx: &Secp256k1<S>, unblinded_path: &[PublicKey], destination: Destination,
- message: OnionMessageContents<T>, mut reply_path: Option<BlindedPath>, session_priv: &SecretKey
+ message: ParsedOnionMessageContents<T>, mut reply_path: Option<BlindedPath>, session_priv: &SecretKey
) -> Result<(Vec<(Payload<T>, [u8; 32])>, Vec<onion_utils::OnionKeys>), secp256k1::Error> {
let num_hops = unblinded_path.len() + destination.num_hops();
let mut payloads = Vec::with_capacity(num_hops);
mod functional_tests;
// Re-export structs so they can be imported with just the `onion_message::` module prefix.
-pub use self::messenger::{CustomOnionMessageContents, CustomOnionMessageHandler, DefaultMessageRouter, Destination, MessageRouter, OnionMessageContents, OnionMessagePath, OnionMessenger, PeeledOnion, SendError, SimpleArcOnionMessenger, SimpleRefOnionMessenger};
+pub use self::messenger::{CustomOnionMessageContents, CustomOnionMessageHandler, DefaultMessageRouter, Destination, MessageRouter, ParsedOnionMessageContents, OnionMessagePath, OnionMessenger, PeeledOnion, SendError, SimpleArcOnionMessenger, SimpleRefOnionMessenger};
pub use self::offers::{OffersMessage, OffersMessageHandler};
pub use self::packet::Packet;
pub(crate) use self::packet::ControlTlvs;
Receive {
control_tlvs: ReceiveControlTlvs,
reply_path: Option<BlindedPath>,
- message: OnionMessageContents<T>,
+ message: ParsedOnionMessageContents<T>,
}
}
+/// The contents of an onion message as read from the wire.
#[derive(Debug)]
-/// The contents of an onion message. In the context of offers, this would be the invoice, invoice
-/// request, or invoice error.
-pub enum OnionMessageContents<T: CustomOnionMessageContents> {
+pub enum ParsedOnionMessageContents<T: CustomOnionMessageContents> {
/// A message related to BOLT 12 Offers.
Offers(OffersMessage),
/// A custom onion message specified by the user.
Custom(T),
}
-impl<T: CustomOnionMessageContents> OnionMessageContents<T> {
+impl<T: CustomOnionMessageContents> ParsedOnionMessageContents<T> {
/// Returns the type that was used to decode the message payload.
///
/// This is not exported to bindings users as methods on non-cloneable enums are not currently exportable
pub fn tlv_type(&self) -> u64 {
match self {
- &OnionMessageContents::Offers(ref msg) => msg.tlv_type(),
- &OnionMessageContents::Custom(ref msg) => msg.tlv_type(),
+ &ParsedOnionMessageContents::Offers(ref msg) => msg.tlv_type(),
+ &ParsedOnionMessageContents::Custom(ref msg) => msg.tlv_type(),
}
}
}
/// This is not exported to bindings users as methods on non-cloneable enums are not currently exportable
-impl<T: CustomOnionMessageContents> Writeable for OnionMessageContents<T> {
+impl<T: CustomOnionMessageContents> Writeable for ParsedOnionMessageContents<T> {
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
match self {
- OnionMessageContents::Offers(msg) => Ok(msg.write(w)?),
- OnionMessageContents::Custom(msg) => Ok(msg.write(w)?),
+ ParsedOnionMessageContents::Offers(msg) => Ok(msg.write(w)?),
+ ParsedOnionMessageContents::Custom(msg) => Ok(msg.write(w)?),
}
}
}
match msg_type {
tlv_type if OffersMessage::is_known_type(tlv_type) => {
let msg = OffersMessage::read(msg_reader, (tlv_type, logger))?;
- message = Some(OnionMessageContents::Offers(msg));
+ message = Some(ParsedOnionMessageContents::Offers(msg));
Ok(true)
},
_ => match handler.read_custom_message(msg_type, msg_reader)? {
Some(msg) => {
- message = Some(OnionMessageContents::Custom(msg));
+ message = Some(ParsedOnionMessageContents::Custom(msg));
Ok(true)
},
None => Ok(false),