@@ -79,7 +79,7 @@ use crate::ln::inbound_payment::{ExpandedKey, IV_LEN};
7979use crate :: ln:: msgs:: DecodeError ;
8080use crate :: offers:: merkle:: { SignError , SignFn , SignatureTlvStream , SignatureTlvStreamRef , TaggedHash , TlvStream , self , SIGNATURE_TLV_RECORD_SIZE } ;
8181use crate :: offers:: nonce:: Nonce ;
82- use crate :: offers:: offer:: { EXPERIMENTAL_OFFER_TYPES , ExperimentalOfferTlvStream , ExperimentalOfferTlvStreamRef , OFFER_TYPES , Offer , OfferContents , OfferId , OfferTlvStream , OfferTlvStreamRef } ;
82+ use crate :: offers:: offer:: { Amount , EXPERIMENTAL_OFFER_TYPES , ExperimentalOfferTlvStream , ExperimentalOfferTlvStreamRef , OFFER_TYPES , Offer , OfferContents , OfferId , OfferTlvStream , OfferTlvStreamRef } ;
8383use crate :: offers:: parse:: { Bolt12ParseError , ParsedMessage , Bolt12SemanticError } ;
8484use crate :: offers:: payer:: { PayerContents , PayerTlvStream , PayerTlvStreamRef } ;
8585use crate :: offers:: signer:: { Metadata , MetadataMaterial } ;
@@ -974,7 +974,15 @@ impl InvoiceRequestContents {
974974 }
975975
976976 pub ( super ) fn amount_msats ( & self ) -> Option < u64 > {
977- self . inner . amount_msats
977+ self . inner
978+ . amount_msats ( )
979+ . or_else ( || match self . inner . offer . amount ( ) {
980+ Some ( Amount :: Bitcoin { amount_msats } ) => {
981+ Some ( amount_msats. saturating_mul ( self . quantity ( ) . unwrap_or ( 1 ) ) )
982+ } ,
983+ Some ( Amount :: Currency { .. } ) => None ,
984+ None => { debug_assert ! ( false ) ; None } ,
985+ } )
978986 }
979987
980988 pub ( super ) fn features ( & self ) -> & InvoiceRequestFeatures {
@@ -1015,6 +1023,10 @@ impl InvoiceRequestContentsWithoutPayerSigningPubkey {
10151023 self . chain . unwrap_or_else ( || self . offer . implied_chain ( ) )
10161024 }
10171025
1026+ pub ( super ) fn amount_msats ( & self ) -> Option < u64 > {
1027+ self . amount_msats
1028+ }
1029+
10181030 pub ( super ) fn as_tlv_stream ( & self ) -> PartialInvoiceRequestTlvStreamRef {
10191031 let payer = PayerTlvStreamRef {
10201032 metadata : self . payer . 0 . as_bytes ( ) ,
@@ -1381,7 +1393,7 @@ mod tests {
13811393 assert_eq ! ( invoice_request. supported_quantity( ) , Quantity :: One ) ;
13821394 assert_eq ! ( invoice_request. issuer_signing_pubkey( ) , Some ( recipient_pubkey( ) ) ) ;
13831395 assert_eq ! ( invoice_request. chain( ) , ChainHash :: using_genesis_block( Network :: Bitcoin ) ) ;
1384- assert_eq ! ( invoice_request. amount_msats( ) , None ) ;
1396+ assert_eq ! ( invoice_request. amount_msats( ) , Some ( 1000 ) ) ;
13851397 assert_eq ! ( invoice_request. invoice_request_features( ) , & InvoiceRequestFeatures :: empty( ) ) ;
13861398 assert_eq ! ( invoice_request. quantity( ) , None ) ;
13871399 assert_eq ! ( invoice_request. payer_note( ) , None ) ;
@@ -1748,6 +1760,44 @@ mod tests {
17481760 }
17491761 }
17501762
1763+ #[ test]
1764+ fn builds_invoice_request_without_amount ( ) {
1765+ let expanded_key = ExpandedKey :: new ( [ 42 ; 32 ] ) ;
1766+ let entropy = FixedEntropy { } ;
1767+ let nonce = Nonce :: from_entropy_source ( & entropy) ;
1768+ let secp_ctx = Secp256k1 :: new ( ) ;
1769+ let payment_id = PaymentId ( [ 1 ; 32 ] ) ;
1770+
1771+ let invoice_request = OfferBuilder :: new ( recipient_pubkey ( ) )
1772+ . amount_msats ( 1000 )
1773+ . build ( ) . unwrap ( )
1774+ . request_invoice ( & expanded_key, nonce, & secp_ctx, payment_id) . unwrap ( )
1775+ . build_and_sign ( ) . unwrap ( ) ;
1776+ let ( _, _, tlv_stream, _, _, _) = invoice_request. as_tlv_stream ( ) ;
1777+ assert_eq ! ( invoice_request. amount_msats( ) , Some ( 1000 ) ) ;
1778+ assert_eq ! ( tlv_stream. amount, None ) ;
1779+
1780+ let invoice_request = OfferBuilder :: new ( recipient_pubkey ( ) )
1781+ . amount_msats ( 1000 )
1782+ . supported_quantity ( Quantity :: Unbounded )
1783+ . build ( ) . unwrap ( )
1784+ . request_invoice ( & expanded_key, nonce, & secp_ctx, payment_id) . unwrap ( )
1785+ . quantity ( 2 ) . unwrap ( )
1786+ . build_and_sign ( ) . unwrap ( ) ;
1787+ let ( _, _, tlv_stream, _, _, _) = invoice_request. as_tlv_stream ( ) ;
1788+ assert_eq ! ( invoice_request. amount_msats( ) , Some ( 2000 ) ) ;
1789+ assert_eq ! ( tlv_stream. amount, None ) ;
1790+
1791+ let invoice_request = OfferBuilder :: new ( recipient_pubkey ( ) )
1792+ . amount ( Amount :: Currency { iso4217_code : * b"USD" , amount : 10 } )
1793+ . build_unchecked ( )
1794+ . request_invoice ( & expanded_key, nonce, & secp_ctx, payment_id) . unwrap ( )
1795+ . build_unchecked_and_sign ( ) ;
1796+ let ( _, _, tlv_stream, _, _, _) = invoice_request. as_tlv_stream ( ) ;
1797+ assert_eq ! ( invoice_request. amount_msats( ) , None ) ;
1798+ assert_eq ! ( tlv_stream. amount, None ) ;
1799+ }
1800+
17511801 #[ test]
17521802 fn builds_invoice_request_with_features ( ) {
17531803 let expanded_key = ExpandedKey :: new ( [ 42 ; 32 ] ) ;
0 commit comments