2023-02-14 17:41:18 -06:00
|
|
|
// This file is Copyright its original authors, visible in version control
|
|
|
|
// history.
|
|
|
|
//
|
|
|
|
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
|
|
|
|
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
|
|
|
|
// You may not use this file except in accordance with one or both of these
|
|
|
|
// licenses.
|
|
|
|
|
|
|
|
//! Message handling for BOLT 12 Offers.
|
|
|
|
|
2023-08-08 12:01:11 +08:00
|
|
|
use core::fmt;
|
2024-06-15 19:22:28 +05:30
|
|
|
use crate::blinded_path::message::OffersContext;
|
2023-02-14 17:41:18 -06:00
|
|
|
use crate::io::{self, Read};
|
|
|
|
use crate::ln::msgs::DecodeError;
|
|
|
|
use crate::offers::invoice_error::InvoiceError;
|
|
|
|
use crate::offers::invoice_request::InvoiceRequest;
|
2023-07-13 13:32:40 -05:00
|
|
|
use crate::offers::invoice::Bolt12Invoice;
|
2023-07-13 16:09:43 -05:00
|
|
|
use crate::offers::parse::Bolt12ParseError;
|
2024-06-13 14:09:12 -04:00
|
|
|
#[cfg(async_payments)]
|
2024-05-29 14:48:05 -04:00
|
|
|
use crate::offers::static_invoice::StaticInvoice;
|
Expose `onion_message` items directly rather than via re-exports
When we originally added the `onion_message` module, there weren't
a lot of public items in it, and it didn't make a lot of sense to
export the whole sub-module structure publicly. So, instead, we
exported the public items via re-exports directly in the
`onion_message` top-level module. However, as time went on, more
and more things entered the module, which left the top-level module
rather cluttered.
Worse, in 0.0.119, we exposed
`onion_message::messenger::SendSuccess` via the return type of
`send_message`, but forgot to re-export the enum itself, making
it impossible to actually use from external code.
Here we address both issues and simply replace the re-export with
the underlying sub-module structure.
2024-01-10 18:27:57 +00:00
|
|
|
use crate::onion_message::packet::OnionMessageContents;
|
2023-02-14 17:41:18 -06:00
|
|
|
use crate::util::logger::Logger;
|
|
|
|
use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer};
|
2024-08-22 01:42:55 +00:00
|
|
|
use crate::onion_message::messenger::{ResponseInstruction, Responder, MessageSendInstructions};
|
2023-02-14 17:41:18 -06:00
|
|
|
|
|
|
|
use crate::prelude::*;
|
|
|
|
|
|
|
|
// TLV record types for the `onionmsg_tlv` TLV stream as defined in BOLT 4.
|
|
|
|
const INVOICE_REQUEST_TLV_TYPE: u64 = 64;
|
|
|
|
const INVOICE_TLV_TYPE: u64 = 66;
|
|
|
|
const INVOICE_ERROR_TLV_TYPE: u64 = 68;
|
2024-06-13 14:09:12 -04:00
|
|
|
#[cfg(async_payments)]
|
2024-05-29 14:48:05 -04:00
|
|
|
const STATIC_INVOICE_TLV_TYPE: u64 = 70;
|
2023-02-14 17:41:18 -06:00
|
|
|
|
2023-02-15 16:10:59 -06:00
|
|
|
/// A handler for an [`OnionMessage`] containing a BOLT 12 Offers message as its payload.
|
|
|
|
///
|
|
|
|
/// [`OnionMessage`]: crate::ln::msgs::OnionMessage
|
|
|
|
pub trait OffersMessageHandler {
|
2023-07-13 13:32:40 -05:00
|
|
|
/// Handles the given message by either responding with an [`Bolt12Invoice`], sending a payment,
|
|
|
|
/// or replying with an error.
|
Enqueue onion messages in handlers
When constructing onion messages to send initially (opposed to replying
to one from a handler), the user must construct an OnionMessagePath first
before calling OnionMessener::send_onion_message. Additionally, having a
reference to OnionMessener isn't always desirable. For instance, in an
upcoming commit, ChannelManager will implement OffersMessageHandler,
which OnionMessenger needs a reference to. If ChannelManager had a
reference to OnionMessenger, too, there would be a dependency cycle.
Instead, modify OffersMessageHandler and CustomOnionMessageHandler's
interfaces to include a method for releasing pending onion messages.
That way, ChannelManager may, for instance, construct and enqueue an
InvoiceRequest for sending without needing a reference to
OnionMessenger.
Additionally, OnionMessenger has responsibility for path finding just as
it does when replying to messages from a handler. It performs this when
extracting messages from the handlers before returning the next message
to send to a peer.
2023-09-13 21:19:50 -05:00
|
|
|
///
|
|
|
|
/// The returned [`OffersMessage`], if any, is enqueued to be sent by [`OnionMessenger`].
|
|
|
|
///
|
Expose `onion_message` items directly rather than via re-exports
When we originally added the `onion_message` module, there weren't
a lot of public items in it, and it didn't make a lot of sense to
export the whole sub-module structure publicly. So, instead, we
exported the public items via re-exports directly in the
`onion_message` top-level module. However, as time went on, more
and more things entered the module, which left the top-level module
rather cluttered.
Worse, in 0.0.119, we exposed
`onion_message::messenger::SendSuccess` via the return type of
`send_message`, but forgot to re-export the enum itself, making
it impossible to actually use from external code.
Here we address both issues and simply replace the re-export with
the underlying sub-module structure.
2024-01-10 18:27:57 +00:00
|
|
|
/// [`OnionMessenger`]: crate::onion_message::messenger::OnionMessenger
|
2024-07-23 18:24:43 -05:00
|
|
|
fn handle_message(
|
|
|
|
&self, message: OffersMessage, context: Option<OffersContext>, responder: Option<Responder>,
|
2024-08-21 15:32:14 +00:00
|
|
|
) -> Option<(OffersMessage, ResponseInstruction)>;
|
Enqueue onion messages in handlers
When constructing onion messages to send initially (opposed to replying
to one from a handler), the user must construct an OnionMessagePath first
before calling OnionMessener::send_onion_message. Additionally, having a
reference to OnionMessener isn't always desirable. For instance, in an
upcoming commit, ChannelManager will implement OffersMessageHandler,
which OnionMessenger needs a reference to. If ChannelManager had a
reference to OnionMessenger, too, there would be a dependency cycle.
Instead, modify OffersMessageHandler and CustomOnionMessageHandler's
interfaces to include a method for releasing pending onion messages.
That way, ChannelManager may, for instance, construct and enqueue an
InvoiceRequest for sending without needing a reference to
OnionMessenger.
Additionally, OnionMessenger has responsibility for path finding just as
it does when replying to messages from a handler. It performs this when
extracting messages from the handlers before returning the next message
to send to a peer.
2023-09-13 21:19:50 -05:00
|
|
|
|
|
|
|
/// Releases any [`OffersMessage`]s that need to be sent.
|
|
|
|
///
|
|
|
|
/// Typically, this is used for messages initiating a payment flow rather than in response to
|
|
|
|
/// another message. The latter should use the return value of [`Self::handle_message`].
|
2024-08-22 01:42:55 +00:00
|
|
|
fn release_pending_messages(&self) -> Vec<(OffersMessage, MessageSendInstructions)> { vec![] }
|
2023-02-15 16:10:59 -06:00
|
|
|
}
|
|
|
|
|
2023-02-14 17:41:18 -06:00
|
|
|
/// Possible BOLT 12 Offers messages sent and received via an [`OnionMessage`].
|
|
|
|
///
|
|
|
|
/// [`OnionMessage`]: crate::ln::msgs::OnionMessage
|
2023-08-08 12:01:11 +08:00
|
|
|
#[derive(Clone)]
|
2023-02-14 17:41:18 -06:00
|
|
|
pub enum OffersMessage {
|
2023-07-13 13:32:40 -05:00
|
|
|
/// A request for a [`Bolt12Invoice`] for a particular [`Offer`].
|
2023-02-14 17:41:18 -06:00
|
|
|
///
|
|
|
|
/// [`Offer`]: crate::offers::offer::Offer
|
|
|
|
InvoiceRequest(InvoiceRequest),
|
|
|
|
|
2023-07-13 13:32:40 -05:00
|
|
|
/// A [`Bolt12Invoice`] sent in response to an [`InvoiceRequest`] or a [`Refund`].
|
2023-02-14 17:41:18 -06:00
|
|
|
///
|
|
|
|
/// [`Refund`]: crate::offers::refund::Refund
|
2023-07-13 13:32:40 -05:00
|
|
|
Invoice(Bolt12Invoice),
|
2023-02-14 17:41:18 -06:00
|
|
|
|
2024-06-13 14:09:12 -04:00
|
|
|
#[cfg(async_payments)]
|
|
|
|
/// A [`StaticInvoice`] sent in response to an [`InvoiceRequest`].
|
2024-05-29 14:48:05 -04:00
|
|
|
StaticInvoice(StaticInvoice),
|
|
|
|
|
2023-02-14 17:41:18 -06:00
|
|
|
/// An error from handling an [`OffersMessage`].
|
|
|
|
InvoiceError(InvoiceError),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl OffersMessage {
|
|
|
|
/// Returns whether `tlv_type` corresponds to a TLV record for Offers.
|
|
|
|
pub fn is_known_type(tlv_type: u64) -> bool {
|
|
|
|
match tlv_type {
|
2024-05-29 14:48:05 -04:00
|
|
|
INVOICE_REQUEST_TLV_TYPE
|
|
|
|
| INVOICE_TLV_TYPE
|
|
|
|
| INVOICE_ERROR_TLV_TYPE => true,
|
2024-06-13 14:09:12 -04:00
|
|
|
#[cfg(async_payments)]
|
|
|
|
STATIC_INVOICE_TLV_TYPE => true,
|
2023-02-14 17:41:18 -06:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-13 16:09:43 -05:00
|
|
|
fn parse(tlv_type: u64, bytes: Vec<u8>) -> Result<Self, Bolt12ParseError> {
|
2023-02-14 17:41:18 -06:00
|
|
|
match tlv_type {
|
|
|
|
INVOICE_REQUEST_TLV_TYPE => Ok(Self::InvoiceRequest(InvoiceRequest::try_from(bytes)?)),
|
2023-07-13 13:32:40 -05:00
|
|
|
INVOICE_TLV_TYPE => Ok(Self::Invoice(Bolt12Invoice::try_from(bytes)?)),
|
2024-06-13 14:09:12 -04:00
|
|
|
#[cfg(async_payments)]
|
2024-05-29 14:48:05 -04:00
|
|
|
STATIC_INVOICE_TLV_TYPE => Ok(Self::StaticInvoice(StaticInvoice::try_from(bytes)?)),
|
2023-07-13 16:09:43 -05:00
|
|
|
_ => Err(Bolt12ParseError::Decode(DecodeError::InvalidValue)),
|
2023-02-14 17:41:18 -06:00
|
|
|
}
|
|
|
|
}
|
2024-08-26 19:25:46 +00:00
|
|
|
|
|
|
|
fn get_msg_type(&self) -> &'static str {
|
|
|
|
match &self {
|
|
|
|
OffersMessage::InvoiceRequest(_) => "Invoice Request",
|
|
|
|
OffersMessage::Invoice(_) => "Invoice",
|
|
|
|
#[cfg(async_payments)]
|
|
|
|
OffersMessage::StaticInvoice(_) => "Static Invoice",
|
|
|
|
OffersMessage::InvoiceError(_) => "Invoice Error",
|
|
|
|
}
|
|
|
|
}
|
2023-02-14 17:41:18 -06:00
|
|
|
}
|
|
|
|
|
2023-08-08 12:01:11 +08:00
|
|
|
impl fmt::Debug for OffersMessage {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
OffersMessage::InvoiceRequest(message) => {
|
|
|
|
write!(f, "{:?}", message.as_tlv_stream())
|
|
|
|
}
|
|
|
|
OffersMessage::Invoice(message) => {
|
|
|
|
write!(f, "{:?}", message.as_tlv_stream())
|
|
|
|
}
|
2024-06-13 14:09:12 -04:00
|
|
|
#[cfg(async_payments)]
|
2024-05-29 14:48:05 -04:00
|
|
|
OffersMessage::StaticInvoice(message) => {
|
|
|
|
write!(f, "{:?}", message)
|
|
|
|
}
|
2023-08-08 12:01:11 +08:00
|
|
|
OffersMessage::InvoiceError(message) => {
|
|
|
|
write!(f, "{:?}", message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-19 12:35:13 -05:00
|
|
|
impl OnionMessageContents for OffersMessage {
|
|
|
|
fn tlv_type(&self) -> u64 {
|
|
|
|
match self {
|
|
|
|
OffersMessage::InvoiceRequest(_) => INVOICE_REQUEST_TLV_TYPE,
|
|
|
|
OffersMessage::Invoice(_) => INVOICE_TLV_TYPE,
|
2024-06-13 14:09:12 -04:00
|
|
|
#[cfg(async_payments)]
|
2024-05-29 14:48:05 -04:00
|
|
|
OffersMessage::StaticInvoice(_) => STATIC_INVOICE_TLV_TYPE,
|
2023-09-19 12:35:13 -05:00
|
|
|
OffersMessage::InvoiceError(_) => INVOICE_ERROR_TLV_TYPE,
|
|
|
|
}
|
|
|
|
}
|
2024-08-26 19:25:46 +00:00
|
|
|
#[cfg(c_bindings)]
|
|
|
|
fn msg_type(&self) -> String {
|
|
|
|
self.get_msg_type().to_string()
|
|
|
|
}
|
|
|
|
#[cfg(not(c_bindings))]
|
2024-04-18 17:01:52 +05:30
|
|
|
fn msg_type(&self) -> &'static str {
|
2024-08-26 19:25:46 +00:00
|
|
|
self.get_msg_type()
|
2024-04-18 17:01:52 +05:30
|
|
|
}
|
2023-09-19 12:35:13 -05:00
|
|
|
}
|
|
|
|
|
2023-02-14 17:41:18 -06:00
|
|
|
impl Writeable for OffersMessage {
|
|
|
|
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
|
|
|
|
match self {
|
|
|
|
OffersMessage::InvoiceRequest(message) => message.write(w),
|
|
|
|
OffersMessage::Invoice(message) => message.write(w),
|
2024-06-13 14:09:12 -04:00
|
|
|
#[cfg(async_payments)]
|
2024-05-29 14:48:05 -04:00
|
|
|
OffersMessage::StaticInvoice(message) => message.write(w),
|
2023-02-14 17:41:18 -06:00
|
|
|
OffersMessage::InvoiceError(message) => message.write(w),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-15 16:10:59 -06:00
|
|
|
impl<L: Logger + ?Sized> ReadableArgs<(u64, &L)> for OffersMessage {
|
2023-02-14 17:41:18 -06:00
|
|
|
fn read<R: Read>(r: &mut R, read_args: (u64, &L)) -> Result<Self, DecodeError> {
|
|
|
|
let (tlv_type, logger) = read_args;
|
|
|
|
if tlv_type == INVOICE_ERROR_TLV_TYPE {
|
|
|
|
return Ok(Self::InvoiceError(InvoiceError::read(r)?));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut bytes = Vec::new();
|
2024-08-16 08:02:50 -07:00
|
|
|
r.read_to_limit(&mut bytes, u64::MAX).unwrap();
|
2023-02-14 17:41:18 -06:00
|
|
|
|
|
|
|
match Self::parse(tlv_type, bytes) {
|
|
|
|
Ok(message) => Ok(message),
|
2023-07-13 16:09:43 -05:00
|
|
|
Err(Bolt12ParseError::Decode(e)) => Err(e),
|
|
|
|
Err(Bolt12ParseError::InvalidSemantics(e)) => {
|
2023-02-14 17:41:18 -06:00
|
|
|
log_trace!(logger, "Invalid semantics for TLV type {}: {:?}", tlv_type, e);
|
|
|
|
Err(DecodeError::InvalidValue)
|
|
|
|
},
|
2023-07-13 16:09:43 -05:00
|
|
|
Err(Bolt12ParseError::InvalidSignature(e)) => {
|
2023-02-14 17:41:18 -06:00
|
|
|
log_trace!(logger, "Invalid signature for TLV type {}: {:?}", tlv_type, e);
|
|
|
|
Err(DecodeError::InvalidValue)
|
|
|
|
},
|
|
|
|
Err(_) => Err(DecodeError::InvalidValue),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|