Skip to content

Commit de3123f

Browse files
committed
review comments [squash]
1 parent f958924 commit de3123f

File tree

7 files changed

+100
-48
lines changed

7 files changed

+100
-48
lines changed

bindings/ldk_node.udl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ interface Bolt11Payment {
134134
[Throws=NodeError]
135135
Bolt11Invoice receive_variable_amount_for_hash([ByRef]Bolt11InvoiceStringDescription description, u32 expiry_secs, PaymentHash payment_hash);
136136
[Throws=NodeError]
137-
Bolt11Invoice receive_via_jit_channel(u64 amount_msat, [ByRef]string description, u32 expiry_secs, u64? max_lsp_fee_limit_msat);
137+
Bolt11Invoice receive_via_jit_channel(u64 amount_msat, [ByRef]Bolt11InvoiceStringDescription description, u32 expiry_secs, u64? max_lsp_fee_limit_msat);
138138
[Throws=NodeError]
139-
Bolt11Invoice receive_variable_amount_via_jit_channel([ByRef]string description, u32 expiry_secs, u64? max_proportional_lsp_fee_limit_ppm_msat);
139+
Bolt11Invoice receive_variable_amount_via_jit_channel([ByRef]Bolt11InvoiceStringDescription description, u32 expiry_secs, u64? max_proportional_lsp_fee_limit_ppm_msat);
140140
};
141141

142142
interface Bolt12Payment {

src/liquidity.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{Config, Error};
1212
use lightning::ln::channelmanager::MIN_FINAL_CLTV_EXPIRY_DELTA;
1313
use lightning::ln::msgs::SocketAddress;
1414
use lightning::routing::router::{RouteHint, RouteHintHop};
15-
use lightning_invoice::{Bolt11Invoice, InvoiceBuilder, RoutingFees};
15+
use lightning_invoice::{Bolt11Invoice, Bolt11InvoiceDescription, InvoiceBuilder, RoutingFees};
1616
use lightning_liquidity::events::Event;
1717
use lightning_liquidity::lsps0::ser::RequestId;
1818
use lightning_liquidity::lsps2::event::LSPS2ClientEvent;
@@ -196,7 +196,7 @@ where
196196
}
197197

198198
pub(crate) async fn lsps2_receive_to_jit_channel(
199-
&self, amount_msat: u64, description: &str, expiry_secs: u32,
199+
&self, amount_msat: u64, description: &Bolt11InvoiceDescription, expiry_secs: u32,
200200
max_total_lsp_fee_limit_msat: Option<u64>,
201201
) -> Result<(Bolt11Invoice, u64), Error> {
202202
let fee_response = self.lsps2_request_opening_fee_params().await?;
@@ -256,7 +256,7 @@ where
256256
}
257257

258258
pub(crate) async fn lsps2_receive_variable_amount_to_jit_channel(
259-
&self, description: &str, expiry_secs: u32,
259+
&self, description: &Bolt11InvoiceDescription, expiry_secs: u32,
260260
max_proportional_lsp_fee_limit_ppm_msat: Option<u64>,
261261
) -> Result<(Bolt11Invoice, u64), Error> {
262262
let fee_response = self.lsps2_request_opening_fee_params().await?;
@@ -373,8 +373,8 @@ where
373373
}
374374

