@@ -216,7 +216,7 @@ macro_rules! invoice_explicit_signing_pubkey_builder_methods { ($self: ident, $s
216
216
invoice_request: & ' a InvoiceRequest , payment_paths: Vec <BlindedPaymentPath >,
217
217
created_at: Duration , payment_hash: PaymentHash , signing_pubkey: PublicKey
218
218
) -> Result <Self , Bolt12SemanticError > {
219
- let amount_msats = Self :: amount_msats( invoice_request) ?;
219
+ let amount_msats = Self :: amount_msats( invoice_request, None ) ?;
220
220
let contents = InvoiceContents :: ForOffer {
221
221
invoice_request: invoice_request. contents. clone( ) ,
222
222
fields: Self :: fields(
@@ -272,9 +272,9 @@ macro_rules! invoice_derived_signing_pubkey_builder_methods { ($self: ident, $se
272
272
#[ cfg_attr( c_bindings, allow( dead_code) ) ]
273
273
pub ( super ) fn for_offer_using_keys(
274
274
invoice_request: & ' a InvoiceRequest , payment_paths: Vec <BlindedPaymentPath >,
275
- created_at: Duration , payment_hash: PaymentHash , keys: Keypair
275
+ created_at: Duration , payment_hash: PaymentHash , keys: Keypair , custom_amount_msats : Option < u64 >
276
276
) -> Result <Self , Bolt12SemanticError > {
277
- let amount_msats = Self :: amount_msats( invoice_request) ?;
277
+ let amount_msats = Self :: amount_msats( invoice_request, custom_amount_msats ) ?;
278
278
let signing_pubkey = keys. public_key( ) ;
279
279
let contents = InvoiceContents :: ForOffer {
280
280
invoice_request: invoice_request. contents. clone( ) ,
@@ -340,18 +340,29 @@ macro_rules! invoice_builder_methods { (
340
340
$self: ident, $self_type: ty, $return_type: ty, $return_value: expr, $type_param: ty $( , $self_mut: tt) ?
341
341
) => {
342
342
pub ( crate ) fn amount_msats(
343
- invoice_request: & InvoiceRequest
343
+ invoice_request: & InvoiceRequest , custom_amount_msats : Option < u64 >
344
344
) -> Result <u64 , Bolt12SemanticError > {
345
- match invoice_request. amount_msats( ) {
346
- Some ( amount_msats) => Ok ( amount_msats) ,
347
- None => match invoice_request. contents. inner. offer. amount( ) {
348
- Some ( Amount :: Bitcoin { amount_msats } ) => {
349
- amount_msats. checked_mul( invoice_request. quantity( ) . unwrap_or( 1 ) )
350
- . ok_or( Bolt12SemanticError :: InvalidAmount )
351
- } ,
352
- Some ( Amount :: Currency { .. } ) => Err ( Bolt12SemanticError :: UnsupportedCurrency ) ,
353
- None => Err ( Bolt12SemanticError :: MissingAmount ) ,
354
- } ,
345
+ if invoice_request. amount_msats( ) . is_some( ) && custom_amount_msats. is_some( ) {
346
+ return Err ( Bolt12SemanticError :: UnexpectedAmount ) ;
347
+ }
348
+
349
+ if let Some ( amount_msats) = invoice_request. amount_msats( ) {
350
+ return Ok ( amount_msats) ;
351
+ }
352
+
353
+ if let Some ( custom_amount_msats) = custom_amount_msats {
354
+ return Ok ( custom_amount_msats) ;
355
+ }
356
+
357
+ match invoice_request. contents. inner. offer. amount( ) {
358
+ Some ( Amount :: Bitcoin { amount_msats } ) => {
359
+ let quantity = invoice_request. quantity( ) . unwrap_or( 1 ) ;
360
+ amount_msats
361
+ . checked_mul( quantity)
362
+ . ok_or( Bolt12SemanticError :: InvalidAmount )
363
+ }
364
+ Some ( Amount :: Currency { .. } ) => Err ( Bolt12SemanticError :: UnsupportedCurrency ) ,
365
+ None => Err ( Bolt12SemanticError :: MissingAmount ) ,
355
366
}
356
367
}
357
368
@@ -1778,7 +1789,7 @@ mod tests {
1778
1789
1779
1790
if let Err ( e) = invoice_request. clone ( )
1780
1791
. verify_using_recipient_data ( nonce, & expanded_key, & secp_ctx) . unwrap ( )
1781
- . respond_using_derived_keys_no_std ( payment_paths ( ) , payment_hash ( ) , now ( ) ) . unwrap ( )
1792
+ . respond_using_derived_keys_no_std ( payment_paths ( ) , payment_hash ( ) , now ( ) , None ) . unwrap ( )
1782
1793
. build_and_sign ( & secp_ctx)
1783
1794
{
1784
1795
panic ! ( "error building invoice: {:?}" , e) ;
@@ -1799,7 +1810,7 @@ mod tests {
1799
1810
1800
1811
match invoice_request
1801
1812
. verify_using_metadata ( & expanded_key, & secp_ctx) . unwrap ( )
1802
- . respond_using_derived_keys_no_std ( payment_paths ( ) , payment_hash ( ) , now ( ) )
1813
+ . respond_using_derived_keys_no_std ( payment_paths ( ) , payment_hash ( ) , now ( ) , None )
1803
1814
{
1804
1815
Ok ( _) => panic ! ( "expected error" ) ,
1805
1816
Err ( e) => assert_eq ! ( e, Bolt12SemanticError :: InvalidMetadata ) ,
0 commit comments