lightning/onion_message/
async_payments.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 async payments.
11
12use crate::blinded_path::message::AsyncPaymentsContext;
13use crate::io;
14use crate::ln::msgs::DecodeError;
15use crate::onion_message::messenger::{MessageSendInstructions, Responder, ResponseInstruction};
16use crate::onion_message::packet::OnionMessageContents;
17use crate::prelude::*;
18use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer};
19
20// TLV record types for the `onionmsg_tlv` TLV stream as defined in BOLT 4.
21const HELD_HTLC_AVAILABLE_TLV_TYPE: u64 = 72;
22const RELEASE_HELD_HTLC_TLV_TYPE: u64 = 74;
23
24/// A handler for an [`OnionMessage`] containing an async payments message as its payload.
25///
26/// [`OnionMessage`]: crate::ln::msgs::OnionMessage
27pub trait AsyncPaymentsMessageHandler {
28	/// Handle a [`HeldHtlcAvailable`] message. A [`ReleaseHeldHtlc`] should be returned to release
29	/// the held funds.
30	fn handle_held_htlc_available(
31		&self, message: HeldHtlcAvailable, responder: Option<Responder>,
32	) -> Option<(ReleaseHeldHtlc, ResponseInstruction)>;
33
34	/// Handle a [`ReleaseHeldHtlc`] message. If authentication of the message succeeds, an HTLC
35	/// should be released to the corresponding payee.
36	fn handle_release_held_htlc(&self, message: ReleaseHeldHtlc, context: AsyncPaymentsContext);
37
38	/// Release any [`AsyncPaymentsMessage`]s that need to be sent.
39	///
40	/// Typically, this is used for messages initiating an async payment flow rather than in response
41	/// to another message.
42	fn release_pending_messages(&self) -> Vec<(AsyncPaymentsMessage, MessageSendInstructions)> {
43		vec![]
44	}
45}
46
47/// Possible async payment messages sent and received via an [`OnionMessage`].
48///
49/// [`OnionMessage`]: crate::ln::msgs::OnionMessage
50#[derive(Clone, Debug)]
51pub enum AsyncPaymentsMessage {
52	/// An HTLC is being held upstream for the often-offline recipient, to be released via
53	/// [`ReleaseHeldHtlc`].
54	HeldHtlcAvailable(HeldHtlcAvailable),
55
56	/// Releases the HTLC corresponding to an inbound [`HeldHtlcAvailable`] message.
57	ReleaseHeldHtlc(ReleaseHeldHtlc),
58}
59
60/// An HTLC destined for the recipient of this message is being held upstream. The reply path
61/// accompanying this onion message should be used to send a [`ReleaseHeldHtlc`] response, which
62/// will cause the upstream HTLC to be released.
63#[derive(Clone, Debug)]
64pub struct HeldHtlcAvailable {}
65
66/// Releases the HTLC corresponding to an inbound [`HeldHtlcAvailable`] message.
67#[derive(Clone, Debug)]
68pub struct ReleaseHeldHtlc {}
69
70impl OnionMessageContents for ReleaseHeldHtlc {
71	fn tlv_type(&self) -> u64 {
72		RELEASE_HELD_HTLC_TLV_TYPE
73	}
74	#[cfg(c_bindings)]
75	fn msg_type(&self) -> String {
76		"Release Held HTLC".to_string()
77	}
78	#[cfg(not(c_bindings))]
79	fn msg_type(&self) -> &'static str {
80		"Release Held HTLC"
81	}
82}
83
84impl_writeable_tlv_based!(HeldHtlcAvailable, {});
85
86impl_writeable_tlv_based!(ReleaseHeldHtlc, {});
87
88impl AsyncPaymentsMessage {
89	/// Returns whether `tlv_type` corresponds to a TLV record for async payment messages.
90	pub fn is_known_type(tlv_type: u64) -> bool {
91		match tlv_type {
92			HELD_HTLC_AVAILABLE_TLV_TYPE | RELEASE_HELD_HTLC_TLV_TYPE => true,
93			_ => false,
94		}
95	}
96}
97
98impl OnionMessageContents for AsyncPaymentsMessage {
99	fn tlv_type(&self) -> u64 {
100		match self {
101			Self::HeldHtlcAvailable(_) => HELD_HTLC_AVAILABLE_TLV_TYPE,
102			Self::ReleaseHeldHtlc(msg) => msg.tlv_type(),
103		}
104	}
105	#[cfg(c_bindings)]
106	fn msg_type(&self) -> String {
107		match &self {
108			Self::HeldHtlcAvailable(_) => "Held HTLC Available".to_string(),
109			Self::ReleaseHeldHtlc(msg) => msg.msg_type(),
110		}
111	}
112	#[cfg(not(c_bindings))]
113	fn msg_type(&self) -> &'static str {
114		match &self {
115			Self::HeldHtlcAvailable(_) => "Held HTLC Available",
116			Self::ReleaseHeldHtlc(msg) => msg.msg_type(),
117		}
118	}
119}
120
121impl Writeable for AsyncPaymentsMessage {
122	fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
123		match self {
124			Self::HeldHtlcAvailable(message) => message.write(w),
125			Self::ReleaseHeldHtlc(message) => message.write(w),
126		}
127	}
128}
129
130impl ReadableArgs<u64> for AsyncPaymentsMessage {
131	fn read<R: io::Read>(r: &mut R, tlv_type: u64) -> Result<Self, DecodeError> {
132		match tlv_type {
133			HELD_HTLC_AVAILABLE_TLV_TYPE => Ok(Self::HeldHtlcAvailable(Readable::read(r)?)),
134			RELEASE_HELD_HTLC_TLV_TYPE => Ok(Self::ReleaseHeldHtlc(Readable::read(r)?)),
135			_ => Err(DecodeError::InvalidValue),
136		}
137	}
138}