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

scan legacy keystores #59

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
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.

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ build-tcx:
cd token-core; cargo build

test-tcx:
cd token-core; cargo test
cd token-core; KDF_ROUNDS=1 cargo test
3 changes: 3 additions & 0 deletions token-core/tcx-btc-kin/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ mod tests {
TEST_PASSWORD,
CurveType::SECP256k1,
Metadata::default(),
None,
)
.unwrap();
keystore.unlock_by_password(TEST_PASSWORD).unwrap();
Expand Down Expand Up @@ -623,6 +624,7 @@ mod tests {
TEST_PASSWORD,
CurveType::SECP256k1,
Metadata::default(),
None,
)
.unwrap();
ks.unlock_by_password(TEST_PASSWORD).unwrap();
Expand Down Expand Up @@ -1377,6 +1379,7 @@ mod tests {
TEST_PASSWORD,
CurveType::SECP256k1,
Metadata::default(),
None,
)
.unwrap();
let _coin_info = coin_info_from_param("LITECOIN", "TESTNET", "NONE", "").unwrap();
Expand Down
1 change: 1 addition & 0 deletions token-core/tcx-ckb/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ mod tests {
TEST_PASSWORD,
CurveType::SECP256k1,
Metadata::default(),
None,
)
.unwrap();
ks.unlock_by_password(TEST_PASSWORD).unwrap();
Expand Down
1 change: 1 addition & 0 deletions token-core/tcx-constants/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tcx-common = { path = "../tcx-common" }
failure = "=0.1.8"
lazy_static = "=1.4.0"
serde_json = "=1.0.89"
Expand Down
26 changes: 22 additions & 4 deletions token-core/tcx-constants/src/coin_info.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
use crate::curve::CurveType;
use crate::Result;
use failure::format_err;
use tcx_common::FromHex;

use parking_lot::RwLock;

// pub enum Coin {
// Ethereum { path: String, chain_id: i32 },
// }

