1 // This file is Copyright its original authors, visible in version control
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
10 //! Message handling for BOLT 12 Offers.
12 use core::convert::TryFrom;
13 use crate::io::{self, Read};
14 use crate::ln::msgs::DecodeError;
15 use crate::offers::invoice_error::InvoiceError;
16 use crate::offers::invoice_request::InvoiceRequest;
17 use crate::offers::invoice::Bolt12Invoice;
18 use crate::offers::parse::Bolt12ParseError;
19 use crate::util::logger::Logger;
20 use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer};
22 use crate::prelude::*;
24 // TLV record types for the `onionmsg_tlv` TLV stream as defined in BOLT 4.
25 const INVOICE_REQUEST_TLV_TYPE: u64 = 64;
26 const INVOICE_TLV_TYPE: u64 = 66;
27 const INVOICE_ERROR_TLV_TYPE: u64 = 68;
29 /// A handler for an [`OnionMessage`] containing a BOLT 12 Offers message as its payload.
31 /// [`OnionMessage`]: crate::ln::msgs::OnionMessage
32 pub trait OffersMessageHandler {
33 /// Handles the given message by either responding with an [`Bolt12Invoice`], sending a payment,
34 /// or replying with an error.
35 fn handle_message(&self, message: OffersMessage) -> Option<OffersMessage>;
38 /// Possible BOLT 12 Offers messages sent and received via an [`OnionMessage`].
40 /// [`OnionMessage`]: crate::ln::msgs::OnionMessage
41 #[derive(Clone, Debug)]
42 pub enum OffersMessage {
43 /// A request for a [`Bolt12Invoice`] for a particular [`Offer`].
45 /// [`Offer`]: crate::offers::offer::Offer
46 InvoiceRequest(InvoiceRequest),
48 /// A [`Bolt12Invoice`] sent in response to an [`InvoiceRequest`] or a [`Refund`].
50 /// [`Refund`]: crate::offers::refund::Refund
51 Invoice(Bolt12Invoice),
53 /// An error from handling an [`OffersMessage`].
54 InvoiceError(InvoiceError),
58 /// Returns whether `tlv_type` corresponds to a TLV record for Offers.
59 pub fn is_known_type(tlv_type: u64) -> bool {
61 INVOICE_REQUEST_TLV_TYPE | INVOICE_TLV_TYPE | INVOICE_ERROR_TLV_TYPE => true,
66 /// The TLV record type for the message as used in an `onionmsg_tlv` TLV stream.
67 pub fn tlv_type(&self) -> u64 {
69 OffersMessage::InvoiceRequest(_) => INVOICE_REQUEST_TLV_TYPE,
70 OffersMessage::Invoice(_) => INVOICE_TLV_TYPE,
71 OffersMessage::InvoiceError(_) => INVOICE_ERROR_TLV_TYPE,
75 fn parse(tlv_type: u64, bytes: Vec<u8>) -> Result<Self, Bolt12ParseError> {
77 INVOICE_REQUEST_TLV_TYPE => Ok(Self::InvoiceRequest(InvoiceRequest::try_from(bytes)?)),
78 INVOICE_TLV_TYPE => Ok(Self::Invoice(Bolt12Invoice::try_from(bytes)?)),
79 _ => Err(Bolt12ParseError::Decode(DecodeError::InvalidValue)),
84 impl Writeable for OffersMessage {
85 fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
87 OffersMessage::InvoiceRequest(message) => message.write(w),
88 OffersMessage::Invoice(message) => message.write(w),
89 OffersMessage::InvoiceError(message) => message.write(w),
94 impl<L: Logger + ?Sized> ReadableArgs<(u64, &L)> for OffersMessage {
95 fn read<R: Read>(r: &mut R, read_args: (u64, &L)) -> Result<Self, DecodeError> {
96 let (tlv_type, logger) = read_args;
97 if tlv_type == INVOICE_ERROR_TLV_TYPE {
98 return Ok(Self::InvoiceError(InvoiceError::read(r)?));
101 let mut bytes = Vec::new();
102 r.read_to_end(&mut bytes).unwrap();
104 match Self::parse(tlv_type, bytes) {
105 Ok(message) => Ok(message),
106 Err(Bolt12ParseError::Decode(e)) => Err(e),
107 Err(Bolt12ParseError::InvalidSemantics(e)) => {
108 log_trace!(logger, "Invalid semantics for TLV type {}: {:?}", tlv_type, e);
109 Err(DecodeError::InvalidValue)
111 Err(Bolt12ParseError::InvalidSignature(e)) => {
112 log_trace!(logger, "Invalid signature for TLV type {}: {:?}", tlv_type, e);
113 Err(DecodeError::InvalidValue)
115 Err(_) => Err(DecodeError::InvalidValue),