Skip to content

Commit a9dcfaf

Browse files
committed
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.
1 parent 4666c33 commit a9dcfaf

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

fuzz/src/full_stack.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ mod tests {
870870
// our network key
871871
ext_from_hex("0100000000000000000000000000000000000000000000000000000000000000", &mut test);
872872
// config
873-
ext_from_hex("0000000000900000000000000000640001000000000001ffff0000000000000000ffffffffffffffffffffffffffffffff0000000000000000ffffffffffffffff000000ffffffff00ffff1a000400010000020400000000040200000a08ffffffffffffffff0001000000", &mut test);
873+
ext_from_hex("0000000000900000000000000000640001000000000001ffff0000000000000000ffffffffffffffffffffffffffffffff0000000000000000ffffffffffffffff000000ffffffff00ffff1a000400010000020400000000040200000a08ffffffffffffffff000100000000", &mut test);
874874

875875
// new outbound connection with id 0
876876
ext_from_hex("00", &mut test);
@@ -1383,7 +1383,7 @@ mod tests {
13831383
// our network key
13841384
ext_from_hex("0100000000000000000000000000000000000000000000000000000000000000", &mut test);
13851385
// config
1386-
ext_from_hex("0000000000900000000000000000640001000000000001ffff0000000000000000ffffffffffffffffffffffffffffffff0000000000000000ffffffffffffffff000000ffffffff00ffff1a000400010000020400000000040200000a08ffffffffffffffff0001000000", &mut test);
1386+
ext_from_hex("0000000000900000000000000000640001000000000001ffff0000000000000000ffffffffffffffffffffffffffffffff0000000000000000ffffffffffffffff000000ffffffff00ffff1a000400010000020400000000040200000a08ffffffffffffffff000100000000", &mut test);
13871387

13881388
// new outbound connection with id 0
13891389
ext_from_hex("00", &mut test);

lightning/src/ln/channelmanager.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10277,21 +10277,25 @@ where
1027710277
}
1027810278
},
1027910279
OffersMessage::Invoice(invoice) => {
10280-
let result = invoice
10281-
.verify(expanded_key, secp_ctx)
10282-
.map_err(|()| InvoiceError::from_string("Unrecognized invoice".to_owned()))
10283-
.and_then(|payment_id| {
10280+
let result = match invoice.verify(expanded_key, secp_ctx) {
10281+
Ok(payment_id) => {
1028410282
let features = self.bolt12_invoice_features();
1028510283
if invoice.invoice_features().requires_unknown_bits_from(&features) {
1028610284
Err(InvoiceError::from(Bolt12SemanticError::UnknownRequiredFeatures))
10285+
} else if self.default_configuration.manually_handle_bolt12_invoices {
10286+
let event = Event::InvoiceReceived { payment_id, invoice, responder };
10287+
self.pending_events.lock().unwrap().push_back((event, None));
10288+
return ResponseInstruction::NoResponse;
1028710289
} else {
1028810290
self.send_payment_for_bolt12_invoice(&invoice, payment_id)
1028910291
.map_err(|e| {
1029010292
log_trace!(self.logger, "Failed paying invoice: {:?}", e);
1029110293
InvoiceError::from_string(format!("{:?}", e))
1029210294
})
1029310295
}
10294-
});
10296+
},
10297+
Err(()) => Err(InvoiceError::from_string("Unrecognized invoice".to_owned())),
10298+
};
1029510299

1029610300
match result {
1029710301
Ok(()) => ResponseInstruction::NoResponse,

lightning/src/util/config.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,16 @@ pub struct UserConfig {
847847
///
848848
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
849849
pub accept_mpp_keysend: bool,
850+
/// If this is set to true, the user needs to manually pay [`Bolt12Invoice`]s when received.
851+
///
852+
/// When set to true, [`Event::InvoiceReceived`] will be generated for each received
853+
/// [`Bolt12Invoice`] instead of being automatically paid after verification.
854+
///
855+
/// Default value: false.
856+
///
857+
/// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
858+
/// [`Event::InvoiceReceived`]: crate::events::Event::InvoiceReceived
859+
pub manually_handle_bolt12_invoices: bool,
850860
}
851861

852862
impl Default for UserConfig {
@@ -860,6 +870,7 @@ impl Default for UserConfig {
860870
manually_accept_inbound_channels: false,
861871
accept_intercept_htlcs: false,
862872
accept_mpp_keysend: false,
873+
manually_handle_bolt12_invoices: false,
863874
}
864875
}
865876
}
@@ -879,6 +890,7 @@ impl Readable for UserConfig {
879890
manually_accept_inbound_channels: Readable::read(reader)?,
880891
accept_intercept_htlcs: Readable::read(reader)?,
881892
accept_mpp_keysend: Readable::read(reader)?,
893+
manually_handle_bolt12_invoices: Readable::read(reader)?,
882894
})
883895
}
884896
}

0 commit comments

Comments
 (0)