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 f2db84c97e53bb35e5a981d320387afe503250de..4c219e6f3d7efc8f796125a958a82c28b232a6e5 100644 --- a/src/entities/vault_account.rs +++ b/src/entities/vault_account.rs @@ -133,21 +133,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(format!( - "Cannot parse DbAccountId for string '{}' - error: {}", - &value, e - ))) - })?)) + 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), } } @@ -186,19 +181,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 + )) + }, + )?)) } } @@ -924,12 +932,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? }