diff --git a/Cargo.lock b/Cargo.lock index c4ff5fc9..af5228e9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -947,7 +947,7 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "ldk-sample" version = "0.1.0" -source = "git+https://github.com/lndk-org/ldk-sample?branch=offers#848e562f0ef57c95e015454cc2c00bca1e315e29" +source = "git+https://github.com/lndk-org/ldk-sample?branch=offer-handling#add68adc8f3c728eea82860015c866275d4c9b02" dependencies = [ "base64 0.13.1", "bech32 0.8.1", diff --git a/Cargo.toml b/Cargo.toml index 4ab98ba7..d96e4f74 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/lib.rs b/src/lib.rs index 5c32a81c..93a985d8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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, @@ -246,7 +247,13 @@ impl OfferHandler { } let invoice_request = self - .create_invoice_request(client.clone(), offer, vec![], network, validated_amount) + .create_invoice_request( + client.clone(), + offer.clone(), + vec![], + network, + validated_amount, + ) .await?; let contents = OffersMessage::InvoiceRequest(invoice_request); @@ -261,7 +268,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(), OfferState::InvoiceRequestSent); Ok(()) } @@ -274,7 +281,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, } }