diff --git a/src/builder.rs b/src/builder.rs index 964684a660..d1bc9cd7ba 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -2882,6 +2882,57 @@ mod tests { assert!(matches!(result, Err(BuildError::ReadFailed))); } + #[test] + fn bolt11_outbound_rejects_wrong_network() { + use bitcoin::Network; + use lightning_invoice::{Bolt11InvoiceDescription, Description}; + + use crate::error::Error; + use crate::ffi::maybe_wrap; + + let build = |network, seed| { + let mut builder = NodeBuilder::new(); + builder.set_network(network); + #[cfg(not(feature = "uniffi"))] + let entropy = NodeEntropy::from_seed_bytes([seed; 64]); + #[cfg(feature = "uniffi")] + let entropy = NodeEntropy::from_seed_bytes(vec![seed; 64]).unwrap(); + builder + .build_with_store_and_logger( + entropy, + InMemoryStore::new(), + Arc::new(Logger::new_log_facade()), + ) + .unwrap() + }; + let sender = build(Network::Regtest, 42); + let recipient = build(Network::Testnet, 43); + let description = + Bolt11InvoiceDescription::Direct(Description::new("test".to_owned()).unwrap()); + let invoice = maybe_wrap( + recipient + .bolt11_payment() + .receive_inner(Some(10_000), &description, 600, None, None) + .unwrap(), + ); + let outbound = sender.bolt11_payment(); + + // Exercise outbound validation without starting background tasks or connecting peers. + *sender.is_running.write().unwrap() = true; + let send = outbound.send(&invoice, None); + let send_amount = outbound.send_using_amount(&invoice, 10_000, None); + let underpay = outbound.send_using_amount_underpaying(&invoice, 5_000, None); + let probe = outbound.send_probes(&invoice, None); + let probe_amount = outbound.send_probes_using_amount(&invoice, 10_000, None); + *sender.is_running.write().unwrap() = false; + + assert_eq!(send, Err(Error::InvalidNetwork)); + assert_eq!(send_amount, Err(Error::InvalidNetwork)); + assert_eq!(underpay, Err(Error::InvalidNetwork)); + assert_eq!(probe, Err(Error::InvalidNetwork)); + assert_eq!(probe_amount, Err(Error::InvalidNetwork)); + } + #[test] fn unstarted_node_rejects_live_node_apis() { use std::str::FromStr; diff --git a/src/payment/bolt11.rs b/src/payment/bolt11.rs index d9ef1fdd32..99fd10ad00 100644 --- a/src/payment/bolt11.rs +++ b/src/payment/bolt11.rs @@ -20,6 +20,7 @@ use lightning::ln::outbound_payment::{Bolt11PaymentError, RetryableSendFailure}; use lightning::routing::router::{PaymentParameters, RouteParameters, RouteParametersConfig}; use lightning_invoice::{ Bolt11Invoice as LdkBolt11Invoice, Bolt11InvoiceDescription as LdkBolt11InvoiceDescription, + Currency, }; use lightning_types::payment::{PaymentHash, PaymentPreimage}; @@ -50,6 +51,7 @@ type Bolt11InvoiceDescription = crate::ffi::Bolt11InvoiceDescription; /// A payment handler allowing to create and pay [BOLT 11] invoices. /// /// Should be retrieved by calling [`Node::bolt11_payment`]. +/// Paying or probing an invoice for a different network returns [`Error::InvalidNetwork`]. /// /// [BOLT 11]: https://github.com/lightning/bolts/blob/master/11-payment-encoding.md /// [`Node::bolt11_payment`]: crate::Node::bolt11_payment @@ -153,6 +155,20 @@ impl Bolt11Payment { } impl Bolt11Payment { + fn checked_outbound_invoice<'a>( + &self, invoice: &'a Bolt11Invoice, + ) -> Result<&'a LdkBolt11Invoice, Error> { + if !*self.is_running.read().expect("lock") { + return Err(Error::NotRunning); + } + + let invoice = maybe_deref(invoice); + if invoice.currency() != Currency::from(self.config.network) { + return Err(Error::InvalidNetwork); + } + Ok(invoice) + } + fn send_internal( &self, invoice: &LdkBolt11Invoice, amount_msat: Option, route_parameters: Option, @@ -263,11 +279,7 @@ impl Bolt11Payment { pub fn send( &self, invoice: &Bolt11Invoice, route_parameters: Option, ) -> Result { - if !*self.is_running.read().expect("lock") { - return Err(Error::NotRunning); - } - - let invoice = maybe_deref(invoice); + let invoice = self.checked_outbound_invoice(invoice)?; self.send_internal( invoice, None, @@ -290,11 +302,7 @@ impl Bolt11Payment { &self, invoice: &Bolt11Invoice, amount_msat: u64, route_parameters: Option, ) -> Result { - if !*self.is_running.read().expect("lock") { - return Err(Error::NotRunning); - } - - let invoice = maybe_deref(invoice); + let invoice = self.checked_outbound_invoice(invoice)?; if let Some(invoice_amount_msat) = invoice.amount_milli_satoshis() { if amount_msat < invoice_amount_msat { log_error!( @@ -329,11 +337,7 @@ impl Bolt11Payment { &self, invoice: &Bolt11Invoice, amount_msat: u64, route_parameters: Option, ) -> Result { - if !*self.is_running.read().expect("lock") { - return Err(Error::NotRunning); - } - - let invoice = maybe_deref(invoice); + let invoice = self.checked_outbound_invoice(invoice)?; let invoice_amount_msat = invoice.amount_milli_satoshis().ok_or_else(|| { log_error!(self.logger, "Failed to underpay as the given invoice is \"zero-amount\"."); Error::InvalidInvoice @@ -769,11 +773,7 @@ impl Bolt11Payment { pub fn send_probes( &self, invoice: &Bolt11Invoice, route_parameters: Option, ) -> Result<(), Error> { - if !*self.is_running.read().expect("lock") { - return Err(Error::NotRunning); - } - - let invoice = maybe_deref(invoice); + let invoice = self.checked_outbound_invoice(invoice)?; let payment_params = PaymentParameters::from_bolt11_invoice(invoice); let amount_msat = invoice.amount_milli_satoshis().ok_or_else(|| { @@ -814,11 +814,7 @@ impl Bolt11Payment { &self, invoice: &Bolt11Invoice, amount_msat: u64, route_parameters: Option, ) -> Result<(), Error> { - if !*self.is_running.read().expect("lock") { - return Err(Error::NotRunning); - } - - let invoice = maybe_deref(invoice); + let invoice = self.checked_outbound_invoice(invoice)?; let payment_params = PaymentParameters::from_bolt11_invoice(invoice); if let Some(invoice_amount_msat) = invoice.amount_milli_satoshis() {