From: Jeffrey Czyz Date: Tue, 21 May 2024 21:58:10 +0000 (-0500) Subject: Add UserConfig::manually_handle_bolt12_invoices X-Git-Tag: v0.0.124-beta~82^2~3 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=a9dcfaf952584ed835d733cb4688d5f96e86349d;p=rust-lightning Add UserConfig::manually_handle_bolt12_invoices 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. --- diff --git a/fuzz/src/full_stack.rs b/fuzz/src/full_stack.rs index 4591888e3..10328cdce 100644 --- a/fuzz/src/full_stack.rs +++ b/fuzz/src/full_stack.rs @@ -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); diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 9ef63973d..fbff775d3 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -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, diff --git a/lightning/src/util/config.rs b/lightning/src/util/config.rs index 2473eea26..3520e219c 100644 --- a/lightning/src/util/config.rs +++ b/lightning/src/util/config.rs @@ -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)?, }) } }