From 1c045ff62a5f01e589e49ec5f20e3048b92f444e Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Tue, 2 Apr 2024 20:18:44 +0100 Subject: [PATCH] feat(cashu_sdk): verify token dleq --- crates/cashu-sdk/src/wallet/mod.rs | 31 ++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/crates/cashu-sdk/src/wallet/mod.rs b/crates/cashu-sdk/src/wallet/mod.rs index 84b973d9..812b1c1b 100644 --- a/crates/cashu-sdk/src/wallet/mod.rs +++ b/crates/cashu-sdk/src/wallet/mod.rs @@ -1133,6 +1133,37 @@ impl Wallet { } Ok(restored_value) } + + /// Verify all proofs in token have a valid DLEQ proof + #[cfg(feature = "nut12")] + pub async fn verify_token_dleq(&self, token: Token) -> Result<(), Error> { + let mut keys_cache: HashMap = HashMap::new(); + + for mint_proof in token.token { + let mint_url = mint_proof.mint; + + for proof in mint_proof.proofs { + let mint_pubkey = match keys_cache.get(&proof.keyset_id) { + Some(keys) => keys.amount_key(proof.amount), + None => { + let keys = self.get_keyset_keys(&mint_url, proof.keyset_id).await?; + + let key = keys.amount_key(proof.amount); + keys_cache.insert(proof.keyset_id, keys); + + key + } + } + .ok_or(Error::UnknownKey)?; + + proof + .verify_dleq(&mint_pubkey) + .map_err(|_| Error::CouldNotVerifyDleq)?; + } + } + + Ok(()) + } } /*