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 Jan 24, 2024
1 parent cc47983 commit 6522401
Show file tree
Hide file tree
Showing 3 changed files with 23 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
25 changes: 21 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 @@ -236,7 +237,13 @@ impl OfferHandler {
}

let invoice_request = self
.create_invoice_request(client.clone(), offer, vec![], network, amount.unwrap())
.create_invoice_request(
client.clone(),
offer.clone(),
vec![],
network,
amount.unwrap(),
)
.await?;

let contents = OffersMessage::InvoiceRequest(invoice_request);
Expand All @@ -251,7 +258,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(offer.to_string().clone(), OfferState::InvoiceRequestSent);

Ok(())
}
Expand All @@ -276,7 +283,17 @@ 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();
match invoice.verify(&self.expanded_key, secp_ctx) {
// TODO: Eventually we can use the returned payment id below to check if this
// payment has been sent twice.
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 6522401

Please sign in to comment.