diff --git a/bindings/cashu-js/src/nuts/mod.rs b/bindings/cashu-js/src/nuts/mod.rs index fff91b37..0b51976a 100644 --- a/bindings/cashu-js/src/nuts/mod.rs +++ b/bindings/cashu-js/src/nuts/mod.rs @@ -2,3 +2,4 @@ mod nut00; mod nut01; mod nut02; mod nut03; +mod nut04; diff --git a/bindings/cashu-js/src/nuts/nut04.rs b/bindings/cashu-js/src/nuts/nut04.rs new file mode 100644 index 00000000..ddfcbccd --- /dev/null +++ b/bindings/cashu-js/src/nuts/nut04.rs @@ -0,0 +1,84 @@ +use std::ops::Deref; + +use cashu::nuts::nut04::{MintRequest, PostMintResponse}; +use wasm_bindgen::prelude::*; + +use crate::{ + error::{into_err, Result}, + types::JsAmount, +}; + +#[wasm_bindgen(js_name = MintRequest)] +pub struct JsMintRequest { + inner: MintRequest, +} + +impl Deref for JsMintRequest { + type Target = MintRequest; + fn deref(&self) -> &Self::Target { + &self.inner + } +} + +impl From for JsMintRequest { + fn from(inner: MintRequest) -> JsMintRequest { + JsMintRequest { inner } + } +} + +#[wasm_bindgen(js_class = MintRequest)] +impl JsMintRequest { + /// Try From Base 64 String + #[wasm_bindgen(constructor)] + pub fn new(outputs: String) -> Result { + let outputs = serde_json::from_str(&outputs).map_err(into_err)?; + Ok(JsMintRequest { + inner: MintRequest { outputs }, + }) + } + + #[wasm_bindgen(getter)] + pub fn outputs(&self) -> Result { + Ok(serde_json::to_string(&self.inner.outputs).map_err(into_err)?) + } + + #[wasm_bindgen(js_name = totalAmount)] + pub fn totoal_amount(&self) -> JsAmount { + self.inner.total_amount().into() + } +} + +#[wasm_bindgen(js_name = PostMintResponse)] +pub struct JsPostMintResponse { + inner: PostMintResponse, +} + +impl Deref for JsPostMintResponse { + type Target = PostMintResponse; + fn deref(&self) -> &Self::Target { + &self.inner + } +} + +impl From for JsPostMintResponse { + fn from(inner: PostMintResponse) -> JsPostMintResponse { + JsPostMintResponse { inner } + } +} + +#[wasm_bindgen(js_class = PostMintResponse)] +impl JsPostMintResponse { + /// Try From Base 64 String + #[wasm_bindgen(constructor)] + pub fn new(promises: String) -> Result { + let promises = serde_json::from_str(&promises).map_err(into_err)?; + Ok(JsPostMintResponse { + inner: PostMintResponse { promises }, + }) + } + + #[wasm_bindgen(getter)] + pub fn promises(&self) -> Result { + Ok(serde_json::to_string(&self.inner.promises).map_err(into_err)?) + } +}