Skip to content

Commit

Permalink
Persist Stronghold's changes only when its handle is dropped
Browse files Browse the repository at this point in the history
  • Loading branch information
UMR1352 committed Apr 26, 2024
1 parent 614719b commit 096bb30
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
17 changes: 14 additions & 3 deletions identity_stronghold/src/stronghold_jwk_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ impl JwkStorage for StrongholdStorage {
.with_custom_message("stronghold public key procedure failed")
.with_source(err)
})?;
persist_changes(self, stronghold).await?;
let public_key: Vec<u8> = procedure_result.into();

let mut params = JwkParamsOkp::new();
Expand Down Expand Up @@ -188,7 +187,6 @@ impl JwkStorage for StrongholdStorage {
.with_custom_message("stronghold write secret failed")
.with_source(err)
})?;
persist_changes(self, stronghold).await?;

Ok(key_id)
}
Expand Down Expand Up @@ -263,7 +261,6 @@ impl JwkStorage for StrongholdStorage {
if !deleted {
return Err(KeyStorageError::new(KeyStorageErrorKind::KeyNotFound));
}
persist_changes(self, stronghold).await?;

Ok(())
}
Expand All @@ -283,3 +280,17 @@ impl JwkStorage for StrongholdStorage {
Ok(exists)
}
}

/// Calls `persist_changes` when `StrongholdStorage` gets dropped.
impl Drop for StrongholdStorage {
fn drop(&mut self) {
let secret_manager = std::mem::replace(&mut self.0, Arc::new(SecretManager::Placeholder));
tokio::spawn(async move {
let SecretManager::Stronghold(stronghold) = secret_manager.as_ref() else {
return;
};
let stronghold = stronghold.inner().await;
let _ = persist_changes(&secret_manager, stronghold).await;
});
}
}
2 changes: 0 additions & 2 deletions identity_stronghold/src/stronghold_jwk_storage_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ impl JwkStorageBbsPlusExt for StrongholdStorage {
.with_source(e)
})?;

persist_changes(self, stronghold).await?;

Ok(JwkGenOutput::new(kid, jwk))
}

Expand Down
6 changes: 3 additions & 3 deletions identity_stronghold/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use identity_storage::KeyStorageError;
use identity_storage::KeyStorageErrorKind;
use identity_storage::KeyStorageResult;
use identity_verification::jws::JwsAlgorithm;
use iota_sdk::client::secret::SecretManager;
use iota_stronghold::Client;
use iota_stronghold::ClientError;
use iota_stronghold::Stronghold;
use rand::distributions::DistString as _;
use tokio::sync::MutexGuard;

use crate::stronghold_key_type::StrongholdKeyType;
use crate::StrongholdStorage;

pub static IDENTITY_VAULT_PATH: &str = "iota_identity_vault";
pub static IDENTITY_CLIENT_PATH: &[u8] = b"iota_identity_client";
Expand Down Expand Up @@ -54,7 +54,7 @@ fn load_or_create_client(stronghold: &Stronghold) -> KeyStorageResult<Client> {
}

pub async fn persist_changes(
secret_manager: &StrongholdStorage,
secret_manager: &SecretManager,
stronghold: MutexGuard<'_, Stronghold>,
) -> KeyStorageResult<()> {
stronghold.write_client(IDENTITY_CLIENT_PATH).map_err(|err| {
Expand All @@ -65,7 +65,7 @@ pub async fn persist_changes(
// Must be dropped since `write_stronghold_snapshot` needs to acquire the stronghold lock.
drop(stronghold);

match secret_manager.as_secret_manager() {
match secret_manager {
iota_sdk::client::secret::SecretManager::Stronghold(stronghold_manager) => {
stronghold_manager
.write_stronghold_snapshot(None)
Expand Down

0 comments on commit 096bb30

Please sign in to comment.