375375
fn lsps2_create_jit_invoice(
376-
&self, buy_response: LSPS2BuyResponse, amount_msat: Option<u64>, description: &str,
377-
expiry_secs: u32,
376+
&self, buy_response: LSPS2BuyResponse, amount_msat: Option<u64>,
377+
description: &Bolt11InvoiceDescription, expiry_secs: u32,
378378
) -> Result<Bolt11Invoice, Error> {
379379
let lsps2_service = self.lsps2_service.as_ref().ok_or(Error::LiquiditySourceUnavailable)?;
380380

@@ -404,7 +404,7 @@ where
404404

405405
let currency = self.config.network.into();
406406
let mut invoice_builder = InvoiceBuilder::new(currency)
407-
.description(description.to_string())
407+
.invoice_description(description.clone())
408408
.payment_hash(payment_hash)
409409
.payment_secret(payment_secret)
410410
.current_timestamp()

src/payment/bolt11.rs

Lines changed: 63 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ use lightning::routing::router::{PaymentParameters, RouteParameters};
3030

3131
use lightning_types::payment::{PaymentHash, PaymentPreimage};
3232

33-
use lightning_invoice::{Bolt11Invoice, Bolt11InvoiceDescription, Description };
33+
use lightning_invoice::{Bolt11Invoice, Bolt11InvoiceDescription, Description};
3434

35-
use bitcoin::hashes::Hash;
3635
use bitcoin::hashes::sha256::Hash as Sha256;
36+
use bitcoin::hashes::Hash;
3737

38-
use std::sync::{Arc, RwLock};
3938
use std::str::FromStr;
39+
use std::sync::{Arc, RwLock};
4040

4141
/// A payment handler allowing to create and pay [BOLT 11] invoices.
4242
///
@@ -418,7 +418,6 @@ impl Bolt11Payment {
418418
let invoice_description: Bolt11InvoiceDescription = description.try_into()?;
419419
self.receive_inner(Some(amount_msat), &invoice_description, expiry_secs, None)
420420
}
421-
422421

423422
/// Returns a payable invoice that can be used to request a payment of the amount
424423
/// given for the given payment hash.
@@ -436,14 +435,16 @@ impl Bolt11Payment {
436435
/// [`fail_for_hash`]: Self::fail_for_hash
437436
#[cfg(not(feature = "uniffi"))]
438437
pub fn receive_for_hash(
439-
&self, amount_msat: u64, description: &Bolt11InvoiceDescription, expiry_secs: u32, payment_hash: PaymentHash,
438+
&self, amount_msat: u64, description: &Bolt11InvoiceDescription, expiry_secs: u32,
439+
payment_hash: PaymentHash,
440440
) -> Result<Bolt11Invoice, Error> {
441441
self.receive_inner(Some(amount_msat), description, expiry_secs, Some(payment_hash))
442442
}
443-
443+
444444
#[cfg(feature = "uniffi")]
445445
pub fn receive_for_hash(
446-
&self, amount_msat: u64, description: &Bolt11InvoiceStringDescription, expiry_secs: u32, payment_hash: PaymentHash,
446+
&self, amount_msat: u64, description: &Bolt11InvoiceStringDescription, expiry_secs: u32,
447+
payment_hash: PaymentHash,
447448
) -> Result<Bolt11Invoice, Error> {
448449
let invoice_description: Bolt11InvoiceDescription = description.try_into()?;
449450
self.receive_inner(Some(amount_msat), &invoice_description, expiry_secs, Some(payment_hash))
@@ -491,15 +492,16 @@ impl Bolt11Payment {
491492

492493
#[cfg(feature = "uniffi")]
493494
pub fn receive_variable_amount_for_hash(
494-
&self, description: &Bolt11InvoiceStringDescription, expiry_secs: u32, payment_hash: PaymentHash,
495+
&self, description: &Bolt11InvoiceStringDescription, expiry_secs: u32,
496+
payment_hash: PaymentHash,
495497
) -> Result<Bolt11Invoice, Error> {
496498
let invoice_description: Bolt11InvoiceDescription = description.try_into()?;
497499
self.receive_inner(None, &invoice_description, expiry_secs, Some(payment_hash))
498500
}
499501

500502
pub(crate) fn receive_inner(
501-
&self, amount_msat: Option<u64>, invoice_description: &Bolt11InvoiceDescription, expiry_secs: u32,
502-
manual_claim_payment_hash: Option<PaymentHash>,
503+
&self, amount_msat: Option<u64>, invoice_description: &Bolt11InvoiceDescription,
504+
expiry_secs: u32, manual_claim_payment_hash: Option<PaymentHash>,
503505
) -> Result<Bolt11Invoice, Error> {
504506
let invoice = {
505507
let invoice_params = Bolt11InvoiceParameters {
@@ -564,8 +566,9 @@ impl Bolt11Payment {
564566
/// channel to us. We'll use its cheapest offer otherwise.
565567
///
566568
/// [LSPS2]: https://github.com/BitcoinAndLightningLayerSpecs/lsp/blob/main/LSPS2/README.md
569+
#[cfg(not(feature = "uniffi"))]
567570
pub fn receive_via_jit_channel(
568-
&self, amount_msat: u64, description: &str, expiry_secs: u32,
571+
&self, amount_msat: u64, description: &Bolt11InvoiceDescription, expiry_secs: u32,
569572
max_total_lsp_fee_limit_msat: Option<u64>,
570573
) -> Result<Bolt11Invoice, Error> {
571574
self.receive_via_jit_channel_inner(
@@ -577,6 +580,21 @@ impl Bolt11Payment {
577580
)
578581
}
579582

583+
#[cfg(feature = "uniffi")]
584+
pub fn receive_via_jit_channel(
585+
&self, amount_msat: u64, description: &Bolt11InvoiceStringDescription, expiry_secs: u32,
586+
max_total_lsp_fee_limit_msat: Option<u64>,
587+
) -> Result<Bolt11Invoice, Error> {
588+
let invoice_description: Bolt11InvoiceDescription = description.try_into()?;
589+
self.receive_via_jit_channel_inner(
590+
Some(amount_msat),
591+
&invoice_description,
592+
expiry_secs,
593+
max_total_lsp_fee_limit_msat,
594+
None,
595+
)
596+
}
597+
580598
/// Returns a payable invoice that can be used to request a variable amount payment (also known
581599
/// as "zero-amount" invoice) and receive it via a newly created just-in-time (JIT) channel.
582600
///
@@ -588,8 +606,9 @@ impl Bolt11Payment {
588606
/// We'll use its cheapest offer otherwise.
589607
///
590608
/// [LSPS2]: https://github.com/BitcoinAndLightningLayerSpecs/lsp/blob/main/LSPS2/README.md
609+
#[cfg(not(feature = "uniffi"))]
591610
pub fn receive_variable_amount_via_jit_channel(
592-
&self, description: &str, expiry_secs: u32,
611+
&self, description: &Bolt11InvoiceDescription, expiry_secs: u32,
593612
max_proportional_lsp_fee_limit_ppm_msat: Option<u64>,
594613
) -> Result<Bolt11Invoice, Error> {
595614
self.receive_via_jit_channel_inner(
@@ -601,8 +620,23 @@ impl Bolt11Payment {
601620
)
602621
}
603622

623+
#[cfg(feature = "uniffi")]
624+
pub fn receive_variable_amount_via_jit_channel(
625+
&self, description: &Bolt11InvoiceStringDescription, expiry_secs: u32,
626+
max_proportional_lsp_fee_limit_ppm_msat: Option<u64>,
627+
) -> Result<Bolt11Invoice, Error> {
628+
let invoice_description: Bolt11InvoiceDescription = description.try_into()?;
629+
self.receive_via_jit_channel_inner(
630+
None,
631+
&invoice_description,
632+
expiry_secs,
633+
None,
634+
max_proportional_lsp_fee_limit_ppm_msat,
635+
)
636+
}
637+
604638
fn receive_via_jit_channel_inner(
605-
&self, amount_msat: Option<u64>, description: &str, expiry_secs: u32,
639+
&self, amount_msat: Option<u64>, description: &Bolt11InvoiceDescription, expiry_secs: u32,
606640
max_total_lsp_fee_limit_msat: Option<u64>,
607641
max_proportional_lsp_fee_limit_ppm_msat: Option<u64>,
608642
) -> Result<Bolt11Invoice, Error> {
@@ -777,30 +811,34 @@ impl Bolt11Payment {
777811

778812
/// Represents the description of an invoice which has to be either a directly included string or
779813
/// a hash of a description provided out of band.
780-
pub enum Bolt11InvoiceStringDescription { // use same name (no string)
814+
pub enum Bolt11InvoiceStringDescription {
815+
// use same name (no string)
781816
/// Contains a full description.
782-
Direct{
817+
Direct {
783818
/// Description of what the invoice is for
784-
description: String
819+
description: String,
785820
},
786821
/// Contains a hash.
787-
Hash{
822+
Hash {
788823
/// Hash of the description of what the invoice is for
789-
hash: String
790-
}
824+
hash: String,
825+
},
791826
}
792827

793828
impl TryInto<Bolt11InvoiceDescription> for &Bolt11InvoiceStringDescription {
794829
type Error = Error;
795830

796831
fn try_into(self) -> Result<Bolt11InvoiceDescription, Self::Error> {
797832
match self {
798-
Bolt11InvoiceStringDescription::Direct{description} => {
799-
Description::new(description.clone()).map(Bolt11InvoiceDescription::Direct).map_err(|_| Error::InvalidInvoice)
800-
},
801-
Bolt11InvoiceStringDescription::Hash{hash} => {
802-
Sha256::from_str(&hash).map(lightning_invoice::Sha256).map(Bolt11InvoiceDescription::Hash).map_err(|_| Error::InvalidInvoice)
833+
Bolt11InvoiceStringDescription::Direct { description } => {
834+
Description::new(description.clone())
835+
.map(Bolt11InvoiceDescription::Direct)
836+
.map_err(|_| Error::InvalidInvoice)
803837
},
838+
Bolt11InvoiceStringDescription::Hash { hash } => Sha256::from_str(&hash)
839+
.map(lightning_invoice::Sha256)
840+
.map(Bolt11InvoiceDescription::Hash)
841+
.map_err(|_| Error::InvalidInvoice),
804842
}
805843
}
806-
}
844+
}

src/payment/mod.rs

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

17+
pub use bolt11::Bolt11InvoiceStringDescription;
1718
pub use bolt11::Bolt11Payment;
1819
pub use bolt12::Bolt12Payment;
1920
pub use onchain::OnchainPayment;
2021
pub use spontaneous::SpontaneousPayment;
2122
pub use store::{LSPFeeLimits, PaymentDetails, PaymentDirection, PaymentKind, PaymentStatus};
2223
pub use unified_qr::{QrPaymentResult, UnifiedQrPayment};
23-
pub use bolt11::Bolt11InvoiceStringDescription;
2424

2525
/// Represents information used to send a payment.
2626
#[derive(Clone, Debug, PartialEq)]

src/payment/unified_qr.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,18 @@ impl UnifiedQrPayment {
102102
let invoice_description = Bolt11InvoiceDescription::Direct(
103103
Description::new(description.to_string()).map_err(|_| Error::InvoiceCreationFailed)?,
104104
);
105-
let bolt11_invoice =
106-
match self.bolt11_invoice.receive_inner(Some(amount_msats), &invoice_description, expiry_sec, None) {
107-
Ok(invoice) => Some(invoice),
108-
Err(e) => {
109-
log_error!(self.logger, "Failed to create invoice {}", e);
110-
return Err(Error::InvoiceCreationFailed);
111-
},
112-
};
105+
let bolt11_invoice = match self.bolt11_invoice.receive_inner(
106+
Some(amount_msats),
107+
&invoice_description,
108+
expiry_sec,
109+
None,
110+
) {
111+
Ok(invoice) => Some(invoice),
112+
Err(e) => {
113+
log_error!(self.logger, "Failed to create invoice {}", e);
114+
return Err(Error::InvoiceCreationFailed);
115+
},
116+
};
113117

114118
let extras = Extras { bolt11_invoice, bolt12_offer };
115119

tests/common/mod.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -553,8 +553,10 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
553553

554554
println!("\nB receive");
555555
let invoice_amount_1_msat = 2500_000;
556-
let invoice_description: Bolt11InvoiceDescription = Bolt11InvoiceDescription::Direct(Description::new(String::from("asdf")).unwrap());
557-
let invoice = node_b.bolt11_payment().receive(invoice_amount_1_msat, &invoice_description, 9217).unwrap();
556+
let invoice_description: Bolt11InvoiceDescription =
557+
Bolt11InvoiceDescription::Direct(Description::new(String::from("asdf")).unwrap());
558+
let invoice =
559+
node_b.bolt11_payment().receive(invoice_amount_1_msat, &invoice_description, 9217).unwrap();
558560

559561
println!("\nA send");
560562
let payment_id = node_a.bolt11_payment().send(&invoice, None).unwrap();
@@ -600,7 +602,8 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
600602

601603
// Test under-/overpayment
602604
let invoice_amount_2_msat = 2500_000;
603-
let invoice = node_b.bolt11_payment().receive(invoice_amount_2_msat, &invoice_description, 9217).unwrap();
605+
let invoice =
606+
node_b.bolt11_payment().receive(invoice_amount_2_msat, &invoice_description, 9217).unwrap();
604607

605608
let underpaid_amount = invoice_amount_2_msat - 1;
606609
assert_eq!(
@@ -609,7 +612,8 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
609612
);
610613

611614
println!("\nB overpaid receive");
612-
let invoice = node_b.bolt11_payment().receive(invoice_amount_2_msat, &invoice_description, 9217).unwrap();
615+
let invoice =
616+
node_b.bolt11_payment().receive(invoice_amount_2_msat, &invoice_description, 9217).unwrap();
613617
let overpaid_amount_msat = invoice_amount_2_msat + 100;
614618

615619
println!("\nA overpaid send");
@@ -716,7 +720,12 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
716720
PaymentHash(Sha256::hash(&manual_fail_preimage.0).to_byte_array());
717721
let manual_fail_invoice = node_b
718722
.bolt11_payment()
719-
.receive_for_hash(invoice_amount_3_msat, &invoice_description, 9217, manual_fail_payment_hash)
723+
.receive_for_hash(
724+
invoice_amount_3_msat,
725+
&invoice_description,
726+
9217,
727+
manual_fail_payment_hash,
728+
)
720729
.unwrap();
721730
let manual_fail_payment_id = node_a.bolt11_payment().send(&manual_fail_invoice, None).unwrap();
722731

tests/integration_tests_rust.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ fn multi_hop_sending() {
190190
max_channel_saturation_power_of_half: Some(2),
191191
};
192192

193-
let invoice_description = Bolt11InvoiceDescription::Direct(Description::new(String::from("asdf")).unwrap());
193+
let invoice_description =
194+
Bolt11InvoiceDescription::Direct(Description::new(String::from("asdf")).unwrap());
194195
let invoice = nodes[4].bolt11_payment().receive(2_500_000, &invoice_description, 9217).unwrap();
195196
nodes[0].bolt11_payment().send(&invoice, Some(sending_params)).unwrap();
196197

0 commit comments

Comments
 (0)