mirror of
https://github.com/aljazceru/cdk.git
synced 2026-02-04 20:55:59 +01:00
refactor: add symbol to keyset_info
This commit is contained in:
@@ -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<Id> keyset_ids);
|
||||
sequence<Id> keyset_ids();
|
||||
constructor(sequence<KeySetInfo> keysets);
|
||||
sequence<KeySetInfo> 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);
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -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<Self> {
|
||||
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<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(),
|
||||
},
|
||||
}
|
||||
@@ -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())
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<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,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<JsId> {
|
||||
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<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(),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Id> keyset_ids);
|
||||
sequence<Id> keyset_ids();
|
||||
constructor(sequence<KeySetInfo> keysets);
|
||||
sequence<KeySetInfo> 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<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();
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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>,
|
||||
@@ -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> {
|
||||
@@ -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,
|
||||
)
|
||||
|
||||
45
bindings/cashu-sdk-ffi/src/types/keyset_info.rs
Normal file
45
bindings/cashu-sdk-ffi/src/types/keyset_info.rs
Normal file
@@ -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,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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,
|
||||
),
|
||||
|
||||
@@ -14,7 +14,8 @@ use cashu::Amount;
|
||||
use tracing::{debug, info};
|
||||
|
||||
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<Id, KeysetInfo>,
|
||||
@@ -26,27 +27,24 @@ pub struct Mint {
|
||||
impl Mint {
|
||||
pub fn new(
|
||||
secret: &str,
|
||||
derivation_path: &str,
|
||||
active_keyset_info: KeysetInfo,
|
||||
inactive_keysets: HashSet<KeysetInfo>,
|
||||
spent_secrets: HashSet<Secret>,
|
||||
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<String>,
|
||||
symbol: impl Into<String>,
|
||||
derivation_path: impl Into<String>,
|
||||
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,
|
||||
)
|
||||
|
||||
@@ -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};
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::nuts::{Id, Proofs};
|
||||
use crate::nuts::{Id, KeySetInfo, Proofs};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct ProofsStatus {
|
||||
@@ -36,9 +36,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<u64>,
|
||||
pub secret: String,
|
||||
pub derivation_path: String,
|
||||
pub max_order: u8,
|
||||
}
|
||||
|
||||
impl From<KeysetInfo> for KeySetInfo {
|
||||
fn from(keyset_info: KeysetInfo) -> Self {
|
||||
Self {
|
||||
id: keyset_info.id,
|
||||
symbol: keyset_info.symbol,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user