From 4a7ea24be00c67c7d49f7d07772cc93ee4723158 Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Mon, 28 Aug 2023 11:24:27 +0100 Subject: [PATCH] check for proofs when creating token --- crates/cashu-sdk/src/wallet.rs | 8 ++++---- crates/cashu/src/error.rs | 4 ++++ crates/cashu/src/nuts/nut00.rs | 14 +++++++++++--- justfile | 6 ++++++ 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/crates/cashu-sdk/src/wallet.rs b/crates/cashu-sdk/src/wallet.rs index bfa63e25..278d99ec 100644 --- a/crates/cashu-sdk/src/wallet.rs +++ b/crates/cashu-sdk/src/wallet.rs @@ -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 @@ -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 @@ -515,12 +515,12 @@ impl Wallet { #[cfg(not(feature = "blocking"))] pub fn proofs_to_token(&self, proofs: Proofs, memo: Option) -> Result { - 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) -> Result { - 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()?) } } diff --git a/crates/cashu/src/error.rs b/crates/cashu/src/error.rs index 82161e7c..0eb4af81 100644 --- a/crates/cashu/src/error.rs +++ b/crates/cashu/src/error.rs @@ -92,6 +92,9 @@ pub mod wallet { Base64Error(base64::DecodeError), /// Unsupported Token UnsupportedToken, + /// Token Requires proofs + ProofsRequired, + /// Custom Error message CustomError(String), } @@ -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",), } } } diff --git a/crates/cashu/src/nuts/nut00.rs b/crates/cashu/src/nuts/nut00.rs index d5f6398e..1a6f740b 100644 --- a/crates/cashu/src/nuts/nut00.rs +++ b/crates/cashu/src/nuts/nut00.rs @@ -109,11 +109,19 @@ pub mod wallet { } impl Token { - pub fn new(mint_url: Url, proofs: Proofs, memo: Option) -> Self { - Self { + pub fn new( + mint_url: Url, + proofs: Proofs, + memo: Option, + ) -> Result { + 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) { diff --git a/justfile b/justfile index e69de29b..64b43c85 100644 --- a/justfile +++ b/justfile @@ -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 \ No newline at end of file