Skip to content

Commit f89d83a

Browse files
committed
Introduce VerifiedInvoiceRequestEnum usage
This commit replaces the legacy `VerifiedInvoiceRequestLegacy` with the new `VerifiedInvoiceRequestEnum` type in the codebase.
1 parent a092188 commit f89d83a

File tree

6 files changed

+142
-119
lines changed

6 files changed

+142
-119
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7199,7 +7199,7 @@ where
71997199
};
72007200
let payment_purpose_context =
72017201
PaymentContext::Bolt12Offer(Bolt12OfferContext {
7202-
offer_id: verified_invreq.offer_id,
7202+
offer_id: verified_invreq.offer_id(),
72037203
invoice_request: verified_invreq.fields(),
72047204
});
72057205
let from_parts_res = events::PaymentPurpose::from_parts(
@@ -14095,7 +14095,7 @@ where
1409514095
};
1409614096

1409714097
let amount_msats = match InvoiceBuilder::<DerivedSigningPubkey>::amount_msats(
14098-
&invoice_request.inner
14098+
&invoice_request.inner()
1409914099
) {
1410014100
Ok(amount_msats) => amount_msats,
1410114101
Err(error) => return Some((OffersMessage::InvoiceError(error.into()), responder.respond())),

lightning/src/ln/offers_tests.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ use crate::ln::msgs::{BaseMessageHandler, ChannelMessageHandler, Init, NodeAnnou
5757
use crate::ln::outbound_payment::IDEMPOTENCY_TIMEOUT_TICKS;
5858
use crate::offers::invoice::Bolt12Invoice;
5959
use crate::offers::invoice_error::InvoiceError;
60-
use crate::offers::invoice_request::{InvoiceRequest, InvoiceRequestFields};
60+
use crate::offers::invoice_request::{InvoiceRequest, InvoiceRequestFields, VerifiedInvoiceRequestEnum};
6161
use crate::offers::nonce::Nonce;
6262
use crate::offers::parse::Bolt12SemanticError;
6363
use crate::onion_message::messenger::{Destination, MessageSendInstructions, NodeIdMessageRouter, NullMessageRouter, PeeledOnion};
@@ -2279,11 +2279,19 @@ fn fails_paying_invoice_with_unknown_required_features() {
22792279
let secp_ctx = Secp256k1::new();
22802280

22812281
let created_at = alice.node.duration_since_epoch();
2282-
let invoice = invoice_request
2283-
.verify_using_recipient_data(nonce, &expanded_key, &secp_ctx).unwrap()
2284-
.respond_using_derived_keys_no_std(payment_paths, payment_hash, created_at).unwrap()
2285-
.features_unchecked(Bolt12InvoiceFeatures::unknown())
2286-
.build_and_sign(&secp_ctx).unwrap();
2282+
let verified_invoice_request = invoice_request
2283+
.verify_using_recipient_data(nonce, &expanded_key, &secp_ctx).unwrap();
2284+
2285+
let invoice = match verified_invoice_request {
2286+
VerifiedInvoiceRequestEnum::WithKeys(request) => {
2287+
request.respond_using_derived_keys_no_std(payment_paths, payment_hash, created_at).unwrap()
2288+
.features_unchecked(Bolt12InvoiceFeatures::unknown())
2289+
.build_and_sign(&secp_ctx).unwrap()
2290+
},
2291+
VerifiedInvoiceRequestEnum::WithoutKeys(_) => {
2292+
panic!("Expected invoice request with keys");
2293+
},
2294+
};
22872295

22882296
// Enqueue an onion message containing the new invoice.
22892297
let instructions = MessageSendInstructions::WithoutReplyPath {

lightning/src/offers/flow.rs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use crate::offers::invoice::{
4141
};
4242
use crate::offers::invoice_error::InvoiceError;
4343
use crate::offers::invoice_request::{
44-
InvoiceRequest, InvoiceRequestBuilder, VerifiedInvoiceRequestLegacy,
44+
InvoiceRequest, InvoiceRequestBuilder, VerifiedInvoiceRequestEnum,
4545
};
4646
use crate::offers::nonce::Nonce;
4747
use crate::offers::offer::{DerivedMetadata, Offer, OfferBuilder};
@@ -406,7 +406,7 @@ fn enqueue_onion_message_with_reply_paths<T: OnionMessageContents + Clone>(
406406
pub enum InvreqResponseInstructions {
407407
/// We are the recipient of this payment, and a [`Bolt12Invoice`] should be sent in response to
408408
/// the invoice request since it is now verified.
409-
SendInvoice(VerifiedInvoiceRequestLegacy),
409+
SendInvoice(VerifiedInvoiceRequestEnum),
410410
/// We are a static invoice server and should respond to this invoice request by retrieving the
411411
/// [`StaticInvoice`] corresponding to the `recipient_id` and `invoice_id` and calling
412412
/// `OffersMessageFlow::enqueue_static_invoice`.
@@ -920,7 +920,7 @@ where
920920
Ok(builder.into())
921921
}
922922

923-
/// Creates a response for the provided [`VerifiedInvoiceRequestLegacy`].
923+
/// Creates a response for the provided [`VerifiedInvoiceRequestEnum`].
924924
///
925925
/// A response can be either an [`OffersMessage::Invoice`] with additional [`MessageContext`],
926926
/// or an [`OffersMessage::InvoiceError`], depending on the [`InvoiceRequest`].
@@ -930,9 +930,8 @@ where
930930
/// - We fail to generate a valid signed [`Bolt12Invoice`] for the [`InvoiceRequest`].
931931
pub fn create_response_for_invoice_request<ES: Deref, NS: Deref, R: Deref>(
932932
&self, signer: &NS, router: &R, entropy_source: ES,
933-
invoice_request: VerifiedInvoiceRequestLegacy, amount_msats: u64,
934-
payment_hash: PaymentHash, payment_secret: PaymentSecret,
935-
usable_channels: Vec<ChannelDetails>,
933+
invoice_request: VerifiedInvoiceRequestEnum, amount_msats: u64, payment_hash: PaymentHash,
934+
payment_secret: PaymentSecret, usable_channels: Vec<ChannelDetails>,
936935
) -> (OffersMessage, Option<MessageContext>)
937936
where
938937
ES::Target: EntropySource,
@@ -945,7 +944,7 @@ where
945944
let relative_expiry = DEFAULT_RELATIVE_EXPIRY.as_secs() as u32;
946945

947946
let context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
948-
offer_id: invoice_request.offer_id,
947+
offer_id: invoice_request.offer_id(),
949948
invoice_request: invoice_request.fields(),
950949
});
951950

@@ -968,35 +967,36 @@ where
968967
#[cfg(not(feature = "std"))]
969968
let created_at = Duration::from_secs(self.highest_seen_timestamp.load(Ordering::Acquire) as u64);
970969

971-
let response = if invoice_request.keys.is_some() {
972-
#[cfg(feature = "std")]
973-
let builder = invoice_request.respond_using_derived_keys(payment_paths, payment_hash);
974-
#[cfg(not(feature = "std"))]
975-
let builder = invoice_request.respond_using_derived_keys_no_std(
976-
payment_paths,
977-
payment_hash,
978-
created_at,
979-
);
980-
builder
981-
.map(InvoiceBuilder::<DerivedSigningPubkey>::from)
982-
.and_then(|builder| builder.allow_mpp().build_and_sign(secp_ctx))
983-
.map_err(InvoiceError::from)
984-
} else {
985-
#[cfg(feature = "std")]
986-
let builder = invoice_request.respond_with(payment_paths, payment_hash);
987-
#[cfg(not(feature = "std"))]
988-
let builder = invoice_request.respond_with_no_std(payment_paths, payment_hash, created_at);
989-
builder
990-
.map(InvoiceBuilder::<ExplicitSigningPubkey>::from)
991-
.and_then(|builder| builder.allow_mpp().build())
992-
.map_err(InvoiceError::from)
993-
.and_then(|invoice| {
994-
#[cfg(c_bindings)]
995-
let mut invoice = invoice;
996-
invoice
997-
.sign(|invoice: &UnsignedBolt12Invoice| signer.sign_bolt12_invoice(invoice))
998-
.map_err(InvoiceError::from)
999-
})
970+
let response = match invoice_request {
971+
VerifiedInvoiceRequestEnum::WithKeys(request) => {
972+
#[cfg(feature = "std")]
973+
let builder = request.respond_using_derived_keys(payment_paths, payment_hash);
974+
#[cfg(not(feature = "std"))]
975+
let builder = request.respond_using_derived_keys_no_std(payment_paths, payment_hash, created_at);
976+
builder
977+
.map(InvoiceBuilder::<DerivedSigningPubkey>::from)
978+
.and_then(|builder| builder.allow_mpp().build_and_sign(secp_ctx))
979+
.map_err(InvoiceError::from)
980+
},
981+
VerifiedInvoiceRequestEnum::WithoutKeys(request) => {
982+
#[cfg(feature = "std")]
983+
let builder = request.respond_with(payment_paths, payment_hash);
984+
#[cfg(not(feature = "std"))]
985+
let builder = request.respond_with_no_std(payment_paths, payment_hash, created_at);
986+
builder
987+
.map(InvoiceBuilder::<ExplicitSigningPubkey>::from)
988+
.and_then(|builder| builder.allow_mpp().build())
989+
.map_err(InvoiceError::from)
990+
.and_then(|invoice| {
991+
#[cfg(c_bindings)]
992+
let mut invoice = invoice;
993+
invoice
994+
.sign(|invoice: &UnsignedBolt12Invoice| {
995+
signer.sign_bolt12_invoice(invoice)
996+
})
997+
.map_err(InvoiceError::from)
998+
})
999+
},
10001000
};
10011001

10021002
match response {

lightning/src/offers/invoice.rs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,6 +1819,7 @@ mod tests {
18191819
use crate::ln::msgs::DecodeError;
18201820
use crate::offers::invoice_request::{
18211821
ExperimentalInvoiceRequestTlvStreamRef, InvoiceRequestTlvStreamRef,
1822+
VerifiedInvoiceRequestEnum,
18221823
};
18231824
use crate::offers::merkle::{self, SignError, SignatureTlvStreamRef, TaggedHash, TlvStream};
18241825
use crate::offers::nonce::Nonce;
@@ -2235,15 +2236,25 @@ mod tests {
22352236
.build_and_sign()
22362237
.unwrap();
22372238

2238-
if let Err(e) = invoice_request
2239+
let verified_request = invoice_request
22392240
.clone()
22402241
.verify_using_recipient_data(nonce, &expanded_key, &secp_ctx)
2241-
.unwrap()
2242-
.respond_using_derived_keys_no_std(payment_paths(), payment_hash(), now())
2243-
.unwrap()
2244-
.build_and_sign(&secp_ctx)
2245-
{
2246-
panic!("error building invoice: {:?}", e);
2242+
.unwrap();
2243+
2244+
match verified_request {
2245+
VerifiedInvoiceRequestEnum::WithKeys(req) => {
2246+
let invoice = req
2247+
.respond_using_derived_keys_no_std(payment_paths(), payment_hash(), now())
2248+
.unwrap()
2249+
.build_and_sign(&secp_ctx);
2250+
2251+
if let Err(e) = invoice {
2252+
panic!("error building invoice: {:?}", e);
2253+
}
2254+
},
2255+
VerifiedInvoiceRequestEnum::WithoutKeys(_) => {
2256+
panic!("expected invoice request with keys");
2257+
},
22472258
}
22482259

22492260
let expanded_key = ExpandedKey::new([41; 32]);
@@ -2263,13 +2274,14 @@ mod tests {
22632274
.build_and_sign()
22642275
.unwrap();
22652276

2266-
match invoice_request
2267-
.verify_using_metadata(&expanded_key, &secp_ctx)
2268-
.unwrap()
2269-
.respond_using_derived_keys_no_std(payment_paths(), payment_hash(), now())
2270-
{
2271-
Ok(_) => panic!("expected error"),
2272-
Err(e) => assert_eq!(e, Bolt12SemanticError::InvalidMetadata),
2277+
let verified_request =
2278+
invoice_request.verify_using_metadata(&expanded_key, &secp_ctx).unwrap();
2279+
2280+
match verified_request {
2281+
VerifiedInvoiceRequestEnum::WithKeys(_) => {
2282+
panic!("expected invoice request without keys")
2283+
},
2284+
VerifiedInvoiceRequestEnum::WithoutKeys(_) => (),
22732285
}
22742286
}
22752287

0 commit comments

Comments
 (0)