Skip to content

Commit

Permalink
refactor: move nut06 to nut03
Browse files Browse the repository at this point in the history
rename 'proofs' to 'to inputs'
  • Loading branch information
thesimplekid committed Nov 25, 2023
1 parent 2d8c682 commit 9f2ec29
Show file tree
Hide file tree
Showing 13 changed files with 294 additions and 168 deletions.
3 changes: 1 addition & 2 deletions bindings/cashu-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ mod ffi {
pub use crate::nuts::nut01::public_key::PublicKey;
pub use crate::nuts::nut01::secret_key::SecretKey;
pub use crate::nuts::nut02::{Id, KeySet, KeySetResponse, MintKeySet};
pub use crate::nuts::nut03::RequestMintResponse;
pub use crate::nuts::nut03::{RequestMintResponse, SplitRequest, SplitResponse};
pub use crate::nuts::nut04::{MintRequest, PostMintResponse};
pub use crate::nuts::nut05::{
CheckFeesRequest, CheckFeesResponse, MeltRequest as Nut05MeltRequest,
MeltResponse as Nut05MeltResponse,
};
pub use crate::nuts::nut06::{SplitRequest, SplitResponse};
pub use crate::nuts::nut07::{CheckSpendableRequest, CheckSpendableResponse};
pub use crate::nuts::nut08::{MeltRequest, MeltResponse};
pub use crate::nuts::nut09::{MintInfo, MintVersion};
Expand Down
1 change: 0 additions & 1 deletion bindings/cashu-ffi/src/nuts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pub mod nut02;
pub mod nut03;
pub mod nut04;
pub mod nut05;
pub mod nut06;
pub mod nut07;
pub mod nut08;
pub mod nut09;
85 changes: 85 additions & 0 deletions bindings/cashu-ffi/src/nuts/nut03/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use std::ops::Deref;
use std::str::FromStr;
use std::sync::Arc;

use cashu::nuts::nut03::RequestMintResponse as RequestMintResponseSdk;
use cashu::nuts::{SplitRequest as SplitRequestSdk, SplitResponse as SplitResponseSdk};
use cashu::Bolt11Invoice;

use crate::error::Result;
use crate::{Amount, BlindedMessage, BlindedSignature, Proof};

pub struct RequestMintResponse {
inner: RequestMintResponseSdk,
Expand Down Expand Up @@ -34,3 +38,84 @@ impl From<cashu::nuts::nut03::RequestMintResponse> for RequestMintResponse {
}
}
}

pub struct SplitRequest {
inner: SplitRequestSdk,
}

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

impl SplitRequest {
pub fn new(proofs: Vec<Arc<Proof>>, outputs: Vec<Arc<BlindedMessage>>) -> Self {
let proofs = proofs.into_iter().map(|p| p.as_ref().into()).collect();
let outputs = outputs.into_iter().map(|o| o.as_ref().into()).collect();

Self {
inner: SplitRequestSdk::new(proofs, outputs),
}
}

pub fn proofs(&self) -> Vec<Arc<Proof>> {
self.inner
.inputs
.clone()
.into_iter()
.map(|p| Arc::new(p.into()))
.collect()
}

pub fn outputs(&self) -> Vec<Arc<BlindedMessage>> {
self.inner
.outputs
.clone()
.into_iter()
.map(|o| Arc::new(o.into()))
.collect()
}

pub fn proofs_amount(&self) -> Arc<Amount> {
Arc::new(self.inner.input_amount().into())
}

pub fn output_amount(&self) -> Arc<Amount> {
Arc::new(self.inner.output_amount().into())
}
}

pub struct SplitResponse {
inner: SplitResponseSdk,
}

impl SplitResponse {
pub fn new(promises: Vec<Arc<BlindedSignature>>) -> Self {
let promises = promises.into_iter().map(|p| p.as_ref().into()).collect();
Self {
inner: SplitResponseSdk::new(promises),
}
}

pub fn promises(&self) -> Vec<Arc<BlindedSignature>> {
self.inner
.promises
.clone()
.unwrap_or_default()
.into_iter()
.map(|p| Arc::new(p.into()))
.collect()
}

pub fn promises_amount(&self) -> Option<Arc<Amount>> {
self.inner.promises_amount().map(|a| Arc::new(a.into()))
}
}

impl From<cashu::nuts::SplitResponse> for SplitResponse {
fn from(inner: cashu::nuts::SplitResponse) -> SplitResponse {
SplitResponse { inner }
}
}
87 changes: 0 additions & 87 deletions bindings/cashu-ffi/src/nuts/nut06/mod.rs

This file was deleted.

