Skip to content

Commit

Permalink
cashu-sdk improve: use Id type
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Sep 9, 2023
1 parent 4e3268a commit 6cde1e7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
9 changes: 5 additions & 4 deletions crates/cashu-sdk/src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use cashu::nuts::nut00::BlindedMessage;
use cashu::nuts::nut00::BlindedSignature;
use cashu::nuts::nut00::Proof;
use cashu::nuts::nut02::mint::KeySet;
use cashu::nuts::nut02::Id;
use cashu::nuts::nut06::SplitRequest;
use cashu::nuts::nut06::SplitResponse;
use cashu::nuts::nut07::CheckSpendableRequest;
Expand All @@ -20,7 +21,7 @@ use cashu::Amount;
pub struct Mint {
// pub pubkey: PublicKey,
pub active_keyset: nut02::mint::KeySet,
pub inactive_keysets: HashMap<String, nut02::mint::KeySet>,
pub inactive_keysets: HashMap<Id, nut02::mint::KeySet>,
pub spent_secrets: HashSet<Secret>,
pub pending_secrets: HashSet<Secret>,
}
Expand All @@ -29,7 +30,7 @@ impl Mint {
pub fn new(
secret: &str,
derivation_path: &str,
inactive_keysets: HashMap<String, nut02::mint::KeySet>,
inactive_keysets: HashMap<Id, nut02::mint::KeySet>,
spent_secrets: HashSet<Secret>,
max_order: u8,
) -> Self {
Expand Down Expand Up @@ -58,8 +59,8 @@ impl Mint {
self.active_keyset.clone()
}

pub fn keyset(&self, id: &str) -> Option<nut02::KeySet> {
if self.active_keyset.id == id {
pub fn keyset(&self, id: &Id) -> Option<nut02::KeySet> {
if self.active_keyset.id.eq(id) {
return Some(self.active_keyset.clone().into());
}

Expand Down
19 changes: 13 additions & 6 deletions crates/cashu/src/nuts/nut00.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{secret::Secret, serde_utils::serde_url};
use serde::{Deserialize, Serialize};

use super::nut01::PublicKey;
use super::nut02::Id;

/// Blinded Message [NUT-00]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
Expand Down Expand Up @@ -182,7 +183,7 @@ impl MintProofs {
/// Promise (BlindedSignature) [NUT-00]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BlindedSignature {
pub id: String,
pub id: Id,
pub amount: Amount,
/// blinded signature (C_) on the secret message `B_` of [BlindedMessage]
#[serde(rename = "C_")]
Expand All @@ -200,7 +201,7 @@ pub struct Proof {
#[serde(rename = "C")]
pub c: PublicKey,
/// `Keyset id`
pub id: Option<String>,
pub id: Option<Id>,
}

/// List of proofs
Expand All @@ -220,7 +221,7 @@ impl From<Proof> for mint::Proof {
pub mod mint {
use serde::{Deserialize, Serialize};

use crate::{secret::Secret, Amount};
use crate::{nuts::nut02::Id, secret::Secret, Amount};

use super::PublicKey;

Expand All @@ -235,7 +236,7 @@ pub mod mint {
#[serde(rename = "C")]
pub c: Option<PublicKey>,
/// `Keyset id`
pub id: Option<String>,
pub id: Option<Id>,
}

/// List of proofs
Expand All @@ -259,7 +260,10 @@ mod tests {
let proof = "[{\"id\":\"DSAl9nvvyfva\",\"amount\":2,\"secret\":\"EhpennC9qB3iFlW8FZ_pZw\",\"C\":\"02c020067db727d586bc3183aecf97fcb800c3f4cc4759f69c626c9db5d8f5b5d4\"},{\"id\":\"DSAl9nvvyfva\",\"amount\":8,\"secret\":\"TmS6Cv0YT5PU_5ATVKnukw\",\"C\":\"02ac910bef28cbe5d7325415d5c263026f15f9b967a079ca9779ab6e5c2db133a7\"}]";
let proof: Proofs = serde_json::from_str(proof).unwrap();

assert_eq!(proof[0].clone().id.unwrap(), "DSAl9nvvyfva");
assert_eq!(
proof[0].clone().id.unwrap(),
Id::try_from_base64("DSAl9nvvyfva").unwrap()
);
}

#[test]
Expand All @@ -271,7 +275,10 @@ mod tests {
token.token[0].mint,
Url::from_str("https://8333.space:3338").unwrap()
);
assert_eq!(token.token[0].proofs[0].clone().id.unwrap(), "DSAl9nvvyfva");
assert_eq!(
token.token[0].proofs[0].clone().id.unwrap(),
Id::try_from_base64("DSAl9nvvyfva").unwrap()
);

let encoded = &token.convert_to_string().unwrap();

Expand Down

0 comments on commit 6cde1e7

Please sign in to comment.