use ln::onion_utils;
use super::blinded_route::{BlindedRoute, ForwardTlvs, ReceiveTlvs};
use util::chacha20poly1305rfc::{ChaChaPolyReadAdapter, ChaChaPolyWriteAdapter};
-use util::ser::{BigSize, FixedLengthReader, LengthRead, LengthReadable, LengthReadableArgs, Readable, ReadableArgs, Writeable, Writer};
+use util::ser::{BigSize, FixedLengthReader, LengthRead, LengthReadable, LengthReadableArgs, MaybeReadableArgs, Readable, ReadableArgs, Writeable, Writer};
use core::cmp;
use io::{self, Read};
while read_idx < hop_data_len {
let mut read_buffer = [0; READ_BUFFER_SIZE];
let read_amt = cmp::min(hop_data_len - read_idx, READ_BUFFER_SIZE);
- r.read_exact(&mut read_buffer[..read_amt]);
+ r.read_exact(&mut read_buffer[..read_amt])?;
hop_data.extend_from_slice(&read_buffer[..read_amt]);
read_idx += read_amt;
}
}
}
-// Coming soon:
-// enum Message {
-// InvoiceRequest(InvoiceRequest),
-// Invoice(Invoice),
-// InvoiceError(InvoiceError),
-// CustomMessage<T>,
-// }
+#[derive(Debug)]
+/// 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> where T: CustomOnionMessageContents {
+ // Coming soon:
+ // Invoice,
+ // InvoiceRequest,
+ // InvoiceError,
+ /// A custom onion message specified by the user.
+ Custom(T),
+}
+
+impl<T> OnionMessageContents<T> where T: CustomOnionMessageContents {
+ /// Returns the type that was used to decode the message payload.
+ pub fn tlv_type(&self) -> u64 {
+ match self {
+ &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::Custom(msg) => Ok(msg.write(w)?),
+ }
+ }
+}
+
+/// The contents of a custom onion message. Must implement `MaybeReadableArgs<u64>` where the `u64`
+/// is the custom TLV type attempting to be read, and return `Ok(None)` if the TLV type is unknown.
+pub trait CustomOnionMessageContents: Writeable + MaybeReadableArgs<u64> {
+ /// Returns the TLV type identifying the message contents. MUST be >= 64.
+ fn tlv_type(&self) -> u64;
+}
/// Forward control TLVs in their blinded and unblinded form.
pub(super) enum ForwardControlTlvs {
// Uses the provided secret to simultaneously decode and decrypt the control TLVs.
impl ReadableArgs<SharedSecret> for Payload {
- fn read<R: Read>(mut r: &mut R, encrypted_tlvs_ss: SharedSecret) -> Result<Self, DecodeError> {
+ fn read<R: Read>(r: &mut R, encrypted_tlvs_ss: SharedSecret) -> Result<Self, DecodeError> {
let v: BigSize = Readable::read(r)?;
let mut rd = FixedLengthReader::new(r, v.0);
let mut reply_path: Option<BlindedRoute> = None;