]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Add UserConfig::manually_handle_bolt12_invoices
authorJeffrey Czyz <jkczyz@gmail.com>
Tue, 21 May 2024 21:58:10 +0000 (16:58 -0500)
committerJeffrey Czyz <jkczyz@gmail.com>
Thu, 13 Jun 2024 00:38:16 +0000 (19:38 -0500)
BOLT12 invoices are automatically paid once they have been verified.
Users may want to manually pay them by first performing additional
checks. Add a manually_handle_bolt12_invoices configuration option that
when set generates an Event::InvoiceReceived instead of paying the
invoice.

fuzz/src/full_stack.rs
lightning/src/ln/channelmanager.rs
lightning/src/util/config.rs

index 4591888e3f0aca100cd6dce88766c65514b01b28..10328cdce812d2992f8d53f18c1102bea7cde444 100644 (file)
@@ -870,7 +870,7 @@ mod tests {
                // our network key
                ext_from_hex("0100000000000000000000000000000000000000000000000000000000000000", &mut test);
                // config
-               ext_from_hex("0000000000900000000000000000640001000000000001ffff0000000000000000ffffffffffffffffffffffffffffffff0000000000000000ffffffffffffffff000000ffffffff00ffff1a000400010000020400000000040200000a08ffffffffffffffff0001000000", &mut test);
+               ext_from_hex("0000000000900000000000000000640001000000000001ffff0000000000000000ffffffffffffffffffffffffffffffff0000000000000000ffffffffffffffff000000ffffffff00ffff1a000400010000020400000000040200000a08ffffffffffffffff000100000000", &mut test);
 
                // new outbound connection with id 0
                ext_from_hex("00", &mut test);
@@ -1383,7 +1383,7 @@ mod tests {
                // our network key
                ext_from_hex("0100000000000000000000000000000000000000000000000000000000000000", &mut test);
                // config
-               ext_from_hex("0000000000900000000000000000640001000000000001ffff0000000000000000ffffffffffffffffffffffffffffffff0000000000000000ffffffffffffffff000000ffffffff00ffff1a000400010000020400000000040200000a08ffffffffffffffff0001000000", &mut test);
+               ext_from_hex("0000000000900000000000000000640001000000000001ffff0000000000000000ffffffffffffffffffffffffffffffff0000000000000000ffffffffffffffff000000ffffffff00ffff1a000400010000020400000000040200000a08ffffffffffffffff000100000000", &mut test);
 
                // new outbound connection with id 0
                ext_from_hex("00", &mut test);
index 9ef63973ddc02f3c348746e9e76401c2bfef1e9c..fbff775d3613290a1147fbccf1766cacc66db4bf 100644 (file)
@@ -10277,13 +10277,15 @@ where
                                }
                        },
                        OffersMessage::Invoice(invoice) => {
-                               let result = invoice
-                                       .verify(expanded_key, secp_ctx)
-                                       .map_err(|()| InvoiceError::from_string("Unrecognized invoice".to_owned()))
-                                       .and_then(|payment_id| {
+                               let result = match invoice.verify(expanded_key, secp_ctx) {
+                                       Ok(payment_id) => {
                                                let features = self.bolt12_invoice_features();
                                                if invoice.invoice_features().requires_unknown_bits_from(&features) {
                                                        Err(InvoiceError::from(Bolt12SemanticError::UnknownRequiredFeatures))
+                                               } else if self.default_configuration.manually_handle_bolt12_invoices {
+                                                       let event = Event::InvoiceReceived { payment_id, invoice, responder };
+                                                       self.pending_events.lock().unwrap().push_back((event, None));
+                                                       return ResponseInstruction::NoResponse;
                                                } else {
                                                        self.send_payment_for_bolt12_invoice(&invoice, payment_id)
                                                                .map_err(|e| {
@@ -10291,7 +10293,9 @@ where
                                                                        InvoiceError::from_string(format!("{:?}", e))
                                                                })
                                                }
-                                       });
+                                       },
+                                       Err(()) => Err(InvoiceError::from_string("Unrecognized invoice".to_owned())),
+                               };
 
                                match result {
                                        Ok(()) => ResponseInstruction::NoResponse,
index 2473eea26274500800a4e5ea11c9ce9e6530fde4..3520e219c453b88c60393413385d157f6b87a13c 100644 (file)
@@ -847,6 +847,16 @@ pub struct UserConfig {
        ///
        /// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
        pub accept_mpp_keysend: bool,
+       /// If this is set to true, the user needs to manually pay [`Bolt12Invoice`]s when received.
+       ///
+       /// When set to true, [`Event::InvoiceReceived`] will be generated for each received
+       /// [`Bolt12Invoice`] instead of being automatically paid after verification.
+       ///
+       /// Default value: false.
+       ///
+       /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
+       /// [`Event::InvoiceReceived`]: crate::events::Event::InvoiceReceived
+       pub manually_handle_bolt12_invoices: bool,
 }
 
 impl Default for UserConfig {
@@ -860,6 +870,7 @@ impl Default for UserConfig {
                        manually_accept_inbound_channels: false,
                        accept_intercept_htlcs: false,
                        accept_mpp_keysend: false,
+                       manually_handle_bolt12_invoices: false,
                }
        }
 }
@@ -879,6 +890,7 @@ impl Readable for UserConfig {
                        manually_accept_inbound_channels: Readable::read(reader)?,
                        accept_intercept_htlcs: Readable::read(reader)?,
                        accept_mpp_keysend: Readable::read(reader)?,
+                       manually_handle_bolt12_invoices: Readable::read(reader)?,
                })
        }
 }