Browse Source

`bindings/cashu` `Bolt11Invoice` ffi type

thesimplekid 1 year ago
parent
commit
8940afac9e

+ 7 - 0
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();

+ 1 - 0
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

+ 34 - 0
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<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)))
+    }
+}

+ 3 - 0
bindings/cashu-ffi/src/types/mod.rs

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