diff --git a/bindings/cashu-ffi/src/cashu.udl b/bindings/cashu-ffi/src/cashu.udl index 75d4ecc3..7d3e6e51 100644 --- a/bindings/cashu-ffi/src/cashu.udl +++ b/bindings/cashu-ffi/src/cashu.udl @@ -5,6 +5,13 @@ interface CashuError { Generic(string err); }; +interface Bolt11Invoice { + [Throws=CashuError] + constructor(string bolt11); + string as_string(); + Amount? amount(); +}; + interface Amount { u64 to_sat(); u64 to_msat(); diff --git a/bindings/cashu-ffi/src/lib.rs b/bindings/cashu-ffi/src/lib.rs index b360bfdf..4226b912 100644 --- a/bindings/cashu-ffi/src/lib.rs +++ b/bindings/cashu-ffi/src/lib.rs @@ -26,6 +26,7 @@ mod ffi { pub use crate::nuts::nut08::{MeltRequest, MeltResponse}; pub use crate::nuts::nut09::{MintInfo, MintVersion}; pub use crate::types::amount::Amount; + pub use crate::types::Bolt11Invoice; pub use cashu::types::InvoiceStatus; // UDL diff --git a/bindings/cashu-ffi/src/types/bolt11_invoice.rs b/bindings/cashu-ffi/src/types/bolt11_invoice.rs new file mode 100644 index 00000000..0b692034 --- /dev/null +++ b/bindings/cashu-ffi/src/types/bolt11_invoice.rs @@ -0,0 +1,34 @@ +use std::{ops::Deref, str::FromStr, sync::Arc}; + +use cashu::Bolt11Invoice as Bolt11InvoiceSdk; + +use crate::{error::Result, Amount}; + +pub struct Bolt11Invoice { + inner: Bolt11InvoiceSdk, +} + +impl Deref for Bolt11Invoice { + type Target = Bolt11InvoiceSdk; + fn deref(&self) -> &Self::Target { + &self.inner + } +} + +impl Bolt11Invoice { + pub fn new(bolt11: String) -> Result { + Ok(Self { + inner: Bolt11InvoiceSdk::from_str(&bolt11)?, + }) + } + + pub fn as_string(&self) -> String { + self.inner.to_string() + } + + pub fn amount(&self) -> Option> { + self.inner + .amount_milli_satoshis() + .map(|a| Arc::new(Amount::from_msat(a))) + } +} diff --git a/bindings/cashu-ffi/src/types/mod.rs b/bindings/cashu-ffi/src/types/mod.rs index 4e3e060e..03a97470 100644 --- a/bindings/cashu-ffi/src/types/mod.rs +++ b/bindings/cashu-ffi/src/types/mod.rs @@ -1 +1,4 @@ pub mod amount; +pub mod bolt11_invoice; + +pub use bolt11_invoice::Bolt11Invoice;