Skip to content

Commit

Permalink
bindings/cashu-sdk add wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Oct 1, 2023
1 parent a9d2411 commit 4148ee8
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .helix/languages.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[[language]]
name = "rust"
config = { cargo = { features = [ "blocking", "wallet", "mint" ] } }
config = { cargo = { features = [ "wallet", "mint" ] } }

1 change: 1 addition & 0 deletions bindings/cashu-js/src/nuts/nut00/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ pub use blinded_message::JsBlindedMessage;
pub use blinded_messages::JsBlindedMessages;
pub use blinded_signature::JsBlindedSignature;
pub use proof::JsProof;
pub use token::JsToken;
2 changes: 2 additions & 0 deletions bindings/cashu-js/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
pub mod amount;
pub mod bolt11_invoice;
pub mod proofs_status;
pub mod secret;

pub use amount::JsAmount;
pub use bolt11_invoice::JsBolt11Invoice;
pub use proofs_status::JsProofsStatus;
pub use secret::JsSecret;
47 changes: 47 additions & 0 deletions bindings/cashu-js/src/types/proofs_status.rs
Original file line number Diff line number Diff line change
@@ -1 +1,48 @@
use std::ops::Deref;

use cashu::types::ProofsStatus;
use wasm_bindgen::prelude::*;

use crate::error::{into_err, Result};

#[wasm_bindgen(js_name = ProofsStatus)]
pub struct JsProofsStatus {
inner: ProofsStatus,
}

impl Deref for JsProofsStatus {
type Target = ProofsStatus;
fn deref(&self) -> &Self::Target {
&self.inner
}
}

impl From<ProofsStatus> for JsProofsStatus {
fn from(inner: ProofsStatus) -> JsProofsStatus {
JsProofsStatus { inner }
}
}

#[wasm_bindgen(js_class = ProofsStatus)]
impl JsProofsStatus {
#[wasm_bindgen(constructor)]
pub fn new(spent_proofs: JsValue, spendable_proofs: JsValue) -> Result<JsProofsStatus> {
let spent = serde_wasm_bindgen::from_value(spent_proofs).map_err(into_err)?;
let spendable = serde_wasm_bindgen::from_value(spendable_proofs).map_err(into_err)?;
Ok(JsProofsStatus {
inner: ProofsStatus { spent, spendable },
})
}

/// Get Spendable Proofs
#[wasm_bindgen(getter)]
pub fn spendable(&self) -> Result<JsValue> {
serde_wasm_bindgen::to_value(&self.inner.spendable).map_err(into_err)
}

/// Get Spent Proofs
#[wasm_bindgen(getter)]
pub fn spent_proofs(&self) -> Result<JsValue> {
serde_wasm_bindgen::to_value(&self.inner.spent).map_err(into_err)
}
}
4 changes: 4 additions & 0 deletions bindings/cashu-sdk-js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ mod error;
mod mint;
mod types;
mod wallet;

pub use client::JsClient;
pub use mint::JsMint;
pub use wallet::JsWallet;
2 changes: 1 addition & 1 deletion bindings/cashu-sdk-js/src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl JsMint {
}

/// Get Keysets
#[wasm_bindgen(js_name = KeySets)]
#[wasm_bindgen(js_name = keySets)]
pub fn keysets(&self) -> JsKeySetsResponse {
self.inner.keysets().into()
}
Expand Down
4 changes: 3 additions & 1 deletion bindings/cashu-sdk-js/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod melted;
mod proof_status;
mod send_proofs;

pub use melted::JsMelted;
pub use send_proofs::JsSendProofs;
48 changes: 0 additions & 48 deletions bindings/cashu-sdk-js/src/types/proof_status.rs

This file was deleted.

143 changes: 140 additions & 3 deletions bindings/cashu-sdk-js/src/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
use std::ops::Deref;

use cashu_js::nuts::nut00::{JsBlindedMessages, JsToken};
use cashu_js::nuts::nut03::JsRequestMintResponse;
use cashu_js::types::{JsBolt11Invoice, JsProofsStatus};
use cashu_js::{nuts::nut01::JsKeys, types::JsAmount};
use cashu_sdk::wallet::Wallet;
use wasm_bindgen::prelude::*;

use crate::error::{into_err, Result};
use crate::types::{JsMelted, JsSendProofs};
use crate::{
error::{into_err, Result},
JsClient,
};

