Skip to content

Commit

Permalink
fix: fix private_key* and json* api
Browse files Browse the repository at this point in the history
  • Loading branch information
XuNeal committed Dec 18, 2023
1 parent b2e4802 commit c230976
Show file tree
Hide file tree
Showing 10 changed files with 299 additions and 186 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions token-core/tcx-migration/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,14 @@ impl LegacyKeystore {
let unlocker = self.crypto.use_key(&key)?;
let sec_key_bytes = unlocker.plaintext()?;
let sec_key = Secp256k1PrivateKey::from_slice(&sec_key_bytes)?;

let pub_key = TypedPublicKey::Secp256k1(sec_key.public_key());
let coin_info = coin_info_from_param("ETHEREUM", "", "", CurveType::SECP256k1.as_str())?;

let calc_address = EthAddress::from_public_key(&pub_key, &coin_info)?.to_string();
let calc_addr_bytes = &Vec::from_hex(calc_address)?;
let addr_bytes = &Vec::from_hex(&self.address)?;
let calc_addr_bytes = &Vec::from_hex_auto(calc_address)?;
let addr_bytes = &Vec::from_hex_auto(&self.address)?;

if calc_addr_bytes == addr_bytes {
Ok(())
} else {
Expand Down
4 changes: 2 additions & 2 deletions token-core/tcx-primitive/src/sr25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ impl TraitPrivateKey for Sr25519PrivateKey {
// MiniSecretKey::from_bytes(data).expect("32 bytes can always build a key; qed");
//
// let kp = mini_key.expand_to_keypair(ExpansionMode::Ed25519);
let pk = SecretKey::from_ed25519_bytes(data).map_err(|_| KeyError::InvalidSr25519Key)?;
// let pk = SecretKey::from_bytes(data).map_err(|_| KeyError::InvalidSr25519Key)?;
// let pk = SecretKey::from_ed25519_bytes(data).map_err(|_| KeyError::InvalidSr25519Key)?;
let pk = SecretKey::from_bytes(data).map_err(|_| KeyError::InvalidSr25519Key)?;
Ok(Sr25519PrivateKey(Pair::from(pk)))
}

Expand Down
5 changes: 3 additions & 2 deletions token-core/tcx-proto/src/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ message SignResultPoc {
}

message PublicKeyDerivation {
string path = 1;
string curve = 2;
string chainType = 1;
string path = 2;
string curve = 3;
}

message GetPublicKeysParam {
Expand Down
24 changes: 0 additions & 24 deletions token-core/tcx-proto/src/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,6 @@ message ImportPrivateKeyParam {
bool overwrite = 5;
}

// FUNCTION: private_key_store_export(PrivateKeyStoreExportParam): ExportResult
//
// export the private key from a private key keystore
message PrivateKeyStoreExportParam {
string id = 1;
string password = 2;
string chainType = 3;
string network = 4;
}

// FUNCTION: keystore_common_exists(KeystoreCommonExistsParam): ExistsKeystoreResult
//
// Check is there a keystore was generate by the special privateKey or mnemonic
Expand Down Expand Up @@ -205,20 +195,6 @@ message DeriveSubAccountsResult {
repeated AccountResponse accounts = 1;
}


message PublicKeyParam {
string id = 1;
string chainType = 2;
string address = 3;
}

message PublicKeyResult {
string id = 1;
string chainType = 2;
string address = 3;
string publicKey = 4;
}

message RemoveWalletParam {
string id = 1;
string password = 2;
Expand Down
1 change: 1 addition & 0 deletions token-core/tcx-substrate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ serde = { version = "1.0.147", features = ["derive"] }
base64 = "0.13.1"
scrypt = { version = "0.10.0", default-features = false }
regex = "1.7.0"
schnorrkel = "0.9.1"


[dev-dependencies]
Expand Down
47 changes: 29 additions & 18 deletions token-core/tcx-substrate/src/keystore.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
use crate::SubstrateAddress;
use rand::Rng;
use serde::{de, Deserialize, Deserializer, Serialize};
use sp_core::Pair;
use std::convert::TryInto;
use tcx_keystore::Address;
use tcx_keystore::{tcx_ensure, Address};

use byteorder::LittleEndian;
use byteorder::{ReadBytesExt, WriteBytesExt};
use regex::Regex;
use schnorrkel::{SecretKey, SECRET_KEY_LENGTH};
use serde::__private::{fmt, PhantomData};
use std::io::Cursor;
use std::time::{SystemTime, UNIX_EPOCH};
use tcx_common::{random_u8_32, FromHex, ToHex};
use tcx_constants::{CoinInfo, Result};
use tcx_primitive::{
DeterministicPrivateKey, PrivateKey, PublicKey, Sr25519PrivateKey, TypedPublicKey,
DeterministicPrivateKey, PrivateKey, PublicKey, Sr25519PrivateKey, Sr25519PublicKey,
TypedPublicKey,
};
use xsalsa20poly1305::aead::{generic_array::GenericArray, Aead};
use xsalsa20poly1305::{KeyInit, XSalsa20Poly1305};
Expand Down Expand Up @@ -321,19 +324,19 @@ fn password_to_key(password_bytes: &[u8]) -> [u8; 32] {
}

pub fn decode_substrate_keystore(keystore: &SubstrateKeystore, password: &str) -> Result<Vec<u8>> {
let (secret_key, pub_key) = keystore.decrypt(password)?;
dbg!(secret_key.to_hex());
// let priv_key = if secret_key.len() == 32 {
// Sr25519PrivateKey::from_seed(&secret_key)
// } else {
// Sr25519PrivateKey::from_slice(&secret_key)
// }?;
let priv_key = Sr25519PrivateKey::from_slice(&secret_key)?;
// if priv_key.public_key().to_bytes() != pub_key {
// return Err(Error::KeystorePublicKeyUnmatch.into());
// }
dbg!("decode_substrate_keystore success");
dbg!(priv_key.to_bytes().to_0x_hex());
let (secret_key_bytes, pub_key) = keystore.decrypt(password)?;
tcx_ensure!(
secret_key_bytes.len() == SECRET_KEY_LENGTH,
format_err!("secret from substrate keystore must be 64 bytes")
);
let secret_key = SecretKey::from_ed25519_bytes(&secret_key_bytes)
.map_err(|_| format_err!("secret key from_ed25519_bytes error"))?;
let priv_key = Sr25519PrivateKey::from_slice(&secret_key.to_bytes())?;

if priv_key.public_key().to_bytes() != pub_key {
return Err(Error::KeystorePublicKeyUnmatch.into());
}

Ok(priv_key.to_bytes())
}

Expand All @@ -342,10 +345,18 @@ pub fn encode_substrate_keystore(
prv_key: &[u8],
coin: &CoinInfo,
) -> Result<SubstrateKeystore> {
let pk = Sr25519PrivateKey::from_slice(prv_key)?;
let pub_key = pk.public_key();
let sec_key = SecretKey::from_bytes(prv_key)
.map_err(|_| format_err!("construct secret key error when encoded_substrate_keystore"))?;
let pair = sp_core::sr25519::Pair::from(sec_key.clone());
let pub_key = Sr25519PublicKey(pair.public());
let addr = SubstrateAddress::from_public_key(&TypedPublicKey::Sr25519(pub_key.clone()), &coin)?;
SubstrateKeystore::new(password, prv_key, &pub_key.to_bytes(), &addr.to_string())
let ed25519_prv_key_bytes = sec_key.to_ed25519_bytes();
SubstrateKeystore::new(
password,
&ed25519_prv_key_bytes,
&pub_key.to_bytes(),
&addr.to_string(),
)
}

#[cfg(test)]
Expand Down
41 changes: 3 additions & 38 deletions token-core/tcx/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ pub struct SignResultPoc {
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct PublicKeyDerivation {
#[prost(string, tag = "1")]
pub path: ::prost::alloc::string::String,
pub chain_type: ::prost::alloc::string::String,
#[prost(string, tag = "2")]
pub path: ::prost::alloc::string::String,
#[prost(string, tag = "3")]
pub curve: ::prost::alloc::string::String,
}
#[allow(clippy::derive_partial_eq_without_eq)]
Expand Down Expand Up @@ -387,21 +389,6 @@ pub struct ImportPrivateKeyParam {
#[prost(bool, tag = "5")]
pub overwrite: bool,
}
/// FUNCTION: private_key_store_export(PrivateKeyStoreExportParam): ExportResult
///
/// export the private key from a private key keystore
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct PrivateKeyStoreExportParam {
#[prost(string, tag = "1")]
pub id: ::prost::alloc::string::String,
#[prost(string, tag = "2")]
pub password: ::prost::alloc::string::String,
#[prost(string, tag = "3")]
pub chain_type: ::prost::alloc::string::String,
#[prost(string, tag = "4")]
pub network: ::prost::alloc::string::String,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct ExistsMnemonicParam {
Expand Down Expand Up @@ -510,28 +497,6 @@ pub struct DeriveSubAccountsResult {
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct PublicKeyParam {
#[prost(string, tag = "1")]
pub id: ::prost::alloc::string::String,
#[prost(string, tag = "2")]
pub chain_type: ::prost::alloc::string::String,
#[prost(string, tag = "3")]
pub address: ::prost::alloc::string::String,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct PublicKeyResult {
#[prost(string, tag = "1")]
pub id: ::prost::alloc::string::String,
#[prost(string, tag = "2")]
pub chain_type: ::prost::alloc::string::String,
#[prost(string, tag = "3")]
pub address: ::prost::alloc::string::String,
#[prost(string, tag = "4")]
pub public_key: ::prost::alloc::string::String,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct RemoveWalletParam {
#[prost(string, tag = "1")]
pub id: ::prost::alloc::string::String,
Expand Down
Loading

0 comments on commit c230976

Please sign in to comment.