--- /dev/null
+// 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;
+use crate::offers::invoice::Invoice;
+use crate::offers::parse::ParseError;
+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;
+
+/// Possible BOLT 12 Offers messages sent and received via an [`OnionMessage`].
+///
+/// [`OnionMessage`]: crate::ln::msgs::OnionMessage
+#[derive(Debug)]
+pub enum OffersMessage {
+ /// A request for an [`Invoice`] for a particular [`Offer`].
+ ///
+ /// [`Offer`]: crate::offers::offer::Offer
+ InvoiceRequest(InvoiceRequest),
+
+ /// An [`Invoice`] sent in response to an [`InvoiceRequest`] or a [`Refund`].
+ ///
+ /// [`Refund`]: crate::offers::refund::Refund
+ Invoice(Invoice),
+
+ /// 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,
+ }
+ }
+
+ fn parse(tlv_type: u64, bytes: Vec<u8>) -> Result<Self, ParseError> {
+ match tlv_type {
+ INVOICE_REQUEST_TLV_TYPE => Ok(Self::InvoiceRequest(InvoiceRequest::try_from(bytes)?)),
+ INVOICE_TLV_TYPE => Ok(Self::Invoice(Invoice::try_from(bytes)?)),
+ _ => Err(ParseError::Decode(DecodeError::InvalidValue)),
+ }
+ }
+}
+
+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),
+ }
+ }
+}
+
+impl<L: Logger> ReadableArgs<(u64, &L)> for OffersMessage {
+ 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),
+ Err(ParseError::Decode(e)) => Err(e),
+ Err(ParseError::InvalidSemantics(e)) => {
+ log_trace!(logger, "Invalid semantics for TLV type {}: {:?}", tlv_type, e);
+ Err(DecodeError::InvalidValue)
+ },
+ Err(ParseError::InvalidSignature(e)) => {
+ log_trace!(logger, "Invalid signature for TLV type {}: {:?}", tlv_type, e);
+ Err(DecodeError::InvalidValue)
+ },
+ Err(_) => Err(DecodeError::InvalidValue),
+ }
+ }
+}
use crate::ln::msgs::DecodeError;
use crate::ln::onion_utils;
use super::messenger::CustomOnionMessageHandler;
+use super::offers::OffersMessage;
use crate::util::chacha20poly1305rfc::{ChaChaPolyReadAdapter, ChaChaPolyWriteAdapter};
use crate::util::logger::Logger;
use crate::util::ser::{BigSize, FixedLengthReader, LengthRead, LengthReadable, LengthReadableArgs, Readable, ReadableArgs, Writeable, Writer};
/// The contents of an onion message. In the context of offers, this would be the invoice, invoice
/// request, or invoice error.
pub enum OnionMessageContents<T: CustomOnionMessageContents> {
- // Coming soon:
- // Invoice,
- // InvoiceRequest,
- // InvoiceError,
+ /// A message related to BOLT 12 Offers.
+ Offers(OffersMessage),
/// A custom onion message specified by the user.
Custom(T),
}
/// This is not exported to bindings users as methods on non-cloneable enums are not currently exportable
pub fn tlv_type(&self) -> u64 {
match self {
+ &OnionMessageContents::Offers(ref msg) => msg.tlv_type(),
&OnionMessageContents::Custom(ref msg) => msg.tlv_type(),
}
}
impl<T: CustomOnionMessageContents> Writeable for OnionMessageContents<T> {
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
match self {
+ OnionMessageContents::Offers(msg) => Ok(msg.write(w)?),
OnionMessageContents::Custom(msg) => Ok(msg.write(w)?),
}
}
impl<H: CustomOnionMessageHandler, L: Logger>
ReadableArgs<(SharedSecret, &H, &L)> for Payload<<H as CustomOnionMessageHandler>::CustomMessage> {
fn read<R: Read>(r: &mut R, args: (SharedSecret, &H, &L)) -> Result<Self, DecodeError> {
- let (encrypted_tlvs_ss, handler, _logger) = args;
+ let (encrypted_tlvs_ss, handler, logger) = args;
let v: BigSize = Readable::read(r)?;
let mut rd = FixedLengthReader::new(r, v.0);
if message_type.is_some() { return Err(DecodeError::InvalidValue) }
message_type = Some(msg_type);
- match handler.read_custom_message(msg_type, msg_reader) {
- Ok(Some(msg)) => {
- message = Some(msg);
+ match msg_type {
+ tlv_type if OffersMessage::is_known_type(tlv_type) => {
+ let msg = OffersMessage::read(msg_reader, (tlv_type, logger))?;
+ message = Some(OnionMessageContents::Offers(msg));
Ok(true)
},
- Ok(None) => Ok(false),
- Err(e) => Err(e),
+ _ => match handler.read_custom_message(msg_type, msg_reader)? {
+ Some(msg) => {
+ message = Some(OnionMessageContents::Custom(msg));
+ Ok(true)
+ },
+ None => Ok(false),
+ },
}
});
rd.eat_remaining().map_err(|_| DecodeError::ShortRead)?;
Ok(Payload::Receive {
control_tlvs: ReceiveControlTlvs::Unblinded(tlvs),
reply_path,
- message: OnionMessageContents::Custom(message.unwrap()),
+ message: message.unwrap(),
})
}
}