@@ -64,6 +64,7 @@ use crate::ln::wire::Encode;
64
64
use crate::offers::invoice::{BlindedPayInfo, Bolt12Invoice, DEFAULT_RELATIVE_EXPIRY, DerivedSigningPubkey, ExplicitSigningPubkey, InvoiceBuilder, UnsignedBolt12Invoice};
65
65
use crate::offers::invoice_error::InvoiceError;
66
66
use crate::offers::invoice_request::{DerivedPayerId, InvoiceRequestBuilder};
67
+ use crate::offers::nonce::Nonce;
67
68
use crate::offers::offer::{Offer, OfferBuilder};
68
69
use crate::offers::parse::Bolt12SemanticError;
69
70
use crate::offers::refund::{Refund, RefundBuilder};
@@ -2254,7 +2255,10 @@ where
2254
2255
event_persist_notifier: Notifier,
2255
2256
needs_persist_flag: AtomicBool,
2256
2257
2258
+ #[cfg(not(any(test, feature = "_test_utils")))]
2257
2259
pending_offers_messages: Mutex<Vec<PendingOnionMessage<OffersMessage>>>,
2260
+ #[cfg(any(test, feature = "_test_utils"))]
2261
+ pub(crate) pending_offers_messages: Mutex<Vec<PendingOnionMessage<OffersMessage>>>,
2258
2262
2259
2263
/// Tracks the message events that are to be broadcasted when we are connected to some peer.
2260
2264
pending_broadcast_messages: Mutex<Vec<MessageSendEvent>>,
@@ -4199,15 +4203,35 @@ where
4199
4203
/// whether or not the payment was successful.
4200
4204
///
4201
4205
/// [timer tick]: Self::timer_tick_occurred
4202
- pub fn send_payment_for_bolt12_invoice(&self, invoice: &Bolt12Invoice) -> Result<(), Bolt12PaymentError> {
4203
- let secp_ctx = &self.secp_ctx;
4204
- let expanded_key = &self.inbound_payment_key;
4205
- match invoice.verify(expanded_key, secp_ctx ) {
4206
+ pub fn send_payment_for_bolt12_invoice(
4207
+ &self, invoice: &Bolt12Invoice, context: &OffersContext,
4208
+ ) -> Result<(), Bolt12PaymentError> {
4209
+ match self.verify_bolt12_invoice(invoice, context ) {
4206
4210
Ok(payment_id) => self.send_payment_for_verified_bolt12_invoice(invoice, payment_id),
4207
4211
Err(()) => Err(Bolt12PaymentError::UnexpectedInvoice),
4208
4212
}
4209
4213
}
4210
4214
4215
+ fn verify_bolt12_invoice(
4216
+ &self, invoice: &Bolt12Invoice, context: &OffersContext,
4217
+ ) -> Result<PaymentId, ()> {
4218
+ let secp_ctx = &self.secp_ctx;
4219
+ let expanded_key = &self.inbound_payment_key;
4220
+
4221
+ match context {
4222
+ OffersContext::Unknown {} if invoice.is_for_refund_without_paths() => {
4223
+ invoice.verify_using_metadata(expanded_key, secp_ctx)
4224
+ },
4225
+ OffersContext::OutboundPayment { payment_id, nonce } => {
4226
+ invoice
4227
+ .verify_using_payer_data(*payment_id, *nonce, expanded_key, secp_ctx)
4228
+ .then(|| *payment_id)
4229
+ .ok_or(())
4230
+ },
4231
+ _ => Err(()),
4232
+ }
4233
+ }
4234
+
4211
4235
fn send_payment_for_verified_bolt12_invoice(&self, invoice: &Bolt12Invoice, payment_id: PaymentId) -> Result<(), Bolt12PaymentError> {
4212
4236
let best_block_height = self.best_block.read().unwrap().height;
4213
4237
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
@@ -8784,13 +8808,12 @@ macro_rules! create_offer_builder { ($self: ident, $builder: ty) => {
8784
8808
let entropy = &*$self.entropy_source;
8785
8809
let secp_ctx = &$self.secp_ctx;
8786
8810
8787
- let path = $self.create_blinded_paths_using_absolute_expiry(OffersContext::Unknown {}, absolute_expiry)
8811
+ let nonce = Nonce::from_entropy_source(entropy);
8812
+ let context = OffersContext::InvoiceRequest { nonce };
8813
+ let path = $self.create_blinded_paths_using_absolute_expiry(context, absolute_expiry)
8788
8814
.and_then(|paths| paths.into_iter().next().ok_or(()))
8789
8815
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
8790
-
8791
- let builder = OfferBuilder::deriving_signing_pubkey(
8792
- node_id, expanded_key, entropy, secp_ctx
8793
- )
8816
+ let builder = OfferBuilder::deriving_signing_pubkey(node_id, expanded_key, nonce, secp_ctx)
8794
8817
.chain_hash($self.chain_hash)
8795
8818
.path(path);
8796
8819
@@ -8858,13 +8881,14 @@ macro_rules! create_refund_builder { ($self: ident, $builder: ty) => {
8858
8881
let entropy = &*$self.entropy_source;
8859
8882
let secp_ctx = &$self.secp_ctx;
8860
8883
8861
- let context = OffersContext::OutboundPayment { payment_id };
8884
+ let nonce = Nonce::from_entropy_source(entropy);
8885
+ let context = OffersContext::OutboundPayment { payment_id, nonce };
8862
8886
let path = $self.create_blinded_paths_using_absolute_expiry(context, Some(absolute_expiry))
8863
8887
.and_then(|paths| paths.into_iter().next().ok_or(()))
8864
8888
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
8865
8889
8866
8890
let builder = RefundBuilder::deriving_payer_id(
8867
- node_id, expanded_key, entropy , secp_ctx, amount_msats, payment_id
8891
+ node_id, expanded_key, nonce , secp_ctx, amount_msats, payment_id
8868
8892
)?
8869
8893
.chain_hash($self.chain_hash)
8870
8894
.absolute_expiry(absolute_expiry)
@@ -8973,8 +8997,9 @@ where
8973
8997
let entropy = &*self.entropy_source;
8974
8998
let secp_ctx = &self.secp_ctx;
8975
8999
9000
+ let nonce = Nonce::from_entropy_source(entropy);
8976
9001
let builder: InvoiceRequestBuilder<DerivedPayerId, secp256k1::All> = offer
8977
- .request_invoice_deriving_payer_id(expanded_key, entropy , secp_ctx, payment_id)?
9002
+ .request_invoice_deriving_payer_id(expanded_key, nonce , secp_ctx, payment_id)?
8978
9003
.into();
8979
9004
let builder = builder.chain_hash(self.chain_hash)?;
8980
9005
@@ -8992,8 +9017,9 @@ where
8992
9017
};
8993
9018
let invoice_request = builder.build_and_sign()?;
8994
9019
8995
- let context = OffersContext::OutboundPayment { payment_id };
8996
- let reply_paths = self.create_blinded_paths(context).map_err(|_| Bolt12SemanticError::MissingPaths)?;
9020
+ let context = OffersContext::OutboundPayment { payment_id, nonce };
9021
+ let reply_paths = self.create_blinded_paths(context)
9022
+ .map_err(|_| Bolt12SemanticError::MissingPaths)?;
8997
9023
8998
9024
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
8999
9025
@@ -10692,7 +10718,7 @@ where
10692
10718
10693
10719
let abandon_if_payment = |context| {
10694
10720
match context {
10695
- OffersContext::OutboundPayment { payment_id } => self.abandon_payment(payment_id),
10721
+ OffersContext::OutboundPayment { payment_id, .. } => self.abandon_payment(payment_id),
10696
10722
_ => {},
10697
10723
}
10698
10724
};
@@ -10703,19 +10729,32 @@ where
10703
10729
Some(responder) => responder,
10704
10730
None => return ResponseInstruction::NoResponse,
10705
10731
};
10732
+
10733
+ let nonce = match context {
10734
+ OffersContext::Unknown {} if invoice_request.metadata().is_some() => None,
10735
+ OffersContext::InvoiceRequest { nonce } => Some(nonce),
10736
+ _ => return ResponseInstruction::NoResponse,
10737
+ };
10738
+
10739
+ let invoice_request = match nonce {
10740
+ Some(nonce) => match invoice_request.verify_using_recipient_data(
10741
+ nonce, expanded_key, secp_ctx,
10742
+ ) {
10743
+ Ok(invoice_request) => invoice_request,
10744
+ Err(()) => return ResponseInstruction::NoResponse,
10745
+ },
10746
+ None => match invoice_request.verify_using_metadata(expanded_key, secp_ctx) {
10747
+ Ok(invoice_request) => invoice_request,
10748
+ Err(()) => return ResponseInstruction::NoResponse,
10749
+ },
10750
+ };
10751
+
10706
10752
let amount_msats = match InvoiceBuilder::<DerivedSigningPubkey>::amount_msats(
10707
- &invoice_request
10753
+ &invoice_request.inner
10708
10754
) {
10709
10755
Ok(amount_msats) => amount_msats,
10710
10756
Err(error) => return responder.respond(OffersMessage::InvoiceError(error.into())),
10711
10757
};
10712
- let invoice_request = match invoice_request.verify(expanded_key, secp_ctx) {
10713
- Ok(invoice_request) => invoice_request,
10714
- Err(()) => {
10715
- let error = Bolt12SemanticError::InvalidMetadata;
10716
- return responder.respond(OffersMessage::InvoiceError(error.into()));
10717
- },
10718
- };
10719
10758
10720
10759
let relative_expiry = DEFAULT_RELATIVE_EXPIRY.as_secs() as u32;
10721
10760
let (payment_hash, payment_secret) = match self.create_inbound_payment(
@@ -10788,24 +10827,28 @@ where
10788
10827
}
10789
10828
},
10790
10829
OffersMessage::Invoice(invoice) => {
10791
- let result = match invoice.verify(expanded_key, secp_ctx) {
10792
- Ok(payment_id) => {
10793
- let features = self.bolt12_invoice_features();
10794
- if invoice.invoice_features().requires_unknown_bits_from(&features) {
10795
- Err(InvoiceError::from(Bolt12SemanticError::UnknownRequiredFeatures))
10796
- } else if self.default_configuration.manually_handle_bolt12_invoices {
10797
- let event = Event::InvoiceReceived { payment_id, invoice, responder };
10798
- self.pending_events.lock().unwrap().push_back((event, None));
10799
- return ResponseInstruction::NoResponse;
10800
- } else {
10801
- self.send_payment_for_verified_bolt12_invoice(&invoice, payment_id)
10802
- .map_err(|e| {
10803
- log_trace!(self.logger, "Failed paying invoice: {:?}", e);
10804
- InvoiceError::from_string(format!("{:?}", e))
10805
- })
10806
- }
10807
- },
10808
- Err(()) => Err(InvoiceError::from_string("Unrecognized invoice".to_owned())),
10830
+ let payment_id = match self.verify_bolt12_invoice(&invoice, &context) {
10831
+ Ok(payment_id) => payment_id,
10832
+ Err(()) => return ResponseInstruction::NoResponse,
10833
+ };
10834
+
10835
+ let result = {
10836
+ let features = self.bolt12_invoice_features();
10837
+ if invoice.invoice_features().requires_unknown_bits_from(&features) {
10838
+ Err(InvoiceError::from(Bolt12SemanticError::UnknownRequiredFeatures))
10839
+ } else if self.default_configuration.manually_handle_bolt12_invoices {
10840
+ let event = Event::InvoiceReceived {
10841
+ payment_id, invoice, context, responder,
10842
+ };
10843
+ self.pending_events.lock().unwrap().push_back((event, None));
10844
+ return ResponseInstruction::NoResponse;
10845
+ } else {
10846
+ self.send_payment_for_verified_bolt12_invoice(&invoice, payment_id)
10847
+ .map_err(|e| {
10848
+ log_trace!(self.logger, "Failed paying invoice: {:?}", e);
10849
+ InvoiceError::from_string(format!("{:?}", e))
10850
+ })
10851
+ }
10809
10852
};
10810
10853
10811
10854
match result {
0 commit comments