Skip to content

Commit 20334f5

Browse files
committed
Introduce InvoiceRequestVerifiedFromOffer usage
This commit replaces the legacy `VerifiedInvoiceRequestLegacy` with the new `InvoiceRequestVerifiedFromOffer` type in the codebase.
1 parent b2d4198 commit 20334f5

File tree

6 files changed

+142
-118
lines changed

6 files changed

+142
-118
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7422,7 +7422,7 @@ where
74227422
};
74237423
let payment_purpose_context =
74247424
PaymentContext::Bolt12Offer(Bolt12OfferContext {
7425-
offer_id: verified_invreq.offer_id,
7425+
offer_id: verified_invreq.offer_id(),
74267426
invoice_request: verified_invreq.fields(),
74277427
});
74287428
let from_parts_res = events::PaymentPurpose::from_parts(
@@ -14330,7 +14330,7 @@ where
1433014330
};
1433114331

1433214332
let amount_msats = match InvoiceBuilder::<DerivedSigningPubkey>::amount_msats(
14333-
&invoice_request.inner
14333+
&invoice_request.inner()
1433414334
) {
1433514335
Ok(amount_msats) => amount_msats,
1433614336
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, InvoiceRequestVerifiedFromOffer};
6161
use crate::offers::nonce::Nonce;
6262
use crate::offers::parse::Bolt12SemanticError;
6363
use crate::onion_message::messenger::{Destination, MessageSendInstructions, NodeIdMessageRouter, NullMessageRouter, PeeledOnion};
@@ -2276,11 +2276,19 @@ fn fails_paying_invoice_with_unknown_required_features() {
22762276
let secp_ctx = Secp256k1::new();
22772277

22782278
let created_at = alice.node.duration_since_epoch();
2279-
let invoice = invoice_request
2280-
.verify_using_recipient_data(nonce, &expanded_key, &secp_ctx).unwrap()
2281-
.respond_using_derived_keys_no_std(payment_paths, payment_hash, created_at).unwrap()
2282-
.features_unchecked(Bolt12InvoiceFeatures::unknown())
2283-
.build_and_sign(&secp_ctx).unwrap();
2279+
let verified_invoice_request = invoice_request
2280+
.verify_using_recipient_data(nonce, &expanded_key, &secp_ctx).unwrap();
2281+
2282+
let invoice = match verified_invoice_request {
2283+
InvoiceRequestVerifiedFromOffer::DerivedKeys(request) => {
2284+
request.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()
2287+
},
2288+
InvoiceRequestVerifiedFromOffer::ExplicitKeys(_) => {
2289+
panic!("Expected invoice request with keys");
2290+
},
2291+
};
22842292

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

lightning/src/offers/flow.rs

Lines changed: 35 additions & 34 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, InvoiceRequestVerifiedFromOffer,
4545
};
4646
use crate::offers::nonce::Nonce;
4747
use crate::offers::offer::{Amount, DerivedMetadata, Offer, OfferBuilder};
@@ -393,7 +393,7 @@ fn enqueue_onion_message_with_reply_paths<T: OnionMessageContents + Clone>(
393393
pub enum InvreqResponseInstructions {
394394
/// We are the recipient of this payment, and a [`Bolt12Invoice`] should be sent in response to
395395
/// the invoice request since it is now verified.
396-
SendInvoice(VerifiedInvoiceRequestLegacy),
396+
SendInvoice(InvoiceRequestVerifiedFromOffer),
397397
/// We are a static invoice server and should respond to this invoice request by retrieving the
398398
/// [`StaticInvoice`] corresponding to the `recipient_id` and `invoice_id` and calling
399399
/// `OffersMessageFlow::enqueue_static_invoice`.
@@ -902,7 +902,7 @@ where
902902
Ok(builder.into())
903903
}
904904

905-
/// Creates a response for the provided [`VerifiedInvoiceRequestLegacy`].
905+
/// Creates a response for the provided [`InvoiceRequestVerifiedFromOffer`].
906906
///
907907
/// A response can be either an [`OffersMessage::Invoice`] with additional [`MessageContext`],
908908
/// or an [`OffersMessage::InvoiceError`], depending on the [`InvoiceRequest`].
@@ -912,7 +912,7 @@ where
912912
/// - We fail to generate a valid signed [`Bolt12Invoice`] for the [`InvoiceRequest`].
913913
pub fn create_response_for_invoice_request<ES: Deref, NS: Deref, R: Deref>(
914914
&self, signer: &NS, router: &R, entropy_source: ES,
915-
invoice_request: VerifiedInvoiceRequestLegacy, amount_msats: u64,
915+
invoice_request: InvoiceRequestVerifiedFromOffer, amount_msats: u64,
916916
payment_hash: PaymentHash, payment_secret: PaymentSecret,
917917
usable_channels: Vec<ChannelDetails>,
918918
) -> (OffersMessage, Option<MessageContext>)
@@ -927,7 +927,7 @@ where
927927
let relative_expiry = DEFAULT_RELATIVE_EXPIRY.as_secs() as u32;
928928

929929
let context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
930-
offer_id: invoice_request.offer_id,
930+
offer_id: invoice_request.offer_id(),
931931
invoice_request: invoice_request.fields(),
932932
});
933933

