Skip to content

Commit 3cfb9e5

Browse files
tnulljoostjager
authored andcommitted
Minor cleanup to DRY up duplicate code
1 parent 7e8a8ab commit 3cfb9e5

File tree

3 files changed

+82
-140
lines changed

3 files changed

+82
-140
lines changed

src/payment/bolt11.rs

Lines changed: 35 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use crate::config::{Config, LDK_PAYMENT_RETRY_TIMEOUT};
1313
use crate::connection::ConnectionManager;
1414
use crate::error::Error;
15-
use crate::hex_utils;
1615
use crate::liquidity::LiquiditySource;
1716
use crate::logger::{log_error, log_info, FilesystemLogger, Logger};
1817
use crate::payment::store::{
@@ -31,14 +30,32 @@ use lightning::routing::router::{PaymentParameters, RouteParameters};
3130

3231
use lightning_types::payment::{PaymentHash, PaymentPreimage};
3332

34-
use lightning_invoice::{Bolt11Invoice, Description};
33+
use lightning_invoice::Bolt11Invoice;
34+
use lightning_invoice::Bolt11InvoiceDescription as LdkBolt11InvoiceDescription;
3535

3636
use bitcoin::hashes::sha256::Hash as Sha256;
3737
use bitcoin::hashes::Hash;
3838

39-
use std::str::FromStr;
4039
use std::sync::{Arc, RwLock};
4140

41+
#[cfg(not(feature = "uniffi"))]
42+
type Bolt11InvoiceDescription = LdkBolt11InvoiceDescription;
43+
#[cfg(feature = "uniffi")]
44+
type Bolt11InvoiceDescription = crate::uniffi_types::Bolt11InvoiceDescription;
45+
46+
macro_rules! maybe_convert_description {
47+
($description: expr) => {{
48+
#[cfg(not(feature = "uniffi"))]
49+
{
50+
$description
51+
}
52+
#[cfg(feature = "uniffi")]
53+
{
54+
&LdkBolt11InvoiceDescription::try_from($description)?
55+
}
56+
}};
57+
}
58+
4259
/// A payment handler allowing to create and pay [BOLT 11] invoices.
4360
///
4461
/// Should be retrieved by calling [`Node::bolt11_payment`].
@@ -405,21 +422,11 @@ impl Bolt11Payment {
405422
/// given.
406423
///
407424
/// The inbound payment will be automatically claimed upon arrival.
408-
#[cfg(not(feature = "uniffi"))]
409-
pub fn receive(
410-
&self, amount_msat: u64, description: &lightning_invoice::Bolt11InvoiceDescription,
411-
expiry_secs: u32,
412-
) -> Result<Bolt11Invoice, Error> {
413-
self.receive_inner(Some(amount_msat), description, expiry_secs, None)
414-
}
415-
416-
#[cfg(feature = "uniffi")]
417425
pub fn receive(
418426
&self, amount_msat: u64, description: &Bolt11InvoiceDescription, expiry_secs: u32,
419427
) -> Result<Bolt11Invoice, Error> {
420-
let invoice_description =
421-
lightning_invoice::Bolt11InvoiceDescription::try_from(description)?;
422-
self.receive_inner(Some(amount_msat), &invoice_description, expiry_secs, None)
428+
let description = maybe_convert_description!(description);
429+
self.receive_inner(Some(amount_msat), description, expiry_secs, None)
423430
}
424431

425432
/// Returns a payable invoice that can be used to request a payment of the amount
@@ -436,42 +443,23 @@ impl Bolt11Payment {
436443
/// [`PaymentClaimable`]: crate::Event::PaymentClaimable
437444
/// [`claim_for_hash`]: Self::claim_for_hash
438445
/// [`fail_for_hash`]: Self::fail_for_hash
439-
#[cfg(not(feature = "uniffi"))]
440-
pub fn receive_for_hash(
441-
&self, amount_msat: u64, description: &lightning_invoice::Bolt11InvoiceDescription,
442-
expiry_secs: u32, payment_hash: PaymentHash,
443-
) -> Result<Bolt11Invoice, Error> {
444-
self.receive_inner(Some(amount_msat), description, expiry_secs, Some(payment_hash))
445-
}
446-
447-
#[cfg(feature = "uniffi")]
448446
pub fn receive_for_hash(
449447
&self, amount_msat: u64, description: &Bolt11InvoiceDescription, expiry_secs: u32,
450448
payment_hash: PaymentHash,
451449
) -> Result<Bolt11Invoice, Error> {
452-
let invoice_description =
453-
lightning_invoice::Bolt11InvoiceDescription::try_from(description)?;
454-
self.receive_inner(Some(amount_msat), &invoice_description, expiry_secs, Some(payment_hash))
450+
let description = maybe_convert_description!(description);
451+
self.receive_inner(Some(amount_msat), description, expiry_secs, Some(payment_hash))
455452
}
456453

457454
/// Returns a payable invoice that can be used to request and receive a payment for which the
458455
/// amount is to be determined by the user, also known as a "zero-amount" invoice.
459456
///
460457
/// The inbound payment will be automatically claimed upon arrival.
461-
#[cfg(not(feature = "uniffi"))]
462-
pub fn receive_variable_amount(
463-
&self, description: &lightning_invoice::Bolt11InvoiceDescription, expiry_secs: u32,
464-
) -> Result<Bolt11Invoice, Error> {
465-
self.receive_inner(None, description, expiry_secs, None)
466-
}
467-
468-
#[cfg(feature = "uniffi")]
469458
pub fn receive_variable_amount(
470459
&self, description: &Bolt11InvoiceDescription, expiry_secs: u32,
471460
) -> Result<Bolt11Invoice, Error> {
472-
let invoice_description =
473-
lightning_invoice::Bolt11InvoiceDescription::try_from(description)?;
474-
self.receive_inner(None, &invoice_description, expiry_secs, None)
461+
let description = maybe_convert_description!(description);
462+
self.receive_inner(None, description, expiry_secs, None)
475463
}
476464

477465
/// Returns a payable invoice that can be used to request a payment for the given payment hash
@@ -488,27 +476,16 @@ impl Bolt11Payment {
488476
/// [`PaymentClaimable`]: crate::Event::PaymentClaimable
489477
/// [`claim_for_hash`]: Self::claim_for_hash
490478
/// [`fail_for_hash`]: Self::fail_for_hash
491-
#[cfg(not(feature = "uniffi"))]
492-
pub fn receive_variable_amount_for_hash(
493-
&self, description: &lightning_invoice::Bolt11InvoiceDescription, expiry_secs: u32,
494-
payment_hash: PaymentHash,
495-
) -> Result<Bolt11Invoice, Error> {
496-
self.receive_inner(None, description, expiry_secs, Some(payment_hash))
497-
}
498-
499-
#[cfg(feature = "uniffi")]
500479
pub fn receive_variable_amount_for_hash(
501480
&self, description: &Bolt11InvoiceDescription, expiry_secs: u32, payment_hash: PaymentHash,
502481
) -> Result<Bolt11Invoice, Error> {
503-
let invoice_description =
504-
lightning_invoice::Bolt11InvoiceDescription::try_from(description)?;
505-
self.receive_inner(None, &invoice_description, expiry_secs, Some(payment_hash))
482+
let description = maybe_convert_description!(description);
483+
self.receive_inner(None, description, expiry_secs, Some(payment_hash))
506484
}
507485

508486
pub(crate) fn receive_inner(
509-
&self, amount_msat: Option<u64>,
510-
invoice_description: &lightning_invoice::Bolt11InvoiceDescription, expiry_secs: u32,
511-
manual_claim_payment_hash: Option<PaymentHash>,
487+
&self, amount_msat: Option<u64>, invoice_description: &LdkBolt11InvoiceDescription,
488+
expiry_secs: u32, manual_claim_payment_hash: Option<PaymentHash>,
512489
) -> Result<Bolt11Invoice, Error> {
513490
let invoice = {
514491
let invoice_params = Bolt11InvoiceParameters {
@@ -573,30 +550,14 @@ impl Bolt11Payment {
573550
/// channel to us. We'll use its cheapest offer otherwise.
574551
///
575552
/// [LSPS2]: https://github.com/BitcoinAndLightningLayerSpecs/lsp/blob/main/LSPS2/README.md
576-
#[cfg(not(feature = "uniffi"))]
577-
pub fn receive_via_jit_channel(
578-
&self, amount_msat: u64, description: &lightning_invoice::Bolt11InvoiceDescription,
579-
expiry_secs: u32, max_total_lsp_fee_limit_msat: Option<u64>,
580-
) -> Result<Bolt11Invoice, Error> {
581-
self.receive_via_jit_channel_inner(
582-
Some(amount_msat),
583-
description,
584-
expiry_secs,
585-
max_total_lsp_fee_limit_msat,
586-
None,
587-
)
588-
}
589-
590-
#[cfg(feature = "uniffi")]
591553
pub fn receive_via_jit_channel(
592554
&self, amount_msat: u64, description: &Bolt11InvoiceDescription, expiry_secs: u32,
593555
max_total_lsp_fee_limit_msat: Option<u64>,
594556
) -> Result<Bolt11Invoice, Error> {
595-
let invoice_description =
596-
lightning_invoice::Bolt11InvoiceDescription::try_from(description)?;
557+
let description = maybe_convert_description!(description);
597558
self.receive_via_jit_channel_inner(
598559
Some(amount_msat),
599-
&invoice_description,
560+
description,
600561
expiry_secs,
601562
max_total_lsp_fee_limit_msat,
602563
None,
@@ -614,38 +575,22 @@ impl Bolt11Payment {
614575
/// We'll use its cheapest offer otherwise.
615576
///
616577
/// [LSPS2]: https://github.com/BitcoinAndLightningLayerSpecs/lsp/blob/main/LSPS2/README.md
617-
#[cfg(not(feature = "uniffi"))]
618-
pub fn receive_variable_amount_via_jit_channel(
619-
&self, description: &lightning_invoice::Bolt11InvoiceDescription, expiry_secs: u32,
620-
max_proportional_lsp_fee_limit_ppm_msat: Option<u64>,
621-
) -> Result<Bolt11Invoice, Error> {
622-
self.receive_via_jit_channel_inner(
623-
None,
624-
description,
625-
expiry_secs,
626-
None,
627-
max_proportional_lsp_fee_limit_ppm_msat,
628-
)
629-
}
630-
631-
#[cfg(feature = "uniffi")]
632578
pub fn receive_variable_amount_via_jit_channel(
633579
&self, description: &Bolt11InvoiceDescription, expiry_secs: u32,
634580
max_proportional_lsp_fee_limit_ppm_msat: Option<u64>,
635581
) -> Result<Bolt11Invoice, Error> {
636-
let invoice_description =
637-
lightning_invoice::Bolt11InvoiceDescription::try_from(description)?;
582+
let description = maybe_convert_description!(description);
638583
self.receive_via_jit_channel_inner(
639584
None,
640-
&invoice_description,
585+
description,
641586
expiry_secs,
642587
None,
643588
max_proportional_lsp_fee_limit_ppm_msat,
644589
)
645590
}
646591

647592
fn receive_via_jit_channel_inner(
648-
&self, amount_msat: Option<u64>, description: &lightning_invoice::Bolt11InvoiceDescription,
593+
&self, amount_msat: Option<u64>, description: &LdkBolt11InvoiceDescription,
649594
expiry_secs: u32, max_total_lsp_fee_limit_msat: Option<u64>,
650595
max_proportional_lsp_fee_limit_ppm_msat: Option<u64>,
651596
) -> Result<Bolt11Invoice, Error> {
@@ -817,49 +762,3 @@ impl Bolt11Payment {
817762
Ok(())
818763
}
819764
}
820-
821-
/// Represents the description of an invoice which has to be either a directly included string or
822-
/// a hash of a description provided out of band.
823-
pub enum Bolt11InvoiceDescription {
824-
/// Contains a full description.
825-
Direct {
826-
/// Description of what the invoice is for
827-
description: String,
828-
},
829-
/// Contains a hash.
830-
Hash {
831-
/// Hash of the description of what the invoice is for
832-
hash: String,
833-
},
834-
}
835-
836-
impl TryFrom<&Bolt11InvoiceDescription> for lightning_invoice::Bolt11InvoiceDescription {
837-
type Error = Error;
838-
839-
fn try_from(value: &Bolt11InvoiceDescription) -> Result<Self, Self::Error> {
840-
match value {
841-
Bolt11InvoiceDescription::Direct { description } => {
842-
Description::new(description.clone())
843-
.map(lightning_invoice::Bolt11InvoiceDescription::Direct)
844-
.map_err(|_| Error::InvoiceCreationFailed)
845-
},
846-
Bolt11InvoiceDescription::Hash { hash } => Sha256::from_str(&hash)
847-
.map(lightning_invoice::Sha256)
848-
.map(lightning_invoice::Bolt11InvoiceDescription::Hash)
849-
.map_err(|_| Error::InvoiceCreationFailed),
850-
}
851-
}
852-
}
853-
854-
impl From<lightning_invoice::Bolt11InvoiceDescription> for Bolt11InvoiceDescription {
855-
fn from(value: lightning_invoice::Bolt11InvoiceDescription) -> Self {
856-
match value {
857-
lightning_invoice::Bolt11InvoiceDescription::Direct(description) => {
858-
Bolt11InvoiceDescription::Direct { description: description.to_string() }
859-
},
860-
lightning_invoice::Bolt11InvoiceDescription::Hash(hash) => {
861-
Bolt11InvoiceDescription::Hash { hash: hex_utils::to_string(hash.0.as_ref()) }
862-
},
863-
}
864-
}
865-
}

src/payment/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ mod spontaneous;
1414
pub(crate) mod store;
1515
mod unified_qr;
1616

17-
pub use bolt11::Bolt11InvoiceDescription;
1817
pub use bolt11::Bolt11Payment;
1918
pub use bolt12::Bolt12Payment;
2019
pub use onchain::OnchainPayment;

src/uniffi_types.rs

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,14 @@ pub use lightning::util::string::UntrustedString;
2828

2929
pub use lightning_types::payment::{PaymentHash, PaymentPreimage, PaymentSecret};
3030

31-
pub use lightning_invoice::Bolt11Invoice;
31+
pub use lightning_invoice::{Bolt11Invoice, Description};
3232

3333
pub use bitcoin::{Address, BlockHash, Network, OutPoint, Txid};
3434

3535
pub use bip39::Mnemonic;
3636

3737
pub use vss_client::headers::{VssHeaderProvider, VssHeaderProviderError};
3838

39-
pub use crate::payment::Bolt11InvoiceDescription;
40-
4139
use crate::UniffiCustomTypeConverter;
4240

4341
use crate::builder::sanitize_alias;
@@ -347,3 +345,49 @@ impl UniffiCustomTypeConverter for NodeAlias {
347345
obj.to_string()
348346
}
349347
}
348+
349+
/// Represents the description of an invoice which has to be either a directly included string or
350+
/// a hash of a description provided out of band.
351+
pub enum Bolt11InvoiceDescription {
352+
/// Contains a full description.
353+
Direct {
354+
/// Description of what the invoice is for
355+
description: String,
356+
},
357+
/// Contains a hash.
358+
Hash {
359+
/// Hash of the description of what the invoice is for
360+
hash: String,
361+
},
362+
}
363+
364+
impl TryFrom<&Bolt11InvoiceDescription> for lightning_invoice::Bolt11InvoiceDescription {
365+
type Error = Error;
366+
367+
fn try_from(value: &Bolt11InvoiceDescription) -> Result<Self, Self::Error> {
368+
match value {
369+
Bolt11InvoiceDescription::Direct { description } => {
370+
Description::new(description.clone())
371+
.map(lightning_invoice::Bolt11InvoiceDescription::Direct)
372+
.map_err(|_| Error::InvoiceCreationFailed)
373+
},
374+
Bolt11InvoiceDescription::Hash { hash } => Sha256::from_str(&hash)
375+
.map(lightning_invoice::Sha256)
376+
.map(lightning_invoice::Bolt11InvoiceDescription::Hash)
377+
.map_err(|_| Error::InvoiceCreationFailed),
378+
}
379+
}
380+
}
381+
382+
impl From<lightning_invoice::Bolt11InvoiceDescription> for Bolt11InvoiceDescription {
383+
fn from(value: lightning_invoice::Bolt11InvoiceDescription) -> Self {
384+
match value {
385+
lightning_invoice::Bolt11InvoiceDescription::Direct(description) => {
386+
Bolt11InvoiceDescription::Direct { description: description.to_string() }
387+
},
388+
lightning_invoice::Bolt11InvoiceDescription::Hash(hash) => {
389+
Bolt11InvoiceDescription::Hash { hash: hex_utils::to_string(hash.0.as_ref()) }
390+
},
391+
}
392+
}
393+
}

0 commit comments

Comments
 (0)