Skip to content

Commit 6a6a6c4

Browse files
committed
Introduce InvoiceSigningInfo usage
This commit replaces the legacy `VerifiedInvoiceRequestLegacy` with the new `InvoiceSigningInfo` type in the codebase.
1 parent 1e362af commit 6a6a6c4

File tree

6 files changed

+143
-123
lines changed

6 files changed

+143
-123
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, InvoiceSigningInfo};
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+
InvoiceSigningInfo::DerivedKeys(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+
InvoiceSigningInfo::ExplicitKeys(_) => {
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 & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ use crate::offers::invoice::{
4040
UnsignedBolt12Invoice, DEFAULT_RELATIVE_EXPIRY,
4141
};
4242
use crate::offers::invoice_error::InvoiceError;
43-
use crate::offers::invoice_request::{
44-
InvoiceRequest, InvoiceRequestBuilder, VerifiedInvoiceRequestLegacy,
45-
};
43+
use crate::offers::invoice_request::{InvoiceRequest, InvoiceRequestBuilder, InvoiceSigningInfo};
4644
use crate::offers::nonce::Nonce;
4745
use crate::offers::offer::{DerivedMetadata, Offer, OfferBuilder};
4846
use crate::offers::parse::Bolt12SemanticError;
@@ -406,7 +404,7 @@ fn enqueue_onion_message_with_reply_paths<T: OnionMessageContents + Clone>(
406404
pub enum InvreqResponseInstructions {
407405
/// We are the recipient of this payment, and a [`Bolt12Invoice`] should be sent in response to
408406
/// the invoice request since it is now verified.
409-
SendInvoice(VerifiedInvoiceRequestLegacy),
407+
SendInvoice(InvoiceSigningInfo),
410408
/// We are a static invoice server and should respond to this invoice request by retrieving the
411409
/// [`StaticInvoice`] corresponding to the `recipient_id` and `invoice_id` and calling
412410
/// `OffersMessageFlow::enqueue_static_invoice`.
@@ -920,7 +918,7 @@ where
920918
Ok(builder.into())
921919
}
922920

923-
/// Creates a response for the provided [`VerifiedInvoiceRequestLegacy`].
921+
/// Creates a response for the provided [`InvoiceSigningInfo`].
924922
///
925923
/// A response can be either an [`OffersMessage::Invoice`] with additional [`MessageContext`],
926924
/// or an [`OffersMessage::InvoiceError`], depending on the [`InvoiceRequest`].
@@ -929,9 +927,8 @@ where
929927
/// - We fail to generate valid payment paths to include in the [`Bolt12Invoice`].
930928
/// - We fail to generate a valid signed [`Bolt12Invoice`] for the [`InvoiceRequest`].
931929
pub fn create_response_for_invoice_request<ES: Deref, NS: Deref, R: Deref>(
932-
&self, signer: &NS, router: &R, entropy_source: ES,
933-
invoice_request: VerifiedInvoiceRequestLegacy, amount_msats: u64,
934-
payment_hash: PaymentHash, payment_secret: PaymentSecret,
930+
&self, signer: &NS, router: &R, entropy_source: ES, invoice_request: InvoiceSigningInfo,
931+
amount_msats: u64, payment_hash: PaymentHash, payment_secret: PaymentSecret,
935932
usable_channels: Vec<ChannelDetails>,
936933
) -> (OffersMessage, Option<MessageContext>)
937934
where
@@ -945,7 +942,7 @@ where
945942
let relative_expiry = DEFAULT_RELATIVE_EXPIRY.as_secs() as u32;
946943

947944
let context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
948-
offer_id: invoice_request.offer_id,
945+
offer_id: invoice_request.offer_id(),
949946
invoice_request: invoice_request.fields(),
950947
});
951948

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

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-
})
968+
let response = match invoice_request {
969+
InvoiceSigningInfo::DerivedKeys(request) => {
970+
#[cfg(feature = "std")]
971+
let builder = request.respond_using_derived_keys(payment_paths, payment_hash);
972+
#[cfg(not(feature = "std"))]
973+
let builder = request.respond_using_derived_keys_no_std(payment_paths, payment_hash, created_at);
974+
builder
975+
.map(InvoiceBuilder::<DerivedSigningPubkey>::from)
976+
.and_then(|builder| builder.allow_mpp().build_and_sign(secp_ctx))
977+
.map_err(InvoiceError::from)
978+
},
979+
InvoiceSigningInfo::ExplicitKeys(request) => {
980+
#[cfg(feature = "std")]
981+
let builder = request.respond_with(payment_paths, payment_hash);
982+
#[cfg(not(feature = "std"))]
983+
let builder = request.respond_with_no_std(payment_paths, payment_hash, created_at);
984+
builder
985+
.map(InvoiceBuilder::<ExplicitSigningPubkey>::from)
986+
.and_then(|builder| builder.allow_mpp().build())
987+
.map_err(InvoiceError::from)
988+
.and_then(|invoice| {
989+
#[cfg(c_bindings)]
990+
let mut invoice = invoice;
991+
invoice
992+
.sign(|invoice: &UnsignedBolt12Invoice| {
993+
signer.sign_bolt12_invoice(invoice)
994+
})
995+
.map_err(InvoiceError::from)
996+
})
997+
},
1000998
};
1001999

10021000
match response {

lightning/src/offers/invoice.rs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,7 +1818,7 @@ mod tests {
18181818
use crate::ln::inbound_payment::ExpandedKey;
18191819
use crate::ln::msgs::DecodeError;
18201820
use crate::offers::invoice_request::{
1821-
ExperimentalInvoiceRequestTlvStreamRef, InvoiceRequestTlvStreamRef,
1821+
ExperimentalInvoiceRequestTlvStreamRef, InvoiceRequestTlvStreamRef, InvoiceSigningInfo,
18221822
};
18231823
use crate::offers::merkle::{self, SignError, SignatureTlvStreamRef, TaggedHash, TlvStream};
18241824
use crate::offers::nonce::Nonce;
@@ -2235,15 +2235,25 @@ mod tests {
22352235
.build_and_sign()
22362236
.unwrap();
22372237

2238-
if let Err(e) = invoice_request
2238+
let verified_request = invoice_request
22392239
.clone()
22402240
.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);
2241+
.unwrap();
2242+
2243+
match verified_request {
2244+
InvoiceSigningInfo::DerivedKeys(req) => {
2245+
let invoice = req
2246+
.respond_using_derived_keys_no_std(payment_paths(), payment_hash(), now())
2247+
.unwrap()
2248+
.build_and_sign(&secp_ctx);
2249+
2250+
if let Err(e) = invoice {
2251+
panic!("error building invoice: {:?}", e);
2252+
}
2253+
},
2254+
InvoiceSigningInfo::ExplicitKeys(_) => {
2255+
panic!("expected invoice request with keys");
2256+
},
22472257
}
22482258

22492259
let expanded_key = ExpandedKey::new([41; 32]);
@@ -2263,13 +2273,14 @@ mod tests {
22632273
.build_and_sign()
22642274
.unwrap();
22652275

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),
2276+
let verified_request =
2277+
invoice_request.verify_using_metadata(&expanded_key, &secp_ctx).unwrap();
2278+
2279+
match verified_request {
2280+
InvoiceSigningInfo::DerivedKeys(_) => {
2281+
panic!("expected invoice request without keys")
2282+
},
2283+
InvoiceSigningInfo::ExplicitKeys(_) => (),
22732284
}
22742285
}
22752286

0 commit comments

Comments
 (0)