diff --git a/token-core/tcx/src/migration.rs b/token-core/tcx/src/migration.rs index 16a42f40..fbc0eb36 100644 --- a/token-core/tcx/src/migration.rs +++ b/token-core/tcx/src/migration.rs @@ -50,7 +50,7 @@ pub fn remove_old_keystore_by_id(id: &str) -> Option> { let migrated_file = result.0; let mut map = result.1; - + let mut is_identity_keystore = false; let marked_files = map.get(id).and_then(|x| Some(x.to_vec())).clone(); if let Some(files) = map.get(id) { for file_id in files.iter() { @@ -59,10 +59,48 @@ pub fn remove_old_keystore_by_id(id: &str) -> Option> { file_path = format!("{}/{}", legacy_file_dir, file_id); } + if !is_identity_keystore { + let json = serde_json::from_str::(&fs::read_to_string(&file_path).unwrap()) + .unwrap(); + let source = json["imTokenMeta"]["source"] + .as_str() + .unwrap_or("") + .to_string(); + if source.ends_with("_IDENTITY") { + is_identity_keystore = true; + } + } + if Path::new(&file_path).exists() { fs::remove_file(&file_path); } } + if is_identity_keystore { + let p = Path::new(legacy_file_dir.as_str()); + let walk_dir = std::fs::read_dir(p).expect("read dir"); + for entry in walk_dir { + let entry = entry.expect("DirEntry"); + let fp = entry.path(); + let mut f = fs::File::open(&fp).expect("open file"); + let mut contents = String::new(); + let read_ret = f.read_to_string(&mut contents); + if read_ret.is_err() { + continue; + } + + let v_result = serde_json::from_str::(&contents); + let Ok(v) = v_result else { + continue; + }; + let source = v["imTokenMeta"]["source"] + .as_str() + .unwrap_or("") + .to_string(); + if source.ends_with("_IDENTITY") { + fs::remove_file(fp); + } + } + } } map.remove(id); diff --git a/token-core/tcx/tests/migration_test.rs b/token-core/tcx/tests/migration_test.rs index 78b7ee6e..414e3830 100644 --- a/token-core/tcx/tests/migration_test.rs +++ b/token-core/tcx/tests/migration_test.rs @@ -11,8 +11,8 @@ use tcx_atom::transaction::{AtomTxInput, AtomTxOutput}; use prost::Message; use tcx::api::{ export_mnemonic_param, migrate_keystore_param, wallet_key_param, BackupResult, - ExportMnemonicParam, ExportMnemonicResult, MigrateKeystoreParam, MigrateKeystoreResult, - SignParam, WalletKeyParam, + ExportMnemonicParam, ExportMnemonicResult, GeneralResult, MigrateKeystoreParam, + MigrateKeystoreResult, SignParam, WalletKeyParam, }; use tcx::handler::encode_message; @@ -21,8 +21,8 @@ use tcx_constants::{OTHER_MNEMONIC, TEST_PASSWORD}; use tcx_keystore::Keystore; use anyhow::{anyhow, format_err}; -use std::fs; use std::path::Path; +use std::{fs, io}; use sp_core::ByteArray; @@ -991,3 +991,60 @@ fn test_migrate_ios_old_eos_private_keystore() { exported ); } + +#[test] +#[serial] +pub fn test_migrate_identity_keystore_then_delete() { + let base_dir = "../test-data/identity-keystore-delete"; + let wallets_dir = "../test-data/identity-keystore-delete/wallets"; + let _ = fs::remove_dir_all(base_dir); + let _ = fs::create_dir_all(wallets_dir); + + let copy_keystore_list = [ + "identity.json", + "00fc0804-7cea-46d8-9e95-ed1efac65358", + "0597526e-105f-425b-bb44-086fc9dc9568", + "1bfddca9-84dc-4561-bbe9-844a9ff2b281", + "6c3eae60-ad03-48db-a5e5-61a6f72aef8d", + "ac59ccc1-285b-47a7-92f5-a6c432cee21a", + ]; + for file in ©_keystore_list { + let mut source_file = fs::File::open(format!("../test-data/wallets/{}", file)).unwrap(); + let mut destination_file = fs::File::create(format!("{}/{}", wallets_dir, file)).unwrap(); + io::copy(&mut source_file, &mut destination_file).unwrap(); + } + + init_token_core_x(base_dir); + + let param: MigrateKeystoreParam = MigrateKeystoreParam { + id: "0597526e-105f-425b-bb44-086fc9dc9568".to_string(), + network: "TESTNET".to_string(), + key: Some(migrate_keystore_param::Key::Password( + TEST_PASSWORD.to_string(), + )), + }; + let ret = call_api("migrate_keystore", param).unwrap(); + let result: MigrateKeystoreResult = MigrateKeystoreResult::decode(ret.as_slice()).unwrap(); + assert!(!result.is_existed); + + let param: MigrateKeystoreParam = MigrateKeystoreParam { + id: "00fc0804-7cea-46d8-9e95-ed1efac65358".to_string(), + network: "TESTNET".to_string(), + key: Some(migrate_keystore_param::Key::Password( + TEST_PASSWORD.to_string(), + )), + }; + let ret = call_api("migrate_keystore", param).unwrap(); + let result: MigrateKeystoreResult = MigrateKeystoreResult::decode(ret.as_slice()).unwrap(); + assert!(result.is_existed); + + let param = WalletKeyParam { + id: "0597526e-105f-425b-bb44-086fc9dc9568".to_string(), + key: Some(wallet_key_param::Key::Password(TEST_PASSWORD.to_string())), + }; + let ret = call_api("delete_keystore", param).unwrap(); + let result: GeneralResult = GeneralResult::decode(ret.as_slice()).unwrap(); + assert!(result.is_success); + let file_count = fs::read_dir(wallets_dir).unwrap().count(); + assert_eq!(file_count, 0); +}