pub fn get_xpub_prefix(network: &str, derivation_path: &str) -> Vec<u8> {
if derivation_path.starts_with("m/49'") {
if network == "MAINNET" {
Vec::from_hex("049d7cb2").unwrap()
} else {
Vec::from_hex("044a5262").unwrap()
}
} else if derivation_path.starts_with("m/84'") {
if network == "MAINNET" {
Vec::from_hex("04b24746").unwrap()
} else {
Vec::from_hex("045f1cf6").unwrap()
}
} else {
if network == "MAINNET" {
Vec::from_hex("0488b21e").unwrap()
} else {
Vec::from_hex("043587cf").unwrap()
}
}
}
/// Blockchain basic config
#[derive(Clone)]
pub struct CoinInfo {
Expand Down
4 changes: 2 additions & 2 deletions token-core/tcx-crypto/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ impl Crypto {
match key {
Key::Password(password) => {
let derived_key = self.derive_key(password)?;

dbg!(derived_key.to_0x_hex());
if !self.mac.is_empty() && !self.verify_derived_key(&derived_key) {
return Err(Error::PasswordIncorrect.into());
}
Expand All @@ -242,7 +242,7 @@ impl Crypto {
})
}
Key::DerivedKey(derived_key_hex) => {
let derived_key = Vec::from_hex(derived_key_hex)?;
let derived_key = Vec::from_hex_auto(derived_key_hex)?;
if !self.verify_derived_key(&derived_key) {
return Err(Error::PasswordIncorrect.into());
}
Expand Down
11 changes: 8 additions & 3 deletions token-core/tcx-eth/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,14 @@ mod test {
use tcx_keystore::{Keystore, MessageSigner, Metadata, SignatureParameters, TransactionSigner};

fn private_key_store(key: &str) -> Keystore {
let mut ks =
Keystore::from_private_key(key, "imToken1", CurveType::SECP256k1, Metadata::default())
.unwrap();
let mut ks = Keystore::from_private_key(
key,
"imToken1",
CurveType::SECP256k1,
Metadata::default(),
None,
)
.unwrap();
ks.unlock_by_password("imToken1").unwrap();
ks
}
Expand Down
3 changes: 3 additions & 0 deletions token-core/tcx-filecoin/src/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ mod tests {
"Password",
CurveType::SECP256k1,
Metadata::default(),
None,
)
.unwrap();
ks.unlock_by_password("Password").unwrap();
Expand Down Expand Up @@ -172,6 +173,7 @@ mod tests {
"Password",
CurveType::SECP256k1,
Metadata::default(),
None,
)
.unwrap();
ks.unlock_by_password("Password").unwrap();
Expand Down Expand Up @@ -218,6 +220,7 @@ mod tests {
"Password",
CurveType::SECP256k1,
Metadata::default(),
None,
)
.unwrap();
ks.unlock_by_password("Password").unwrap();
Expand Down
12 changes: 9 additions & 3 deletions token-core/tcx-keystore/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ mod test {
&PASSWORD,
tcx_constants::CurveType::SECP256k1,
meta,
None,
)
.unwrap();
let identity = &keystore.store().identity;
Expand All @@ -410,9 +411,14 @@ mod test {
fn test_create_identity_from_private_key_testnet() {
let mut meta = Metadata::default();
meta.network = IdentityNetwork::Testnet;
let keystore =
PrivateKeystore::from_private_key(&PRIVATE_KEY, &PASSWORD, CurveType::SECP256k1, meta)
.unwrap();
let keystore = PrivateKeystore::from_private_key(
&PRIVATE_KEY,
&PASSWORD,
CurveType::SECP256k1,
meta,
None,
)
.unwrap();
let identity = &keystore.store().identity;

assert_eq!(
Expand Down
1 change: 1 addition & 0 deletions token-core/tcx-keystore/src/keystore/guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ mod tests {
TEST_PASSWORD,
CurveType::SECP256k1,
Metadata::default(),
None,
)
.unwrap();
let derived_key = ks.get_derived_key(&TEST_PASSWORD).unwrap();
Expand Down
28 changes: 4 additions & 24 deletions token-core/tcx-keystore/src/keystore/hd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::{transform_mnemonic_error, Account, Address, Error, Metadata, Result,
use std::collections::{hash_map::Entry, HashMap};

use tcx_common::{FromHex, ToHex};
use tcx_constants::{CoinInfo, CurveType};
use tcx_constants::{coin_info::get_xpub_prefix, CoinInfo, CurveType};
use tcx_crypto::{Crypto, Key};
use tcx_primitive::{
generate_mnemonic, get_account_path, Bip32DeterministicPrivateKey, Derive,
Expand Down Expand Up @@ -176,34 +176,13 @@ impl HdKeystore {
meta,
identity,
curve: None,
enc_original: None,
},

cache: None,
})
}

pub fn get_ext_version(network: &str, derivation_path: &str) -> Vec<u8> {
if derivation_path.starts_with("m/49'") {
if network == "MAINNET" {
Vec::from_hex("049d7cb2").unwrap()
} else {
Vec::from_hex("044a5262").unwrap()
}
} else if derivation_path.starts_with("m/84'") {
if network == "MAINNET" {
Vec::from_hex("04b24746").unwrap()
} else {
Vec::from_hex("045f1cf6").unwrap()
}
} else {
if network == "MAINNET" {
Vec::from_hex("0488b21e").unwrap()
} else {
Vec::from_hex("043587cf").unwrap()
}
}
}

pub fn derive_coin<A: Address>(&mut self, coin_info: &CoinInfo) -> Result<Account> {
let cache = self.cache.as_ref().ok_or(Error::KeystoreLocked)?;

Expand All @@ -218,7 +197,7 @@ impl HdKeystore {
_ => root
.derive(&get_account_path(&coin_info.derivation_path)?)?
.deterministic_public_key()
.to_ss58check_with_version(&Self::get_ext_version(
.to_ss58check_with_version(&get_xpub_prefix(
&coin_info.network,
&coin_info.derivation_path,
)),
Expand Down Expand Up @@ -288,6 +267,7 @@ mod tests {
timestamp: metadata_default_time(),
source: Source::Mnemonic,
network: IdentityNetwork::Mainnet,
identified_chain_types: None,
};

assert_eq!(meta.name, expected.name);
Expand Down
12 changes: 10 additions & 2 deletions token-core/tcx-keystore/src/keystore/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub use self::{
};

use crate::identity::Identity;
use tcx_crypto::{Crypto, Key};
use tcx_crypto::{Crypto, EncPair, Key};
use tcx_primitive::{Derive, TypedDeterministicPublicKey, TypedPrivateKey, TypedPublicKey};

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand All @@ -32,7 +32,8 @@ pub struct Store {
pub identity: Identity,
#[serde(skip_serializing_if = "Option::is_none")]
pub curve: Option<CurveType>,

#[serde(skip_serializing_if = "Option::is_none")]
pub enc_original: Option<EncPair>,
#[serde(rename = "imTokenMeta")]
pub meta: Metadata,
}
Expand Down Expand Up @@ -183,6 +184,8 @@ pub struct Metadata {
pub source: Source,
#[serde(default = "metadata_default_network")]
pub network: IdentityNetwork,
#[serde(skip_serializing_if = "Option::is_none")]
pub identified_chain_types: Option<Vec<String>>,
}

fn metadata_default_time() -> i64 {
Expand All @@ -207,6 +210,7 @@ impl Default for Metadata {
timestamp: metadata_default_time(),
source: Source::Mnemonic,
network: IdentityNetwork::Mainnet,
identified_chain_types: None,
}
}
}
Expand All @@ -223,12 +227,14 @@ impl Keystore {
password: &str,
curve: CurveType,
meta: Metadata,
original: Option<String>,
) -> Result<Keystore> {
Ok(Keystore::PrivateKey(PrivateKeystore::from_private_key(
private_key,
password,
curve,
meta,
original,
)?))
}

Expand Down Expand Up @@ -842,6 +848,7 @@ pub(crate) mod tests {
TEST_PASSWORD,
CurveType::SECP256k1,
meta,
None,
)
.unwrap();
let keystore = PrivateKey(pk_store);
Expand Down Expand Up @@ -927,6 +934,7 @@ pub(crate) mod tests {
TEST_PASSWORD,
CurveType::SECP256k1,
Metadata::default(),
None,
)
.unwrap();

Expand Down
8 changes: 8 additions & 0 deletions token-core/tcx-keystore/src/keystore/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,17 @@ impl PrivateKeystore {
password: &str,
curve: CurveType,
meta: Metadata,
original: Option<String>,
) -> Result<PrivateKeystore> {
let key_data: Vec<u8> = Vec::from_hex_auto(private_key)?;
let fingerprint = fingerprint_from_private_key(&key_data)?;
let crypto: Crypto = Crypto::new(password, &key_data);
let unlocker = crypto.use_key(&Key::Password(password.to_string()))?;
let identity = Identity::from_private_key(private_key, &unlocker, &meta.network)?;

let enc_original =
original.and_then(|ori| unlocker.encrypt_with_random_iv(ori.as_bytes()).ok());

let store = Store {
source_fingerprint: fingerprint,
crypto,
Expand All @@ -106,6 +110,7 @@ impl PrivateKeystore {
version: PrivateKeystore::VERSION,
identity,
curve: Some(curve),
enc_original: enc_original,
};

Ok(PrivateKeystore {
Expand Down Expand Up @@ -169,6 +174,7 @@ mod tests {
TEST_PASSWORD,
CurveType::SECP256k1,
meta,
None,
)
.unwrap();

Expand All @@ -191,6 +197,7 @@ mod tests {
TEST_PASSWORD,
CurveType::SECP256k1,
Metadata::default(),
None,
)
.unwrap();

Expand Down Expand Up @@ -229,6 +236,7 @@ mod tests {
TEST_PASSWORD,
CurveType::SECP256k1,
Metadata::default(),
None,
)
.unwrap();
keystore
Expand Down
Loading
Loading