Skip to content

Commit

Permalink
multi: add verification details to invoice request
Browse files Browse the repository at this point in the history
Once we get an invoice returned from the offer creator, we'll be able to
verify that the invoice is a response to the invoice request we sent. We can
also use the PaymentId we set to verify we didn't already pay the invoice.
  • Loading branch information
orbitalturtle committed Mar 6, 2024
1 parent b85cad4 commit bf03063
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ use bitcoin::network::constants::Network;
use bitcoin::secp256k1::{Error as Secp256k1Error, PublicKey};
use home::home_dir;
use lightning::blinded_path::BlindedPath;
use lightning::ln::inbound_payment::ExpandedKey;
use lightning::ln::peer_handler::IgnoringMessageHandler;
use lightning::offers::offer::Offer;
use lightning::onion_message::{
DefaultMessageRouter, Destination, OffersMessage, OffersMessageHandler, OnionMessenger,
PendingOnionMessage,
};
use lightning::sign::{EntropySource, KeyMaterial};
use log::{error, info, LevelFilter};
use log4rs::append::console::ConsoleAppender;
use log4rs::append::file::FileAppender;
Expand Down Expand Up @@ -183,6 +185,7 @@ pub struct OfferHandler {
active_offers: Mutex<HashMap<String, OfferState>>,
pending_messages: Mutex<Vec<PendingOnionMessage<OffersMessage>>>,
messenger_utils: MessengerUtilities,
expanded_key: ExpandedKey,
}

#[derive(Clone)]
Expand All @@ -199,10 +202,15 @@ pub struct PayOfferParams {

impl OfferHandler {
pub fn new() -> Self {
let messenger_utils = MessengerUtilities::new();
let random_bytes = messenger_utils.get_secure_random_bytes();
let expanded_key = ExpandedKey::new(&KeyMaterial(random_bytes));

OfferHandler {
active_offers: Mutex::new(HashMap::new()),
pending_messages: Mutex::new(Vec::new()),
messenger_utils: MessengerUtilities::new(),
messenger_utils,
expanded_key,
}
}

Expand Down
18 changes: 15 additions & 3 deletions src/lndk_offers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ use bitcoin::secp256k1::schnorr::Signature;
use bitcoin::secp256k1::{Error as Secp256k1Error, PublicKey, Secp256k1};
use futures::executor::block_on;
use lightning::blinded_path::BlindedPath;
use lightning::ln::channelmanager::PaymentId;
use lightning::offers::invoice_request::{InvoiceRequest, UnsignedInvoiceRequest};
use lightning::offers::merkle::SignError;
use lightning::offers::offer::{Amount, Offer};
use lightning::offers::parse::{Bolt12ParseError, Bolt12SemanticError};
use lightning::onion_message::{Destination, OffersMessage, PendingOnionMessage};
use lightning::sign::EntropySource;
use log::error;
use std::error::Error;
use std::fmt::Display;
Expand Down Expand Up @@ -151,7 +153,7 @@ impl OfferHandler {
&self,
mut signer: impl MessageSigner + std::marker::Send + 'static,
offer: Offer,
metadata: Vec<u8>,
_metadata: Vec<u8>,
network: Network,
msats: u64,
) -> Result<InvoiceRequest, OfferError<bitcoin::secp256k1::Error>> {
Expand All @@ -169,8 +171,19 @@ impl OfferHandler {
let pubkey =
PublicKey::from_slice(&pubkey_bytes).expect("failed to deserialize public key");

// Generate a new payment id for this payment.
let bytes = self.messenger_utils.get_secure_random_bytes();
// We need to add some metadata to the invoice request to help with verification of the invoice
// once returned from the offer maker. Once we get an invoice back, this metadata will help us
// to determine: 1) That the invoice is truly for the invoice request we sent. 2) We don't pay
// duplicate invoices.
let unsigned_invoice_req = offer
.request_invoice(metadata, pubkey)
.request_invoice_deriving_metadata(
pubkey,
&self.expanded_key,
&self.messenger_utils,
PaymentId(bytes),
)
.unwrap()
.chain(network)
.unwrap()
Expand Down Expand Up @@ -349,7 +362,6 @@ impl PeerConnector for Client {
let list_req = ListPeersRequest {
..Default::default()
};

self.lightning()
.list_peers(list_req)
.await
Expand Down

0 comments on commit bf03063

Please sign in to comment.