Add InvoiceRequest::has_amount_msats

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.
This commit is contained in:
Jeffrey Czyz 2025-01-15 09:56:04 -06:00
parent 99889d27fd
commit c2360be049
No known key found for this signature in database
GPG Key ID: 3A4E08275D5E96D2

View File

@ -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()
@ -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
}
@ -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));
@ -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));
@ -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));
@ -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);
@ -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);
@ -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);
}