Skip to content

Commit

Permalink
refactor: add symbol to keyset_info
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Nov 25, 2023
1 parent 1f0bdde commit 6e80a1d
Show file tree
Hide file tree
Showing 15 changed files with 145 additions and 71 deletions.
10 changes: 5 additions & 5 deletions bindings/cashu-ffi/src/cashu.udl
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,14 @@ interface Keys {
};

interface KeySet {
constructor(Id id, Keys keys);
constructor(Id id, string symbol, Keys keys);
Id id();
Keys keys();
};

interface MintKeySet {
[Name=generate]
constructor(string secret, string derivation_path, u8 max_order);
constructor(string secret, string symbol, string derivation_path, u8 max_order);
};


Expand All @@ -140,8 +140,8 @@ interface KeysResponse {
};

interface KeySetResponse {
constructor(sequence<Id> keyset_ids);
sequence<Id> keyset_ids();
constructor(sequence<KeySetInfo> keysets);
sequence<KeySetInfo> keysets();
};

interface RequestMintResponse {
Expand Down Expand Up @@ -246,7 +246,7 @@ interface MintInfo {
};

interface KeySetInfo {
constructor(Id id, u64 valid_from, u64? valid_to, string secret, string derivation_path, u8 max_order);
constructor(Id id, string symbol);

};

Expand Down
24 changes: 17 additions & 7 deletions bindings/cashu-ffi/src/nuts/nut02/key_set.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::ops::Deref;
use std::str::FromStr;
use std::sync::Arc;

use cashu::nuts::nut02::{Id as IdSdk, KeySet as KeySetSdk, KeysetResponse as KeysetResponseSdk};
use cashu::nuts::{Id as IdSdk, KeySet as KeySetSdk, KeysetResponse as KeysetResponseSdk};

use crate::error::Result;
use crate::nuts::nut01::keys::Keys;
use crate::KeySetInfo;

pub struct Id {
inner: IdSdk,
Expand All @@ -19,7 +21,7 @@ impl Deref for Id {
impl Id {
pub fn new(id: String) -> Result<Self> {
Ok(Self {
inner: IdSdk::try_from_base64(&id)?,
inner: IdSdk::from_str(&id)?,
})
}
}
Expand Down Expand Up @@ -48,10 +50,11 @@ impl Deref for KeySet {
}

impl KeySet {
pub fn new(id: Arc<Id>, keys: Arc<Keys>) -> Self {
pub fn new(id: Arc<Id>, symbol: String, keys: Arc<Keys>) -> Self {
Self {
inner: KeySetSdk {
id: *id.as_ref().deref(),
symbol,
keys: keys.as_ref().deref().clone(),
},
}
Expand All @@ -61,6 +64,10 @@ impl KeySet {
Arc::new(self.inner.id.into())
}

pub fn symbol(&self) -> String {
self.inner.symbol.clone()
}

pub fn keys(&self) -> Arc<Keys> {
Arc::new(self.inner.keys.clone().into())
}
Expand All @@ -77,19 +84,22 @@ pub struct KeySetResponse {
}

impl KeySetResponse {
pub fn new(keyset_ids: Vec<Arc<Id>>) -> Self {
let keysets = keyset_ids.into_iter().map(|id| id.inner).collect();
pub fn new(keyset_ids: Vec<Arc<KeySetInfo>>) -> Self {
let keysets = keyset_ids
.into_iter()
.map(|ki| ki.as_ref().deref().clone())
.collect();
Self {
inner: KeysetResponseSdk { keysets },
}
}

pub fn keyset_ids(&self) -> Vec<Arc<Id>> {
pub fn keysets(&self) -> Vec<Arc<KeySetInfo>> {
self.inner
.clone()
.keysets
.into_iter()
.map(|id| Arc::new(id.into()))
.map(|keyset_info| Arc::new(keyset_info.into()))
.collect()
}
}
Expand Down
9 changes: 7 additions & 2 deletions bindings/cashu-ffi/src/nuts/nut02/mint_keyset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ impl Deref for MintKeySet {
}

impl MintKeySet {
pub fn generate(secret: String, derivation_path: String, max_order: u8) -> Self {
pub fn generate(
secret: String,
symbol: String,
derivation_path: String,
max_order: u8,
) -> Self {
Self {
inner: KeySetSdk::generate(secret, derivation_path, max_order),
inner: KeySetSdk::generate(secret, symbol, derivation_path, max_order),
}
}
}
Expand Down
17 changes: 3 additions & 14 deletions bindings/cashu-ffi/src/types/keyset_info.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::ops::Deref;
use std::sync::Arc;

use cashu::types::KeysetInfo as KeySetInfoSdk;
use cashu::nuts::KeySetInfo as KeySetInfoSdk;

use crate::Id;

Expand All @@ -23,22 +23,11 @@ impl From<KeySetInfoSdk> for KeySetInfo {
}

impl KeySetInfo {
pub fn new(
id: Arc<Id>,
valid_from: u64,
valid_to: Option<u64>,
secret: String,
derivation_path: String,
max_order: u8,
) -> Self {
pub fn new(id: Arc<Id>, symbol: String) -> Self {
Self {
inner: KeySetInfoSdk {
id: *id.as_ref().deref(),
valid_from,
valid_to,
secret,
derivation_path,
max_order,
symbol,
},
}
}
Expand Down
6 changes: 4 additions & 2 deletions bindings/cashu-js/src/nuts/nut02/keyset.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::ops::Deref;
use std::str::FromStr;

use cashu::nuts::{Id, KeySet, KeysResponse, KeysetResponse};
use wasm_bindgen::prelude::*;
Expand Down Expand Up @@ -30,7 +31,7 @@ impl JsId {
#[wasm_bindgen(js_name = tryFromBase64)]
pub fn try_from_base64(id: String) -> Result<JsId> {
Ok(JsId {
inner: Id::try_from_base64(&id).map_err(into_err)?,
inner: Id::from_str(&id).map_err(into_err)?,
})
}

Expand Down Expand Up @@ -63,10 +64,11 @@ impl From<KeySet> for JsKeySet {
impl JsKeySet {
/// From Hex
#[wasm_bindgen(constructor)]
pub fn new(id: JsId, keys: JsKeys) -> JsKeySet {
pub fn new(id: JsId, symbol: String, keys: JsKeys) -> JsKeySet {
Self {
inner: KeySet {
id: *id.deref(),
symbol,
keys: keys.deref().clone(),
},
}
Expand Down
9 changes: 7 additions & 2 deletions bindings/cashu-js/src/nuts/nut02/mint_keyset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@ impl From<KeySet> for JsMintKeySet {
impl JsMintKeySet {
/// Generate
#[wasm_bindgen(constructor)]
pub fn generate(secret: String, derivation_path: String, max_order: u8) -> JsMintKeySet {
pub fn generate(
secret: String,
symbol: String,
derivation_path: String,
max_order: u8,
) -> JsMintKeySet {
Self {
inner: KeySet::generate(secret, derivation_path, max_order),
inner: KeySet::generate(secret, symbol, derivation_path, max_order),
}
}
}
16 changes: 10 additions & 6 deletions bindings/cashu-sdk-ffi/src/cashu_sdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ interface Keys {
};

interface KeySet {
constructor(Id id, Keys keys);
constructor(Id id, string symbol, Keys keys);
Id id();
Keys keys();
};


interface MintKeySet {
[Name=generate]
constructor(string secret, string derivation_path, u8 max_order);
constructor(string secret, string symbol, string derivation_path, u8 max_order);
};


Expand All @@ -144,8 +144,8 @@ interface KeysResponse {
};

interface KeySetResponse {
constructor(sequence<Id> keyset_ids);
sequence<Id> keyset_ids();
constructor(sequence<KeySetInfo> keysets);
sequence<KeySetInfo> keysets();
};

interface RequestMintResponse {
Expand Down Expand Up @@ -264,12 +264,16 @@ interface ProofsStatus {


interface KeySetInfo {
constructor(Id id, u64 valid_from, u64? valid_to, string secret, string derivation_path, u8 max_order);
constructor(Id id, string symbol);

};

// Cashu Sdk

interface MintKeySetInfo {
constructor(Id id, string symbol, u64 valid_from, u64? valid_to, string derivation_path, u8 max_order);
};


[Error]
interface CashuSdkError {
Expand Down Expand Up @@ -315,7 +319,7 @@ interface Wallet {

interface Mint {
[Throws=CashuSdkError]
constructor(string secret, string derivation_path, sequence<KeySetInfo> inactive_keysets, sequence<Secret> spent_secrets, u8 max_order, Amount min_fee_reserve, f32 percent_fee_reserve);
constructor(string secret, MintKeySetInfo active_keyset_info, sequence<MintKeySetInfo> inactive_keysets, sequence<Secret> spent_secrets, Amount min_fee_reserve, f32 percent_fee_reserve);
KeysResponse active_keyset_pubkeys();
KeySetResponse keysets();
MintKeySet active_keyset();
Expand Down
2 changes: 1 addition & 1 deletion bindings/cashu-sdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod ffi {

pub use crate::error::CashuSdkError;
pub use crate::mint::Mint;
pub use crate::types::{Melted, ProofsStatus, SendProofs};
pub use crate::types::{Melted, MintKeySetInfo, ProofsStatus, SendProofs};
pub use crate::wallet::Wallet;

// UDL
Expand Down
11 changes: 5 additions & 6 deletions bindings/cashu-sdk-ffi/src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ use std::ops::Deref;
use std::sync::{Arc, RwLock};

use cashu_ffi::{
Amount, CheckSpendableRequest, CheckSpendableResponse, Id, KeySet, KeySetInfo, KeySetResponse,
Amount, CheckSpendableRequest, CheckSpendableResponse, Id, KeySet, KeySetResponse,
KeysResponse, MeltRequest, MeltResponse, MintKeySet, MintRequest, PostMintResponse, Secret,
SplitRequest, SplitResponse,
};
use cashu_sdk::mint::Mint as MintSdk;

use crate::error::Result;
use crate::types::MintKeySetInfo;

pub struct Mint {
inner: RwLock<MintSdk>,
Expand All @@ -17,10 +18,9 @@ pub struct Mint {
impl Mint {
pub fn new(
secret: String,
derivation_path: String,
inactive_keysets: Vec<Arc<KeySetInfo>>,
active_keyset_info: Arc<MintKeySetInfo>,
inactive_keysets: Vec<Arc<MintKeySetInfo>>,
spent_secrets: Vec<Arc<Secret>>,
max_order: u8,
min_fee_reserve: Arc<Amount>,
percent_fee_reserve: f32,
) -> Result<Self> {
Expand All @@ -37,10 +37,9 @@ impl Mint {
Ok(Self {
inner: MintSdk::new(
&secret,
&derivation_path,
active_keyset_info.as_ref().deref().clone(),
inactive_keysets,
spent_secrets,
max_order,
*min_fee_reserve.as_ref().deref(),
percent_fee_reserve,
)
Expand Down
45 changes: 45 additions & 0 deletions bindings/cashu-sdk-ffi/src/types/keyset_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use std::ops::Deref;
use std::sync::Arc;

use cashu_sdk::types::KeysetInfo as KeySetInfoSdk;

use crate::Id;

pub struct KeySetInfo {
inner: KeySetInfoSdk,
}

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

impl From<KeySetInfoSdk> for KeySetInfo {
fn from(inner: KeySetInfoSdk) -> KeySetInfo {
KeySetInfo { inner }
}
}

impl KeySetInfo {
pub fn new(
id: Arc<Id>,
symbol: String,
valid_from: u64,
valid_to: Option<u64>,
derivation_path: String,
max_order: u8,
) -> Self {
Self {
inner: KeySetInfoSdk {
id: *id.as_ref().deref(),
symbol,
valid_from,
valid_to,
derivation_path,
max_order,
},
}
}
}
2 changes: 2 additions & 0 deletions bindings/cashu-sdk-ffi/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
pub mod keyset_info;
pub mod melted;
pub mod proofs_status;
pub mod send_proofs;

pub use keyset_info::KeySetInfo as MintKeySetInfo;
pub use melted::Melted;
pub use proofs_status::ProofsStatus;
pub use send_proofs::SendProofs;
8 changes: 4 additions & 4 deletions bindings/cashu-sdk-js/src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,22 @@ impl JsMint {
#[wasm_bindgen(constructor)]
pub fn new(
secret: String,
derivation_path: String,
active_keyset_info: JsValue,
inactive_keyset: JsValue,
spent_secrets: JsValue,
max_order: u8,
min_fee_reserve: JsAmount,
percent_fee_reserve: f32,
) -> Result<JsMint> {
let active_keyset_info =
serde_wasm_bindgen::from_value(active_keyset_info).map_err(into_err)?;
let inactive_keyset = serde_wasm_bindgen::from_value(inactive_keyset).map_err(into_err)?;
let spent_secrets = serde_wasm_bindgen::from_value(spent_secrets).map_err(into_err)?;
Ok(JsMint {
inner: Mint::new(
&secret,
&derivation_path,
active_keyset_info,
inactive_keyset,
spent_secrets,
max_order,
*min_fee_reserve.deref(),
percent_fee_reserve,
),
Expand Down
Loading

0 comments on commit 6e80a1d

Please sign in to comment.