Skip to content

Commit

Permalink
feat: add mnemonic_to_public api
Browse files Browse the repository at this point in the history
  • Loading branch information
XuNeal committed Dec 12, 2023
1 parent a893caf commit 26db2f0
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 124 deletions.
6 changes: 0 additions & 6 deletions token-core/tcx-keystore/src/keystore/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,13 +359,7 @@ impl Keystore {
xpub: &TypedDeterministicPublicKey,
coin_info: &CoinInfo,
) -> Result<Account> {
// let acc_path = get_account_path(path)?;
// let change_path = format!("{}/{}", acc_path, relative_path);
// TODO: test derive from relative path?

let typed_pk = xpub.derive(&coin_info.derivation_path)?.public_key();
// let typed_pk = TypedPublicKey::Secp256k1(public_key);

let address = A::from_public_key(&typed_pk, &coin_info)?.to_string();
let account = Account {
address,
Expand Down
12 changes: 11 additions & 1 deletion token-core/tcx-primitive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ mod rand;
mod secp256k1;
mod sr25519;
mod subkey;

use core::result;
use tcx_common::ToHex;

pub type Result<T> = result::Result<T, failure::Error>;

use tcx_constants::CurveType;

pub use crate::bip32::{Bip32DeterministicPrivateKey, Bip32DeterministicPublicKey};
pub use crate::derive::{get_account_path, Derive, DeriveJunction, DerivePath};
pub use crate::ecc::{
Expand Down Expand Up @@ -45,3 +47,11 @@ pub trait Ss58Codec: Sized {
/// Return the ss58-check string for this key.
fn to_ss58check_with_version(&self, version: &[u8]) -> String;
}

pub fn mnemonic_to_public(mnemonic: &str, path: &str, curve: &str) -> Result<TypedPublicKey> {
let curve_type = CurveType::from_str(curve);
let root = TypedDeterministicPrivateKey::from_mnemonic(curve_type, mnemonic)?;

let private_key = root.derive(&path)?.private_key();
Ok(private_key.public_key())
}
93 changes: 28 additions & 65 deletions token-core/tcx-proto/src/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -182,44 +182,6 @@ message DeriveSubAccountsResult {
repeated AccountResponse accounts = 1;
}

// btc-fork
// message ExternalAddressParam {
// string id = 1;
// string chainType = 2;
// uint32 externalIdx = 3;
// }

// message ExternalAddressResult {
// string address = 1;
// string derivedPath = 2;
// string type = 3;
// }

// message ExternalAddressExtra {
// string encXpub = 1;
// message ExternalAddress {
// string address = 1;
// string derivedPath = 2;
// string type = 3;
// }
// ExternalAddress externalAddress = 2;
// }

// message BtcForkDeriveExtraParam {
// string network = 1;
// string segWit = 2;
// }

// message HdStoreExtendedPublicKeyParam {
// string id = 1;
// string password = 2;
// string chainType = 3;
// string address = 4;
// }

// message HdStoreExtendedPublicKeyResponse {
// string extendedPublicKey = 1;
// }

message PublicKeyParam {
string id = 1;
Expand All @@ -234,42 +196,32 @@ message PublicKeyResult {
string publicKey = 4;
}


// message KeystoreUpdateAccount {
// string id = 1;
// string password = 2;
// string accountName = 3;
// }




message StoreDeleteParam {
message RemoveWalletParam {
string id = 1;
string password = 2;
}

message StoreDeleteResult {
message RemoveWalletResult {
bool isSuccess = 1;
}

message V3KeystoreImportInput {
string keystore = 1;
string password = 2;
bool overwrite = 3;
string name = 4;
string chainType = 5;
string source = 6;
}
// message V3KeystoreImportInput {
// string keystore = 1;
// string password = 2;
// bool overwrite = 3;
// string name = 4;
// string chainType = 5;
// string source = 6;
// }

message V3KeystoreExportInput {
string id = 1;
string password = 2;
}
// message V3KeystoreExportInput {
// string id = 1;
// string password = 2;
// }

message V3KeystoreExportOutput {
string json = 1;
}
// message V3KeystoreExportOutput {
// string json = 1;
// }

message EncryptDataToIpfsParam {
string identifier = 1;
Expand Down Expand Up @@ -305,4 +257,15 @@ message SignAuthenticationMessageResult {

message GenerateMnemonicResult {
string mnemonic = 1;
}

message MnemonicToPublicKeyParam {
string mnemonic = 1;
string path = 2;
string curve = 3;
string encoding = 4;
}

message MnemonicToPublicKeyResult {
string publicKey = 1;
}
52 changes: 20 additions & 32 deletions token-core/tcx/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,50 +482,20 @@ pub struct PublicKeyResult {
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct StoreDeleteParam {
pub struct RemoveWalletParam {
#[prost(string, tag = "1")]
pub id: ::prost::alloc::string::String,
#[prost(string, tag = "2")]
pub password: ::prost::alloc::string::String,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct StoreDeleteResult {
pub struct RemoveWalletResult {
#[prost(bool, tag = "1")]
pub is_success: bool,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct V3KeystoreImportInput {
#[prost(string, tag = "1")]
pub keystore: ::prost::alloc::string::String,
#[prost(string, tag = "2")]
pub password: ::prost::alloc::string::String,
#[prost(bool, tag = "3")]
pub overwrite: bool,
#[prost(string, tag = "4")]
pub name: ::prost::alloc::string::String,
#[prost(string, tag = "5")]
pub chain_type: ::prost::alloc::string::String,
#[prost(string, tag = "6")]
pub source: ::prost::alloc::string::String,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct V3KeystoreExportInput {
#[prost(string, tag = "1")]
pub id: ::prost::alloc::string::String,
#[prost(string, tag = "2")]
pub password: ::prost::alloc::string::String,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct V3KeystoreExportOutput {
#[prost(string, tag = "1")]
pub json: ::prost::alloc::string::String,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct EncryptDataToIpfsParam {
#[prost(string, tag = "1")]
pub identifier: ::prost::alloc::string::String,
Expand Down Expand Up @@ -582,6 +552,24 @@ pub struct GenerateMnemonicResult {
#[prost(string, tag = "1")]
pub mnemonic: ::prost::alloc::string::String,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct MnemonicToPublicKeyParam {
#[prost(string, tag = "1")]
pub mnemonic: ::prost::alloc::string::String,
#[prost(string, tag = "2")]
pub path: ::prost::alloc::string::String,
#[prost(string, tag = "3")]
pub curve: ::prost::alloc::string::String,
#[prost(string, tag = "4")]
pub encoding: ::prost::alloc::string::String,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct MnemonicToPublicKeyResult {
#[prost(string, tag = "1")]
pub public_key: ::prost::alloc::string::String,
}
/// only support two types
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
#[repr(i32)]
Expand Down
30 changes: 22 additions & 8 deletions token-core/tcx/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ use std::fs;
use std::io::Read;
use std::path::Path;
use std::str::FromStr;
use tcx_eos::address::EosAddress;
use tcx_keystore::keystore::IdentityNetwork;

use tcx_common::{FromHex, ToHex};
use tcx_primitive::{private_key_without_version, TypedPrivateKey};

use tcx_btc_kin::WIFDisplay;
use tcx_keystore::{
key_hash_from_mnemonic, key_hash_from_private_key, Keystore, KeystoreGuard,
key_hash_from_mnemonic, key_hash_from_private_key, Address, Keystore, KeystoreGuard,
SignatureParameters, Signer,
};
use tcx_keystore::{Account, HdKeystore, Metadata, PrivateKeystore, Source};
Expand All @@ -29,12 +30,12 @@ use crate::api::{
ExistsMnemonicParam, ExistsPrivateKeyParam, ExportPrivateKeyParam, ExportResult, GeneralResult,
GenerateMnemonicResult, GetExtendedPublicKeysParam, GetExtendedPublicKeysResult,
GetPublicKeysParam, GetPublicKeysResult, ImportMnemonicParam, ImportPrivateKeyParam, KeyType,
KeystoreCommonExistsParam, KeystoreMigrationParam, KeystoreResult,
KeystoreCommonExistsParam, KeystoreMigrationParam, KeystoreResult, MnemonicToPublicKeyParam,
MnemonicToPublicKeyResult, RemoveWalletParam, RemoveWalletResult,
SignAuthenticationMessageParam, SignAuthenticationMessageResult, SignHashesParam,
SignHashesResult, StoreDeleteParam, StoreDeleteResult, WalletKeyParam,
ZksyncPrivateKeyFromSeedParam, ZksyncPrivateKeyFromSeedResult,
ZksyncPrivateKeyToPubkeyHashParam, ZksyncPrivateKeyToPubkeyHashResult, ZksyncSignMusigParam,
ZksyncSignMusigResult,
SignHashesResult, WalletKeyParam, ZksyncPrivateKeyFromSeedParam,
ZksyncPrivateKeyFromSeedResult, ZksyncPrivateKeyToPubkeyHashParam,
ZksyncPrivateKeyToPubkeyHashResult, ZksyncSignMusigParam, ZksyncSignMusigResult,
};
use crate::api::{InitTokenCoreXParam, SignParam};
use crate::error_handling::Result;
Expand Down Expand Up @@ -269,6 +270,7 @@ fn encrypt_xpub(xpub: &str, network: &str) -> Result<String> {
Ok(base64::encode(&encrypted))
}

#[allow(dead_code)]
fn decrypt_xpub(enc_xpub: &str) -> Result<Bip32DeterministicPublicKey> {
let encrypted = base64::decode(enc_xpub)?;
let key = tcx_crypto::XPUB_COMMON_KEY_128.read();
Expand Down Expand Up @@ -897,7 +899,7 @@ pub(crate) fn generate_mnemonic() -> Result<Vec<u8>> {
}

pub(crate) fn remove_wallet(data: &[u8]) -> Result<Vec<u8>> {
let param: StoreDeleteParam = StoreDeleteParam::decode(data)?;
let param: RemoveWalletParam = RemoveWalletParam::decode(data)?;
let map = KEYSTORE_MAP.read();
let keystore: &Keystore = match map.get(&param.id) {
Some(keystore) => Ok(keystore),
Expand All @@ -909,7 +911,7 @@ pub(crate) fn remove_wallet(data: &[u8]) -> Result<Vec<u8>> {
}
filemanager::delete_keystore_file(&param.id)?;

let result = StoreDeleteResult { is_success: true };
let result = RemoveWalletResult { is_success: true };
encode_message(result)
}

Expand Down Expand Up @@ -1076,3 +1078,15 @@ pub(crate) fn migrate_keystore(data: &[u8]) -> Result<Vec<u8>> {
Err(format_err!("invalid version in keystore"))
}
}

pub(crate) fn mnemonic_to_public(data: &[u8]) -> Result<Vec<u8>> {
let param = MnemonicToPublicKeyParam::decode(data)?;
let public_key = tcx_primitive::mnemonic_to_public(&param.mnemonic, &param.path, &param.curve)?;
let public_key_str = match param.encoding.to_uppercase().as_str() {
"EOS" => EosAddress::from_public_key(&public_key, &CoinInfo::default())?.to_string(),
_ => public_key.to_bytes().to_0x_hex(),
};
encode_message(MnemonicToPublicKeyResult {
public_key: public_key_str,
})
}
Loading

0 comments on commit 26db2f0

Please sign in to comment.