Browse Source

check for proofs when creating token

thesimplekid 1 year ago
parent
commit
4a7ea24be0
4 changed files with 25 additions and 7 deletions
  1. 4 4
      crates/cashu-sdk/src/wallet.rs
  2. 4 0
      crates/cashu/src/error.rs
  3. 11 3
      crates/cashu/src/nuts/nut00.rs
  4. 6 0
      justfile

+ 4 - 4
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<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()?)
     }
 }
 

+ 4 - 0
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",),
             }
         }
     }

+ 11 - 3
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<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) {

+ 6 - 0
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