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

feat: open tcx scan_keystores api[R2D2-13481] #141

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 4 additions & 2 deletions token-core/tcx-proto/src/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ message KeystoreResult {
// ture if mnemonic already existed when call import_mnemonic
bool isExisted = 8;
string existedId = 9;
bool isMigrated = 10;
string status = 10; // This field can have one of three values: "migrated", "unmigrated", or "new".
}

// FUNCTION: import_mnemonic(ImportMnemonicParam): KeystoreResult
Expand Down Expand Up @@ -59,7 +59,7 @@ message ImportPrivateKeyResult {
string sourceFingerprint = 10;
bool isExisted = 11;
string existedId = 12;
bool isMigrated = 13;
string status = 13;// This field can have one of three values: "migrated", "unmigrated", or "new".
}

//
Expand Down Expand Up @@ -238,6 +238,7 @@ message MigrateKeystoreResult {
message ScanKeystoresResult {
repeated KeystoreResult hdKeystores = 1;
repeated ImportPrivateKeyResult privateKeyKeystores = 2;
repeated LegacyKeystoreResult legacyKeystores = 3;
}

message LegacyKeystoreResult {
Expand All @@ -246,6 +247,7 @@ message LegacyKeystoreResult {
string source = 3;
string createdAt = 4;
repeated AccountResponse accounts = 5;
string status = 6;// This field can have one of three values: "migrated", "unmigrated", or "new".
}

message ScanLegacyKeystoresResult {
Expand Down
15 changes: 11 additions & 4 deletions token-core/tcx/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,9 @@ pub struct KeystoreResult {
pub is_existed: bool,
#[prost(string, tag = "9")]
pub existed_id: ::prost::alloc::string::String,
#[prost(bool, tag = "10")]
pub is_migrated: bool,
/// This field can have one of three values: "migrated", "unmigrated", or "new".
#[prost(string, tag = "10")]
pub status: ::prost::alloc::string::String,
}
/// FUNCTION: import_mnemonic(ImportMnemonicParam): KeystoreResult
///
Expand Down Expand Up @@ -352,8 +353,9 @@ pub struct ImportPrivateKeyResult {
pub is_existed: bool,
#[prost(string, tag = "12")]
pub existed_id: ::prost::alloc::string::String,
#[prost(bool, tag = "13")]
pub is_migrated: bool,
/// This field can have one of three values: "migrated", "unmigrated", or "new".
#[prost(string, tag = "13")]
pub status: ::prost::alloc::string::String,
}
///
/// derive new accounts from a hd keystore
Expand Down Expand Up @@ -673,6 +675,8 @@ pub struct ScanKeystoresResult {
pub hd_keystores: ::prost::alloc::vec::Vec<KeystoreResult>,
#[prost(message, repeated, tag = "2")]
pub private_key_keystores: ::prost::alloc::vec::Vec<ImportPrivateKeyResult>,
#[prost(message, repeated, tag = "3")]
pub legacy_keystores: ::prost::alloc::vec::Vec<LegacyKeystoreResult>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
Expand All @@ -687,6 +691,9 @@ pub struct LegacyKeystoreResult {
pub created_at: ::prost::alloc::string::String,
#[prost(message, repeated, tag = "5")]
pub accounts: ::prost::alloc::vec::Vec<AccountResponse>,
/// This field can have one of three values: "migrated", "unmigrated", or "new".
#[prost(string, tag = "6")]
pub status: ::prost::alloc::string::String,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里直接叫 status 太过于generic 了。建议改成 migration_status

}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
Expand Down
14 changes: 7 additions & 7 deletions token-core/tcx/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ use tcx_tezos::{encode_tezos_private_key, parse_tezos_private_key};

use crate::macros::{impl_to_key, use_chains};
use crate::migration::{
is_migrated_keystore, read_all_identity_wallet_ids, remove_all_identity_wallets,
remove_old_keystore_by_id,
get_migrated_status, read_all_identity_wallet_ids, remove_all_identity_wallets,
remove_old_keystore_by_id, scan_legacy_keystores,
};
use crate::reset_password::assert_seed_equals;

Expand Down Expand Up @@ -445,9 +445,7 @@ pub fn scan_keystores() -> Result<ScanKeystoresResult> {

if version == HdKeystore::VERSION || version == PrivateKeystore::VERSION {
let keystore = Keystore::from_json(&contents)?;
//Verify that the keystore has been migrated
let is_migrated = is_migrated_keystore(&keystore.id());

let status = get_migrated_status(WALLET_V2_DIR, &keystore.id())?;
if version == HdKeystore::VERSION {
let keystore_result = KeystoreResult {
id: keystore.id(),
Expand All @@ -457,7 +455,7 @@ pub fn scan_keystores() -> Result<ScanKeystoresResult> {
source: keystore.meta().source.to_string(),
created_at: keystore.meta().timestamp,
source_fingerprint: keystore.fingerprint().to_string(),
is_migrated,
status,
..Default::default()
};
hd_keystores.push(keystore_result);
Expand All @@ -476,18 +474,20 @@ pub fn scan_keystores() -> Result<ScanKeystoresResult> {
identified_chain_types: curve_to_chain_type(&curve),
identified_network: keystore.meta().network.to_string(),
identified_curve: curve.as_str().to_string(),
is_migrated,
status,
..Default::default()
};
private_key_keystores.push(kestore_result);
}
cache_keystore(keystore);
}
}
let legacy_keystores = scan_legacy_keystores()?.keystores;

Ok(ScanKeystoresResult {
hd_keystores,
private_key_keystores,
legacy_keystores,
})
}

Expand Down
22 changes: 17 additions & 5 deletions token-core/tcx/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::api::{
ReadKeystoreMnemonicPathResult, ScanLegacyKeystoresResult, WalletId,
};
use crate::error_handling::Result;
use crate::filemanager::{cache_keystore, KEYSTORE_MAP, WALLET_FILE_DIR};
use crate::filemanager::{cache_keystore, KEYSTORE_MAP, WALLET_FILE_DIR, WALLET_V1_DIR};
use crate::filemanager::{flush_keystore, LEGACY_WALLET_FILE_DIR};
use crate::handler::{encode_message, encrypt_xpub};
use anyhow::anyhow;
Expand Down Expand Up @@ -191,7 +191,7 @@ pub(crate) fn migrate_keystore(data: &[u8]) -> Result<Vec<u8>> {
source_fingerprint: keystore.fingerprint().to_string(),
is_existed,
existed_id,
is_migrated: true,
status: "migrated".to_string(),
};

let ret = encode_message(MigrateKeystoreResult {
Expand Down Expand Up @@ -443,12 +443,14 @@ fn parse_legacy_kesytore(contents: String) -> Result<LegacyKeystoreResult> {
NumberOrNumberStr::Number(t) => t,
NumberOrNumberStr::NumberStr(t) => f64::from_str(&t).expect("f64 from timestamp") as i64,
};
let status = get_migrated_status(WALLET_V1_DIR, &legacy_keystore.id)?;
let keystore_result = LegacyKeystoreResult {
id: legacy_keystore.id.to_string(),
name: meta.name.to_string(),
source: meta.source.as_ref().unwrap_or(&"".to_string()).to_string(),
created_at: created_at.to_string(),
accounts: vec![account],
status,
};
Ok(keystore_result)
}
Expand Down Expand Up @@ -501,12 +503,14 @@ fn parse_tcx_keystore(v: &Value) -> Result<LegacyKeystoreResult> {
}
let id = v["id"].as_str().expect("keystore id").to_string();
let meta: Metadata = serde_json::from_value(v["imTokenMeta"].clone())?;
let status = get_migrated_status(WALLET_V1_DIR, &id)?;
let keystore_result = LegacyKeystoreResult {
id,
name: meta.name.to_string(),
source: meta.source.to_string(),
created_at: meta.timestamp.to_string(),
accounts: account_responses,
status,
};
Ok(keystore_result)
}
Expand All @@ -524,11 +528,19 @@ fn merge_migrate_source(id: &str, ori_source: &str) -> String {
}
}

pub fn is_migrated_keystore(id: &str) -> bool {
pub fn get_migrated_status(dir: &str, id: &str) -> Result<String> {
let migrated_id_map = read_migrated_map().1;
migrated_id_map
let is_migrated = migrated_id_map
.values()
.any(|ids| ids.contains(&id.to_string()))
.any(|ids| ids.contains(&id.to_string()));
let status = match (dir, is_migrated) {
(WALLET_V1_DIR, true) => "migrated".to_string(),
(WALLET_V1_DIR, false) => "unmigrated".to_string(),
(WALLET_V2_DIR, true) => "migrated".to_string(),
(WALLET_V2_DIR, false) => "new".to_string(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xiaoguang1010 需要确认这里如果两个老的 keystore 都有相同的助记词,并且都迁移之后,这里是否会正确的返回migrated

_ => return Err(anyhow!("{}", "get_migrated_status_error")),
};
Ok(status)
}

#[cfg(test)]
Expand Down
11 changes: 9 additions & 2 deletions token-core/tcx/tests/migration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use tcx::api::{
use tcx::handler::encode_message;
use tcx_constants::CurveType;
use tcx_constants::{OTHER_MNEMONIC, TEST_PASSWORD};
use tcx_keystore::Keystore;
use tcx_keystore::{keystore, Keystore};

use anyhow::{anyhow, format_err};
use std::path::Path;
Expand Down Expand Up @@ -77,7 +77,14 @@ pub fn test_migrate_keystores_existed() {

let ret = call_api("scan_keystores", "".to_string()).unwrap();
let resp: ScanKeystoresResult = ScanKeystoresResult::decode(ret.as_slice()).unwrap();
assert!(resp.hd_keystores[0].is_migrated);
assert_eq!(resp.hd_keystores[0].status, "migrated");
for keystore in resp.legacy_keystores {
if "0a2756cd-ff70-437b-9bdb-ad46b8bb0819".eq(&keystore.id)
|| "00fc0804-7cea-46d8-9e95-ed1efac65358".eq(&keystore.id)
{
assert_eq!(&keystore.status, "migrated");
}
}

fs::remove_dir_all("../test-data/walletsV2").unwrap();
}
Expand Down
Loading