Skip to content

Commit

Permalink
feat: add read_keystore_menmonic_path api
Browse files Browse the repository at this point in the history
  • Loading branch information
XuNeal committed Mar 26, 2024
1 parent b1c5fd0 commit 216d551
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 4 deletions.
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 @@ -282,4 +282,8 @@ message MigrateKeystoreParam {
message MarkIdentityWalletsParam {
repeated string ids = 1;
string source = 2;
}

message ReadKeystoreMnemonicPathResult {
string path = 1;
}
6 changes: 6 additions & 0 deletions token-core/tcx/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,12 @@ pub struct MarkIdentityWalletsParam {
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct ReadKeystoreMnemonicPathResult {
#[prost(string, tag = "1")]
pub path: ::prost::alloc::string::String,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct VerifyDerivedKeyParam {
#[prost(string, tag = "1")]
pub id: ::prost::alloc::string::String,
Expand Down
5 changes: 4 additions & 1 deletion token-core/tcx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::os::raw::c_char;

use anyhow::anyhow;
use handler::{backup, sign_bls_to_execution_change};
use migration::mark_identity_wallets;
use migration::{mark_identity_wallets, read_legacy_keystore_mnemonic_path};
use prost::Message;

pub mod api;
Expand Down Expand Up @@ -79,6 +79,9 @@ pub unsafe extern "C" fn call_tcx_api(hex_str: *const c_char) -> *const c_char {
let ret = scan_legacy_keystores()?;
encode_message(ret)
}),
"read_keystore_mnemonic_path" => {
landingpad(|| read_legacy_keystore_mnemonic_path(&action.param.unwrap().value))
}
"create_keystore" => landingpad(|| create_keystore(&action.param.unwrap().value)),
"import_mnemonic" => landingpad(|| import_mnemonic(&action.param.unwrap().value)),
"export_mnemonic" => landingpad(|| export_mnemonic(&action.param.unwrap().value)),
Expand Down
33 changes: 31 additions & 2 deletions token-core/tcx/src/migration.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::api::{
migrate_keystore_param, AccountResponse, GeneralResult, KeystoreResult, LegacyKeystoreResult,
MarkIdentityWalletsParam, MigrateKeystoreParam, MigrateKeystoreResult,
ScanLegacyKeystoresResult,
ReadKeystoreMnemonicPathResult, ScanLegacyKeystoresResult, WalletId,
};
use crate::error_handling::Result;
use crate::filemanager::{cache_keystore, KEYSTORE_MAP, WALLET_FILE_DIR};
Expand Down Expand Up @@ -224,6 +224,35 @@ pub(crate) fn mark_identity_wallets(data: &[u8]) -> Result<Vec<u8>> {
return encode_message(ret);
}

pub(crate) fn read_legacy_keystore_mnemonic_path(data: &[u8]) -> Result<Vec<u8>> {
let param: WalletId = WalletId::decode(data).expect("param: WalletId");

let legacy_file_dir = {
let dir = LEGACY_WALLET_FILE_DIR.read();
dir.to_string()
};

let mut file_path = format!("{}/{}", legacy_file_dir, param.id);
if !Path::new(&file_path).exists() {
file_path = format!("{}/{}.json", legacy_file_dir, param.id);
}
let path = Path::new(&file_path);
if path.exists() {
let json_str = fs::read_to_string(path)?;
let json: Value = serde_json::from_str(&json_str)?;

if let Some(path) = json["mnemonicPath"].as_str() {
return encode_message(ReadKeystoreMnemonicPathResult {
path: path.to_string(),
});
}
}

return encode_message(ReadKeystoreMnemonicPathResult {
path: "".to_string(),
});
}

pub fn read_all_identity_wallet_ids() -> Option<AllIdentityWallets> {
let file_path_str = format!(
"{}/{}",
Expand Down Expand Up @@ -391,7 +420,7 @@ fn parse_legacy_kesytore(contents: String) -> Result<LegacyKeystoreResult> {
};
let account = AccountResponse {
chain_type,
address: legacy_keystore.address.expect("legacy address"),
address: legacy_keystore.address.unwrap_or("".to_string()),
path,
curve: "secp256k1".to_string(),
public_key,
Expand Down
28 changes: 27 additions & 1 deletion token-core/tcx/tests/migration_test.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use libc::id_t;
use serial_test::serial;

mod common;
Expand All @@ -12,7 +13,8 @@ use prost::Message;
use tcx::api::{
export_mnemonic_param, migrate_keystore_param, wallet_key_param, BackupResult,
ExportMnemonicParam, ExportMnemonicResult, GeneralResult, MarkIdentityWalletsParam,
MigrateKeystoreParam, MigrateKeystoreResult, SignParam, WalletKeyParam,
MigrateKeystoreParam, MigrateKeystoreResult, ReadKeystoreMnemonicPathResult, SignParam,
WalletId, WalletKeyParam,
};

use tcx::handler::encode_message;
Expand Down Expand Up @@ -1055,3 +1057,27 @@ fn test_delete_all_identity_wallets() {
false
);
}

#[test]
#[serial]
fn test_read_keystore_mnemonic_path() {
setup_test("../test-data/identity-keystore-delete");

let wallet_ids = vec![
("0a2756cd-ff70-437b-9bdb-ad46b8bb0819".to_string(), ""),
(
"00fc0804-7cea-46d8-9e95-ed1efac65358".to_string(),
"m/49'/1'/0'",
),
(
"6c3eae60-ad03-48db-a5e5-61a6f72aef8d".to_string(),
"m/44'/194'/0'/0/0",
),
];
for (id, path) in wallet_ids {
let param = WalletId { id };
let ret = call_api("read_keystore_mnemonic_path", param).unwrap();
let mnemonic_path = ReadKeystoreMnemonicPathResult::decode(ret.as_slice()).unwrap();
assert_eq!(mnemonic_path.path, path);
}
}

0 comments on commit 216d551

Please sign in to comment.