Skip to content

Commit

Permalink
Add InvoiceRequest::has_amount_msats
Browse files Browse the repository at this point in the history
When InvoiceRequest::amount_msats returns Some, it may have been
inferred from the Offer::amount and InvoiceRequest::quantity. Add a
method to InvoiceRequest for determining if the amount was explicitly
set.
  • Loading branch information
jkczyz authored and TheBlueMatt committed Jan 15, 2025
1 parent 3386c4b commit 3c356ab
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lightning/src/offers/invoice_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,15 @@ macro_rules! invoice_request_accessors { ($self: ident, $contents: expr) => {
$contents.amount_msats()
}

/// Returns whether an amount was set in the request; otherwise, if [`amount_msats`] is `Some`
/// then it was inferred from the [`Offer::amount`] and [`quantity`].
///
/// [`amount_msats`]: Self::amount_msats
/// [`quantity`]: Self::quantity
pub fn has_amount_msats(&$self) -> bool {
$contents.has_amount_msats()
}

/// Features pertaining to requesting an invoice.
pub fn invoice_request_features(&$self) -> &InvoiceRequestFeatures {
&$contents.features()
Expand Down Expand Up @@ -985,6 +994,10 @@ impl InvoiceRequestContents {
})
}

pub(super) fn has_amount_msats(&self) -> bool {
self.inner.amount_msats().is_some()
}

pub(super) fn features(&self) -> &InvoiceRequestFeatures {
&self.inner.features
}
Expand Down Expand Up @@ -1669,6 +1682,7 @@ mod tests {
.amount_msats(1000).unwrap()
.build_and_sign().unwrap();
let (_, _, tlv_stream, _, _, _) = invoice_request.as_tlv_stream();
assert!(invoice_request.has_amount_msats());
assert_eq!(invoice_request.amount_msats(), Some(1000));
assert_eq!(tlv_stream.amount, Some(1000));

Expand All @@ -1680,6 +1694,7 @@ mod tests {
.amount_msats(1000).unwrap()
.build_and_sign().unwrap();
let (_, _, tlv_stream, _, _, _) = invoice_request.as_tlv_stream();
assert!(invoice_request.has_amount_msats());
assert_eq!(invoice_request.amount_msats(), Some(1000));
assert_eq!(tlv_stream.amount, Some(1000));

Expand All @@ -1690,6 +1705,7 @@ mod tests {
.amount_msats(1001).unwrap()
.build_and_sign().unwrap();
let (_, _, tlv_stream, _, _, _) = invoice_request.as_tlv_stream();
assert!(invoice_request.has_amount_msats());
assert_eq!(invoice_request.amount_msats(), Some(1001));
assert_eq!(tlv_stream.amount, Some(1001));

Expand Down Expand Up @@ -1774,6 +1790,7 @@ mod tests {
.request_invoice(&expanded_key, nonce, &secp_ctx, payment_id).unwrap()
.build_and_sign().unwrap();
let (_, _, tlv_stream, _, _, _) = invoice_request.as_tlv_stream();
assert!(!invoice_request.has_amount_msats());
assert_eq!(invoice_request.amount_msats(), Some(1000));
assert_eq!(tlv_stream.amount, None);

Expand All @@ -1785,6 +1802,7 @@ mod tests {
.quantity(2).unwrap()
.build_and_sign().unwrap();
let (_, _, tlv_stream, _, _, _) = invoice_request.as_tlv_stream();
assert!(!invoice_request.has_amount_msats());
assert_eq!(invoice_request.amount_msats(), Some(2000));
assert_eq!(tlv_stream.amount, None);

Expand All @@ -1794,6 +1812,7 @@ mod tests {
.request_invoice(&expanded_key, nonce, &secp_ctx, payment_id).unwrap()
.build_unchecked_and_sign();
let (_, _, tlv_stream, _, _, _) = invoice_request.as_tlv_stream();
assert!(!invoice_request.has_amount_msats());
assert_eq!(invoice_request.amount_msats(), None);
assert_eq!(tlv_stream.amount, None);
}
Expand Down

0 comments on commit 3c356ab

Please sign in to comment.