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 5 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
6 changes: 3 additions & 3 deletions .github/workflows/build-release-ios.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ jobs:

- name: Install Rust
run: |
rustup toolchain install nightly-2022-10-31
rustup default nightly-2022-10-31-x86_64-apple-darwin
rustup toolchain install nightly-2023-06-15
rustup default nightly-2023-06-15-x86_64-apple-darwin
rustup target add aarch64-apple-ios x86_64-apple-ios
rustup show
cargo install cargo-lipo
cargo install cbindgen
cargo install cbindgen --version 0.26.0
brew install protobuf

- name: Read VERSION file
Expand Down
4 changes: 4 additions & 0 deletions token-core/tcx-proto/src/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ message KeystoreResult {
// ture if mnemonic already existed when call import_mnemonic
bool isExisted = 8;
string existedId = 9;
string status = 10; // This field can have one of three values: "migrated", "unmigrated", or "new".
}

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

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

message LegacyKeystoreResult {
Expand All @@ -244,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
11 changes: 11 additions & 0 deletions token-core/tcx/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@ pub struct KeystoreResult {
pub is_existed: bool,
#[prost(string, tag = "9")]
pub existed_id: ::prost::alloc::string::String,
/// 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 @@ -350,6 +353,9 @@ pub struct ImportPrivateKeyResult {
pub is_existed: bool,
#[prost(string, tag = "12")]
pub existed_id: ::prost::alloc::string::String,
/// 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 @@ -669,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 @@ -683,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
9 changes: 7 additions & 2 deletions token-core/tcx/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ use tcx_tezos::{encode_tezos_private_key, parse_tezos_private_key};

use crate::macros::{impl_to_key, use_chains};
use crate::migration::{
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 @@ -444,7 +445,7 @@ pub fn scan_keystores() -> Result<ScanKeystoresResult> {

if version == HdKeystore::VERSION || version == PrivateKeystore::VERSION {
let keystore = Keystore::from_json(&contents)?;

let status = get_migrated_status(WALLET_V2_DIR, &keystore.id())?;
if version == HdKeystore::VERSION {
let keystore_result = KeystoreResult {
id: keystore.id(),
Expand All @@ -454,6 +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(),
status,
..Default::default()
};
hd_keystores.push(keystore_result);
Expand All @@ -472,17 +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(),
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
8 changes: 6 additions & 2 deletions token-core/tcx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ use crate::handler::{
encode_message, encrypt_data_to_ipfs, eth_batch_personal_sign, exists_json, exists_mnemonic,
exists_private_key, export_json, export_mnemonic, export_private_key, get_derived_key,
get_extended_public_keys, get_public_keys, import_json, import_mnemonic, import_private_key,
mnemonic_to_public, sign_authentication_message, sign_hashes, sign_message, sign_psbt,
sign_psbts, sign_tx, unlock_then_crash, verify_password,
mnemonic_to_public, scan_keystores, sign_authentication_message, sign_hashes, sign_message,
sign_psbt, sign_psbts, sign_tx, unlock_then_crash, verify_password,
};
use crate::migration::{migrate_keystore, scan_legacy_keystores};

Expand Down Expand Up @@ -80,6 +80,10 @@ pub unsafe extern "C" fn call_tcx_api(hex_str: *const c_char) -> *const c_char {
let ret = scan_legacy_keystores()?;
encode_message(ret)
}),
"scan_keystores" => landingpad(|| {
let ret = scan_keystores()?;
encode_message(ret)
}),
"read_keystore_mnemonic_path" => {
landingpad(|| read_legacy_keystore_mnemonic_path(&action.param.unwrap().value))
}
Expand Down
22 changes: 21 additions & 1 deletion 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,6 +191,7 @@ pub(crate) fn migrate_keystore(data: &[u8]) -> Result<Vec<u8>> {
source_fingerprint: keystore.fingerprint().to_string(),
is_existed,
existed_id,
status: "migrated".to_string(),
};

let ret = encode_message(MigrateKeystoreResult {
Expand Down Expand Up @@ -442,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 @@ -500,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 @@ -523,6 +528,21 @@ fn merge_migrate_source(id: &str, ori_source: &str) -> String {
}
}

pub fn get_migrated_status(dir: &str, id: &str) -> Result<String> {
let migrated_id_map = read_migrated_map().1;
let is_migrated = migrated_id_map
.values()
.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)]
mod tests {
use std::fs;
Expand Down
4 changes: 2 additions & 2 deletions token-core/tcx/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub fn import_default_pk_store() -> ImportPrivateKeyResult {
ImportPrivateKeyResult::decode(ret.as_slice()).unwrap()
}

pub fn import_filecoin_pk_store() -> KeystoreResult {
pub fn import_filecoin_pk_store() -> ImportPrivateKeyResult {
let param: ImportPrivateKeyParam = ImportPrivateKeyParam {
private_key: "f15716d3b003b304b8055d9cc62e6b9c869d56cc930c3858d4d7c31f5f53f14a".to_string(),
password: TEST_PASSWORD.to_string(),
Expand All @@ -98,7 +98,7 @@ pub fn import_filecoin_pk_store() -> KeystoreResult {
};

let ret = import_private_key(&encode_message(param).unwrap()).unwrap();
KeystoreResult::decode(ret.as_slice()).unwrap()
ImportPrivateKeyResult::decode(ret.as_slice()).unwrap()
}

pub fn import_and_derive(derivation: Derivation) -> (KeystoreResult, DeriveAccountsResult) {
Expand Down
3 changes: 2 additions & 1 deletion token-core/tcx/tests/export_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ pub fn test_tezos_import_private_key_export() {
};

let ret = import_private_key(&encode_message(param).unwrap()).unwrap();
let import_result: KeystoreResult = KeystoreResult::decode(ret.as_slice()).unwrap();
let import_result: ImportPrivateKeyResult =
ImportPrivateKeyResult::decode(ret.as_slice()).unwrap();

let derivations = vec![Derivation {
chain_type: "TEZOS".to_string(),
Expand Down
18 changes: 12 additions & 6 deletions token-core/tcx/tests/import_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,8 @@ pub fn test_filecoin_private_key_secp256k1_import() {
};

let ret = import_private_key(&encode_message(param).unwrap()).unwrap();
let import_result: KeystoreResult = KeystoreResult::decode(ret.as_slice()).unwrap();
let import_result: ImportPrivateKeyResult =
ImportPrivateKeyResult::decode(ret.as_slice()).unwrap();

let derivations = vec![Derivation {
chain_type: "FILECOIN".to_string(),
Expand Down Expand Up @@ -500,7 +501,8 @@ pub fn test_filecoin_private_key_bls_import() {
};

let ret = import_private_key(&encode_message(param).unwrap()).unwrap();
let import_result: KeystoreResult = KeystoreResult::decode(ret.as_slice()).unwrap();
let import_result: ImportPrivateKeyResult =
ImportPrivateKeyResult::decode(ret.as_slice()).unwrap();

let derivations = vec![Derivation {
chain_type: "FILECOIN".to_string(),
Expand Down Expand Up @@ -612,7 +614,8 @@ pub fn test_fil_bls_tezos_reimport() {
};

let ret = import_private_key(&encode_message(param).unwrap()).unwrap();
let pk_import_result: KeystoreResult = KeystoreResult::decode(ret.as_slice()).unwrap();
let pk_import_result: ImportPrivateKeyResult =
ImportPrivateKeyResult::decode(ret.as_slice()).unwrap();

let derivations = vec![Derivation {
chain_type: case.0.to_string(),
Expand Down Expand Up @@ -832,7 +835,8 @@ pub fn test_import_substrate_keystore() {
assert!(!exists_result.is_exists);

let ret_bytes = call_api("import_json", param.clone()).unwrap();
let wallet_ret: KeystoreResult = KeystoreResult::decode(ret_bytes.as_slice()).unwrap();
let wallet_ret: ImportPrivateKeyResult =
ImportPrivateKeyResult::decode(ret_bytes.as_slice()).unwrap();

let ret_bytes = call_api("exists_json", param.clone()).unwrap();
let exists_result: ExistsKeystoreResult =
Expand Down Expand Up @@ -923,7 +927,8 @@ pub fn test_import_substrate_keystore_v3() {
assert!(!exists_result.is_exists);

let ret_bytes = call_api("import_json", param.clone()).unwrap();
let wallet_ret: KeystoreResult = KeystoreResult::decode(ret_bytes.as_slice()).unwrap();
let wallet_ret: ImportPrivateKeyResult =
ImportPrivateKeyResult::decode(ret_bytes.as_slice()).unwrap();

let ret_bytes = call_api("exists_json", param.clone()).unwrap();
let exists_result: ExistsKeystoreResult =
Expand Down Expand Up @@ -1012,7 +1017,8 @@ pub fn test_import_multi_curve() {
};

let ret_bytes = call_api("import_json", param).unwrap();
let wallet_ret: KeystoreResult = KeystoreResult::decode(ret_bytes.as_slice()).unwrap();
let wallet_ret: ImportPrivateKeyResult =
ImportPrivateKeyResult::decode(ret_bytes.as_slice()).unwrap();
let derivation = Derivation {
chain_type: "KUSAMA".to_string(),
path: "".to_string(),
Expand Down
17 changes: 14 additions & 3 deletions token-core/tcx/tests/migration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ use prost::Message;
use tcx::api::{
export_mnemonic_param, migrate_keystore_param, wallet_key_param, BackupResult,
ExportMnemonicParam, ExportMnemonicResult, GeneralResult, MarkIdentityWalletsParam,
MigrateKeystoreParam, MigrateKeystoreResult, ReadKeystoreMnemonicPathResult, SignParam,
WalletId, WalletKeyParam,
MigrateKeystoreParam, MigrateKeystoreResult, ReadKeystoreMnemonicPathResult,
ScanKeystoresResult, SignParam, WalletId, WalletKeyParam,
};

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 @@ -75,6 +75,17 @@ pub fn test_migrate_keystores_existed() {
assert!(result.is_existed);
assert_eq!(result.existed_id, "0a2756cd-ff70-437b-9bdb-ad46b8bb0819");

let ret = call_api("scan_keystores", "".to_string()).unwrap();
let resp: ScanKeystoresResult = ScanKeystoresResult::decode(ret.as_slice()).unwrap();
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
Loading