diff --git a/src/lib.rs b/src/lib.rs index 4286b634..49f875a1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; @@ -183,6 +185,7 @@ pub struct OfferHandler { active_offers: Mutex>, pending_messages: Mutex>>, messenger_utils: MessengerUtilities, + expanded_key: ExpandedKey, } #[derive(Clone)] @@ -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, } } diff --git a/src/lndk_offers.rs b/src/lndk_offers.rs index 3fa25eb3..34c73892 100644 --- a/src/lndk_offers.rs +++ b/src/lndk_offers.rs @@ -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; @@ -151,7 +153,7 @@ impl OfferHandler { &self, mut signer: impl MessageSigner + std::marker::Send + 'static, offer: Offer, - metadata: Vec, + _metadata: Vec, network: Network, msats: u64, ) -> Result> { @@ -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() @@ -349,7 +362,6 @@ impl PeerConnector for Client { let list_req = ListPeersRequest { ..Default::default() }; - self.lightning() .list_peers(list_req) .await