bindings/cashu-sdk add wallet

This commit is contained in:
thesimplekid
2023-10-01 09:03:40 +01:00
parent a9d2411946
commit 4148ee8365
9 changed files with 199 additions and 54 deletions

View File

@@ -1,4 +1,4 @@
[[language]]
name = "rust"
config = { cargo = { features = [ "blocking", "wallet", "mint" ] } }
config = { cargo = { features = [ "wallet", "mint" ] } }

View File

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

View File

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

View File

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

View File

@@ -3,3 +3,7 @@ mod error;
mod mint;
mod types;
mod wallet;
pub use client::JsClient;
pub use mint::JsMint;
pub use wallet::JsWallet;

View File

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

View File

@@ -1,3 +1,5 @@
mod melted;
mod proof_status;
mod send_proofs;
pub use melted::JsMelted;
pub use send_proofs::JsSendProofs;

View File

@@ -1,48 +0,0 @@
use std::ops::Deref;
use cashu_sdk::types::ProofsStatus;
use wasm_bindgen::prelude::*;
use crate::error::{into_err, Result};
#[wasm_bindgen(js_name = ProofStatus)]
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(spendable: JsValue, spent: JsValue) -> Result<JsProofsStatus> {
let spendable = serde_wasm_bindgen::from_value(spendable).map_err(into_err)?;
let spent = serde_wasm_bindgen::from_value(spent).map_err(into_err)?;
Ok(JsProofsStatus {
inner: ProofsStatus { spendable, spent },
})
}
/// Get Spendable
#[wasm_bindgen(getter)]
pub fn spendable(&self) -> Result<JsValue> {
serde_wasm_bindgen::to_value(&self.inner.spendable).map_err(into_err)
}
/// Get Spent
#[wasm_bindgen(getter)]
pub fn spent(&self) -> Result<JsValue> {
serde_wasm_bindgen::to_value(&self.inner.spent).map_err(into_err)
}
}

View File

@@ -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 {
@@ -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())
}
}