Move common BOLT 12 invoice builder setters to new macro.
[rust-lightning] / lightning / src / offers / invoice_macros.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
8 // licenses.
9
10 //! Shared code between BOLT 12 static and single-use invoices.
11
12 macro_rules! invoice_builder_methods_common { (
13         $self: ident, $self_type: ty, $invoice_fields: expr, $return_type: ty, $return_value: expr, $type_param: ty $(, $self_mut: tt)?
14 ) => {
15         /// Sets the [`Bolt12Invoice::relative_expiry`] as seconds since [`Bolt12Invoice::created_at`].
16         /// Any expiry that has already passed is valid and can be checked for using
17         /// [`Bolt12Invoice::is_expired`].
18         ///
19         /// Successive calls to this method will override the previous setting.
20         pub fn relative_expiry($($self_mut)* $self: $self_type, relative_expiry_secs: u32) -> $return_type {
21                 let relative_expiry = Duration::from_secs(relative_expiry_secs as u64);
22                 $invoice_fields.relative_expiry = Some(relative_expiry);
23                 $return_value
24         }
25
26         /// Adds a P2WSH address to [`Bolt12Invoice::fallbacks`].
27         ///
28         /// Successive calls to this method will add another address. Caller is responsible for not
29         /// adding duplicate addresses and only calling if capable of receiving to P2WSH addresses.
30         pub fn fallback_v0_p2wsh(
31                 $($self_mut)* $self: $self_type, script_hash: &bitcoin::WScriptHash
32         ) -> $return_type {
33                 use bitcoin::hashes::Hash;
34
35                 let address = FallbackAddress {
36                         version: bitcoin::WitnessVersion::V0.to_num(),
37                         program: Vec::from(script_hash.to_byte_array()),
38                 };
39                 $invoice_fields.fallbacks.get_or_insert_with(Vec::new).push(address);
40                 $return_value
41         }
42
43         /// Adds a P2WPKH address to [`Bolt12Invoice::fallbacks`].
44         ///
45         /// Successive calls to this method will add another address. Caller is responsible for not
46         /// adding duplicate addresses and only calling if capable of receiving to P2WPKH addresses.
47         pub fn fallback_v0_p2wpkh(
48                 $($self_mut)* $self: $self_type, pubkey_hash: &bitcoin::WPubkeyHash
49         ) -> $return_type {
50                 use bitcoin::hashes::Hash;
51
52                 let address = FallbackAddress {
53                         version: bitcoin::WitnessVersion::V0.to_num(),
54                         program: Vec::from(pubkey_hash.to_byte_array()),
55                 };
56                 $invoice_fields.fallbacks.get_or_insert_with(Vec::new).push(address);
57                 $return_value
58         }
59
60         /// Adds a P2TR address to [`Bolt12Invoice::fallbacks`].
61         ///
62         /// Successive calls to this method will add another address. Caller is responsible for not
63         /// adding duplicate addresses and only calling if capable of receiving to P2TR addresses.
64         pub fn fallback_v1_p2tr_tweaked(
65                 $($self_mut)* $self: $self_type, output_key: &bitcoin::key::TweakedPublicKey
66         ) -> $return_type {
67                 let address = FallbackAddress {
68                         version: bitcoin::WitnessVersion::V1.to_num(),
69                         program: Vec::from(&output_key.serialize()[..]),
70                 };
71                 $invoice_fields.fallbacks.get_or_insert_with(Vec::new).push(address);
72                 $return_value
73         }
74
75         /// Sets [`Bolt12Invoice::invoice_features`] to indicate MPP may be used. Otherwise, MPP is
76         /// disallowed.
77         pub fn allow_mpp($($self_mut)* $self: $self_type) -> $return_type {
78                 $invoice_fields.features.set_basic_mpp_optional();
79                 $return_value
80         }
81 } }
82
83 pub(super) use invoice_builder_methods_common;