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.
|
|
|
|
|
|
|
|
use core::convert::TryFrom;
|
|
|
|
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;
|
2023-02-14 17:41:18 -06:00
|
|
|
use crate::util::logger::Logger;
|
|
|
|
use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer};
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
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.
|
2023-02-18 19:29:14 -06:00
|
|
|
fn handle_message(&self, message: OffersMessage) -> Option<OffersMessage>;
|
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
|
|
|
|
#[derive(Debug)]
|
|
|
|
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
|
|
|
|
|
|
|
/// 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 {
|
|
|
|
INVOICE_REQUEST_TLV_TYPE | INVOICE_TLV_TYPE | INVOICE_ERROR_TLV_TYPE => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The TLV record type for the message as used in an `onionmsg_tlv` TLV stream.
|
|
|
|
pub fn tlv_type(&self) -> u64 {
|
|
|
|
match self {
|
|
|
|
OffersMessage::InvoiceRequest(_) => INVOICE_REQUEST_TLV_TYPE,
|
|
|
|
OffersMessage::Invoice(_) => INVOICE_TLV_TYPE,
|
|
|
|
OffersMessage::InvoiceError(_) => INVOICE_ERROR_TLV_TYPE,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)?)),
|
2023-07-13 16:09:43 -05:00
|
|
|
_ => Err(Bolt12ParseError::Decode(DecodeError::InvalidValue)),
|
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),
|
|
|
|
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();
|
|
|
|
r.read_to_end(&mut bytes).unwrap();
|
|
|
|
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|