Refactor: Convert fields function to macro
What changed, and why it matters
This commit is a pure code cleanup: it takes an existing function and turns it into a reusable macro (template). The actual behavior of the code does not change; it is only reorganized so the same logic can be reused in future commits. There is no security-relevant change visible in the diff.
No security action needed. Treat as ordinary refactoring review; verify follow-up commits that add `fields_accessor!` to other types do not alter behavior.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors VerifiedInvoiceRequest::fields() into a new fields_accessor! macro and invokes that macro for VerifiedInvoiceRequest. The generated code is functionally identical to the previous hand-written method: it destructures InvoiceRequestContents, copies payer_signing_pubkey and quantity, clones and safely truncates payer_note, and clones offer_from_hrn(). No logic, bounds, or data-flow changes are introduced.
Changed components
lightning/src/offers/invoice_request.rsInspect captured patch +34 / −25
diff --git a/lightning/src/offers/invoice_request.rs b/lightning/src/offers/invoice_request.rs
index d5d3c4d..dc058c2 100644
--- a/lightning/src/offers/invoice_request.rs
+++ b/lightning/src/offers/invoice_request.rs
@@ -970,9 +970,43 @@ macro_rules! invoice_request_respond_with_derived_signing_pubkey_methods { (
}
} }
+macro_rules! fields_accessor {
+ ($self:ident, $inner:expr) => {
+ /// Fetch the [`InvoiceRequestFields`] for this verified invoice.
+ ///
+ /// These are fields which we expect to be useful when receiving a payment for this invoice
+ /// request, and include the returned [`InvoiceRequestFields`] in the
+ /// [`PaymentContext::Bolt12Offer`].
+ ///
+ /// [`PaymentContext::Bolt12Offer`]: crate::blinded_path::payment::PaymentContext::Bolt12Offer
+ pub fn fields(&$self) -> InvoiceRequestFields {
+ let InvoiceRequestContents {
+ payer_signing_pubkey,
+ inner: InvoiceRequestContentsWithoutPayerSigningPubkey {
+ quantity,
+ payer_note,
+ ..
+ },
+ } = &$inner;
+
+ InvoiceRequestFields {
+ payer_signing_pubkey: *payer_signing_pubkey,
+ quantity: *quantity,
+ payer_note_truncated: payer_note
+ .clone()
+ // Truncate the payer note to `PAYER_NOTE_LIMIT` bytes, rounding
+ // down to the nearest valid UTF-8 code point boundary.
+ .map(|s| UntrustedString(string_truncate_safe(s, PAYER_NOTE_LIMIT))),
+ human_readable_name: $self.offer_from_hrn().clone(),
+ }
+ }
+ };
+}
+
impl VerifiedInvoiceRequest {
offer_accessors!(self, self.inner.contents.inner.offer);
invoice_request_accessors!(self, self.inner.contents);
+ fields_accessor!(self, self.inner.contents);
#[cfg(not(c_bindings))]
invoice_request_respond_with_explicit_signing_pubkey_methods!(
self,
@@ -997,31 +1031,6 @@ impl VerifiedInvoiceRequest {
self.inner,
InvoiceWithDerivedSigningPubkeyBuilder
);
-
- /// Fetch the [`InvoiceRequestFields`] for this verified invoice.
- ///
- /// These are fields which we expect to be useful when receiving a payment for this invoice
- /// request, and include the returned [`InvoiceRequestFields`] in the
- /// [`PaymentContext::Bolt12Offer`].
- ///
- /// [`PaymentContext::Bolt12Offer`]: crate::blinded_path::payment::PaymentContext::Bolt12Offer
- pub fn fields(&self) -> InvoiceRequestFields {
- let InvoiceRequestContents {
- payer_signing_pubkey,
- inner: InvoiceRequestContentsWithoutPayerSigningPubkey { quantity, payer_note, .. },
- } = &self.inner.contents;
-
- InvoiceRequestFields {
- payer_signing_pubkey: *payer_signing_pubkey,
- quantity: *quantity,
- payer_note_truncated: payer_note
- .clone()
- // Truncate the payer note to `PAYER_NOTE_LIMIT` bytes, rounding
- // down to the nearest valid UTF-8 code point boundary.
- .map(|s| UntrustedString(string_truncate_safe(s, PAYER_NOTE_LIMIT))),
- human_readable_name: self.offer_from_hrn().clone(),
- }
- }
}
/// `String::truncate(new_len)` panics if you split inside a UTF-8 code point,
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.