10 changes: 9 additions & 1 deletion bindings/cashu-js/src/nuts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ pub mod nut02;
pub mod nut03;
pub mod nut04;
pub mod nut05;
pub mod nut06;
#[cfg(feature = "nut07")]
pub mod nut07;
pub mod nut08;
#[cfg(feature = "nut09")]
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};
#[cfg(feature = "nut07")]
pub use nut07::{JsCheckSpendableRequest, JsCheckSpendableResponse};
pub use nut08::{JsMeltRequest, JsMeltResponse};
104 changes: 102 additions & 2 deletions bindings/cashu-js/src/nuts/nut03.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::ops::Deref;

use cashu::nuts::nut03::RequestMintResponse;
use cashu::nuts::{RequestMintResponse, SplitRequest, SplitResponse};
use wasm_bindgen::prelude::*;

use crate::types::JsBolt11Invoice;
use crate::error::{into_err, Result};
use crate::types::{JsAmount, JsBolt11Invoice};

#[wasm_bindgen(js_name = RequestMintResponse)]
pub struct JsRequestMintResponse {
Expand Down Expand Up @@ -47,3 +48,102 @@ impl JsRequestMintResponse {
self.inner.hash.to_string()
}
}

#[wasm_bindgen(js_name = SplitRequest)]
pub struct JsSplitRequest {
inner: SplitRequest,
}

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

impl From<SplitRequest> for JsSplitRequest {
fn from(inner: SplitRequest) -> JsSplitRequest {
JsSplitRequest { inner }
}
}

#[wasm_bindgen(js_class = SplitRequest)]
impl JsSplitRequest {
#[wasm_bindgen(constructor)]
pub fn new(inputs: JsValue, outputs: JsValue) -> Result<JsSplitRequest> {
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 },
})
}

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

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

/// Proofs Amount
#[wasm_bindgen(js_name = proofsAmount)]
pub fn proofs_amount(&self) -> JsAmount {
self.inner.input_amount().into()
}

/// Output Amount
#[wasm_bindgen(js_name = outputAmount)]
pub fn output_amount(&self) -> JsAmount {
self.inner.output_amount().into()
}
}

#[wasm_bindgen(js_name = SplitResponse)]
pub struct JsSplitResponse {
inner: SplitResponse,
}

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

impl From<SplitResponse> for JsSplitResponse {
fn from(inner: SplitResponse) -> JsSplitResponse {
JsSplitResponse { inner }
}
}

#[wasm_bindgen(js_class = SplitResponse)]
impl JsSplitResponse {
#[wasm_bindgen(constructor)]
pub fn new(promises: JsValue) -> Result<JsSplitResponse> {
let promises = serde_wasm_bindgen::from_value(promises).map_err(into_err)?;

Ok(JsSplitResponse {
inner: SplitResponse {
promises: Some(promises),
},
})
}

/// Get Promises
#[wasm_bindgen(getter)]
pub fn promises(&self) -> Result<JsValue> {
serde_wasm_bindgen::to_value(&self.inner.promises).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())
}
}
18 changes: 6 additions & 12 deletions bindings/cashu-js/src/nuts/nut06.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::ops::Deref;

use cashu::nuts::nut06::{SplitRequest, SplitResponse};
use cashu::nuts::{SplitRequest, SplitResponse};
use wasm_bindgen::prelude::*;

use crate::error::{into_err, Result};
Expand All @@ -27,23 +27,19 @@ impl From<SplitRequest> for JsSplitRequest {
#[wasm_bindgen(js_class = SplitRequest)]
impl JsSplitRequest {
#[wasm_bindgen(constructor)]
pub fn new(proofs: JsValue, outputs: JsValue) -> Result<JsSplitRequest> {
let proofs = serde_wasm_bindgen::from_value(proofs).map_err(into_err)?;
pub fn new(inputs: JsValue, outputs: JsValue) -> Result<JsSplitRequest> {
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 {
amount: None,
proofs,
outputs,
},
inner: SplitRequest { inputs, outputs },
})
}

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

/// Get Outputs
Expand All @@ -55,7 +51,7 @@ impl JsSplitRequest {
/// Proofs Amount
#[wasm_bindgen(js_name = proofsAmount)]
pub fn proofs_amount(&self) -> JsAmount {
self.inner.proofs_amount().into()
self.inner.input_amount().into()
}

/// Output Amount
Expand Down Expand Up @@ -91,8 +87,6 @@ impl JsSplitResponse {

Ok(JsSplitResponse {
inner: SplitResponse {
fst: None,
snd: None,
promises: Some(promises),
},
})
Expand Down
Loading

0 comments on commit 9f2ec29

Please sign in to comment.