bindings/cashu Bolt11Invoice ffi type

This commit is contained in:
thesimplekid
2023-08-31 09:06:05 +01:00
parent c21dc01f13
commit 8940afac9e
4 changed files with 45 additions and 0 deletions

View File

@@ -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();

View File

@@ -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

View File

@@ -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<Self> {
Ok(Self {
inner: Bolt11InvoiceSdk::from_str(&bolt11)?,
})
}
pub fn as_string(&self) -> String {
self.inner.to_string()
}
pub fn amount(&self) -> Option<Arc<Amount>> {
self.inner
.amount_milli_satoshis()
.map(|a| Arc::new(Amount::from_msat(a)))
}
}

View File

@@ -1 +1,4 @@
pub mod amount;
pub mod bolt11_invoice;
pub use bolt11_invoice::Bolt11Invoice;