|
| 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::Invoice; |
| 18 | +use crate::offers::parse::ParseError; |
| 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 | +/// Possible BOLT 12 Offers messages sent and received via an [`OnionMessage`]. |
| 30 | +/// |
| 31 | +/// [`OnionMessage`]: crate::ln::msgs::OnionMessage |
| 32 | +#[derive(Debug)] |
| 33 | +pub enum OffersMessage { |
| 34 | + /// A request for an [`Invoice`] for a particular [`Offer`]. |
| 35 | + /// |
| 36 | + /// [`Offer`]: crate::offers::offer::Offer |
| 37 | + InvoiceRequest(InvoiceRequest), |
| 38 | + |
| 39 | + /// An [`Invoice`] sent in response to an [`InvoiceRequest`] or a [`Refund`]. |
| 40 | + /// |
| 41 | + /// [`Refund`]: crate::offers::refund::Refund |
| 42 | + Invoice(Invoice), |
| 43 | + |
| 44 | + /// An error from handling an [`OffersMessage`]. |
| 45 | + InvoiceError(InvoiceError), |
| 46 | +} |
| 47 | + |
| 48 | +impl OffersMessage { |
| 49 | + /// Returns whether `tlv_type` corresponds to a TLV record for Offers. |
| 50 | + pub fn is_known_type(tlv_type: u64) -> bool { |
| 51 | + match tlv_type { |
| 52 | + INVOICE_REQUEST_TLV_TYPE | INVOICE_TLV_TYPE | INVOICE_ERROR_TLV_TYPE => true, |
| 53 | + _ => false, |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /// The TLV record type for the message as used in an `onionmsg_tlv` TLV stream. |
| 58 | + pub fn tlv_type(&self) -> u64 { |
| 59 | + match self { |
| 60 | + OffersMessage::InvoiceRequest(_) => INVOICE_REQUEST_TLV_TYPE, |
| 61 | + OffersMessage::Invoice(_) => INVOICE_TLV_TYPE, |
| 62 | + OffersMessage::InvoiceError(_) => INVOICE_ERROR_TLV_TYPE, |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + fn parse(tlv_type: u64, bytes: Vec<u8>) -> Result<Self, ParseError> { |
| 67 | + match tlv_type { |
| 68 | + INVOICE_REQUEST_TLV_TYPE => Ok(Self::InvoiceRequest(InvoiceRequest::try_from(bytes)?)), |
| 69 | + INVOICE_TLV_TYPE => Ok(Self::Invoice(Invoice::try_from(bytes)?)), |
| 70 | + _ => Err(ParseError::Decode(DecodeError::InvalidValue)), |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +impl Writeable for OffersMessage { |
| 76 | + fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> { |
| 77 | + match self { |
| 78 | + OffersMessage::InvoiceRequest(message) => message.write(w), |
| 79 | + OffersMessage::Invoice(message) => message.write(w), |
| 80 | + OffersMessage::InvoiceError(message) => message.write(w), |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +impl<L: Logger> ReadableArgs<(u64, &L)> for OffersMessage { |
| 86 | + fn read<R: Read>(r: &mut R, read_args: (u64, &L)) -> Result<Self, DecodeError> { |
| 87 | + let (tlv_type, logger) = read_args; |
| 88 | + if tlv_type == INVOICE_ERROR_TLV_TYPE { |
| 89 | + return Ok(Self::InvoiceError(InvoiceError::read(r)?)); |
| 90 | + } |
| 91 | + |
| 92 | + let mut bytes = Vec::new(); |
| 93 | + r.read_to_end(&mut bytes).unwrap(); |
| 94 | + |
| 95 | + match Self::parse(tlv_type, bytes) { |
| 96 | + Ok(message) => Ok(message), |
| 97 | + Err(ParseError::Decode(e)) => Err(e), |
| 98 | + Err(ParseError::InvalidSemantics(e)) => { |
| 99 | + log_trace!(logger, "Invalid semantics for TLV type {}: {:?}", tlv_type, e); |
| 100 | + Err(DecodeError::InvalidValue) |
| 101 | + }, |
| 102 | + Err(ParseError::InvalidSignature(e)) => { |
| 103 | + log_trace!(logger, "Invalid signature for TLV type {}: {:?}", tlv_type, e); |
| 104 | + Err(DecodeError::InvalidValue) |
| 105 | + }, |
| 106 | + Err(_) => Err(DecodeError::InvalidValue), |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments