Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
46 changes: 21 additions & 25 deletions src/payment/bolt11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<u64>,
route_parameters: Option<RouteParametersConfig>,
Expand Down Expand Up @@ -263,11 +279,7 @@ impl Bolt11Payment {
pub fn send(
&self, invoice: &Bolt11Invoice, route_parameters: Option<RouteParametersConfig>,
) -> Result<PaymentId, Error> {
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,
Expand All @@ -290,11 +302,7 @@ impl Bolt11Payment {
&self, invoice: &Bolt11Invoice, amount_msat: u64,
route_parameters: Option<RouteParametersConfig>,
) -> Result<PaymentId, Error> {
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!(
Expand Down Expand Up @@ -329,11 +337,7 @@ impl Bolt11Payment {
&self, invoice: &Bolt11Invoice, amount_msat: u64,
route_parameters: Option<RouteParametersConfig>,
) -> Result<PaymentId, 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 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
Expand Down Expand Up @@ -769,11 +773,7 @@ impl Bolt11Payment {
pub fn send_probes(
&self, invoice: &Bolt11Invoice, route_parameters: Option<RouteParametersConfig>,
) -> 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(|| {
Expand Down Expand Up @@ -814,11 +814,7 @@ impl Bolt11Payment {
&self, invoice: &Bolt11Invoice, amount_msat: u64,
route_parameters: Option<RouteParametersConfig>,
) -> 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() {
Expand Down
Loading