From 9a0557d4974cbf3854c458fbd0af7a135b3edbe7 Mon Sep 17 00:00:00 2001 From: Alex Coats Date: Wed, 27 Sep 2023 13:34:28 -0400 Subject: [PATCH 1/3] clean up storage constant usage --- sdk/src/wallet/core/builder.rs | 2 +- sdk/src/wallet/core/operations/storage.rs | 19 ++++++++++--------- .../stronghold_backup/stronghold_snapshot.rs | 11 +---------- sdk/src/wallet/migration/chrysalis.rs | 11 +---------- 4 files changed, 13 insertions(+), 30 deletions(-) diff --git a/sdk/src/wallet/core/builder.rs b/sdk/src/wallet/core/builder.rs index 31e2bba876..0ee6ff7718 100644 --- a/sdk/src/wallet/core/builder.rs +++ b/sdk/src/wallet/core/builder.rs @@ -292,7 +292,7 @@ pub(crate) mod dto { #[cfg(feature = "storage")] use crate::{client::secret::SecretManage, wallet::storage::StorageOptions}; - #[derive(Debug, Deserialize)] + #[derive(Default, Debug, Deserialize)] #[serde(rename_all = "camelCase")] pub struct WalletBuilderDto { #[serde(default, skip_serializing_if = "Option::is_none")] diff --git a/sdk/src/wallet/core/operations/storage.rs b/sdk/src/wallet/core/operations/storage.rs index d50b42e135..6ce659ca4e 100644 --- a/sdk/src/wallet/core/operations/storage.rs +++ b/sdk/src/wallet/core/operations/storage.rs @@ -52,18 +52,19 @@ mod storage_stub { storage: &impl StorageAdapter, ) -> crate::wallet::Result> { log::debug!("get_wallet_data"); - if let Some(data) = storage.get::(WALLET_INDEXATION_KEY).await? { - log::debug!("get_wallet_data {data:?}"); + let data = storage.get::(WALLET_INDEXATION_KEY).await?; + log::debug!("get_wallet_data {data:?}"); - let secret_manager_dto = storage.get(SECRET_MANAGER_KEY).await?; - log::debug!("get_secret_manager {secret_manager_dto:?}"); + let secret_manager_dto = storage.get(SECRET_MANAGER_KEY).await?; + log::debug!("get_secret_manager {secret_manager_dto:?}"); - Ok(Some(Self::from(data).with_secret_manager( - secret_manager_dto.map(|dto| S::from_config(&dto)).transpose()?, - ))) - } else { - Ok(None) + if data.is_none() && secret_manager_dto.is_none() { + return Ok(None); } + + Ok(Some(Self::from(data.unwrap_or_default()).with_secret_manager( + secret_manager_dto.map(|dto| S::from_config(&dto)).transpose()?, + ))) } } diff --git a/sdk/src/wallet/core/operations/stronghold_backup/stronghold_snapshot.rs b/sdk/src/wallet/core/operations/stronghold_backup/stronghold_snapshot.rs index 010a0524eb..799dd3139b 100644 --- a/sdk/src/wallet/core/operations/stronghold_backup/stronghold_snapshot.rs +++ b/sdk/src/wallet/core/operations/stronghold_backup/stronghold_snapshot.rs @@ -5,8 +5,7 @@ use std::{collections::HashMap, path::Path, sync::atomic::Ordering}; use crate::{ client::{ - constants::IOTA_COIN_TYPE, secret::SecretManagerConfig, storage::StorageAdapter, stronghold::StrongholdAdapter, - Error as ClientError, + secret::SecretManagerConfig, storage::StorageAdapter, stronghold::StrongholdAdapter, Error as ClientError, }, types::TryFromDto, wallet::{ @@ -19,7 +18,6 @@ use crate::{ }, }; -pub(crate) const WALLET_INDEXATION_KEY: &str = "iota-wallet-account-manager"; pub(crate) const CLIENT_OPTIONS_KEY: &str = "client_options"; pub(crate) const COIN_TYPE_KEY: &str = "coin_type"; pub(crate) const SECRET_MANAGER_KEY: &str = "secret_manager"; @@ -176,13 +174,6 @@ pub(crate) async fn migrate_snapshot_from_chrysalis_to_stardust( .await?; if let Some(secret_manager_dto) = secret_manager_dto { - // This is required for the secret manager to be loaded - stronghold_adapter - .set( - WALLET_INDEXATION_KEY, - format!("{{ \"coinType\": {IOTA_COIN_TYPE}}}").as_bytes(), - ) - .await?; stronghold_adapter .set_bytes(SECRET_MANAGER_KEY, secret_manager_dto.as_bytes()) .await?; diff --git a/sdk/src/wallet/migration/chrysalis.rs b/sdk/src/wallet/migration/chrysalis.rs index db53b66bb8..79ace4aa4d 100644 --- a/sdk/src/wallet/migration/chrysalis.rs +++ b/sdk/src/wallet/migration/chrysalis.rs @@ -217,9 +217,7 @@ pub(crate) mod rocksdb { wallet::{ migration::{MigrationData, MIGRATION_VERSION_KEY}, storage::{ - constants::{ - ACCOUNTS_INDEXATION_KEY, ACCOUNT_INDEXATION_KEY, SECRET_MANAGER_KEY, WALLET_INDEXATION_KEY, - }, + constants::{ACCOUNTS_INDEXATION_KEY, ACCOUNT_INDEXATION_KEY, SECRET_MANAGER_KEY}, StorageManager, }, }, @@ -281,13 +279,6 @@ pub(crate) mod rocksdb { } if let Some(secret_manager_dto) = secret_manager_dto { - // This is required for the secret manager to be loaded - stardust_storage - .set( - WALLET_INDEXATION_KEY, - &serde_json::from_str::(&format!("{{ \"coinType\": {IOTA_COIN_TYPE}}}"))?, - ) - .await?; stardust_storage .set(SECRET_MANAGER_KEY, &serde_json::from_str::(&secret_manager_dto)?) .await?; From 2ff2d37e1a6db518abb942d8fc6ab147b14ad349 Mon Sep 17 00:00:00 2001 From: Alex Coats Date: Wed, 27 Sep 2023 14:19:07 -0400 Subject: [PATCH 2/3] fix tests --- sdk/tests/wallet/chrysalis_migration.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sdk/tests/wallet/chrysalis_migration.rs b/sdk/tests/wallet/chrysalis_migration.rs index a3d831fb32..92bea75dcb 100644 --- a/sdk/tests/wallet/chrysalis_migration.rs +++ b/sdk/tests/wallet/chrysalis_migration.rs @@ -36,6 +36,7 @@ async fn migrate_chrysalis_db() -> Result<()> { let wallet = Wallet::builder() .with_storage_path("migrate_chrysalis_db") .with_client_options(client_options) + .with_coin_type(IOTA_COIN_TYPE) .finish() .await?; @@ -126,6 +127,7 @@ async fn migrate_chrysalis_db_encrypted() -> Result<()> { let wallet = Wallet::builder() .with_storage_path("migrate_chrysalis_db_encrypted") .with_client_options(client_options) + .with_coin_type(IOTA_COIN_TYPE) .finish() .await?; @@ -212,6 +214,7 @@ async fn migrate_chrysalis_db_encrypted_encrypt_new() -> Result<()> { .with_encryption_key([0u8; 32]), ) .with_client_options(client_options) + .with_coin_type(IOTA_COIN_TYPE) .finish() .await?; From 084e7c5f3fdaba75dd09c49766d998a6698d1d23 Mon Sep 17 00:00:00 2001 From: Alex Coats Date: Thu, 28 Sep 2023 08:18:28 -0400 Subject: [PATCH 3/3] fix merged test --- sdk/tests/wallet/chrysalis_migration.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/sdk/tests/wallet/chrysalis_migration.rs b/sdk/tests/wallet/chrysalis_migration.rs index d591b362f5..8ff3459eac 100644 --- a/sdk/tests/wallet/chrysalis_migration.rs +++ b/sdk/tests/wallet/chrysalis_migration.rs @@ -404,6 +404,7 @@ async fn migrate_chrysalis_db_ledger() -> Result<()> { let wallet = Wallet::builder() .with_storage_path("migrate_chrysalis_db_ledger") .with_client_options(client_options) + .with_coin_type(IOTA_COIN_TYPE) .finish() .await?;