Skip to content

Commit

Permalink
offers: verify invoice upon return
Browse files Browse the repository at this point in the history
  • Loading branch information
orbitalturtle committed Feb 6, 2024
1 parent ca71daf commit b221d0e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ triggered = "0.1.2"
bitcoincore-rpc = { package="core-rpc", version = "0.17.0" }
bitcoind = { version = "0.30.0", features = [ "22_0" ] }
chrono = { version = "0.4.26" }
ldk-sample = { git = "https://github.com/lndk-org/ldk-sample", branch = "offers" }
ldk-sample = { git = "https://github.com/lndk-org/ldk-sample", branch = "offer-handling" }
mockall = "0.11.3"
tempfile = "3.5.0"

Expand Down
22 changes: 18 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ use crate::lnd::{
use crate::lndk_offers::{connect_to_peer, validate_amount, OfferError};
use crate::onion_messenger::MessengerUtilities;
use bitcoin::network::constants::Network;
use bitcoin::secp256k1::{Error as Secp256k1Error, PublicKey};
use bitcoin::secp256k1::{Error as Secp256k1Error, PublicKey, Secp256k1};
use home::home_dir;
use lightning::blinded_path::BlindedPath;
use lightning::ln::inbound_payment::ExpandedKey;
use lightning::ln::peer_handler::IgnoringMessageHandler;
use lightning::offers::invoice_error::InvoiceError;
use lightning::offers::offer::Offer;
use lightning::onion_message::{
DefaultMessageRouter, Destination, OffersMessage, OffersMessageHandler, OnionMessenger,
Expand Down Expand Up @@ -244,7 +245,7 @@ impl OfferHandler {
let invoice_request = self
.create_invoice_request(
cfg.client.clone(),
cfg.offer,
cfg.offer.clone(),
vec![],
cfg.network,
validated_amount,
Expand Down Expand Up @@ -275,7 +276,7 @@ impl OfferHandler {
std::mem::drop(pending_messages);

let mut active_offers = self.active_offers.lock().unwrap();
active_offers.insert(offer_id, OfferState::InvoiceRequestSent);
active_offers.insert(cfg.offer.to_string(), OfferState::InvoiceRequestSent);

Ok(())
}
Expand All @@ -294,7 +295,20 @@ impl OffersMessageHandler for OfferHandler {
log::error!("Invoice request received, payment not yet supported.");
None
}
OffersMessage::Invoice(_invoice) => None,
OffersMessage::Invoice(invoice) => {
let secp_ctx = &Secp256k1::new();
// We verify that this invoice is a response to the invoice request we just sent.
match invoice.verify(&self.expanded_key, secp_ctx) {
// TODO: Eventually when we allow for multiple payments in flight, we can use the
// returned payment id below to check if we already processed an invoice for
// this payment. Right now it's safe to let this be because we won't try to pay
// a second invoice (if it comes through).
Ok(_payment_id) => Some(OffersMessage::Invoice(invoice)),
Err(()) => Some(OffersMessage::InvoiceError(InvoiceError::from_string(
String::from("invoice verification failure"),
))),
}
}
OffersMessage::InvoiceError(_error) => None,
}
}
Expand Down

0 comments on commit b221d0e

Please sign in to comment.