@@ -79,7 +79,7 @@ use crate::ln::inbound_payment::{ExpandedKey, IV_LEN};
79
79
use crate :: ln:: msgs:: DecodeError ;
80
80
use crate :: offers:: merkle:: { SignError , SignFn , SignatureTlvStream , SignatureTlvStreamRef , TaggedHash , TlvStream , self , SIGNATURE_TLV_RECORD_SIZE } ;
81
81
use 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 } ;
83
83
use crate :: offers:: parse:: { Bolt12ParseError , ParsedMessage , Bolt12SemanticError } ;
84
84
use crate :: offers:: payer:: { PayerContents , PayerTlvStream , PayerTlvStreamRef } ;
85
85
use crate :: offers:: signer:: { Metadata , MetadataMaterial } ;
@@ -974,7 +974,15 @@ impl InvoiceRequestContents {
974
974
}
975
975
976
976
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
+ } )
978
986
}
979
987
980
988
pub ( super ) fn features ( & self ) -> & InvoiceRequestFeatures {
@@ -1015,6 +1023,10 @@ impl InvoiceRequestContentsWithoutPayerSigningPubkey {
1015
1023
self . chain . unwrap_or_else ( || self . offer . implied_chain ( ) )
1016
1024
}
1017
1025
1026
+ pub ( super ) fn amount_msats ( & self ) -> Option < u64 > {
1027
+ self . amount_msats
1028
+ }
1029
+
1018
1030
pub ( super ) fn as_tlv_stream ( & self ) -> PartialInvoiceRequestTlvStreamRef {
1019
1031
let payer = PayerTlvStreamRef {
1020
1032
metadata : self . payer . 0 . as_bytes ( ) ,
@@ -1381,7 +1393,7 @@ mod tests {
1381
1393
assert_eq ! ( invoice_request. supported_quantity( ) , Quantity :: One ) ;
1382
1394
assert_eq ! ( invoice_request. issuer_signing_pubkey( ) , Some ( recipient_pubkey( ) ) ) ;
1383
1395
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 ) ) ;
1385
1397
assert_eq ! ( invoice_request. invoice_request_features( ) , & InvoiceRequestFeatures :: empty( ) ) ;
1386
1398
assert_eq ! ( invoice_request. quantity( ) , None ) ;
1387
1399
assert_eq ! ( invoice_request. payer_note( ) , None ) ;
@@ -1748,6 +1760,44 @@ mod tests {
1748
1760
}
1749
1761
}
1750
1762
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
+
1751
1801
#[ test]
1752
1802
fn builds_invoice_request_with_features ( ) {
1753
1803
let expanded_key = ExpandedKey :: new ( [ 42 ; 32 ] ) ;
0 commit comments