diff --git a/src/commands/vault.rs b/src/commands/vault.rs index 8d6de10f65b0529b449934d4e174cb7c25348151..2f53bbf5f44b32575407bbe25fa7b861ee37f127 100644 --- a/src/commands/vault.rs +++ b/src/commands/vault.rs @@ -729,7 +729,7 @@ where println!(); let vault_account = if let Some(existing_vault_account) = - vault_account::find_by_id(db_tx, &DbAccountId::from(address_to_import.clone())).await? + vault_account::find_by_id(db_tx, &DbAccountId::try_from(address_to_import.clone())?).await? { if existing_vault_account.is_base_account() { println!("You are trying to add {address_to_import} as a <Base> account while it already exists as a <Base> account."); @@ -853,7 +853,8 @@ where println!("Trying to create derivation with address '{derivation_address}'"); println!(); let vault_account = if let Some(existing_vault_account) = - vault_account::find_by_id(db_tx, &DbAccountId::from(derivation_address.clone())).await? + vault_account::find_by_id(db_tx, &DbAccountId::try_from(derivation_address.clone())?) + .await? { // Existing account println!("You are trying to derive '{derivation_path}' from parent '{parent_address}'"); @@ -922,7 +923,7 @@ where // Since links are made based on address / parent(address) we can just edit the existing entry and it should be fine let mut vault_account: ActiveModel = existing_vault_account.into(); vault_account.path = Set(Some(derivation_path.clone())); - vault_account.parent = Set(Some(DbAccountId::from(parent_address.clone()))); + vault_account.parent = Set(Some(DbAccountId::try_from(parent_address.clone())?)); vault_account.crypto_scheme = Set(None); vault_account.encrypted_suri = Set(None); vault_account.name = Set(name.clone()); diff --git a/src/entities/vault_account.rs b/src/entities/vault_account.rs index d04b02ad71ce2f1805c7b63a683a28d834d545ad..ec81c6509fe82ddebca8ae09ec8b1c13a7a15abf 100644 --- a/src/entities/vault_account.rs +++ b/src/entities/vault_account.rs @@ -2,11 +2,11 @@ use crate::commands::{cesium, vault}; use crate::runtime_config::AccountId; use crate::utils::GcliError; use anyhow::anyhow; +use log::debug; use sea_orm::entity::prelude::*; use sea_orm::prelude::async_trait::async_trait; use sea_orm::prelude::StringLen; use sea_orm::ActiveValue::Set; -use sea_orm::PaginatorTrait; use sea_orm::QueryFilter; use sea_orm::TryGetError; use sea_orm::{ @@ -14,6 +14,7 @@ use sea_orm::{ ModelTrait, QueryOrder, RelationDef, RelationTrait, TryFromU64, }; use sea_orm::{ActiveModelTrait, ConnectionTrait, PrimaryKeyTrait}; +use sea_orm::{Condition, PaginatorTrait}; use sea_orm::{DeriveActiveEnum, EntityTrait}; use std::cell::RefCell; use std::collections::HashMap; @@ -133,18 +134,16 @@ impl sea_orm::TryGetable for DbAccountId { let value: String = res .try_get_by(idx) .map_err(|e| TryGetError::Null(e.to_string()))?; - Ok(DbAccountId(AccountId::from_str(&value).map_err(|e| { - TryGetError::DbErr(DbErr::Custom(e.to_string())) - })?)) + DbAccountId::from_str(&value).map_err(|e| TryGetError::DbErr(DbErr::Custom(e.to_string()))) } } impl sea_orm::sea_query::ValueType for DbAccountId { fn try_from(v: Value) -> Result<Self, sea_orm::sea_query::ValueTypeErr> { match v { - Value::String(Some(value)) => Ok(DbAccountId( - AccountId::from_str(&value).map_err(|_| sea_orm::sea_query::ValueTypeErr)?, - )), + Value::String(Some(value)) => { + Ok(DbAccountId::from_str(&value).map_err(|_| sea_orm::sea_query::ValueTypeErr)?) + } _ => Err(sea_orm::sea_query::ValueTypeErr), } } @@ -164,7 +163,12 @@ impl sea_orm::sea_query::ValueType for DbAccountId { impl From<DbAccountId> for Value { fn from(account_id: DbAccountId) -> Self { - Value::String(Some(Box::new(account_id.0.to_string()))) + let account_id_string = account_id.0.to_string(); + debug!( + "DB converting DbAccountId to string:'{}'", + account_id_string + ); + Value::String(Some(Box::new(account_id_string))) } } @@ -183,19 +187,32 @@ impl Display for DbAccountId { } } -impl FromStr for DbAccountId { - type Err = GcliError; +impl TryFrom<String> for DbAccountId { + type Error = GcliError; - fn from_str(s: &str) -> Result<Self, Self::Err> { - Ok(DbAccountId( - AccountId::from_str(s).map_err(|e| GcliError::Input(e.to_string()))?, - )) + fn try_from(value: String) -> Result<Self, Self::Error> { + DbAccountId::from_str(&value) } } -impl From<String> for DbAccountId { - fn from(s: String) -> Self { - DbAccountId(AccountId::from_str(&s).expect("Invalid AccountId format")) +/// This one is used by the other methods converting from String and does potential extra cleanup of the string before parsing. +/// +/// Due to a hard to reproduce issue (!48) we added trim of double quotes (") at start and end of the string. +impl FromStr for DbAccountId { + type Err = GcliError; + + fn from_str(s: &str) -> Result<Self, Self::Err> { + // Remove surrounding double quotes if present + let cleaned_value = s.trim_matches('"'); + + Ok(DbAccountId(AccountId::from_str(cleaned_value).map_err( + |e| { + GcliError::Input(format!( + "Cannot parse DbAccountId for raw string '{}' - error: {}", + s, e + )) + }, + )?)) } } @@ -757,6 +774,9 @@ where Ok(Some(base_parent_account)) } +/// Fetches all the children accounts of current_account +/// +/// Due to a hard to reproduce issue (!48) had to adapt the filter on Parent to also match if the Parent value has those extra double quotes around the value async fn find_direct_children_accounts<C>( db: &C, current_account: &Model, @@ -765,7 +785,11 @@ where C: ConnectionTrait, { Entity::find() - .filter(Column::Parent.eq(current_account.address.clone())) + .filter( + Condition::any() + .add(Column::Parent.eq(current_account.address.clone())) + .add(Column::Parent.eq(format!("\"{}\"", current_account.address))), + ) .order_by_asc(Column::Address) .all(db) .await @@ -921,12 +945,12 @@ where } None => { let vault_account = ActiveModel { - address: Set(address.to_string().into()), + address: Set(address.to_string().try_into()?), name: Set(name.cloned()), path: Set(Some(derivation_path.to_string())), crypto_scheme: Set(None), encrypted_suri: Set(None), - parent: Set(Some(parent_address.to_string().into())), + parent: Set(Some(parent_address.to_string().try_into()?)), }; vault_account.insert(db).await? }