+use core::ops::Deref;
+
+/// Some information provided on receipt of payment depends on whether the payment received is a
+/// spontaneous payment or a "conventional" lightning payment that's paying an invoice.
+#[derive(Clone, Debug)]
+pub enum PaymentPurpose {
+ /// Information for receiving a payment that we generated an invoice for.
+ InvoicePayment {
+ /// The preimage to the payment_hash, if the payment hash (and secret) were fetched via
+ /// [`ChannelManager::create_inbound_payment`]. If provided, this can be handed directly to
+ /// [`ChannelManager::claim_funds`].
+ ///
+ /// [`ChannelManager::create_inbound_payment`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment
+ /// [`ChannelManager::claim_funds`]: crate::ln::channelmanager::ChannelManager::claim_funds
+ payment_preimage: Option<PaymentPreimage>,
+ /// The "payment secret". This authenticates the sender to the recipient, preventing a
+ /// number of deanonymization attacks during the routing process.
+ /// It is provided here for your reference, however its accuracy is enforced directly by
+ /// [`ChannelManager`] using the values you previously provided to
+ /// [`ChannelManager::create_inbound_payment`] or
+ /// [`ChannelManager::create_inbound_payment_for_hash`].
+ ///
+ /// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
+ /// [`ChannelManager::create_inbound_payment`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment
+ /// [`ChannelManager::create_inbound_payment_for_hash`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash
+ payment_secret: PaymentSecret,
+ /// This is the `user_payment_id` which was provided to
+ /// [`ChannelManager::create_inbound_payment_for_hash`] or
+ /// [`ChannelManager::create_inbound_payment`]. It has no meaning inside of LDK and is
+ /// simply copied here. It may be used to correlate PaymentReceived events with invoice
+ /// metadata stored elsewhere.
+ ///
+ /// [`ChannelManager::create_inbound_payment`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment
+ /// [`ChannelManager::create_inbound_payment_for_hash`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash
+ user_payment_id: u64,
+ },
+ /// Because this is a spontaneous payment, the payer generated their own preimage rather than us
+ /// (the payee) providing a preimage.
+ SpontaneousPayment(PaymentPreimage),
+}
+
+#[derive(Clone, Debug)]
+pub enum ClosureDescriptor {
+ /// Closure generated from ChannelManager::force_close_channel or receiving a peer error
+ /// message by ChannelManager::handle_error
+ ForceClosed {
+ /// If the error is coming from the peer, there should be a human-readable msg
+ peer_msg: Option<String>,
+ },
+ /// Closure generated from receiving a peer's ClosingSigned message. Note the shutdown
+ /// sequence might have been initially initiated by us.
+ CooperativeClosure,
+ /// Closure generated from receiving chain::Watch's CommitmentTxBroadcast event.
+ CommitmentTxBroadcasted,
+ /// Closure generated from processing an event, likely a HTLC forward/relay/reception.
+ ProcessingError {
+ err: String,
+ },
+ /// Closure generated from ChannelManager::peer_disconnected.
+ DisconnectedPeer,
+}
+
+impl Writeable for ClosureDescriptor {
+ fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+ match self {
+ ClosureDescriptor::ForceClosed { peer_msg } => {
+ 0u8.write(writer)?;
+ if let Some(peer_msg) = peer_msg {
+ 0u8.write(writer)?;
+ let bytes = peer_msg.clone().into_bytes();
+ (bytes.len() as u64).write(writer)?;
+ for b in bytes.iter() {
+ b.write(writer)?;
+ }
+ } else {
+ 1u8.write(writer)?;
+ }
+ }
+ ClosureDescriptor::CooperativeClosure => 1u8.write(writer)?,
+ ClosureDescriptor::CommitmentTxBroadcasted => 2u8.write(writer)?,
+ ClosureDescriptor::ProcessingError { err } => {
+ 3u8.write(writer)?;
+ let bytes = err.clone().into_bytes();
+ (bytes.len() as u64).write(writer)?;
+ for b in bytes.iter() {
+ b.write(writer)?;
+ }
+ },
+ ClosureDescriptor::DisconnectedPeer => 4u8.write(writer)?,
+ }
+ Ok(())
+ }
+}
+
+impl Readable for ClosureDescriptor {
+ fn read<R: ::std::io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
+ Ok(match <u8 as Readable>::read(reader)? {
+ 0 => {
+ let peer_msg = match <u8 as Readable>::read(reader)? {
+ 0 => {
+ let bytes_len: u64 = Readable::read(reader)?;
+ let mut bytes: Vec<u8> = Vec::with_capacity(bytes_len as usize);
+ for _ in 0..bytes_len {
+ bytes.push(Readable::read(reader)?);
+ }
+ let err = String::from_utf8(bytes).unwrap();
+ Some(err)
+ },
+ 1 => None,
+ _ => return Err(DecodeError::InvalidValue),
+ };
+ ClosureDescriptor::ForceClosed { peer_msg }
+ },
+ 1 => ClosureDescriptor::CooperativeClosure,
+ 2 => ClosureDescriptor::CommitmentTxBroadcasted,
+ 3 => {
+ let bytes_len: u64 = Readable::read(reader)?;
+ let mut bytes: Vec<u8> = Vec::with_capacity(bytes_len as usize);
+ for _ in 0..bytes_len {
+ bytes.push(Readable::read(reader)?);
+ }
+ let err = String::from_utf8(bytes).unwrap();
+ ClosureDescriptor::ProcessingError { err }
+ },
+ 4 => ClosureDescriptor::DisconnectedPeer,
+ _ => return Err(DecodeError::InvalidValue),
+ })
+ }
+}