Skip to content

Commit

Permalink
rename uniffi type
Browse files Browse the repository at this point in the history
  • Loading branch information
joostjager committed Jan 23, 2025
1 parent aa2bced commit 0a5baa4
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ class LibraryTest {
else -> return
}

val description = Bolt11InvoiceStringDescription.Direct("asdf")
val description = Bolt11InvoiceDescription.Direct("asdf")
val invoice = node2.bolt11Payment().receive(2500000u, description, 9217u)

node1.bolt11Payment().send(invoice, null)
Expand Down
14 changes: 7 additions & 7 deletions bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ interface Node {
};

[Enum]
interface Bolt11InvoiceStringDescription {
interface Bolt11InvoiceDescription {
Hash(string hash);
Direct(string description);
};
Expand All @@ -126,17 +126,17 @@ interface Bolt11Payment {
[Throws=NodeError]
void fail_for_hash(PaymentHash payment_hash);
[Throws=NodeError]
Bolt11Invoice receive(u64 amount_msat, [ByRef]Bolt11InvoiceStringDescription description, u32 expiry_secs);
Bolt11Invoice receive(u64 amount_msat, [ByRef]Bolt11InvoiceDescription description, u32 expiry_secs);
[Throws=NodeError]
Bolt11Invoice receive_for_hash(u64 amount_msat, [ByRef]Bolt11InvoiceStringDescription description, u32 expiry_secs, PaymentHash payment_hash);
Bolt11Invoice receive_for_hash(u64 amount_msat, [ByRef]Bolt11InvoiceDescription description, u32 expiry_secs, PaymentHash payment_hash);
[Throws=NodeError]
Bolt11Invoice receive_variable_amount([ByRef]Bolt11InvoiceStringDescription description, u32 expiry_secs);
Bolt11Invoice receive_variable_amount([ByRef]Bolt11InvoiceDescription description, u32 expiry_secs);
[Throws=NodeError]
Bolt11Invoice receive_variable_amount_for_hash([ByRef]Bolt11InvoiceStringDescription description, u32 expiry_secs, PaymentHash payment_hash);
Bolt11Invoice receive_variable_amount_for_hash([ByRef]Bolt11InvoiceDescription description, u32 expiry_secs, PaymentHash payment_hash);
[Throws=NodeError]
Bolt11Invoice receive_via_jit_channel(u64 amount_msat, [ByRef]Bolt11InvoiceStringDescription description, u32 expiry_secs, u64? max_lsp_fee_limit_msat);
Bolt11Invoice receive_via_jit_channel(u64 amount_msat, [ByRef]Bolt11InvoiceDescription description, u32 expiry_secs, u64? max_lsp_fee_limit_msat);
[Throws=NodeError]
Bolt11Invoice receive_variable_amount_via_jit_channel([ByRef]Bolt11InvoiceStringDescription description, u32 expiry_secs, u64? max_proportional_lsp_fee_limit_ppm_msat);
Bolt11Invoice receive_variable_amount_via_jit_channel([ByRef]Bolt11InvoiceDescription description, u32 expiry_secs, u64? max_proportional_lsp_fee_limit_ppm_msat);
};

interface Bolt12Payment {
Expand Down
69 changes: 34 additions & 35 deletions src/payment/bolt11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use lightning::routing::router::{PaymentParameters, RouteParameters};

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

use lightning_invoice::{Bolt11Invoice, Bolt11InvoiceDescription, Description};
use lightning_invoice::{Bolt11Invoice, Description};

use bitcoin::hashes::sha256::Hash as Sha256;
use bitcoin::hashes::Hash;
Expand Down Expand Up @@ -407,16 +407,16 @@ impl Bolt11Payment {
/// The inbound payment will be automatically claimed upon arrival.
#[cfg(not(feature = "uniffi"))]
pub fn receive(
&self, amount_msat: u64, description: &Bolt11InvoiceDescription, expiry_secs: u32,
&self, amount_msat: u64, description: &lightning_invoice::Bolt11InvoiceDescription, expiry_secs: u32,
) -> Result<Bolt11Invoice, Error> {
self.receive_inner(Some(amount_msat), description, expiry_secs, None)
}

#[cfg(feature = "uniffi")]
pub fn receive(
&self, amount_msat: u64, description: &Bolt11InvoiceStringDescription, expiry_secs: u32,
&self, amount_msat: u64, description: &Bolt11InvoiceDescription, expiry_secs: u32,
) -> Result<Bolt11Invoice, Error> {
let invoice_description = Bolt11InvoiceDescription::try_from(description)?;
let invoice_description = lightning_invoice::Bolt11InvoiceDescription::try_from(description)?;
self.receive_inner(Some(amount_msat), &invoice_description, expiry_secs, None)
}

Expand All @@ -436,18 +436,18 @@ impl Bolt11Payment {
/// [`fail_for_hash`]: Self::fail_for_hash
#[cfg(not(feature = "uniffi"))]
pub fn receive_for_hash(
&self, amount_msat: u64, description: &Bolt11InvoiceDescription, expiry_secs: u32,
&self, amount_msat: u64, description: &lightning_invoice::Bolt11InvoiceDescription, expiry_secs: u32,
payment_hash: PaymentHash,
) -> Result<Bolt11Invoice, Error> {
self.receive_inner(Some(amount_msat), description, expiry_secs, Some(payment_hash))
}

#[cfg(feature = "uniffi")]
pub fn receive_for_hash(
&self, amount_msat: u64, description: &Bolt11InvoiceStringDescription, expiry_secs: u32,
&self, amount_msat: u64, description: &Bolt11InvoiceDescription, expiry_secs: u32,
payment_hash: PaymentHash,
) -> Result<Bolt11Invoice, Error> {
let invoice_description = Bolt11InvoiceDescription::try_from(description)?;
let invoice_description = lightning_invoice::Bolt11InvoiceDescription::try_from(description)?;
self.receive_inner(Some(amount_msat), &invoice_description, expiry_secs, Some(payment_hash))
}

Expand All @@ -457,16 +457,16 @@ impl Bolt11Payment {
/// The inbound payment will be automatically claimed upon arrival.
#[cfg(not(feature = "uniffi"))]
pub fn receive_variable_amount(
&self, description: &Bolt11InvoiceDescription, expiry_secs: u32,
&self, description: &lightning_invoice::Bolt11InvoiceDescription, expiry_secs: u32,
) -> Result<Bolt11Invoice, Error> {
self.receive_inner(None, description, expiry_secs, None)
}

#[cfg(feature = "uniffi")]
pub fn receive_variable_amount(
&self, description: &Bolt11InvoiceStringDescription, expiry_secs: u32,
&self, description: &Bolt11InvoiceDescription, expiry_secs: u32,
) -> Result<Bolt11Invoice, Error> {
let invoice_description = Bolt11InvoiceDescription::try_from(description)?;
let invoice_description = lightning_invoice::Bolt11InvoiceDescription::try_from(description)?;
self.receive_inner(None, &invoice_description, expiry_secs, None)
}

Expand All @@ -486,22 +486,22 @@ impl Bolt11Payment {
/// [`fail_for_hash`]: Self::fail_for_hash
#[cfg(not(feature = "uniffi"))]
pub fn receive_variable_amount_for_hash(
&self, description: &Bolt11InvoiceDescription, expiry_secs: u32, payment_hash: PaymentHash,
&self, description: &lightning_invoice::Bolt11InvoiceDescription, expiry_secs: u32, payment_hash: PaymentHash,
) -> Result<Bolt11Invoice, Error> {
self.receive_inner(None, description, expiry_secs, Some(payment_hash))
}

#[cfg(feature = "uniffi")]
pub fn receive_variable_amount_for_hash(
&self, description: &Bolt11InvoiceStringDescription, expiry_secs: u32,
&self, description: &Bolt11InvoiceDescription, expiry_secs: u32,
payment_hash: PaymentHash,
) -> Result<Bolt11Invoice, Error> {
let invoice_description = Bolt11InvoiceDescription::try_from(description)?;
let invoice_description = lightning_invoice::Bolt11InvoiceDescription::try_from(description)?;
self.receive_inner(None, &invoice_description, expiry_secs, Some(payment_hash))
}

pub(crate) fn receive_inner(
&self, amount_msat: Option<u64>, invoice_description: &Bolt11InvoiceDescription,
&self, amount_msat: Option<u64>, invoice_description: &lightning_invoice::Bolt11InvoiceDescription,
expiry_secs: u32, manual_claim_payment_hash: Option<PaymentHash>,
) -> Result<Bolt11Invoice, Error> {
let invoice = {
Expand Down Expand Up @@ -569,7 +569,7 @@ impl Bolt11Payment {
/// [LSPS2]: https://github.com/BitcoinAndLightningLayerSpecs/lsp/blob/main/LSPS2/README.md
#[cfg(not(feature = "uniffi"))]
pub fn receive_via_jit_channel(
&self, amount_msat: u64, description: &Bolt11InvoiceDescription, expiry_secs: u32,
&self, amount_msat: u64, description: &lightning_invoice::Bolt11InvoiceDescription, expiry_secs: u32,
max_total_lsp_fee_limit_msat: Option<u64>,
) -> Result<Bolt11Invoice, Error> {
self.receive_via_jit_channel_inner(
Expand All @@ -583,10 +583,10 @@ impl Bolt11Payment {

#[cfg(feature = "uniffi")]
pub fn receive_via_jit_channel(
&self, amount_msat: u64, description: &Bolt11InvoiceStringDescription, expiry_secs: u32,
&self, amount_msat: u64, description: &Bolt11InvoiceDescription, expiry_secs: u32,
max_total_lsp_fee_limit_msat: Option<u64>,
) -> Result<Bolt11Invoice, Error> {
let invoice_description = Bolt11InvoiceDescription::try_from(description)?;
let invoice_description = lightning_invoice::Bolt11InvoiceDescription::try_from(description)?;
self.receive_via_jit_channel_inner(
Some(amount_msat),
&invoice_description,
Expand All @@ -609,7 +609,7 @@ impl Bolt11Payment {
/// [LSPS2]: https://github.com/BitcoinAndLightningLayerSpecs/lsp/blob/main/LSPS2/README.md
#[cfg(not(feature = "uniffi"))]
pub fn receive_variable_amount_via_jit_channel(
&self, description: &Bolt11InvoiceDescription, expiry_secs: u32,
&self, description: &lightning_invoice::Bolt11InvoiceDescription, expiry_secs: u32,
max_proportional_lsp_fee_limit_ppm_msat: Option<u64>,
) -> Result<Bolt11Invoice, Error> {
self.receive_via_jit_channel_inner(
Expand All @@ -623,10 +623,10 @@ impl Bolt11Payment {

#[cfg(feature = "uniffi")]
pub fn receive_variable_amount_via_jit_channel(
&self, description: &Bolt11InvoiceStringDescription, expiry_secs: u32,
&self, description: &Bolt11InvoiceDescription, expiry_secs: u32,
max_proportional_lsp_fee_limit_ppm_msat: Option<u64>,
) -> Result<Bolt11Invoice, Error> {
let invoice_description = Bolt11InvoiceDescription::try_from(description)?;
let invoice_description = lightning_invoice::Bolt11InvoiceDescription::try_from(description)?;
self.receive_via_jit_channel_inner(
None,
&invoice_description,
Expand All @@ -637,7 +637,7 @@ impl Bolt11Payment {
}

fn receive_via_jit_channel_inner(
&self, amount_msat: Option<u64>, description: &Bolt11InvoiceDescription, expiry_secs: u32,
&self, amount_msat: Option<u64>, description: &lightning_invoice::Bolt11InvoiceDescription, expiry_secs: u32,
max_total_lsp_fee_limit_msat: Option<u64>,
max_proportional_lsp_fee_limit_ppm_msat: Option<u64>,
) -> Result<Bolt11Invoice, Error> {
Expand Down Expand Up @@ -812,8 +812,7 @@ impl Bolt11Payment {

/// Represents the description of an invoice which has to be either a directly included string or
/// a hash of a description provided out of band.
pub enum Bolt11InvoiceStringDescription {
// use same name (no string)
pub enum Bolt11InvoiceDescription {
/// Contains a full description.
Direct {
/// Description of what the invoice is for
Expand All @@ -826,32 +825,32 @@ pub enum Bolt11InvoiceStringDescription {
},
}

impl TryFrom<&Bolt11InvoiceStringDescription> for Bolt11InvoiceDescription {
impl TryFrom<&Bolt11InvoiceDescription> for lightning_invoice::Bolt11InvoiceDescription {
type Error = Error;

fn try_from(value: &Bolt11InvoiceStringDescription) -> Result<Self, Self::Error> {
fn try_from(value: &Bolt11InvoiceDescription) -> Result<Self, Self::Error> {
match value {
Bolt11InvoiceStringDescription::Direct { description } => {
Bolt11InvoiceDescription::Direct { description } => {
Description::new(description.clone())
.map(Bolt11InvoiceDescription::Direct)
.map(lightning_invoice::Bolt11InvoiceDescription::Direct)
.map_err(|_| Error::InvoiceCreationFailed)
},
Bolt11InvoiceStringDescription::Hash { hash } => Sha256::from_str(&hash)
Bolt11InvoiceDescription::Hash { hash } => Sha256::from_str(&hash)
.map(lightning_invoice::Sha256)
.map(Bolt11InvoiceDescription::Hash)
.map(lightning_invoice::Bolt11InvoiceDescription::Hash)
.map_err(|_| Error::InvoiceCreationFailed),
}
}
}

impl From<Bolt11InvoiceDescription> for Bolt11InvoiceStringDescription {
fn from(value: Bolt11InvoiceDescription) -> Self {
impl From<lightning_invoice::Bolt11InvoiceDescription> for Bolt11InvoiceDescription {
fn from(value: lightning_invoice::Bolt11InvoiceDescription) -> Self {
match value {
Bolt11InvoiceDescription::Direct(description) => {
Bolt11InvoiceStringDescription::Direct { description: description.to_string() }
lightning_invoice::Bolt11InvoiceDescription::Direct(description) => {
Bolt11InvoiceDescription::Direct { description: description.to_string() }
},
Bolt11InvoiceDescription::Hash(hash) => {
Bolt11InvoiceStringDescription::Hash { hash: hex_utils::to_string(hash.0.as_ref()) }
lightning_invoice::Bolt11InvoiceDescription::Hash(hash) => {
Bolt11InvoiceDescription::Hash { hash: hex_utils::to_string(hash.0.as_ref()) }
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/payment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod spontaneous;
pub(crate) mod store;
mod unified_qr;

pub use bolt11::Bolt11InvoiceStringDescription;
pub use bolt11::Bolt11InvoiceDescription;
pub use bolt11::Bolt11Payment;
pub use bolt12::Bolt12Payment;
pub use onchain::OnchainPayment;
Expand Down
2 changes: 1 addition & 1 deletion src/uniffi_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub use bip39::Mnemonic;

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

pub use crate::payment::Bolt11InvoiceStringDescription;
pub use crate::payment::Bolt11InvoiceDescription;

use crate::UniffiCustomTypeConverter;

Expand Down

0 comments on commit 0a5baa4

Please sign in to comment.