Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix identity keystore delete bug[R2D2-10888] #81

Merged
merged 2 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion token-core/tcx/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub fn remove_old_keystore_by_id(id: &str) -> Option<Vec<String>> {

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() {
Expand All @@ -59,10 +59,48 @@ pub fn remove_old_keystore_by_id(id: &str) -> Option<Vec<String>> {
file_path = format!("{}/{}", legacy_file_dir, file_id);
}

if !is_identity_keystore {
let json = serde_json::from_str::<Value>(&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::<Value>(&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);
Expand Down
63 changes: 60 additions & 3 deletions token-core/tcx/tests/migration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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 &copy_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);
}
Loading