Skip to content

Commit

Permalink
feat: add the exists_* api
Browse files Browse the repository at this point in the history
  • Loading branch information
XuNeal committed Dec 6, 2023
1 parent 6791ec3 commit 42d810e
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 57 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.7.0
2.7.0
6 changes: 3 additions & 3 deletions token-core/tcx-examples/RN/ios/RN/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ message DeriveAccountsResult {
repeated AccountResponse accounts = 1;
}

// FUNCTION: hd_store_export(ExportResult): KeystoreCommonExistsResult
// FUNCTION: hd_store_export(ExportResult): ExistsKeystoreResult
//
// export the mnemonic from a hd keystore
message ExportResult {
Expand Down Expand Up @@ -103,15 +103,15 @@ message PrivateKeyStoreExportParam {
//
// delete the keystore

// FUNCTION: keystore_common_exists(KeystoreCommonExistsParam): KeystoreCommonExistsResult
// FUNCTION: keystore_common_exists(KeystoreCommonExistsParam): ExistsKeystoreResult
//
// Check is there a keystore was generate by the special privateKey or mnemonic
message KeystoreCommonExistsParam {
KeyType type = 1;
string value = 2;
}

message KeystoreCommonExistsResult {
message ExistsKeystoreResult {
bool isExists = 1;
string id = 2;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ enum KeyType {
PRIVATE_KEY = 1;
}

message KeystoreCommonExistsResult {
message ExistsKeystoreResult {
bool isExists = 1;
string id = 2;
}
Expand Down
15 changes: 12 additions & 3 deletions token-core/tcx-proto/src/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ message DeriveAccountsResult {
repeated AccountResponse accounts = 1;
}

// FUNCTION: hd_store_export(ExportResult): KeystoreCommonExistsResult
// FUNCTION: hd_store_export(ExportResult): ExistsKeystoreResult
//
// export the mnemonic from a hd keystore
message ExportResult {
Expand Down Expand Up @@ -117,7 +117,7 @@ message PrivateKeyStoreExportParam {
//
// delete the keystore

// FUNCTION: keystore_common_exists(KeystoreCommonExistsParam): KeystoreCommonExistsResult
// FUNCTION: keystore_common_exists(KeystoreCommonExistsParam): ExistsKeystoreResult
//
// Check is there a keystore was generate by the special privateKey or mnemonic
message KeystoreCommonExistsParam {
Expand All @@ -126,7 +126,16 @@ message KeystoreCommonExistsParam {
string encoding = 3;
}

message KeystoreCommonExistsResult {
message ExistsMnemonicParam {
string mnemonic = 1;
}

message ExistsPrivateKeyParam {
string private_key = 1;
string encoding = 2;
}

message ExistsKeystoreResult {
bool isExists = 1;
string id = 2;
}
Expand Down
20 changes: 17 additions & 3 deletions token-core/tcx/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ pub struct DeriveAccountsResult {
#[prost(message, repeated, tag = "1")]
pub accounts: ::prost::alloc::vec::Vec<AccountResponse>,
}
/// FUNCTION: hd_store_export(ExportResult): KeystoreCommonExistsResult
/// FUNCTION: hd_store_export(ExportResult): ExistsKeystoreResult
///
/// export the mnemonic from a hd keystore
#[allow(clippy::derive_partial_eq_without_eq)]
Expand Down Expand Up @@ -353,7 +353,7 @@ pub struct PrivateKeyStoreExportParam {
#[prost(string, tag = "4")]
pub network: ::prost::alloc::string::String,
}
/// FUNCTION: keystore_common_exists(KeystoreCommonExistsParam): KeystoreCommonExistsResult
/// FUNCTION: keystore_common_exists(KeystoreCommonExistsParam): ExistsKeystoreResult
///
/// Check is there a keystore was generate by the special privateKey or mnemonic
#[allow(clippy::derive_partial_eq_without_eq)]
Expand All @@ -368,7 +368,21 @@ pub struct KeystoreCommonExistsParam {
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct KeystoreCommonExistsResult {
pub struct ExistsMnemonicParam {
#[prost(string, tag = "1")]
pub mnemonic: ::prost::alloc::string::String,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct ExistsPrivateKeyParam {
#[prost(string, tag = "1")]
pub private_key: ::prost::alloc::string::String,
#[prost(string, tag = "2")]
pub encoding: ::prost::alloc::string::String,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct ExistsKeystoreResult {
#[prost(bool, tag = "1")]
pub is_exists: bool,
#[prost(string, tag = "2")]
Expand Down
75 changes: 61 additions & 14 deletions token-core/tcx/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@ use crate::api::sign_param::Key;
use crate::api::{
AccountResponse, CalcExternalAddressParam, CalcExternalAddressResult, CreateKeystoreParam,
DecryptDataFromIpfsParam, DecryptDataFromIpfsResult, DeriveAccountsParam, DeriveAccountsResult,
DerivedKeyResult, EncryptDataToIpfsParam, EncryptDataToIpfsResult, ExportPrivateKeyParam,
ExportResult, GeneralResult, GenerateMnemonicResult, GetExtendedPublicKeysParam,
GetExtendedPublicKeysResult, GetPublicKeysParam, GetPublicKeysResult, IdentityResult,
ImportMnemonicParam, ImportPrivateKeyParam, KeyType, KeystoreCommonAccountsParam,
KeystoreCommonExistsParam, KeystoreCommonExistsResult, KeystoreMigrationParam, KeystoreResult,
PrivateKeyStoreExportParam, PublicKeyParam, PublicKeyResult, SignAuthenticationMessageParam,
SignAuthenticationMessageResult, SignHashesParam, SignHashesResult, SignParamPoc,
SignResultPoc, StoreDeleteParam, StoreDeleteResult, V3KeystoreExportInput,
V3KeystoreExportOutput, V3KeystoreImportInput, WalletKeyParam, ZksyncPrivateKeyFromSeedParam,
ZksyncPrivateKeyFromSeedResult, ZksyncPrivateKeyToPubkeyHashParam,
ZksyncPrivateKeyToPubkeyHashResult, ZksyncSignMusigParam, ZksyncSignMusigResult,
DerivedKeyResult, EncryptDataToIpfsParam, EncryptDataToIpfsResult, ExistsKeystoreResult,
ExistsMnemonicParam, ExistsPrivateKeyParam, ExportPrivateKeyParam, ExportResult, GeneralResult,
GenerateMnemonicResult, GetExtendedPublicKeysParam, GetExtendedPublicKeysResult,
GetPublicKeysParam, GetPublicKeysResult, IdentityResult, ImportMnemonicParam,
ImportPrivateKeyParam, KeyType, KeystoreCommonAccountsParam, KeystoreCommonExistsParam,
KeystoreMigrationParam, KeystoreResult, PrivateKeyStoreExportParam, PublicKeyParam,
PublicKeyResult, SignAuthenticationMessageParam, SignAuthenticationMessageResult,
SignHashesParam, SignHashesResult, SignParamPoc, SignResultPoc, StoreDeleteParam,
StoreDeleteResult, V3KeystoreExportInput, V3KeystoreExportOutput, V3KeystoreImportInput,
WalletKeyParam, ZksyncPrivateKeyFromSeedParam, ZksyncPrivateKeyFromSeedResult,
ZksyncPrivateKeyToPubkeyHashParam, ZksyncPrivateKeyToPubkeyHashResult, ZksyncSignMusigParam,
ZksyncSignMusigResult,
};
use crate::api::{InitTokenCoreXParam, SignParam};
use crate::error_handling::Result;
Expand Down Expand Up @@ -521,6 +522,52 @@ pub(crate) fn delete_keystore(data: &[u8]) -> Result<Vec<u8>> {
}
}

pub(crate) fn exists_private_key(data: &[u8]) -> Result<Vec<u8>> {
let param: ExistsPrivateKeyParam =
ExistsPrivateKeyParam::decode(data).expect("ExistsPrivateKeyParam");
let key_hash = if param.encoding.eq("TEZOS") {
key_hash_from_tezos_format_pk(&param.private_key)?
} else {
key_hash_from_any_format_pk(&param.private_key)?
};
exists_key_hash(&key_hash)
}

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

let mnemonic: &str = &&param
.mnemonic
.split_whitespace()
.collect::<Vec<&str>>()
.join(" ");
let key_hash = key_hash_from_mnemonic(mnemonic)?;

exists_key_hash(&key_hash)
}

fn exists_key_hash(key_hash: &str) -> Result<Vec<u8>> {
let map = &KEYSTORE_MAP.read();

let founded: Option<&Keystore> = map
.values()
.find(|keystore| keystore.key_hash() == key_hash);
let result: ExistsKeystoreResult;
if let Some(ks) = founded {
result = ExistsKeystoreResult {
is_exists: true,
id: ks.id(),
}
} else {
result = ExistsKeystoreResult {
is_exists: false,
id: "".to_owned(),
}
}
encode_message(result)
}

pub(crate) fn keystore_common_exists(data: &[u8]) -> Result<Vec<u8>> {
let param: KeystoreCommonExistsParam =
KeystoreCommonExistsParam::decode(data).expect("keystore_common_exists params");
Expand All @@ -544,14 +591,14 @@ pub(crate) fn keystore_common_exists(data: &[u8]) -> Result<Vec<u8>> {
let founded: Option<&Keystore> = map
.values()
.find(|keystore| keystore.key_hash() == key_hash);
let result: KeystoreCommonExistsResult;
let result: ExistsKeystoreResult;
if let Some(ks) = founded {
result = KeystoreCommonExistsResult {
result = ExistsKeystoreResult {
is_exists: true,
id: ks.id(),
}
} else {
result = KeystoreCommonExistsResult {
result = ExistsKeystoreResult {
is_exists: false,
id: "".to_owned(),
}
Expand Down
64 changes: 32 additions & 32 deletions token-core/tcx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::ffi::{CStr, CString};
use std::os::raw::c_char;

use handler::{
calc_external_address, decrypt_data_from_ipfs, encrypt_data_to_ipfs, migrate_keystore,
remove_wallet, sign_authentication_message, sign_message,
calc_external_address, decrypt_data_from_ipfs, encrypt_data_to_ipfs, exists_mnemonic,
exists_private_key, migrate_keystore, remove_wallet, sign_authentication_message, sign_message,
};
// use handler::{eth_v3keystore_export, eth_v3keystore_import};
use prost::Message;
Expand Down Expand Up @@ -91,9 +91,8 @@ pub unsafe extern "C" fn call_tcx_api(hex_str: *const c_char) -> *const c_char {
"export_private_key" => landingpad(|| export_private_key(&action.param.unwrap().value)),
"verify_password" => landingpad(|| verify_password(&action.param.unwrap().value)),
"delete_keystore" => landingpad(|| delete_keystore(&action.param.unwrap().value)),
"keystore_common_exists" => {
landingpad(|| keystore_common_exists(&action.param.unwrap().value))
}
"exists_mnemonic" => landingpad(|| exists_mnemonic(&action.param.unwrap().value)),
"exists_private_key" => landingpad(|| exists_private_key(&action.param.unwrap().value)),
"calc_external_address" => {
landingpad(|| calc_external_address(&action.param.unwrap().value))
}
Expand Down Expand Up @@ -177,8 +176,8 @@ pub unsafe extern "C" fn get_last_err_message() -> *const c_char {
#[cfg(test)]
mod tests {
use super::*;
use crate::api::sign_hashes_param::DataToSign;
use crate::api::derive_accounts_param::Derivation;
use crate::api::sign_hashes_param::DataToSign;
use crate::filemanager::KEYSTORE_MAP;
use api::sign_param::Key;
use error_handling::Result;
Expand All @@ -194,16 +193,17 @@ mod tests {
sign_param, AccountResponse, CalcExternalAddressParam, CalcExternalAddressResult,
CreateKeystoreParam, DecryptDataFromIpfsParam, DecryptDataFromIpfsResult,
DeriveAccountsParam, DeriveAccountsResult, DerivedKeyResult, EncryptDataToIpfsParam,
EncryptDataToIpfsResult, ExportPrivateKeyParam, ExportResult, GeneralResult,
GenerateMnemonicResult, IdentityResult, ImportMnemonicParam, ImportPrivateKeyParam,
InitTokenCoreXParam, KeyType, KeystoreCommonAccountsParam, KeystoreCommonExistsParam,
KeystoreCommonExistsResult, KeystoreMigrationParam, KeystoreResult,
PrivateKeyStoreExportParam, PublicKeyParam, PublicKeyResult,
SignAuthenticationMessageParam, SignAuthenticationMessageResult, SignParam,
StoreDeleteParam, StoreDeleteResult, V3KeystoreExportInput, V3KeystoreExportOutput,
V3KeystoreImportInput, WalletKeyParam, ZksyncPrivateKeyFromSeedParam,
ZksyncPrivateKeyFromSeedResult, ZksyncPrivateKeyToPubkeyHashParam,
ZksyncPrivateKeyToPubkeyHashResult, ZksyncSignMusigParam, ZksyncSignMusigResult, SignHashesParam, GetPublicKeysParam, PublicKeyDerivation, GetPublicKeysResult, SignHashesResult,
EncryptDataToIpfsResult, ExistsKeystoreResult, ExportPrivateKeyParam, ExportResult,
GeneralResult, GenerateMnemonicResult, GetPublicKeysParam, GetPublicKeysResult,
IdentityResult, ImportMnemonicParam, ImportPrivateKeyParam, InitTokenCoreXParam, KeyType,
KeystoreCommonAccountsParam, KeystoreCommonExistsParam, KeystoreMigrationParam,
KeystoreResult, PrivateKeyStoreExportParam, PublicKeyDerivation, PublicKeyParam,
PublicKeyResult, SignAuthenticationMessageParam, SignAuthenticationMessageResult,
SignHashesParam, SignHashesResult, SignParam, StoreDeleteParam, StoreDeleteResult,
V3KeystoreExportInput, V3KeystoreExportOutput, V3KeystoreImportInput, WalletKeyParam,
ZksyncPrivateKeyFromSeedParam, ZksyncPrivateKeyFromSeedResult,
ZksyncPrivateKeyToPubkeyHashParam, ZksyncPrivateKeyToPubkeyHashResult,
ZksyncSignMusigParam, ZksyncSignMusigResult,
};
use crate::handler::import_mnemonic;
use crate::handler::{encode_message, import_private_key};
Expand Down Expand Up @@ -1007,8 +1007,8 @@ mod tests {
};

let ret_bytes = call_api("keystore_common_exists", param).unwrap();
let result: KeystoreCommonExistsResult =
KeystoreCommonExistsResult::decode(ret_bytes.as_slice()).unwrap();
let result: ExistsKeystoreResult =
ExistsKeystoreResult::decode(ret_bytes.as_slice()).unwrap();
assert!(result.is_exists);
assert_eq!(result.id, import_result.id);

Expand Down Expand Up @@ -1649,8 +1649,8 @@ mod tests {
};

let ret_bytes = call_api("keystore_common_exists", param).unwrap();
let ret: KeystoreCommonExistsResult =
KeystoreCommonExistsResult::decode(ret_bytes.as_slice()).unwrap();
let ret: ExistsKeystoreResult =
ExistsKeystoreResult::decode(ret_bytes.as_slice()).unwrap();

assert_eq!(false, ret.is_exists);
})
Expand All @@ -1667,8 +1667,8 @@ mod tests {
};

let ret_bytes = call_api("keystore_common_exists", param).unwrap();
let result: KeystoreCommonExistsResult =
KeystoreCommonExistsResult::decode(ret_bytes.as_slice()).unwrap();
let result: ExistsKeystoreResult =
ExistsKeystoreResult::decode(ret_bytes.as_slice()).unwrap();
assert!(result.is_exists);
assert_eq!(result.id, wallet.id);

Expand All @@ -1680,8 +1680,8 @@ mod tests {
};

let ret_bytes = call_api("keystore_common_exists", param).unwrap();
let result: KeystoreCommonExistsResult =
KeystoreCommonExistsResult::decode(ret_bytes.as_slice()).unwrap();
let result: ExistsKeystoreResult =
ExistsKeystoreResult::decode(ret_bytes.as_slice()).unwrap();
assert!(result.is_exists);
assert_eq!(result.id, wallet.id);

Expand All @@ -1693,8 +1693,8 @@ mod tests {
};

let ret_bytes = call_api("keystore_common_exists", param).unwrap();
let result: KeystoreCommonExistsResult =
KeystoreCommonExistsResult::decode(ret_bytes.as_slice()).unwrap();
let result: ExistsKeystoreResult =
ExistsKeystoreResult::decode(ret_bytes.as_slice()).unwrap();
assert!(result.is_exists);
assert_eq!(result.id, wallet.id);

Expand All @@ -1706,8 +1706,8 @@ mod tests {
};

let ret_bytes = call_api("keystore_common_exists", param).unwrap();
let result: KeystoreCommonExistsResult =
KeystoreCommonExistsResult::decode(ret_bytes.as_slice()).unwrap();
let result: ExistsKeystoreResult =
ExistsKeystoreResult::decode(ret_bytes.as_slice()).unwrap();
assert!(result.is_exists);
assert_eq!(result.id, wallet.id);

Expand Down Expand Up @@ -2176,16 +2176,16 @@ mod tests {

let ret_bytes = call_api("exists_json", param.clone()).unwrap();

let exists_result: KeystoreCommonExistsResult =
KeystoreCommonExistsResult::decode(ret_bytes.as_slice()).unwrap();
let exists_result: ExistsKeystoreResult =
ExistsKeystoreResult::decode(ret_bytes.as_slice()).unwrap();
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 ret_bytes = call_api("exists_json", param.clone()).unwrap();
let exists_result: KeystoreCommonExistsResult =
KeystoreCommonExistsResult::decode(ret_bytes.as_slice()).unwrap();
let exists_result: ExistsKeystoreResult =
ExistsKeystoreResult::decode(ret_bytes.as_slice()).unwrap();
assert!(exists_result.is_exists);

let derivation = Derivation {
Expand Down

0 comments on commit 42d810e

Please sign in to comment.