Skip to content

Commit

Permalink
check for proofs when creating token
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Aug 28, 2023
1 parent f1d98a5 commit 4a7ea24
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
8 changes: 4 additions & 4 deletions crates/cashu-sdk/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl Wallet {
let proofs = self.mint(amount, hash).await?;

let token = Token::new(self.client.mint_url.clone(), proofs, None);
Ok(token)
Ok(token?)
}

/// Blocking Mint Token
Expand All @@ -135,7 +135,7 @@ impl Wallet {
let proofs = self.mint(amount, hash)?;

let token = Token::new(self.client.client.mint_url.clone(), proofs, None);
Ok(token)
Ok(token?)
}

/// Mint Proofs
Expand Down Expand Up @@ -515,12 +515,12 @@ impl Wallet {

#[cfg(not(feature = "blocking"))]
pub fn proofs_to_token(&self, proofs: Proofs, memo: Option<String>) -> Result<String, Error> {
Ok(Token::new(self.client.mint_url.clone(), proofs, memo).convert_to_string()?)
Ok(Token::new(self.client.mint_url.clone(), proofs, memo)?.convert_to_string()?)
}

#[cfg(feature = "blocking")]
pub fn proofs_to_token(&self, proofs: Proofs, memo: Option<String>) -> Result<String, Error> {
Ok(Token::new(self.client.client.mint_url.clone(), proofs, memo).convert_to_string()?)
Ok(Token::new(self.client.client.mint_url.clone(), proofs, memo)?.convert_to_string()?)
}
}

Expand Down
4 changes: 4 additions & 0 deletions crates/cashu/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ pub mod wallet {
Base64Error(base64::DecodeError),
/// Unsupported Token
UnsupportedToken,
/// Token Requires proofs
ProofsRequired,
/// Custom Error message
CustomError(String),
}

Expand All @@ -107,6 +110,7 @@ pub mod wallet {
Error::UnsupportedToken => write!(f, "Unsuppported Token"),
Error::EllipticError(err) => write!(f, "{}", err),
Error::SerdeJsonError(err) => write!(f, "{}", err),
Error::ProofsRequired => write!(f, "Token must have at least one proof",),
}
}
}
Expand Down
14 changes: 11 additions & 3 deletions crates/cashu/src/nuts/nut00.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,19 @@ pub mod wallet {
}

impl Token {
pub fn new(mint_url: Url, proofs: Proofs, memo: Option<String>) -> Self {
Self {
pub fn new(
mint_url: Url,
proofs: Proofs,
memo: Option<String>,
) -> Result<Self, wallet::Error> {
if proofs.is_empty() {
return Err(wallet::Error::ProofsRequired);
}

Ok(Self {
token: vec![MintProofs::new(mint_url, proofs)],
memo,
}
})
}

pub fn token_info(&self) -> (u64, String) {
Expand Down
6 changes: 6 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
precommit:
cargo check --no-default-features --features mint
cargo check --no-default-features --features wallet
cargo check --no-default-features --features blocking
typos
cargo test

0 comments on commit 4a7ea24

Please sign in to comment.