Skip to content

Commit

Permalink
Return error on secret manager mismatch (#946)
Browse files Browse the repository at this point in the history
* Enable CI workflows (#941)

* return error on mismatch

* update changelog

---------

Co-authored-by: Thibault Martinez <[email protected]>
  • Loading branch information
Alex6323 and thibault-martinez committed Jul 29, 2023
1 parent 83a8c12 commit 571baed
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
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

0 comments on commit 571baed

Please sign in to comment.