From eb81cf80308cfa61846b1ee83cf5e9f094651a71 Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Mon, 1 Jan 2024 12:47:15 +0000 Subject: [PATCH] fix: cashu-sdk-js --- bindings/cashu-sdk-js/src/wallet.rs | 61 ++++++++++++++++++++--------- crates/cashu-sdk/src/wallet.rs | 10 +++++ 2 files changed, 53 insertions(+), 18 deletions(-) diff --git a/bindings/cashu-sdk-js/src/wallet.rs b/bindings/cashu-sdk-js/src/wallet.rs index 30df3929..df802aaa 100644 --- a/bindings/cashu-sdk-js/src/wallet.rs +++ b/bindings/cashu-sdk-js/src/wallet.rs @@ -1,3 +1,4 @@ +use std::collections::HashMap; use std::ops::Deref; use std::str::FromStr; @@ -7,7 +8,8 @@ use cashu_js::JsAmount; #[cfg(feature = "nut07")] use cashu_js::JsProofsStatus; use cashu_sdk::client::gloo_client::HttpClient; -use cashu_sdk::nuts::CurrencyUnit; +use cashu_sdk::nuts::{CurrencyUnit, Id}; +use cashu_sdk::url::UncheckedUrl; use cashu_sdk::wallet::Wallet; use wasm_bindgen::prelude::*; @@ -36,17 +38,21 @@ impl From> for JsWallet { impl JsWallet { // TODO: Quotes #[wasm_bindgen(constructor)] - pub fn new(mint_url: String, mint_keys: JsKeys) -> JsWallet { + pub fn new(mint_urls: Vec, mint_keys: Vec) -> JsWallet { let client = HttpClient {}; + let mints = mint_urls + .iter() + .map(|u| (UncheckedUrl::from_str(u).unwrap(), None)) + .collect(); + + let keys = mint_keys + .iter() + .map(|k| (Id::from(k.deref()), k.deref().clone())) + .collect(); + JsWallet { - inner: Wallet::new( - client, - mint_url.into(), - vec![], - vec![], - mint_keys.deref().clone(), - ), + inner: Wallet::new(client, mints, HashMap::new(), vec![], vec![], None, keys), } } @@ -68,15 +74,17 @@ impl JsWallet { #[wasm_bindgen(js_name = mintToken)] pub async fn mint_token( &mut self, + mint_url: String, amount: JsAmount, memo: Option, unit: Option, ) -> Result { + let mint_url = UncheckedUrl::from_str(&mint_url).map_err(into_err)?; let unit = unit.map(|u| CurrencyUnit::from_str(&u).unwrap_or_default()); Ok(self .inner - .mint_token(*amount.deref(), memo, unit) + .mint_token(mint_url, *amount.deref(), memo, unit) .await .map_err(into_err)? .into()) @@ -84,14 +92,15 @@ impl JsWallet { /// Mint #[wasm_bindgen(js_name = mint)] - pub async fn mint(&mut self, quote: String) -> Result { - serde_wasm_bindgen::to_value(&self.inner.mint("e).await.map_err(into_err)?) + pub async fn mint(&mut self, mint_url: String, quote: String) -> Result { + let mint_url = UncheckedUrl::from_str(&mint_url).map_err(into_err)?; + serde_wasm_bindgen::to_value(&self.inner.mint(mint_url, "e).await.map_err(into_err)?) .map_err(into_err) } /// Receive #[wasm_bindgen(js_name = receive)] - pub async fn receive(&self, token: String) -> Result { + pub async fn receive(&mut self, token: String) -> Result { serde_wasm_bindgen::to_value(&self.inner.receive(&token).await.map_err(into_err)?) .map_err(into_err) } @@ -116,12 +125,20 @@ impl JsWallet { /// Send #[wasm_bindgen(js_name = send)] - pub async fn send(&self, amount: JsAmount, proofs: JsValue) -> Result { + pub async fn send( + &mut self, + mint_url: String, + amount: JsAmount, + unit: String, + proofs: JsValue, + ) -> Result { + let mint_url = UncheckedUrl::from_str(&mint_url).map_err(into_err)?; + let unit = CurrencyUnit::from_str(&unit).map_err(into_err)?; let proofs = serde_wasm_bindgen::from_value(proofs).map_err(into_err)?; Ok(self .inner - .send(*amount.deref(), proofs) + .send(&mint_url, &unit, *amount.deref(), proofs) .await .map_err(into_err)? .into()) @@ -129,12 +146,18 @@ impl JsWallet { /// Melt #[wasm_bindgen(js_name = melt)] - pub async fn melt(&self, quote: String, proofs: JsValue) -> Result { + pub async fn melt( + &mut self, + mint_url: String, + quote: String, + proofs: JsValue, + ) -> Result { + let mint_url = UncheckedUrl::from_str(&mint_url).map_err(into_err)?; let proofs = serde_wasm_bindgen::from_value(proofs).map_err(into_err)?; Ok(self .inner - .melt("e, proofs) + .melt(&mint_url, "e, proofs) .await .map_err(into_err)? .into()) @@ -144,10 +167,12 @@ impl JsWallet { #[wasm_bindgen(js_name = proofsToToken)] pub fn proofs_to_token( &self, + mint_url: String, proofs: JsValue, unit: Option, memo: Option, ) -> Result { + let mint_url = UncheckedUrl::from_str(&mint_url).map_err(into_err)?; let proofs = serde_wasm_bindgen::from_value(proofs).map_err(into_err)?; let unit = unit.map(|u| { @@ -157,7 +182,7 @@ impl JsWallet { }); self.inner - .proofs_to_token(proofs, memo, unit) + .proofs_to_token(mint_url, proofs, memo, unit) .map_err(into_err) } } diff --git a/crates/cashu-sdk/src/wallet.rs b/crates/cashu-sdk/src/wallet.rs index 23d2e745..481aae97 100644 --- a/crates/cashu-sdk/src/wallet.rs +++ b/crates/cashu-sdk/src/wallet.rs @@ -82,6 +82,16 @@ impl Wallet { } } + /// Back up seed + pub fn mnemonic(&self) -> Option { + self.backup_info.clone().map(|b| b.mnemonic) + } + + /// Back up keyset counters + pub fn keyset_counters(&self) -> Option> { + self.backup_info.clone().map(|b| b.counter) + } + /// Check if a proof is spent #[cfg(feature = "nut07")] pub async fn check_proofs_spent(