From 6e80a1d628804906e691e0ebb7af07a420ad4a04 Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Sat, 25 Nov 2023 20:43:52 +0000 Subject: [PATCH] refactor: add symbol to keyset_info --- bindings/cashu-ffi/src/cashu.udl | 10 ++--- bindings/cashu-ffi/src/nuts/nut02/key_set.rs | 24 +++++++--- .../cashu-ffi/src/nuts/nut02/mint_keyset.rs | 9 +++- bindings/cashu-ffi/src/types/keyset_info.rs | 17 ++----- bindings/cashu-js/src/nuts/nut02/keyset.rs | 6 ++- .../cashu-js/src/nuts/nut02/mint_keyset.rs | 9 +++- bindings/cashu-sdk-ffi/src/cashu_sdk.udl | 16 ++++--- bindings/cashu-sdk-ffi/src/lib.rs | 2 +- bindings/cashu-sdk-ffi/src/mint.rs | 11 +++-- .../cashu-sdk-ffi/src/types/keyset_info.rs | 45 +++++++++++++++++++ bindings/cashu-sdk-ffi/src/types/mod.rs | 2 + bindings/cashu-sdk-js/src/mint.rs | 8 ++-- crates/cashu-sdk/src/mint.rs | 42 +++++++++-------- crates/cashu/src/nuts/mod.rs | 2 +- crates/cashu/src/types.rs | 13 +++++- 15 files changed, 145 insertions(+), 71 deletions(-) create mode 100644 bindings/cashu-sdk-ffi/src/types/keyset_info.rs diff --git a/bindings/cashu-ffi/src/cashu.udl b/bindings/cashu-ffi/src/cashu.udl index 65b5f12c7..e28cefb66 100644 --- a/bindings/cashu-ffi/src/cashu.udl +++ b/bindings/cashu-ffi/src/cashu.udl @@ -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); }; @@ -140,8 +140,8 @@ interface KeysResponse { }; interface KeySetResponse { - constructor(sequence keyset_ids); - sequence keyset_ids(); + constructor(sequence keysets); + sequence keysets(); }; interface RequestMintResponse { @@ -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); }; diff --git a/bindings/cashu-ffi/src/nuts/nut02/key_set.rs b/bindings/cashu-ffi/src/nuts/nut02/key_set.rs index 49bef9e50..94e3a012a 100644 --- a/bindings/cashu-ffi/src/nuts/nut02/key_set.rs +++ b/bindings/cashu-ffi/src/nuts/nut02/key_set.rs @@ -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, @@ -19,7 +21,7 @@ impl Deref for Id { impl Id { pub fn new(id: String) -> Result { Ok(Self { - inner: IdSdk::try_from_base64(&id)?, + inner: IdSdk::from_str(&id)?, }) } } @@ -48,10 +50,11 @@ impl Deref for KeySet { } impl KeySet { - pub fn new(id: Arc, keys: Arc) -> Self { + pub fn new(id: Arc, symbol: String, keys: Arc) -> Self { Self { inner: KeySetSdk { id: *id.as_ref().deref(), + symbol, keys: keys.as_ref().deref().clone(), }, } @@ -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 { Arc::new(self.inner.keys.clone().into()) } @@ -77,19 +84,22 @@ pub struct KeySetResponse { } impl KeySetResponse { - pub fn new(keyset_ids: Vec>) -> Self { - let keysets = keyset_ids.into_iter().map(|id| id.inner).collect(); + pub fn new(keyset_ids: Vec>) -> 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> { + pub fn keysets(&self) -> Vec> { self.inner .clone() .keysets .into_iter() - .map(|id| Arc::new(id.into())) + .map(|keyset_info| Arc::new(keyset_info.into())) .collect() } } diff --git a/bindings/cashu-ffi/src/nuts/nut02/mint_keyset.rs b/bindings/cashu-ffi/src/nuts/nut02/mint_keyset.rs index ccd6e3674..ce6c3ad9c 100644 --- a/bindings/cashu-ffi/src/nuts/nut02/mint_keyset.rs +++ b/bindings/cashu-ffi/src/nuts/nut02/mint_keyset.rs @@ -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), } } } diff --git a/bindings/cashu-ffi/src/types/keyset_info.rs b/bindings/cashu-ffi/src/types/keyset_info.rs index d248be35b..3b6ac2d90 100644 --- a/bindings/cashu-ffi/src/types/keyset_info.rs +++ b/bindings/cashu-ffi/src/types/keyset_info.rs @@ -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; @@ -23,22 +23,11 @@ impl From for KeySetInfo { } impl KeySetInfo { - pub fn new( - id: Arc, - valid_from: u64, - valid_to: Option, - secret: String, - derivation_path: String, - max_order: u8, - ) -> Self { + pub fn new(id: Arc, symbol: String) -> Self { Self { inner: KeySetInfoSdk { id: *id.as_ref().deref(), - valid_from, - valid_to, - secret, - derivation_path, - max_order, + symbol, }, } } diff --git a/bindings/cashu-js/src/nuts/nut02/keyset.rs b/bindings/cashu-js/src/nuts/nut02/keyset.rs index e4bd31e9f..886bd9d24 100644 --- a/bindings/cashu-js/src/nuts/nut02/keyset.rs +++ b/bindings/cashu-js/src/nuts/nut02/keyset.rs @@ -1,4 +1,5 @@ use std::ops::Deref; +use std::str::FromStr; use cashu::nuts::{Id, KeySet, KeysResponse, KeysetResponse}; use wasm_bindgen::prelude::*; @@ -30,7 +31,7 @@ impl JsId { #[wasm_bindgen(js_name = tryFromBase64)] pub fn try_from_base64(id: String) -> Result { Ok(JsId { - inner: Id::try_from_base64(&id).map_err(into_err)?, + inner: Id::from_str(&id).map_err(into_err)?, }) } @@ -63,10 +64,11 @@ impl From 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(), }, } diff --git a/bindings/cashu-js/src/nuts/nut02/mint_keyset.rs b/bindings/cashu-js/src/nuts/nut02/mint_keyset.rs index 3cdde4d6d..60862db56 100644 --- a/bindings/cashu-js/src/nuts/nut02/mint_keyset.rs +++ b/bindings/cashu-js/src/nuts/nut02/mint_keyset.rs @@ -25,9 +25,14 @@ impl From 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), } } } diff --git a/bindings/cashu-sdk-ffi/src/cashu_sdk.udl b/bindings/cashu-sdk-ffi/src/cashu_sdk.udl index 6e4287466..52ff22467 100644 --- a/bindings/cashu-sdk-ffi/src/cashu_sdk.udl +++ b/bindings/cashu-sdk-ffi/src/cashu_sdk.udl @@ -127,7 +127,7 @@ interface Keys { }; interface KeySet { - constructor(Id id, Keys keys); + constructor(Id id, string symbol, Keys keys); Id id(); Keys keys(); }; @@ -135,7 +135,7 @@ interface KeySet { interface MintKeySet { [Name=generate] - constructor(string secret, string derivation_path, u8 max_order); + constructor(string secret, string symbol, string derivation_path, u8 max_order); }; @@ -144,8 +144,8 @@ interface KeysResponse { }; interface KeySetResponse { - constructor(sequence keyset_ids); - sequence keyset_ids(); + constructor(sequence keysets); + sequence keysets(); }; interface RequestMintResponse { @@ -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 { @@ -315,7 +319,7 @@ interface Wallet { interface Mint { [Throws=CashuSdkError] - constructor(string secret, string derivation_path, sequence inactive_keysets, sequence spent_secrets, u8 max_order, Amount min_fee_reserve, f32 percent_fee_reserve); + constructor(string secret, MintKeySetInfo active_keyset_info, sequence inactive_keysets, sequence spent_secrets, Amount min_fee_reserve, f32 percent_fee_reserve); KeysResponse active_keyset_pubkeys(); KeySetResponse keysets(); MintKeySet active_keyset(); diff --git a/bindings/cashu-sdk-ffi/src/lib.rs b/bindings/cashu-sdk-ffi/src/lib.rs index f84d29548..9329550a7 100644 --- a/bindings/cashu-sdk-ffi/src/lib.rs +++ b/bindings/cashu-sdk-ffi/src/lib.rs @@ -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 diff --git a/bindings/cashu-sdk-ffi/src/mint.rs b/bindings/cashu-sdk-ffi/src/mint.rs index cbded3e58..be48d045a 100644 --- a/bindings/cashu-sdk-ffi/src/mint.rs +++ b/bindings/cashu-sdk-ffi/src/mint.rs @@ -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, @@ -17,10 +18,9 @@ pub struct Mint { impl Mint { pub fn new( secret: String, - derivation_path: String, - inactive_keysets: Vec>, + active_keyset_info: Arc, + inactive_keysets: Vec>, spent_secrets: Vec>, - max_order: u8, min_fee_reserve: Arc, percent_fee_reserve: f32, ) -> Result { @@ -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, ) diff --git a/bindings/cashu-sdk-ffi/src/types/keyset_info.rs b/bindings/cashu-sdk-ffi/src/types/keyset_info.rs new file mode 100644 index 000000000..0b9c93daa --- /dev/null +++ b/bindings/cashu-sdk-ffi/src/types/keyset_info.rs @@ -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 for KeySetInfo { + fn from(inner: KeySetInfoSdk) -> KeySetInfo { + KeySetInfo { inner } + } +} + +impl KeySetInfo { + pub fn new( + id: Arc, + symbol: String, + valid_from: u64, + valid_to: Option, + derivation_path: String, + max_order: u8, + ) -> Self { + Self { + inner: KeySetInfoSdk { + id: *id.as_ref().deref(), + symbol, + valid_from, + valid_to, + derivation_path, + max_order, + }, + } + } +} diff --git a/bindings/cashu-sdk-ffi/src/types/mod.rs b/bindings/cashu-sdk-ffi/src/types/mod.rs index 88836ac75..cf604c97c 100644 --- a/bindings/cashu-sdk-ffi/src/types/mod.rs +++ b/bindings/cashu-sdk-ffi/src/types/mod.rs @@ -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; diff --git a/bindings/cashu-sdk-js/src/mint.rs b/bindings/cashu-sdk-js/src/mint.rs index 7c43f47ff..1f36cdb9e 100644 --- a/bindings/cashu-sdk-js/src/mint.rs +++ b/bindings/cashu-sdk-js/src/mint.rs @@ -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 { + 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, ), diff --git a/crates/cashu-sdk/src/mint.rs b/crates/cashu-sdk/src/mint.rs index ea08daf59..6b84c575d 100644 --- a/crates/cashu-sdk/src/mint.rs +++ b/crates/cashu-sdk/src/mint.rs @@ -14,7 +14,8 @@ use cashu::Amount; use tracing::debug; pub struct Mint { - // pub pubkey: PublicKey, + // pub pubkey: PublicKey + secret: String, pub active_keyset: nut02::mint::KeySet, pub active_keyset_info: KeysetInfo, pub inactive_keysets: HashMap, @@ -26,27 +27,24 @@ pub struct Mint { impl Mint { pub fn new( secret: &str, - derivation_path: &str, + active_keyset_info: KeysetInfo, inactive_keysets: HashSet, spent_secrets: HashSet, - max_order: u8, min_fee_reserve: Amount, percent_fee_reserve: f32, ) -> Self { - let active_keyset = nut02::mint::KeySet::generate(secret, derivation_path, max_order); - let id = active_keyset.id; + let active_keyset = nut02::mint::KeySet::generate( + secret, + active_keyset_info.symbol.clone(), + active_keyset_info.derivation_path.clone(), + active_keyset_info.max_order, + ); Self { + secret: secret.to_string(), active_keyset, inactive_keysets: inactive_keysets.into_iter().map(|ks| (ks.id, ks)).collect(), - active_keyset_info: KeysetInfo { - id, - valid_from: 0, - valid_to: None, - secret: secret.to_string(), - derivation_path: derivation_path.to_string(), - max_order, - }, + active_keyset_info, spent_secrets, pending_secrets: HashSet::new(), fee_reserve: FeeReserve { @@ -66,8 +64,12 @@ impl Mint { /// Return a list of all supported keysets pub fn keysets(&self) -> KeysetResponse { - let mut keysets: HashSet<_> = self.inactive_keysets.keys().cloned().collect(); - keysets.insert(self.active_keyset.id); + let mut keysets: HashSet<_> = self.inactive_keysets.values().cloned().collect(); + + keysets.insert(self.active_keyset_info.clone()); + + let keysets = keysets.into_iter().map(|k| k.into()).collect(); + KeysetResponse { keysets } } @@ -81,7 +83,8 @@ impl Mint { } self.inactive_keysets.get(id).map(|k| { - nut02::mint::KeySet::generate(&k.secret, &k.derivation_path, k.max_order).into() + nut02::mint::KeySet::generate(&self.secret, &k.symbol, &k.derivation_path, k.max_order) + .into() }) } @@ -89,7 +92,7 @@ impl Mint { /// Generate new keyset pub fn rotate_keyset( &mut self, - secret: impl Into, + symbol: impl Into, derivation_path: impl Into, max_order: u8, ) { @@ -97,7 +100,7 @@ impl Mint { self.inactive_keysets .insert(self.active_keyset.id, self.active_keyset_info.clone()); - self.active_keyset = MintKeySet::generate(secret, derivation_path, max_order); + self.active_keyset = MintKeySet::generate(&self.secret, symbol, derivation_path, max_order); } pub fn process_mint_request( @@ -207,7 +210,8 @@ impl Mint { |id| { if let Some(keyset) = self.inactive_keysets.get(id) { nut02::mint::KeySet::generate( - &keyset.secret, + &self.secret, + &keyset.symbol, &keyset.derivation_path, keyset.max_order, ) diff --git a/crates/cashu/src/nuts/mod.rs b/crates/cashu/src/nuts/mod.rs index c2bbf8f9b..7273f380b 100644 --- a/crates/cashu/src/nuts/mod.rs +++ b/crates/cashu/src/nuts/mod.rs @@ -17,7 +17,7 @@ pub use nut00::wallet::{BlindedMessages, Token}; pub use nut00::{BlindedMessage, BlindedSignature, Proof}; pub use nut01::{Keys, KeysResponse, PublicKey, SecretKey}; pub use nut02::mint::KeySet as MintKeySet; -pub use nut02::{Id, KeySet, KeysetResponse}; +pub use nut02::{Id, KeySet, KeySetInfo, KeysetResponse}; pub use nut03::RequestMintResponse; pub use nut04::{MintRequest, PostMintResponse}; pub use nut05::{CheckFeesRequest, CheckFeesResponse}; diff --git a/crates/cashu/src/types.rs b/crates/cashu/src/types.rs index ce2bb65f9..3e9d8c991 100644 --- a/crates/cashu/src/types.rs +++ b/crates/cashu/src/types.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; use crate::nuts::nut00::mint; -use crate::nuts::{Id, Proofs}; +use crate::nuts::{Id, KeySetInfo, Proofs}; #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct ProofsStatus { @@ -37,9 +37,18 @@ pub enum InvoiceStatus { #[derive(Debug, Hash, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct KeysetInfo { pub id: Id, + pub symbol: String, pub valid_from: u64, pub valid_to: Option, - pub secret: String, pub derivation_path: String, pub max_order: u8, } + +impl From for KeySetInfo { + fn from(keyset_info: KeysetInfo) -> Self { + Self { + id: keyset_info.id, + symbol: keyset_info.symbol, + } + } +}