Skip to content

Commit

Permalink
Merge pull request #412 from getlipa/feature/fail-invoice-decoding-on…
Browse files Browse the repository at this point in the history
…-wrong-network

Fail `decode_invoice()` on wrong network
  • Loading branch information
danielgranhao authored Jun 5, 2023
2 parents 8932014 + 91decb3 commit 8bb491b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions eel/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub enum RuntimeErrorCode {
NonExistingWallet,

AlreadyUsedInvoice,
InvoiceNetworkMismatch,
PayingToSelf,
NoRouteFound,
SendFailure,
Expand Down
12 changes: 11 additions & 1 deletion eel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,17 @@ impl LightningNode {
}

pub fn decode_invoice(&self, invoice: String) -> Result<Invoice> {
invoice::parse_invoice(&invoice)
let invoice = invoice::parse_invoice(&invoice)?;
if self.config.lock().unwrap().network != invoice.network() {
return Err(runtime_error(
RuntimeErrorCode::InvoiceNetworkMismatch,
format!(
"Invoice belongs to a different network: {}",
invoice.network()
),
));
}
Ok(invoice)
}

pub fn pay_invoice(&self, invoice: String, metadata: String) -> Result<()> {
Expand Down
13 changes: 13 additions & 0 deletions eel/tests/sending_payments_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod setup_env;

#[cfg(feature = "nigiri")]
mod sending_payments_test {
use eel::errors::RuntimeErrorCode;
use eel::LightningNode;
use lightning_invoice::{Description, Invoice, InvoiceDescription};
use serial_test::file_serial;
Expand Down Expand Up @@ -166,6 +167,8 @@ mod sending_payments_test {
);
}

const MAINNET_INVOICE: &str = "lnbc1m1pj8m78dpp5zumxkd5hhc54rrjhhg8whcp67shph50gkln8hlwnar77rrljq0aqdqvtfz5y32yg4zscqzzsxqzjcsp5tn4l5acc5fwtdq5kz966uyhyqsf9vlj8quekfuf2wrz2u9k762js9qyyssqp5rjsuewp6lrldclvjqpt8gx7a0mk76qhypug40vpzg5cr72cdghcpcxh3t8pyfr7t6l9n4u97d8zupcnwte9vys660wcjcevktxm0cpstydgt";

fn invoice_decode_test(node: &LightningNode) {
// Test invoice from CLN
let invoice = nigiri::issue_invoice(
Expand Down Expand Up @@ -238,6 +241,16 @@ mod sending_payments_test {
Duration::from_secs(SECONDS_IN_AN_HOUR),
&nigiri::query_node_info(LspdLnd).unwrap().pub_key,
);

// Test invoice from different network (mainnet)
let decode_result = node.decode_invoice(String::from(MAINNET_INVOICE));
assert!(matches!(
decode_result,
Err(perro::Error::RuntimeError {
code: RuntimeErrorCode::InvoiceNetworkMismatch,
..
})
));
}

fn assert_invoice_details(
Expand Down
1 change: 1 addition & 0 deletions src/lipalightninglib.udl
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ enum RuntimeErrorCode {
"NonExistingWallet", // A recovery process couldn't be completed because no recovery data could be found. Most likely the recovery phrase wasn't created by the lipa wallet.

// PAYMENT SENDING ERRORS
"InvoiceNetworkMismatch", // The invoice belongs to different network. Please use a different one.
"AlreadyUsedInvoice", // An already recognized invoice tried to be paid. Either a payment attempt is in progress or the invoice has already been paid.
"PayingToSelf", // A locally issued invoice tried to be paid. Self-payments are not supported.
"NoRouteFound", // Many things can cause this. Do we have enough funds?
Expand Down

0 comments on commit 8bb491b

Please sign in to comment.