From 571baede0e3fb4d60c1ece20a5a46da7ea6529b8 Mon Sep 17 00:00:00 2001 From: /alex/ Date: Sat, 29 Jul 2023 12:06:41 +0200 Subject: [PATCH] Return error on secret manager mismatch (#946) * Enable CI workflows (#941) * return error on mismatch * update changelog --------- Co-authored-by: Thibault Martinez --- sdk/CHANGELOG.md | 1 + sdk/examples/client/stronghold.rs | 2 +- sdk/src/wallet/core/operations/stronghold.rs | 34 ++++++++++++-------- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/sdk/CHANGELOG.md b/sdk/CHANGELOG.md index 76b8c9337c..f2657b58eb 100644 --- a/sdk/CHANGELOG.md +++ b/sdk/CHANGELOG.md @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - `StrongholdAdapterBuilder` updated to be slightly more ergonomic; +- `Wallet::{set_stronghold_password, change_stronghold_password, set_stronghold_password_clear_interval, store_mnemonic}` return an `Err` instead of `Ok` in case of a non-stronghold secret manager; ## 1.0.1 - 2023-07-25 diff --git a/sdk/examples/client/stronghold.rs b/sdk/examples/client/stronghold.rs index a16b52ad35..50ed797069 100644 --- a/sdk/examples/client/stronghold.rs +++ b/sdk/examples/client/stronghold.rs @@ -5,7 +5,7 @@ //! //! Rename `.env.example` to `.env` first, then run the command: //! ```sh -//! cargo run --release --all-features --example client_stronghold +//! cargo run --release --all-features --example stronghold //! ``` use iota_sdk::{ diff --git a/sdk/src/wallet/core/operations/stronghold.rs b/sdk/src/wallet/core/operations/stronghold.rs index b3d997fc03..d3c72db45c 100644 --- a/sdk/src/wallet/core/operations/stronghold.rs +++ b/sdk/src/wallet/core/operations/stronghold.rs @@ -17,8 +17,10 @@ impl Wallet { if let SecretManager::Stronghold(stronghold) = &mut *self.secret_manager.write().await { stronghold.set_password(password).await?; + Ok(()) + } else { + Err(crate::client::Error::SecretManagerMismatch.into()) } - Ok(()) } /// Change the Stronghold password to another one and also re-encrypt the values in the loaded snapshot with it. @@ -33,44 +35,50 @@ impl Wallet { if let SecretManager::Stronghold(stronghold) = &mut *self.secret_manager.write().await { stronghold.set_password(current_password).await?; stronghold.change_password(new_password).await?; + Ok(()) + } else { + Err(crate::client::Error::SecretManagerMismatch.into()) } - Ok(()) } /// Sets the Stronghold password clear interval pub async fn set_stronghold_password_clear_interval(&self, timeout: Option) -> crate::wallet::Result<()> { if let SecretManager::Stronghold(stronghold) = &mut *self.secret_manager.write().await { stronghold.set_timeout(timeout).await; + Ok(()) + } else { + Err(crate::client::Error::SecretManagerMismatch.into()) } - Ok(()) } /// Stores a mnemonic into the Stronghold vault pub async fn store_mnemonic(&self, mnemonic: Mnemonic) -> crate::wallet::Result<()> { if let SecretManager::Stronghold(stronghold) = &mut *self.secret_manager.write().await { stronghold.store_mnemonic(mnemonic).await?; + Ok(()) + } else { + Err(crate::client::Error::SecretManagerMismatch.into()) } - Ok(()) } /// Clears the Stronghold password from memory. pub async fn clear_stronghold_password(&self) -> crate::wallet::Result<()> { log::debug!("[clear_stronghold_password]"); - let mut secret_manager = self.secret_manager.write().await; - match &mut *secret_manager { - SecretManager::Stronghold(stronghold) => stronghold.clear_key().await, - _ => return Err(crate::client::Error::SecretManagerMismatch.into()), + if let SecretManager::Stronghold(stronghold) = &mut *self.secret_manager.write().await { + stronghold.clear_key().await; + Ok(()) + } else { + Err(crate::client::Error::SecretManagerMismatch.into()) } - Ok(()) } /// Checks if the Stronghold password is available. pub async fn is_stronghold_password_available(&self) -> crate::wallet::Result { log::debug!("[is_stronghold_password_available]"); - let mut secret_manager = self.secret_manager.write().await; - match &mut *secret_manager { - SecretManager::Stronghold(stronghold) => Ok(stronghold.is_key_available().await), - _ => Err(crate::client::Error::SecretManagerMismatch.into()), + if let SecretManager::Stronghold(stronghold) = &*self.secret_manager.write().await { + Ok(stronghold.is_key_available().await) + } else { + Err(crate::client::Error::SecretManagerMismatch.into()) } } }