Skip to content

Commit d0f87d7

Browse files
Move common BOLT 12 invoice builder setters to new macro.
Will be useful when we later add support for static BOLT 12 invoices.
1 parent aeffcae commit d0f87d7

File tree

3 files changed

+90
-61
lines changed

3 files changed

+90
-61
lines changed

lightning/src/offers/invoice.rs

Lines changed: 6 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,10 @@
103103
//! ```
104104
105105
use bitcoin::blockdata::constants::ChainHash;
106-
use bitcoin::hash_types::{WPubkeyHash, WScriptHash};
107106
use bitcoin::network::constants::Network;
108107
use bitcoin::secp256k1::{KeyPair, PublicKey, Secp256k1, self};
109108
use bitcoin::secp256k1::schnorr::Signature;
110109
use bitcoin::address::{Address, Payload, WitnessProgram, WitnessVersion};
111-
use bitcoin::key::TweakedPublicKey;
112110
use core::time::Duration;
113111
use core::hash::{Hash, Hasher};
114112
use crate::io;
@@ -118,6 +116,7 @@ use crate::ln::channelmanager::PaymentId;
118116
use crate::ln::features::{BlindedHopFeatures, Bolt12InvoiceFeatures, InvoiceRequestFeatures, OfferFeatures};
119117
use crate::ln::inbound_payment::ExpandedKey;
120118
use crate::ln::msgs::DecodeError;
119+
use crate::offers::invoice_macros::invoice_builder_methods_common;
121120
use crate::offers::invoice_request::{INVOICE_REQUEST_PAYER_ID_TYPE, INVOICE_REQUEST_TYPES, IV_BYTES as INVOICE_REQUEST_IV_BYTES, InvoiceRequest, InvoiceRequestContents, InvoiceRequestTlvStream, InvoiceRequestTlvStreamRef};
122121
use crate::offers::merkle::{SignError, SignFn, SignatureTlvStream, SignatureTlvStreamRef, TaggedHash, TlvStream, WithoutSignatures, self};
123122
use crate::offers::offer::{Amount, OFFER_TYPES, OfferTlvStream, OfferTlvStreamRef, Quantity};
@@ -372,65 +371,6 @@ macro_rules! invoice_builder_methods { (
372371

373372
Ok(Self { invreq_bytes, invoice: contents, signing_pubkey_strategy })
374373
}
375-
376-
/// Sets the [`Bolt12Invoice::relative_expiry`] as seconds since [`Bolt12Invoice::created_at`].
377-
/// Any expiry that has already passed is valid and can be checked for using
378-
/// [`Bolt12Invoice::is_expired`].
379-
///
380-
/// Successive calls to this method will override the previous setting.
381-
pub fn relative_expiry($($self_mut)* $self: $self_type, relative_expiry_secs: u32) -> $return_type {
382-
let relative_expiry = Duration::from_secs(relative_expiry_secs as u64);
383-
$self.invoice.fields_mut().relative_expiry = Some(relative_expiry);
384-
$return_value
385-
}
386-
387-
/// Adds a P2WSH address to [`Bolt12Invoice::fallbacks`].
388-
///
389-
/// Successive calls to this method will add another address. Caller is responsible for not
390-
/// adding duplicate addresses and only calling if capable of receiving to P2WSH addresses.
391-
pub fn fallback_v0_p2wsh($($self_mut)* $self: $self_type, script_hash: &WScriptHash) -> $return_type {
392-
use bitcoin::hashes::Hash;
393-
let address = FallbackAddress {
394-
version: WitnessVersion::V0.to_num(),
395-
program: Vec::from(script_hash.to_byte_array()),
396-
};
397-
$self.invoice.fields_mut().fallbacks.get_or_insert_with(Vec::new).push(address);
398-
$return_value
399-
}
400-
401-
/// Adds a P2WPKH address to [`Bolt12Invoice::fallbacks`].
402-
///
403-
/// Successive calls to this method will add another address. Caller is responsible for not
404-
/// adding duplicate addresses and only calling if capable of receiving to P2WPKH addresses.
405-
pub fn fallback_v0_p2wpkh($($self_mut)* $self: $self_type, pubkey_hash: &WPubkeyHash) -> $return_type {
406-
use bitcoin::hashes::Hash;
407-
let address = FallbackAddress {
408-
version: WitnessVersion::V0.to_num(),
409-
program: Vec::from(pubkey_hash.to_byte_array()),
410-
};
411-
$self.invoice.fields_mut().fallbacks.get_or_insert_with(Vec::new).push(address);
412-
$return_value
413-
}
414-
415-
/// Adds a P2TR address to [`Bolt12Invoice::fallbacks`].
416-
///
417-
/// Successive calls to this method will add another address. Caller is responsible for not
418-
/// adding duplicate addresses and only calling if capable of receiving to P2TR addresses.
419-
pub fn fallback_v1_p2tr_tweaked($($self_mut)* $self: $self_type, output_key: &TweakedPublicKey) -> $return_type {
420-
let address = FallbackAddress {
421-
version: WitnessVersion::V1.to_num(),
422-
program: Vec::from(&output_key.serialize()[..]),
423-
};
424-
$self.invoice.fields_mut().fallbacks.get_or_insert_with(Vec::new).push(address);
425-
$return_value
426-
}
427-
428-
/// Sets [`Bolt12Invoice::invoice_features`] to indicate MPP may be used. Otherwise, MPP is
429-
/// disallowed.
430-
pub fn allow_mpp($($self_mut)* $self: $self_type) -> $return_type {
431-
$self.invoice.fields_mut().features.set_basic_mpp_optional();
432-
$return_value
433-
}
434374
} }
435375

