From faff824d61d85c6a02630526ae4a015edb1ad9a0 Mon Sep 17 00:00:00 2001 From: Nander Stabel Date: Wed, 26 Feb 2025 23:35:21 +0100 Subject: [PATCH] fix: catch IOTA Client error --- agent_identity/src/document/aggregate.rs | 24 ++++++++++++++++++++++-- agent_identity/src/state.rs | 12 +++++++----- agent_secret_manager/src/lib.rs | 3 +-- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/agent_identity/src/document/aggregate.rs b/agent_identity/src/document/aggregate.rs index d8a70a58..6c939b15 100644 --- a/agent_identity/src/document/aggregate.rs +++ b/agent_identity/src/document/aggregate.rs @@ -13,7 +13,8 @@ use identity_iota::{ verification::{MethodScope, MethodType, VerificationMethod}, }; use iota_sdk::client::api::input_selection::Error::MissingInputWithEd25519Address; -use iota_sdk::client::error::Error::{InputAddressNotFound, InputSelection}; +use iota_sdk::client::error::Error::{Block, InputAddressNotFound, InputSelection}; +use iota_sdk::types::block::Error::InsufficientStorageDepositAmount; use iota_sdk::{ client::Client, types::block::{ @@ -109,13 +110,21 @@ impl Aggregate for Document { let controller = document.id().clone(); info!("Found an existing controller for DID method `{did_method}`: `{controller}`"); - // Create a new DID Document from skratch. + // Create a new DID Document from scratch. let document = IotaDocument::new_with_id(controller.clone()); + let rent_structure: RentStructure = + iota_client.get_rent_structure().await.map_err(IotaClientError)?; + // Update the DID Document output with the latest state. let alias_output: AliasOutput = iota_client.update_did_output(document).await.map_err(IotaClientError)?; + let alias_output: AliasOutput = AliasOutputBuilder::from(&alias_output) + .with_minimum_storage_deposit(rent_structure) + .finish() + .map_err(|err| AliasOutputBuilderError(err.to_string()))?; + // Publish the updated Alias Output and get the published DID document. let test_publish_result = iota_client .publish_did_output(stronghold_storage.as_secret_manager(), alias_output) @@ -138,6 +147,17 @@ impl Aggregate for Document { // that later on a new DID Document will be created using the current // wallet address. None + } else if let Block(InsufficientStorageDepositAmount { amount, required }) = + &**error + { + warn!( + "The current `{did_method}` DID `{controller}` has insufficient storage deposit amount: `{amount}`, \ + required: `{required}`." + ); + return Err(InsufficientDepositError( + network_name.to_string(), + wallet_address.to_string(), + )); } else { return Err(IotaClientError(test_publish_error)); } diff --git a/agent_identity/src/state.rs b/agent_identity/src/state.rs index a480bca6..92150ba9 100644 --- a/agent_identity/src/state.rs +++ b/agent_identity/src/state.rs @@ -193,12 +193,14 @@ async fn initialize_documents(state: &IdentityState) -> anyhow::Result<()> { if let Some((document_id, command)) = document_id_and_command { command_handler(&document_id, &state.command.document, command).await?; - let command = DocumentCommand::UpdatePublicKeys { - document_id: document_id.clone(), - public_key_jwks: vec![], - }; + if enabled { + let command = DocumentCommand::UpdatePublicKeys { + document_id: document_id.clone(), + public_key_jwks: vec![], + }; - command_handler(&document_id, &state.command.document, command).await?; + command_handler(&document_id, &state.command.document, command).await?; + } } } diff --git a/agent_secret_manager/src/lib.rs b/agent_secret_manager/src/lib.rs index 3f78c0df..e27c3dc9 100644 --- a/agent_secret_manager/src/lib.rs +++ b/agent_secret_manager/src/lib.rs @@ -8,7 +8,6 @@ use identity_iota::{ use iota_sdk::client::secret::stronghold::StrongholdSecretManager; use jsonwebtoken::Algorithm; use log::info; -use serde::Serialize; use std::collections::HashMap; pub mod service; @@ -52,7 +51,7 @@ impl StrongholdManager { } } -#[derive(Serialize, Clone, Eq, PartialEq, Hash)] +#[derive(Clone, Eq, PartialEq, Hash)] pub struct StorageKey { pub did_method: SupportedDidMethod, pub algorithm: Algorithm,