#[wasm_bindgen(js_name = Wallet)]
pub struct JsWallet {
Expand All @@ -26,7 +34,136 @@ impl From<Wallet> for JsWallet {
#[wasm_bindgen(js_class = Wallet)]
impl JsWallet {
#[wasm_bindgen(constructor)]
pub fn new() -> Result<JsWallet> {
todo!()
pub fn new(client: JsClient, mint_keys: JsKeys) -> JsWallet {
JsWallet {
inner: Wallet::new(client.deref().clone(), mint_keys.deref().clone()),
}
}

/// Check Proofs spent
#[wasm_bindgen(js_name = checkProofsSpent)]
pub async fn check_proofs_spent(&self, proofs: JsValue) -> Result<JsProofsStatus> {
let proofs = serde_wasm_bindgen::from_value(proofs).map_err(into_err)?;

Ok(self
.inner
.check_proofs_spent(&proofs)
.await
.map_err(into_err)?
.into())
}

/// Request Mint
#[wasm_bindgen(js_name = requestMint)]
pub async fn request_mint(&self, amount: JsAmount) -> Result<JsRequestMintResponse> {
Ok(self
.inner
.request_mint(*amount.deref())
.await
.map_err(into_err)?
.into())
}

/// Mint Token
#[wasm_bindgen(js_name = mintToken)]
pub async fn mint_token(&self, amount: JsAmount, hash: String) -> Result<JsToken> {
Ok(self
.inner
.mint_token(*amount.deref(), &hash)
.await
.map_err(into_err)?
.into())
}

/// Mint
#[wasm_bindgen(js_name = mint)]
pub async fn mint(&self, amount: JsAmount, hash: String) -> Result<JsValue> {
serde_wasm_bindgen::to_value(
&self
.inner
.mint(*amount.deref(), &hash)
.await
.map_err(into_err)?,
)
.map_err(into_err)
}

/// Check Fee
#[wasm_bindgen(js_name = checkFee)]
pub async fn check_fee(&self, invoice: JsBolt11Invoice) -> Result<JsAmount> {
Ok(self
.inner
.check_fee(invoice.deref().clone())
.await
.map_err(into_err)?
.into())
}

/// Receive
#[wasm_bindgen(js_name = receive)]
pub async fn receive(&self, token: String) -> Result<JsValue> {
serde_wasm_bindgen::to_value(&self.inner.receive(&token).await.map_err(into_err)?)
.map_err(into_err)
}

/// Process Split
#[wasm_bindgen(js_name = processSplitResponse)]
pub fn process_split_response(
&self,
blinded_messages: JsBlindedMessages,
promises: JsValue,
) -> Result<JsValue> {
let promises = serde_wasm_bindgen::from_value(promises).map_err(into_err)?;

serde_wasm_bindgen::to_value(
&self
.inner
.process_split_response(blinded_messages.deref().clone(), promises)
.map_err(into_err)?,
)
.map_err(into_err)
}

/// Send
#[wasm_bindgen(js_name = processSplitResponse)]
pub async fn send(&self, amount: JsAmount, proofs: JsValue) -> Result<JsSendProofs> {
let proofs = serde_wasm_bindgen::from_value(proofs).map_err(into_err)?;

Ok(self
.inner
.send(*amount.deref(), proofs)
.await
.map_err(into_err)?
.into())
}

/// Melt
#[wasm_bindgen(js_name = melt)]
pub async fn melt(
&self,
invoice: JsBolt11Invoice,
proofs: JsValue,
fee_reserve: JsAmount,
) -> Result<JsMelted> {
let proofs = serde_wasm_bindgen::from_value(proofs).map_err(into_err)?;

Ok(self
.inner
.melt(invoice.deref().clone(), proofs, *fee_reserve.deref())
.await
.map_err(into_err)?
.into())
}

/// Proofs to token
#[wasm_bindgen(js_name = proofsToToken)]
pub fn proofs_to_token(&self, proofs: JsValue, memo: Option<String>) -> Result<String> {
let proofs = serde_wasm_bindgen::from_value(proofs).map_err(into_err)?;

Ok(self
.inner
.proofs_to_token(proofs, memo)
.map_err(into_err)?
.into())
}
}

0 comments on commit 4148ee8

Please sign in to comment.