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