Move common BOLT 12 accessor methods 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,
14         $type_param: ty $(, $self_mut: tt)?
15 ) => {
16         /// Sets the [`Bolt12Invoice::relative_expiry`] as seconds since [`Bolt12Invoice::created_at`].
17         /// Any expiry that has already passed is valid and can be checked for using
18         /// [`Bolt12Invoice::is_expired`].
19         ///
20         /// Successive calls to this method will override the previous setting.
21         pub fn relative_expiry($($self_mut)* $self: $self_type, relative_expiry_secs: u32) -> $return_type {
22                 let relative_expiry = Duration::from_secs(relative_expiry_secs as u64);
23                 $invoice_fields.relative_expiry = Some(relative_expiry);
24                 $return_value
25         }
26
27         /// Adds a P2WSH address to [`Bolt12Invoice::fallbacks`].
28         ///
29         /// Successive calls to this method will add another address. Caller is responsible for not
30         /// adding duplicate addresses and only calling if capable of receiving to P2WSH addresses.
31         pub fn fallback_v0_p2wsh(
32                 $($self_mut)* $self: $self_type, script_hash: &bitcoin::WScriptHash
33         ) -> $return_type {
34                 use bitcoin::hashes::Hash;
35
36                 let address = FallbackAddress {
37                         version: bitcoin::WitnessVersion::V0.to_num(),
38                         program: Vec::from(script_hash.to_byte_array()),
39                 };
40                 $invoice_fields.fallbacks.get_or_insert_with(Vec::new).push(address);
41                 $return_value
42         }
43
44         /// Adds a P2WPKH address to [`Bolt12Invoice::fallbacks`].
45         ///
46         /// Successive calls to this method will add another address. Caller is responsible for not
47         /// adding duplicate addresses and only calling if capable of receiving to P2WPKH addresses.
48         pub fn fallback_v0_p2wpkh(
49                 $($self_mut)* $self: $self_type, pubkey_hash: &bitcoin::WPubkeyHash
50         ) -> $return_type {
51                 use bitcoin::hashes::Hash;
52
53                 let address = FallbackAddress {
54                         version: bitcoin::WitnessVersion::V0.to_num(),
55                         program: Vec::from(pubkey_hash.to_byte_array()),
56                 };
57                 $invoice_fields.fallbacks.get_or_insert_with(Vec::new).push(address);
58                 $return_value
59         }
60
61         /// Adds a P2TR address to [`Bolt12Invoice::fallbacks`].
62         ///
63         /// Successive calls to this method will add another address. Caller is responsible for not
64         /// adding duplicate addresses and only calling if capable of receiving to P2TR addresses.
65         pub fn fallback_v1_p2tr_tweaked(
66                 $($self_mut)* $self: $self_type, output_key: &bitcoin::key::TweakedPublicKey
67         ) -> $return_type {
68                 let address = FallbackAddress {
69                         version: bitcoin::WitnessVersion::V1.to_num(),
70                         program: Vec::from(&output_key.serialize()[..]),
71                 };
72                 $invoice_fields.fallbacks.get_or_insert_with(Vec::new).push(address);
73                 $return_value
74         }
75
76         /// Sets [`Bolt12Invoice::invoice_features`] to indicate MPP may be used. Otherwise, MPP is
77         /// disallowed.
78         pub fn allow_mpp($($self_mut)* $self: $self_type) -> $return_type {
79                 $invoice_fields.features.set_basic_mpp_optional();
80                 $return_value
81         }
82 } }
83
84 macro_rules! invoice_accessors_common { ($self: ident, $contents: expr) => {
85         /// Paths to the recipient originating from publicly reachable nodes, including information
86         /// needed for routing payments across them.
87         ///
88         /// Blinded paths provide recipient privacy by obfuscating its node id. Note, however, that this
89         /// privacy is lost if a public node id is used for [`Bolt12Invoice::signing_pubkey`].
90         ///
91         /// This is not exported to bindings users as slices with non-reference types cannot be ABI
92         /// matched in another language.
93         pub fn payment_paths(&$self) -> &[(BlindedPayInfo, BlindedPath)] {
94                 $contents.payment_paths()
95         }
96
97         /// Duration since the Unix epoch when the invoice was created.
98         pub fn created_at(&$self) -> Duration {
99                 $contents.created_at()
100         }
101
102         /// Duration since [`Bolt12Invoice::created_at`] when the invoice has expired and therefore
103         /// should no longer be paid.
104         pub fn relative_expiry(&$self) -> Duration {
105                 $contents.relative_expiry()
106         }
107
108         /// Whether the invoice has expired.
109         #[cfg(feature = "std")]
110         pub fn is_expired(&$self) -> bool {
111                 $contents.is_expired()
112         }
113
114         /// Fallback addresses for paying the invoice on-chain, in order of most-preferred to
115         /// least-preferred.
116         pub fn fallbacks(&$self) -> Vec<Address> {
117                 $contents.fallbacks()
118         }
119
120         /// Features pertaining to paying an invoice.
121         pub fn invoice_features(&$self) -> &Bolt12InvoiceFeatures {
122                 $contents.features()
123         }
124
125         /// The public key corresponding to the key used to sign the invoice.
126         pub fn signing_pubkey(&$self) -> PublicKey {
127                 $contents.signing_pubkey()
128         }
129 } }
130
131 pub(super) use invoice_accessors_common;
132 pub(super) use invoice_builder_methods_common;