@@ -950,35 +950,36 @@ where
950950
#[cfg(not(feature = "std"))]
951951
let created_at = Duration::from_secs(self.highest_seen_timestamp.load(Ordering::Acquire) as u64);
952952

953-
let response = if invoice_request.keys.is_some() {
954-
#[cfg(feature = "std")]
955-
let builder = invoice_request.respond_using_derived_keys(payment_paths, payment_hash);
956-
#[cfg(not(feature = "std"))]
957-
let builder = invoice_request.respond_using_derived_keys_no_std(
958-
payment_paths,
959-
payment_hash,
960-
created_at,
961-
);
962-
builder
963-
.map(InvoiceBuilder::<DerivedSigningPubkey>::from)
964-
.and_then(|builder| builder.allow_mpp().build_and_sign(secp_ctx))
965-
.map_err(InvoiceError::from)
966-
} else {
967-
#[cfg(feature = "std")]
968-
let builder = invoice_request.respond_with(payment_paths, payment_hash);
969-
#[cfg(not(feature = "std"))]
970-
let builder = invoice_request.respond_with_no_std(payment_paths, payment_hash, created_at);
971-
builder
972-
.map(InvoiceBuilder::<ExplicitSigningPubkey>::from)
973-
.and_then(|builder| builder.allow_mpp().build())
974-
.map_err(InvoiceError::from)
975-
.and_then(|invoice| {
976-
#[cfg(c_bindings)]
977-
let mut invoice = invoice;
978-
invoice
979-
.sign(|invoice: &UnsignedBolt12Invoice| signer.sign_bolt12_invoice(invoice))
980-
.map_err(InvoiceError::from)
981-
})
953+
let response = match invoice_request {
954+
InvoiceRequestVerifiedFromOffer::DerivedKeys(request) => {
955+
#[cfg(feature = "std")]
956+
let builder = request.respond_using_derived_keys(payment_paths, payment_hash);
957+
#[cfg(not(feature = "std"))]
958+
let builder = request.respond_using_derived_keys_no_std(payment_paths, payment_hash, created_at);
959+
builder
960+
.map(InvoiceBuilder::<DerivedSigningPubkey>::from)
961+
.and_then(|builder| builder.allow_mpp().build_and_sign(secp_ctx))
962+
.map_err(InvoiceError::from)
963+
},
964+
InvoiceRequestVerifiedFromOffer::ExplicitKeys(request) => {
965+
#[cfg(feature = "std")]
966+
let builder = request.respond_with(payment_paths, payment_hash);
967+
#[cfg(not(feature = "std"))]
968+
let builder = request.respond_with_no_std(payment_paths, payment_hash, created_at);
969+
builder
970+
.map(InvoiceBuilder::<ExplicitSigningPubkey>::from)
971+
.and_then(|builder| builder.allow_mpp().build())
972+
.map_err(InvoiceError::from)
973+
.and_then(|invoice| {
974+
#[cfg(c_bindings)]
975+
let mut invoice = invoice;
976+
invoice
977+
.sign(|invoice: &UnsignedBolt12Invoice| {
978+
signer.sign_bolt12_invoice(invoice)
979+
})
980+
.map_err(InvoiceError::from)
981+
})
982+
},
982983
};
983984

984985
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+
InvoiceRequestVerifiedFromOffer,
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+
InvoiceRequestVerifiedFromOffer::DerivedKeys(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+
InvoiceRequestVerifiedFromOffer::ExplicitKeys(_) => {
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+
InvoiceRequestVerifiedFromOffer::DerivedKeys(_) => {
2282+
panic!("expected invoice request without keys")
2283+
},
2284+
InvoiceRequestVerifiedFromOffer::ExplicitKeys(_) => (),
22732285
}
22742286
}
22752287

0 commit comments

Comments
 (0)