]> git.bitcoin.ninja Git - rust-lightning/blob - lightning/src/onion_message/offers.rs
Disconnect peer when force closing a funded channel with an error
[rust-lightning] / lightning / src / onion_message / offers.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
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
8 // licenses.
9
10 //! Message handling for BOLT 12 Offers.
11
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};
21
22 use crate::prelude::*;
23
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;
28
29 /// A handler for an [`OnionMessage`] containing a BOLT 12 Offers message as its payload.
30 ///
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>;
36 }
37
38 /// Possible BOLT 12 Offers messages sent and received via an [`OnionMessage`].
39 ///
40 /// [`OnionMessage`]: crate::ln::msgs::OnionMessage
41 #[derive(Clone, Debug)]
42 pub enum OffersMessage {
43         /// A request for a [`Bolt12Invoice`] for a particular [`Offer`].
44         ///
45         /// [`Offer`]: crate::offers::offer::Offer
46         InvoiceRequest(InvoiceRequest),
47
48         /// A [`Bolt12Invoice`] sent in response to an [`InvoiceRequest`] or a [`Refund`].
49         ///
50         /// [`Refund`]: crate::offers::refund::Refund
51         Invoice(Bolt12Invoice),
52
53         /// An error from handling an [`OffersMessage`].
54         InvoiceError(InvoiceError),
55 }
56
57 impl OffersMessage {
58         /// Returns whether `tlv_type` corresponds to a TLV record for Offers.
59         pub fn is_known_type(tlv_type: u64) -> bool {
60                 match tlv_type {
61                         INVOICE_REQUEST_TLV_TYPE | INVOICE_TLV_TYPE | INVOICE_ERROR_TLV_TYPE => true,
62                         _ => false,
63                 }
64         }
65
66         /// The TLV record type for the message as used in an `onionmsg_tlv` TLV stream.
67         pub fn tlv_type(&self) -> u64 {
68                 match self {
69                         OffersMessage::InvoiceRequest(_) => INVOICE_REQUEST_TLV_TYPE,
70                         OffersMessage::Invoice(_) => INVOICE_TLV_TYPE,
71                         OffersMessage::InvoiceError(_) => INVOICE_ERROR_TLV_TYPE,
72                 }
73         }
74
75         fn parse(tlv_type: u64, bytes: Vec<u8>) -> Result<Self, Bolt12ParseError> {
76                 match tlv_type {
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)),
80                 }
81         }
82 }
83
84 impl Writeable for OffersMessage {
85         fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
86                 match self {
87                         OffersMessage::InvoiceRequest(message) => message.write(w),
88                         OffersMessage::Invoice(message) => message.write(w),
89                         OffersMessage::InvoiceError(message) => message.write(w),
90                 }
91         }
92 }
93
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)?));
99                 }
100
101                 let mut bytes = Vec::new();
102                 r.read_to_end(&mut bytes).unwrap();
103
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)
110                         },
111                         Err(Bolt12ParseError::InvalidSignature(e)) => {
112                                 log_trace!(logger, "Invalid signature for TLV type {}: {:?}", tlv_type, e);
113                                 Err(DecodeError::InvalidValue)
114                         },
115                         Err(_) => Err(DecodeError::InvalidValue),
116                 }
117         }
118 }