From 0791dddf6c931e1333d6816f021fef9f542ffbfb Mon Sep 17 00:00:00 2001 From: xiaoguang Date: Tue, 26 Mar 2024 01:47:08 +0800 Subject: [PATCH 1/2] fix: fix identity keystore deletion bug after migration --- token-core/tcx/src/migration.rs | 34 ++++++++++++++++++- token-core/tcx/tests/migration_test.rs | 45 +++++++++++++++++++++++--- 2 files changed, 73 insertions(+), 6 deletions(-) diff --git a/token-core/tcx/src/migration.rs b/token-core/tcx/src/migration.rs index 16a42f40..65bdd205 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,42 @@ 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..a134d875 100644 --- a/token-core/tcx/tests/migration_test.rs +++ b/token-core/tcx/tests/migration_test.rs @@ -9,11 +9,7 @@ use tcx::*; 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, -}; +use tcx::api::{export_mnemonic_param, migrate_keystore_param, wallet_key_param, BackupResult, ExportMnemonicParam, ExportMnemonicResult, MigrateKeystoreParam, MigrateKeystoreResult, SignParam, WalletKeyParam, GeneralResult}; use tcx::handler::encode_message; use tcx_constants::CurveType; @@ -991,3 +987,42 @@ fn test_migrate_ios_old_eos_private_keystore() { exported ); } + +#[test] +#[serial] +pub fn test_migrate_identity_then_delete() { + let _ = fs::remove_dir_all("../test-data/walletsV2"); + let _ = fs::remove_file("../test-data/wallets/_migrated.json"); + init_token_core_x("../test-data"); + + 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(); + println!("{}", result.keystore.unwrap().source); + + 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); + +} From 55d2c68a6ff1e82e951fe7473b0b18809e9200f3 Mon Sep 17 00:00:00 2001 From: xiaoguang Date: Tue, 26 Mar 2024 09:07:49 +0800 Subject: [PATCH 2/2] test: otpimization test case --- token-core/tcx/src/migration.rs | 14 +++++++--- token-core/tcx/tests/migration_test.rs | 38 ++++++++++++++++++++------ 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/token-core/tcx/src/migration.rs b/token-core/tcx/src/migration.rs index 65bdd205..fbc0eb36 100644 --- a/token-core/tcx/src/migration.rs +++ b/token-core/tcx/src/migration.rs @@ -60,8 +60,12 @@ pub fn remove_old_keystore_by_id(id: &str) -> Option> { } 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(); + 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; } @@ -88,13 +92,15 @@ pub fn remove_old_keystore_by_id(id: &str) -> Option> { let Ok(v) = v_result else { continue; }; - let source = v["imTokenMeta"]["source"].as_str().unwrap_or("").to_string(); + 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 a134d875..414e3830 100644 --- a/token-core/tcx/tests/migration_test.rs +++ b/token-core/tcx/tests/migration_test.rs @@ -9,7 +9,11 @@ use tcx::*; 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, GeneralResult}; +use tcx::api::{ + export_mnemonic_param, migrate_keystore_param, wallet_key_param, BackupResult, + ExportMnemonicParam, ExportMnemonicResult, GeneralResult, MigrateKeystoreParam, + MigrateKeystoreResult, SignParam, WalletKeyParam, +}; use tcx::handler::encode_message; use tcx_constants::CurveType; @@ -17,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; @@ -990,10 +994,27 @@ fn test_migrate_ios_old_eos_private_keystore() { #[test] #[serial] -pub fn test_migrate_identity_then_delete() { - let _ = fs::remove_dir_all("../test-data/walletsV2"); - let _ = fs::remove_file("../test-data/wallets/_migrated.json"); - init_token_core_x("../test-data"); +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(), @@ -1004,7 +1025,7 @@ pub fn test_migrate_identity_then_delete() { }; let ret = call_api("migrate_keystore", param).unwrap(); let result: MigrateKeystoreResult = MigrateKeystoreResult::decode(ret.as_slice()).unwrap(); - println!("{}", result.keystore.unwrap().source); + assert!(!result.is_existed); let param: MigrateKeystoreParam = MigrateKeystoreParam { id: "00fc0804-7cea-46d8-9e95-ed1efac65358".to_string(), @@ -1024,5 +1045,6 @@ pub fn test_migrate_identity_then_delete() { 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); }