436376
impl<'a> InvoiceBuilder<'a, ExplicitSigningPubkey> {
@@ -443,30 +383,35 @@ impl<'a> InvoiceBuilder<'a, DerivedSigningPubkey> {
443383

444384
impl<'a, S: SigningPubkeyStrategy> InvoiceBuilder<'a, S> {
445385
invoice_builder_methods!(self, Self, Self, self, S, mut);
386+
invoice_builder_methods_common!(self, Self, self.invoice.fields_mut(), Self, self, S, mut);
446387
}
447388

448389
#[cfg(all(c_bindings, not(test)))]
449390
impl<'a> InvoiceWithExplicitSigningPubkeyBuilder<'a> {
450391
invoice_explicit_signing_pubkey_builder_methods!(self, &mut Self);
451392
invoice_builder_methods!(self, &mut Self, (), (), ExplicitSigningPubkey);
393+
invoice_builder_methods_common!(self, &mut Self, self.invoice.fields_mut(), (), (), ExplicitSigningPubkey);
452394
}
453395

454396
#[cfg(all(c_bindings, test))]
455397
impl<'a> InvoiceWithExplicitSigningPubkeyBuilder<'a> {
456398
invoice_explicit_signing_pubkey_builder_methods!(self, &mut Self);
457399
invoice_builder_methods!(self, &mut Self, &mut Self, self, ExplicitSigningPubkey);
400+
invoice_builder_methods_common!(self, &mut Self, self.invoice.fields_mut(), &mut Self, self, ExplicitSigningPubkey);
458401
}
459402

460403
#[cfg(all(c_bindings, not(test)))]
461404
impl<'a> InvoiceWithDerivedSigningPubkeyBuilder<'a> {
462405
invoice_derived_signing_pubkey_builder_methods!(self, &mut Self);
463406
invoice_builder_methods!(self, &mut Self, (), (), DerivedSigningPubkey);
407+
invoice_builder_methods_common!(self, &mut Self, self.invoice.fields_mut(), (), (), DerivedSigningPubkey);
464408
}
465409

466410
#[cfg(all(c_bindings, test))]
467411
impl<'a> InvoiceWithDerivedSigningPubkeyBuilder<'a> {
468412
invoice_derived_signing_pubkey_builder_methods!(self, &mut Self);
469413
invoice_builder_methods!(self, &mut Self, &mut Self, self, DerivedSigningPubkey);
414+
invoice_builder_methods_common!(self, &mut Self, self.invoice.fields_mut(), &mut Self, self, DerivedSigningPubkey);
470415
}
471416

472417
#[cfg(c_bindings)]
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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::hash_types::WScriptHash
32+
) -> $return_type {
33+
use bitcoin::hashes::Hash;
34+
35+
let address = FallbackAddress {
36+
version: bitcoin::address::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::hash_types::WPubkeyHash
49+
) -> $return_type {
50+
use bitcoin::hashes::Hash;
51+
52+
let address = FallbackAddress {
53+
version: bitcoin::address::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::address::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;

lightning/src/offers/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub mod offer;
1717

1818
pub mod invoice;
1919
pub mod invoice_error;
20+
mod invoice_macros;
2021
pub mod invoice_request;
2122
pub mod merkle;
2223
pub mod parse;

0 commit comments

Comments
 (0)