Explorar o código

refactor: ffi for v1 mint

thesimplekid hai 1 ano
pai
achega
40f6bc6bd9

+ 4 - 1
bindings/cashu-ffi/src/cashu.udl

@@ -31,8 +31,11 @@ interface Secret {
 	sequence<u8> as_bytes();	
 };
 
-// NUT00
+interface MintQuoteInfo {
+	constructor(string id, Amount amount, string unit, Bolt11Invoice? request, boolean paid, u64 boolean);
+};
 
+// NUT00
 
 interface PublicKey {
     [Throws=CashuError, Name=from_hex]

+ 1 - 2
bindings/cashu-ffi/src/lib.rs

@@ -29,8 +29,7 @@ mod ffi {
     pub use crate::nuts::nut06::{MintInfo, MintVersion};
     pub use crate::nuts::nut07::{CheckSpendableRequest, CheckSpendableResponse};
     pub use crate::nuts::nut08::{MeltBolt11Request, MeltBolt11Response};
-    pub use crate::types::amount::Amount;
-    pub use crate::types::{Bolt11Invoice, KeySetInfo, Secret};
+    pub use crate::types::{Amount, Bolt11Invoice, KeySetInfo, MintQuoteInfo, Secret};
 
     // UDL
     uniffi::include_scaffolding!("cashu");

+ 47 - 0
bindings/cashu-ffi/src/types/mint_quote_info.rs

@@ -0,0 +1,47 @@
+use std::ops::Deref;
+use std::str::FromStr;
+use std::sync::Arc;
+
+use cashu::nuts::CurrencyUnit;
+use cashu::types::MintQuoteInfo as MintQuoteInfoSdk;
+
+use crate::{Amount, Bolt11Invoice};
+
+pub struct MintQuoteInfo {
+    inner: MintQuoteInfoSdk,
+}
+
+impl Deref for MintQuoteInfo {
+    type Target = MintQuoteInfoSdk;
+    fn deref(&self) -> &Self::Target {
+        &self.inner
+    }
+}
+
+impl From<MintQuoteInfoSdk> for MintQuoteInfo {
+    fn from(inner: MintQuoteInfoSdk) -> MintQuoteInfo {
+        MintQuoteInfo { inner }
+    }
+}
+
+impl MintQuoteInfo {
+    pub fn new(
+        id: String,
+        amount: Arc<Amount>,
+        unit: String,
+        request: Option<Arc<Bolt11Invoice>>,
+        paid: bool,
+        expiry: u64,
+    ) -> Self {
+        Self {
+            inner: MintQuoteInfoSdk {
+                id,
+                amount: amount.as_ref().deref().clone(),
+                unit: CurrencyUnit::from_str(&unit).unwrap(),
+                request: request.map(|r| r.as_ref().deref().clone()),
+                paid,
+                expiry,
+            },
+        }
+    }
+}

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

@@ -1,8 +1,11 @@
 pub mod amount;
 pub mod bolt11_invoice;
 pub mod keyset_info;
+pub mod mint_quote_info;
 pub mod secret;
 
+pub use amount::Amount;
 pub use bolt11_invoice::Bolt11Invoice;
 pub use keyset_info::KeySetInfo;
+pub use mint_quote_info::MintQuoteInfo;
 pub use secret::Secret;

+ 6 - 3
bindings/cashu-sdk-ffi/src/cashu_sdk.udl

@@ -34,8 +34,11 @@ interface Secret {
 	sequence<u8> as_bytes();	
 };
 
-// NUT00
+interface MintQuoteInfo {
+	constructor(string id, Amount amount, string unit, Bolt11Invoice? request, boolean paid, u64 boolean);
+};
 
+// NUT00
 
 interface PublicKey {
     [Throws=CashuError, Name=from_hex]
@@ -299,9 +302,9 @@ interface Wallet {
 	// [Throws=CashuSdkError]
 	// ProofsStatus check_proofs_spent(sequence<Proof> proofs);
     [Throws=CashuSdkError]
-	Token mint_token(Amount amount, string hash,CurrencyUnit? unit, string? memo);
+	Token mint_token(Amount amount, CurrencyUnit? unit, string? memo);
     [Throws=CashuSdkError]
-	sequence<Proof> mint(Amount amount, string hash);
+	sequence<Proof> mint(string quote);
     [Throws=CashuSdkError]
 	sequence<Proof> receive(string encoded_token);
     [Throws=CashuSdkError]

+ 3 - 2
bindings/cashu-sdk-ffi/src/lib.rs

@@ -10,8 +10,9 @@ mod ffi {
         KeySetResponse, Keys, KeysResponse, MeltBolt11Request, MeltBolt11Response,
         MeltQuoteBolt11Request, MeltQuoteBolt11Response, MintBolt11Request, MintBolt11Response,
         MintInfo, MintKeySet, MintProof, MintProofs, MintQuoteBolt11Request,
-        MintQuoteBolt11Response, MintVersion, Nut05MeltBolt11Request, Nut05MeltBolt11Response,
-        PreMintSecrets, Proof, PublicKey, Secret, SecretKey, SwapRequest, SwapResponse, Token,
+        MintQuoteBolt11Response, MintQuoteInfo, MintVersion, Nut05MeltBolt11Request,
+        Nut05MeltBolt11Response, PreMintSecrets, Proof, PublicKey, Secret, SecretKey, SwapRequest,
+        SwapResponse, Token,
     };
 
     pub use crate::error::CashuSdkError;

+ 26 - 17
bindings/cashu-sdk-ffi/src/wallet.rs

@@ -1,7 +1,7 @@
 use std::ops::Deref;
-use std::sync::Arc;
+use std::sync::{Arc, RwLock};
 
-use cashu_ffi::{BlindedSignature, CurrencyUnit, PreMintSecrets, Proof, Token};
+use cashu_ffi::{BlindedSignature, CurrencyUnit, MintQuoteInfo, PreMintSecrets, Proof, Token};
 use cashu_sdk::client::minreq_client::HttpClient;
 use cashu_sdk::types::ProofsStatus;
 use cashu_sdk::url::UncheckedUrl;
@@ -16,24 +16,31 @@ use crate::{Amount, Keys};
 static RUNTIME: Lazy<Runtime> = Lazy::new(|| Runtime::new().expect("Can't start Tokio runtime"));
 
 pub struct Wallet {
-    inner: WalletSdk<HttpClient>,
+    inner: RwLock<WalletSdk<HttpClient>>,
 }
 
 impl Wallet {
-    pub fn new(mint_url: &str, mint_keys: Arc<Keys>) -> Self {
+    pub fn new(mint_url: &str, mint_keys: Arc<Keys>, quotes: Vec<Arc<MintQuoteInfo>>) -> Self {
         let client = HttpClient {};
         Self {
             inner: WalletSdk::new(
                 client,
                 UncheckedUrl::new(mint_url),
+                quotes
+                    .into_iter()
+                    .map(|q| q.as_ref().deref().clone())
+                    .collect(),
                 mint_keys.as_ref().deref().clone(),
-            ),
+            )
+            .into(),
         }
     }
 
     pub fn check_proofs_spent(&self, proofs: Vec<Arc<Proof>>) -> Result<Arc<ProofsStatus>> {
         let proofs = RUNTIME.block_on(async {
             self.inner
+                .read()
+                .unwrap()
                 .check_proofs_spent(proofs.iter().map(|p| p.as_ref().deref().clone()).collect())
                 .await
         })?;
@@ -44,33 +51,29 @@ impl Wallet {
     pub fn mint_token(
         &self,
         amount: Arc<Amount>,
-        hash: String,
         unit: Option<CurrencyUnit>,
         memo: Option<String>,
     ) -> Result<Arc<Token>> {
         let token = RUNTIME.block_on(async {
             self.inner
-                .mint_token(
-                    *amount.as_ref().deref(),
-                    &hash,
-                    memo,
-                    unit.map(|u| u.into()),
-                )
+                .write()
+                .unwrap()
+                .mint_token(*amount.as_ref().deref(), memo, unit.map(|u| u.into()))
                 .await
         })?;
 
         Ok(Arc::new(token.into()))
     }
 
-    pub fn mint(&self, amount: Arc<Amount>, hash: String) -> Result<Vec<Arc<Proof>>> {
-        let proofs =
-            RUNTIME.block_on(async { self.inner.mint(*amount.as_ref().deref(), &hash).await })?;
+    pub fn mint(&self, quote: String) -> Result<Vec<Arc<Proof>>> {
+        let proofs = RUNTIME.block_on(async { self.inner.write().unwrap().mint(&quote).await })?;
 
         Ok(proofs.into_iter().map(|p| Arc::new(p.into())).collect())
     }
 
     pub fn receive(&self, encoded_token: String) -> Result<Vec<Arc<Proof>>> {
-        let proofs = RUNTIME.block_on(async { self.inner.receive(&encoded_token).await })?;
+        let proofs = RUNTIME
+            .block_on(async { self.inner.write().unwrap().receive(&encoded_token).await })?;
 
         Ok(proofs.into_iter().map(|p| Arc::new(p.into())).collect())
     }
@@ -82,6 +85,8 @@ impl Wallet {
     ) -> Result<Vec<Arc<Proof>>> {
         Ok(self
             .inner
+            .read()
+            .unwrap()
             .process_split_response(
                 blinded_messages.as_ref().deref().clone(),
                 promises.iter().map(|p| p.as_ref().into()).collect(),
@@ -94,6 +99,8 @@ impl Wallet {
     pub fn send(&self, amount: Arc<Amount>, proofs: Vec<Arc<Proof>>) -> Result<Arc<SendProofs>> {
         let send_proofs = RUNTIME.block_on(async {
             self.inner
+                .read()
+                .unwrap()
                 .send(
                     *amount.as_ref().deref(),
                     proofs.iter().map(|p| p.as_ref().deref().clone()).collect(),
@@ -112,6 +119,8 @@ impl Wallet {
     ) -> Result<Arc<Melted>> {
         let melted = RUNTIME.block_on(async {
             self.inner
+                .write()
+                .unwrap()
                 .melt(
                     quote,
                     proofs.iter().map(|p| p.as_ref().deref().clone()).collect(),
@@ -129,7 +138,7 @@ impl Wallet {
         unit: Option<CurrencyUnit>,
         memo: Option<String>,
     ) -> Result<String> {
-        Ok(self.inner.proofs_to_token(
+        Ok(self.inner.read().unwrap().proofs_to_token(
             proofs.iter().map(|p| p.as_ref().deref().clone()).collect(),
             memo,
             unit.map(|u| u.into()),