refactor: cashu-js

This commit is contained in:
thesimplekid
2023-12-19 22:58:00 +00:00
parent 003ecb9dba
commit 3b49289adc
8 changed files with 105 additions and 121 deletions

View File

@@ -13,8 +13,8 @@ pub mod nut09;
pub use nut00::{JsBlindedMessage, JsBlindedMessages, JsBlindedSignature, JsProof, JsToken};
pub use nut01::{JsKeyPair, JsKeys, JsPublicKey, JsSecretKey};
pub use nut02::{JsId, JsKeySet, JsKeySetsResponse, JsKeysResponse, JsMintKeySet};
pub use nut03::{JsRequestMintResponse, JsSplitRequest, JsSplitResponse};
pub use nut04::{JsMintRequest, JsPostMintResponse};
pub use nut03::{JsSwapRequest, JsSwapResponse};
pub use nut04::{JsMintBolt11Request, JsMintBolt11Response};
#[cfg(feature = "nut07")]
pub use nut07::{JsCheckSpendableRequest, JsCheckSpendableResponse};
pub use nut08::{JsMeltRequest, JsMeltResponse};
pub use nut08::{JsMeltBolt11Request, JsMeltBolt11Response};

View File

@@ -23,10 +23,10 @@ impl Deref for JsBlindedSignature {
impl JsBlindedSignature {
#[allow(clippy::new_without_default)]
#[wasm_bindgen(constructor)]
pub fn new(id: JsId, amount: JsAmount, c: JsPublicKey) -> Self {
pub fn new(keyset_id: JsId, amount: JsAmount, c: JsPublicKey) -> Self {
Self {
inner: BlindedSignature {
id: *id.deref(),
keyset_id: *keyset_id.deref(),
amount: *amount.deref(),
c: c.deref().clone(),
},

View File

@@ -28,13 +28,13 @@ impl From<Proof> for JsProof {
#[wasm_bindgen(js_class = Proof)]
impl JsProof {
#[wasm_bindgen(constructor)]
pub fn new(amount: JsAmount, secret: JsSecret, c: JsPublicKey, id: JsId) -> JsProof {
pub fn new(amount: JsAmount, secret: JsSecret, c: JsPublicKey, keyset_id: JsId) -> JsProof {
Self {
inner: Proof {
amount: *amount.deref(),
secret: secret.deref().clone(),
c: c.deref().clone(),
id: *id.deref(),
keyset_id: *keyset_id.deref(),
},
}
}
@@ -57,9 +57,9 @@ impl JsProof {
self.inner.c.clone().into()
}
/// Id
/// Keyset Id
#[wasm_bindgen(getter)]
pub fn id(&self) -> JsId {
self.inner.id.into()
pub fn keyset_id(&self) -> JsId {
self.inner.keyset_id.into()
}
}

View File

@@ -1,38 +1,38 @@
use std::ops::Deref;
use cashu::nuts::{SplitRequest, SplitResponse};
use cashu::nuts::{SwapRequest, SwapResponse};
use wasm_bindgen::prelude::*;
use crate::error::{into_err, Result};
use crate::types::JsAmount;
#[wasm_bindgen(js_name = SplitRequest)]
pub struct JsSplitRequest {
inner: SplitRequest,
#[wasm_bindgen(js_name = SwapRequest)]
pub struct JsSwapRequest {
inner: SwapRequest,
}
impl Deref for JsSplitRequest {
type Target = SplitRequest;
impl Deref for JsSwapRequest {
type Target = SwapRequest;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl From<SplitRequest> for JsSplitRequest {
fn from(inner: SplitRequest) -> JsSplitRequest {
JsSplitRequest { inner }
impl From<SwapRequest> for JsSwapRequest {
fn from(inner: SwapRequest) -> JsSwapRequest {
JsSwapRequest { inner }
}
}
#[wasm_bindgen(js_class = SplitRequest)]
impl JsSplitRequest {
#[wasm_bindgen(js_class = SwapRequest)]
impl JsSwapRequest {
#[wasm_bindgen(constructor)]
pub fn new(inputs: JsValue, outputs: JsValue) -> Result<JsSplitRequest> {
pub fn new(inputs: JsValue, outputs: JsValue) -> Result<JsSwapRequest> {
let inputs = serde_wasm_bindgen::from_value(inputs).map_err(into_err)?;
let outputs = serde_wasm_bindgen::from_value(outputs).map_err(into_err)?;
Ok(JsSplitRequest {
inner: SplitRequest { inputs, outputs },
Ok(JsSwapRequest {
inner: SwapRequest { inputs, outputs },
})
}
@@ -62,45 +62,43 @@ impl JsSplitRequest {
}
#[wasm_bindgen(js_name = SplitResponse)]
pub struct JsSplitResponse {
inner: SplitResponse,
pub struct JsSwapResponse {
inner: SwapResponse,
}
impl Deref for JsSplitResponse {
type Target = SplitResponse;
impl Deref for JsSwapResponse {
type Target = SwapResponse;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl From<SplitResponse> for JsSplitResponse {
fn from(inner: SplitResponse) -> JsSplitResponse {
JsSplitResponse { inner }
impl From<SwapResponse> for JsSwapResponse {
fn from(inner: SwapResponse) -> JsSwapResponse {
JsSwapResponse { inner }
}
}
#[wasm_bindgen(js_class = SplitResponse)]
impl JsSplitResponse {
impl JsSwapResponse {
#[wasm_bindgen(constructor)]
pub fn new(promises: JsValue) -> Result<JsSplitResponse> {
let promises = serde_wasm_bindgen::from_value(promises).map_err(into_err)?;
pub fn new(signatures: JsValue) -> Result<JsSwapResponse> {
let signatures = serde_wasm_bindgen::from_value(signatures).map_err(into_err)?;
Ok(JsSplitResponse {
inner: SplitResponse {
promises: Some(promises),
},
Ok(JsSwapResponse {
inner: SwapResponse { signatures },
})
}
/// Get Promises
#[wasm_bindgen(getter)]
pub fn promises(&self) -> Result<JsValue> {
serde_wasm_bindgen::to_value(&self.inner.promises).map_err(into_err)
pub fn signatures(&self) -> Result<JsValue> {
serde_wasm_bindgen::to_value(&self.inner.signatures).map_err(into_err)
}
/// Promises Amount
#[wasm_bindgen(js_name = promisesAmount)]
pub fn promises_amount(&self) -> Option<JsAmount> {
self.inner.promises_amount().map(|a| a.into())
pub fn promises_amount(&self) -> JsAmount {
self.inner.promises_amount().into()
}
}

View File

@@ -1,37 +1,37 @@
use std::ops::Deref;
use cashu::nuts::nut04::{MintRequest, PostMintResponse};
use cashu::nuts::nut04::{MintBolt11Request, MintBolt11Response};
use wasm_bindgen::prelude::*;
use crate::error::{into_err, Result};
use crate::types::JsAmount;
#[wasm_bindgen(js_name = MintRequest)]
pub struct JsMintRequest {
inner: MintRequest,
#[wasm_bindgen(js_name = MintBolt11Request)]
pub struct JsMintBolt11Request {
inner: MintBolt11Request,
}
impl Deref for JsMintRequest {
type Target = MintRequest;
impl Deref for JsMintBolt11Request {
type Target = MintBolt11Request;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl From<MintRequest> for JsMintRequest {
fn from(inner: MintRequest) -> JsMintRequest {
JsMintRequest { inner }
impl From<MintBolt11Request> for JsMintBolt11Request {
fn from(inner: MintBolt11Request) -> JsMintBolt11Request {
JsMintBolt11Request { inner }
}
}
#[wasm_bindgen(js_class = MintRequest)]
impl JsMintRequest {
#[wasm_bindgen(js_class = MintBolt11Request)]
impl JsMintBolt11Request {
/// Try From Base 64 String
#[wasm_bindgen(constructor)]
pub fn new(outputs: JsValue) -> Result<JsMintRequest> {
pub fn new(quote: String, outputs: JsValue) -> Result<JsMintBolt11Request> {
let outputs = serde_wasm_bindgen::from_value(outputs).map_err(into_err)?;
Ok(JsMintRequest {
inner: MintRequest { outputs },
Ok(JsMintBolt11Request {
inner: MintBolt11Request { quote, outputs },
})
}
@@ -47,36 +47,36 @@ impl JsMintRequest {
}
#[wasm_bindgen(js_name = PostMintResponse)]
pub struct JsPostMintResponse {
inner: PostMintResponse,
pub struct JsMintBolt11Response {
inner: MintBolt11Response,
}
impl Deref for JsPostMintResponse {
type Target = PostMintResponse;
impl Deref for JsMintBolt11Response {
type Target = MintBolt11Response;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl From<PostMintResponse> for JsPostMintResponse {
fn from(inner: PostMintResponse) -> JsPostMintResponse {
JsPostMintResponse { inner }
impl From<MintBolt11Response> for JsMintBolt11Response {
fn from(inner: MintBolt11Response) -> JsMintBolt11Response {
JsMintBolt11Response { inner }
}
}
#[wasm_bindgen(js_class = PostMintResponse)]
impl JsPostMintResponse {
impl JsMintBolt11Response {
/// Try From Base 64 String
#[wasm_bindgen(constructor)]
pub fn new(promises: JsValue) -> Result<JsPostMintResponse> {
let promises = serde_wasm_bindgen::from_value(promises).map_err(into_err)?;
Ok(JsPostMintResponse {
inner: PostMintResponse { promises },
pub fn new(signatures: JsValue) -> Result<JsMintBolt11Response> {
let signatures = serde_wasm_bindgen::from_value(signatures).map_err(into_err)?;
Ok(JsMintBolt11Response {
inner: MintBolt11Response { signatures },
})
}
#[wasm_bindgen(getter)]
pub fn promises(&self) -> Result<JsValue> {
serde_wasm_bindgen::to_value(&self.inner.promises).map_err(into_err)
pub fn signatures(&self) -> Result<JsValue> {
serde_wasm_bindgen::to_value(&self.inner.signatures).map_err(into_err)
}
}

View File

@@ -8,27 +8,27 @@ use crate::error::{into_err, Result};
use crate::types::JsAmount;
#[wasm_bindgen(js_name = MeltRequest)]
pub struct JsMeltRequest {
pub struct JsMeltBolt11Request {
inner: MeltBolt11Request,
}
impl Deref for JsMeltRequest {
impl Deref for JsMeltBolt11Request {
type Target = MeltBolt11Request;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl From<MeltBolt11Request> for JsMeltRequest {
fn from(inner: MeltBolt11Request) -> JsMeltRequest {
JsMeltRequest { inner }
impl From<MeltBolt11Request> for JsMeltBolt11Request {
fn from(inner: MeltBolt11Request) -> JsMeltBolt11Request {
JsMeltBolt11Request { inner }
}
}
#[wasm_bindgen(js_class = MeltRequest)]
impl JsMeltRequest {
#[wasm_bindgen(js_class = MeltBolt11Request)]
impl JsMeltBolt11Request {
#[wasm_bindgen(constructor)]
pub fn new(quote: String, inputs: JsValue, outputs: JsValue) -> Result<JsMeltRequest> {
pub fn new(quote: String, inputs: JsValue, outputs: JsValue) -> Result<JsMeltBolt11Request> {
let inputs: Vec<Proof> = serde_wasm_bindgen::from_value(inputs).map_err(into_err)?;
let outputs: Option<Vec<BlindedMessage>> = if !outputs.is_null() {
Some(serde_wasm_bindgen::from_value(outputs).map_err(into_err)?)
@@ -36,7 +36,7 @@ impl JsMeltRequest {
None
};
Ok(JsMeltRequest {
Ok(JsMeltBolt11Request {
inner: MeltBolt11Request {
quote,
inputs,
@@ -65,36 +65,40 @@ impl JsMeltRequest {
}
#[wasm_bindgen(js_name = MeltResponse)]
pub struct JsMeltResponse {
pub struct JsMeltBolt11Response {
inner: MeltBolt11Response,
}
impl Deref for JsMeltResponse {
impl Deref for JsMeltBolt11Response {
type Target = MeltBolt11Response;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl From<MeltBolt11Response> for JsMeltResponse {
fn from(inner: MeltBolt11Response) -> JsMeltResponse {
JsMeltResponse { inner }
impl From<MeltBolt11Response> for JsMeltBolt11Response {
fn from(inner: MeltBolt11Response) -> JsMeltBolt11Response {
JsMeltBolt11Response { inner }
}
}
#[wasm_bindgen(js_class = MeltResponse)]
impl JsMeltResponse {
#[wasm_bindgen(js_class = MeltBolt11Response)]
impl JsMeltBolt11Response {
#[wasm_bindgen(constructor)]
pub fn new(paid: bool, proof: String, change: JsValue) -> Result<JsMeltResponse> {
pub fn new(
paid: bool,
payment_preimage: Option<String>,
change: JsValue,
) -> Result<JsMeltBolt11Response> {
let change: Option<Vec<BlindedSignature>> = if change.is_null() {
Some(serde_wasm_bindgen::from_value(change).map_err(into_err)?)
} else {
None
};
Ok(JsMeltResponse {
Ok(JsMeltBolt11Response {
inner: MeltBolt11Response {
proof,
payment_preimage,
paid,
change,
},
@@ -109,8 +113,8 @@ impl JsMeltResponse {
/// Get Preimage
#[wasm_bindgen(getter)]
pub fn proof(&self) -> String {
self.inner.proof.clone()
pub fn payment_preimage(&self) -> Option<String> {
self.inner.payment_preimage.clone()
}
/// Get Change

View File

@@ -23,43 +23,23 @@ impl From<Amount> for JsAmount {
}
}
impl From<u64> for JsAmount {
fn from(amount: u64) -> JsAmount {
JsAmount {
inner: Amount::from(amount),
}
}
}
#[wasm_bindgen(js_class = Amount)]
impl JsAmount {
#[wasm_bindgen(constructor)]
pub fn new(sats: u32) -> Self {
Self {
inner: Amount::from_sat(sats as u64),
inner: Amount::from(sats as u64),
}
}
/// From Sats
#[wasm_bindgen(js_name = fromSat)]
pub fn from_sat(sats: u64) -> Self {
Self {
inner: Amount::from_sat(sats),
}
}
/// From Msats
#[wasm_bindgen(js_name = fromMSat)]
pub fn from_msat(msats: u64) -> Self {
Self {
inner: Amount::from_msat(msats),
}
}
/// Get as sats
#[wasm_bindgen(js_name = toSat)]
pub fn to_sat(&self) -> u64 {
self.inner.to_sat()
}
/// Get as msats
#[wasm_bindgen(js_name = toMSat)]
pub fn to_msat(&self) -> u64 {
self.inner.to_msat()
}
/// Split amount returns sat vec of sats
#[wasm_bindgen(js_name = split)]
pub fn split(&self) -> Result<JsValue> {

View File

@@ -37,7 +37,9 @@ impl JsBolt11Invoice {
/// Amount
#[wasm_bindgen(getter)]
pub fn amount(&self) -> Option<JsAmount> {
self.inner.amount_milli_satoshis().map(JsAmount::from_msat)
self.inner
.amount_milli_satoshis()
.map(|a| JsAmount::from(a / 1000))
}
/// Invoice as string