1 // This file is Copyright its original authors, visible in version control
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
10 //! Shared code between BOLT 12 static and single-use invoices.
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)?
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`].
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);
27 /// Adds a P2WSH address to [`Bolt12Invoice::fallbacks`].
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
34 use bitcoin::hashes::Hash;
36 let address = FallbackAddress {
37 version: bitcoin::WitnessVersion::V0.to_num(),
38 program: Vec::from(script_hash.to_byte_array()),
40 $invoice_fields.fallbacks.get_or_insert_with(Vec::new).push(address);
44 /// Adds a P2WPKH address to [`Bolt12Invoice::fallbacks`].
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
51 use bitcoin::hashes::Hash;
53 let address = FallbackAddress {
54 version: bitcoin::WitnessVersion::V0.to_num(),
55 program: Vec::from(pubkey_hash.to_byte_array()),
57 $invoice_fields.fallbacks.get_or_insert_with(Vec::new).push(address);
61 /// Adds a P2TR address to [`Bolt12Invoice::fallbacks`].
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
68 let address = FallbackAddress {
69 version: bitcoin::WitnessVersion::V1.to_num(),
70 program: Vec::from(&output_key.serialize()[..]),
72 $invoice_fields.fallbacks.get_or_insert_with(Vec::new).push(address);
76 /// Sets [`Bolt12Invoice::invoice_features`] to indicate MPP may be used. Otherwise, MPP is
78 pub fn allow_mpp($($self_mut)* $self: $self_type) -> $return_type {
79 $invoice_fields.features.set_basic_mpp_optional();
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.
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`].
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()
97 /// Duration since the Unix epoch when the invoice was created.
98 pub fn created_at(&$self) -> Duration {
99 $contents.created_at()
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()
108 /// Whether the invoice has expired.
109 #[cfg(feature = "std")]
110 pub fn is_expired(&$self) -> bool {
111 $contents.is_expired()
114 /// Fallback addresses for paying the invoice on-chain, in order of most-preferred to
116 pub fn fallbacks(&$self) -> Vec<Address> {
117 $contents.fallbacks()
120 /// Features pertaining to paying an invoice.
121 pub fn invoice_features(&$self) -> &Bolt12InvoiceFeatures {
125 /// The public key corresponding to the key used to sign the invoice.
126 pub fn signing_pubkey(&$self) -> PublicKey {
127 $contents.signing_pubkey()
131 pub(super) use invoice_accessors_common;
132 pub(super) use invoice_builder_methods_common;