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, $type_param: ty $(, $self_mut: tt)?
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`].
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);
26 /// Adds a P2WSH address to [`Bolt12Invoice::fallbacks`].
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
33 use bitcoin::hashes::Hash;
35 let address = FallbackAddress {
36 version: bitcoin::WitnessVersion::V0.to_num(),
37 program: Vec::from(script_hash.to_byte_array()),
39 $invoice_fields.fallbacks.get_or_insert_with(Vec::new).push(address);
43 /// Adds a P2WPKH address to [`Bolt12Invoice::fallbacks`].
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
50 use bitcoin::hashes::Hash;
52 let address = FallbackAddress {
53 version: bitcoin::WitnessVersion::V0.to_num(),
54 program: Vec::from(pubkey_hash.to_byte_array()),
56 $invoice_fields.fallbacks.get_or_insert_with(Vec::new).push(address);
60 /// Adds a P2TR address to [`Bolt12Invoice::fallbacks`].
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
67 let address = FallbackAddress {
68 version: bitcoin::WitnessVersion::V1.to_num(),
69 program: Vec::from(&output_key.serialize()[..]),
71 $invoice_fields.fallbacks.get_or_insert_with(Vec::new).push(address);
75 /// Sets [`Bolt12Invoice::invoice_features`] to indicate MPP may be used. Otherwise, MPP is
77 pub fn allow_mpp($($self_mut)* $self: $self_type) -> $return_type {
78 $invoice_fields.features.set_basic_mpp_optional();
83 pub(super) use invoice_builder_methods_common;