@@ -18,6 +18,7 @@ pub mod bump_transaction;
1818
1919pub use bump_transaction:: BumpTransactionEvent ;
2020
21+ use crate :: blinded_path:: payment:: { Bolt12OfferContext , Bolt12RefundContext , PaymentContext , PaymentContextRef } ;
2122use crate :: sign:: SpendableOutputDescriptor ;
2223use crate :: ln:: channelmanager:: { InterceptId , PaymentId , RecipientOnionFields } ;
2324use crate :: ln:: channel:: FUNDING_CONF_DEADLINE_BLOCKS ;
@@ -70,6 +71,46 @@ pub enum PaymentPurpose {
7071 /// [`ChannelManager::create_inbound_payment_for_hash`]: crate::ln::channelmanager::ChannelManager::create_inbound_payment_for_hash
7172 payment_secret : PaymentSecret ,
7273 } ,
74+ /// A payment for a BOLT 12 [`Offer`].
75+ ///
76+ /// [`Offer`]: crate::offers::offer::Offer
77+ Bolt12OfferPayment {
78+ /// The preimage to the payment hash. If provided, this can be handed directly to
79+ /// [`ChannelManager::claim_funds`].
80+ ///
81+ /// [`ChannelManager::claim_funds`]: crate::ln::channelmanager::ChannelManager::claim_funds
82+ payment_preimage : Option < PaymentPreimage > ,
83+ /// The secret used to authenticate the sender to the recipient, preventing a number of
84+ /// de-anonymization attacks while routing a payment.
85+ ///
86+ /// See [`PaymentPurpose::Bolt11InvoicePayment::payment_secret`] for further details.
87+ payment_secret : PaymentSecret ,
88+ /// The context of the payment such as information about the corresponding [`Offer`] and
89+ /// [`InvoiceRequest`].
90+ ///
91+ /// [`Offer`]: crate::offers::offer::Offer
92+ /// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
93+ payment_context : Bolt12OfferContext ,
94+ } ,
95+ /// A payment for a BOLT 12 [`Refund`].
96+ ///
97+ /// [`Refund`]: crate::offers::refund::Refund
98+ Bolt12RefundPayment {
99+ /// The preimage to the payment hash. If provided, this can be handed directly to
100+ /// [`ChannelManager::claim_funds`].
101+ ///
102+ /// [`ChannelManager::claim_funds`]: crate::ln::channelmanager::ChannelManager::claim_funds
103+ payment_preimage : Option < PaymentPreimage > ,
104+ /// The secret used to authenticate the sender to the recipient, preventing a number of
105+ /// de-anonymization attacks while routing a payment.
106+ ///
107+ /// See [`PaymentPurpose::Bolt11InvoicePayment::payment_secret`] for further details.
108+ payment_secret : PaymentSecret ,
109+ /// The context of the payment such as information about the corresponding [`Refund`].
110+ ///
111+ /// [`Refund`]: crate::offers::refund::Refund
112+ payment_context : Bolt12RefundContext ,
113+ } ,
73114 /// Because this is a spontaneous payment, the payer generated their own preimage rather than us
74115 /// (the payee) providing a preimage.
75116 SpontaneousPayment ( PaymentPreimage ) ,
@@ -80,13 +121,17 @@ impl PaymentPurpose {
80121 pub fn preimage ( & self ) -> Option < PaymentPreimage > {
81122 match self {
82123 PaymentPurpose :: Bolt11InvoicePayment { payment_preimage, .. } => * payment_preimage,
124+ PaymentPurpose :: Bolt12OfferPayment { payment_preimage, .. } => * payment_preimage,
125+ PaymentPurpose :: Bolt12RefundPayment { payment_preimage, .. } => * payment_preimage,
83126 PaymentPurpose :: SpontaneousPayment ( preimage) => Some ( * preimage) ,
84127 }
85128 }
86129
87130 pub ( crate ) fn is_keysend ( & self ) -> bool {
88131 match self {
89132 PaymentPurpose :: Bolt11InvoicePayment { .. } => false ,
133+ PaymentPurpose :: Bolt12OfferPayment { .. } => false ,
134+ PaymentPurpose :: Bolt12RefundPayment { .. } => false ,
90135 PaymentPurpose :: SpontaneousPayment ( ..) => true ,
91136 }
92137 }
@@ -96,7 +141,18 @@ impl_writeable_tlv_based_enum!(PaymentPurpose,
96141 ( 0 , Bolt11InvoicePayment ) => {
97142 ( 0 , payment_preimage, option) ,
98143 ( 2 , payment_secret, required) ,
99- } ;
144+ } ,
145+ ( 4 , Bolt12OfferPayment ) => {
146+ ( 0 , payment_preimage, option) ,
147+ ( 2 , payment_secret, required) ,
148+ ( 4 , payment_context, required) ,
149+ } ,
150+ ( 6 , Bolt12RefundPayment ) => {
151+ ( 0 , payment_preimage, option) ,
152+ ( 2 , payment_secret, required) ,
153+ ( 4 , payment_context, required) ,
154+ } ,
155+ ;
100156 ( 2 , SpontaneousPayment )
101157) ;
102158
@@ -1065,13 +1121,28 @@ impl Writeable for Event {
10651121 1u8 . write ( writer) ?;
10661122 let mut payment_secret = None ;
10671123 let payment_preimage;
1124+ let mut payment_context = None ;
10681125 match & purpose {
10691126 PaymentPurpose :: Bolt11InvoicePayment {
10701127 payment_preimage : preimage, payment_secret : secret
10711128 } => {
10721129 payment_secret = Some ( secret) ;
10731130 payment_preimage = * preimage;
10741131 } ,
1132+ PaymentPurpose :: Bolt12OfferPayment {
1133+ payment_preimage : preimage, payment_secret : secret, payment_context : context
1134+ } => {
1135+ payment_secret = Some ( secret) ;
1136+ payment_preimage = * preimage;
1137+ payment_context = Some ( PaymentContextRef :: Bolt12Offer ( context) ) ;
1138+ } ,
1139+ PaymentPurpose :: Bolt12RefundPayment {
1140+ payment_preimage : preimage, payment_secret : secret, payment_context : context
1141+ } => {
1142+ payment_secret = Some ( secret) ;
1143+ payment_preimage = * preimage;
1144+ payment_context = Some ( PaymentContextRef :: Bolt12Refund ( context) ) ;
1145+ } ,
10751146 PaymentPurpose :: SpontaneousPayment ( preimage) => {
10761147 payment_preimage = Some ( * preimage) ;
10771148 }
@@ -1090,6 +1161,7 @@ impl Writeable for Event {
10901161 ( 8 , payment_preimage, option) ,
10911162 ( 9 , onion_fields, option) ,
10921163 ( 10 , skimmed_fee_opt, option) ,
1164+ ( 11 , payment_context, option) ,
10931165 } ) ;
10941166 } ,
10951167 & Event :: PaymentSent { ref payment_id, ref payment_preimage, ref payment_hash, ref fee_paid_msat } => {
@@ -1320,6 +1392,7 @@ impl MaybeReadable for Event {
13201392 let mut claim_deadline = None ;
13211393 let mut via_user_channel_id = None ;
13221394 let mut onion_fields = None ;
1395+ let mut payment_context = None ;
13231396 read_tlv_fields ! ( reader, {
13241397 ( 0 , payment_hash, required) ,
13251398 ( 1 , receiver_node_id, option) ,
@@ -1332,11 +1405,30 @@ impl MaybeReadable for Event {
13321405 ( 8 , payment_preimage, option) ,
13331406 ( 9 , onion_fields, option) ,
13341407 ( 10 , counterparty_skimmed_fee_msat_opt, option) ,
1408+ ( 11 , payment_context, option) ,
13351409 } ) ;
13361410 let purpose = match payment_secret {
1337- Some ( secret) => PaymentPurpose :: Bolt11InvoicePayment {
1338- payment_preimage,
1339- payment_secret : secret
1411+ Some ( secret) => match payment_context {
1412+ Some ( PaymentContext :: Unknown ( _) ) | None => {
1413+ PaymentPurpose :: Bolt11InvoicePayment {
1414+ payment_preimage,
1415+ payment_secret : secret,
1416+ }
1417+ } ,
1418+ Some ( PaymentContext :: Bolt12Offer ( context) ) => {
1419+ PaymentPurpose :: Bolt12OfferPayment {
1420+ payment_preimage,
1421+ payment_secret : secret,
1422+ payment_context : context,
1423+ }
1424+ } ,
1425+ Some ( PaymentContext :: Bolt12Refund ( context) ) => {
1426+ PaymentPurpose :: Bolt12RefundPayment {
1427+ payment_preimage,
1428+ payment_secret : secret,
1429+ payment_context : context,
1430+ }
1431+ } ,
13401432 } ,
13411433 None if payment_preimage. is_some ( ) => PaymentPurpose :: SpontaneousPayment ( payment_preimage. unwrap ( ) ) ,
13421434 None => return Err ( msgs:: DecodeError :: InvalidValue ) ,
0 commit comments