Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return error on secret manager mismatch #946

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/client/stronghold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down
34 changes: 21 additions & 13 deletions sdk/src/wallet/core/operations/stronghold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<Duration>) -> 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<bool> {
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())
}
}
}
Expand Down
Loading