Skip to content

Commit

Permalink
feat: support old eth keystore
Browse files Browse the repository at this point in the history
  • Loading branch information
XuNeal committed Jun 4, 2024
1 parent 8570d6f commit b50364a
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 26 deletions.
12 changes: 12 additions & 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 VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.7.2
2.7.3
1 change: 1 addition & 0 deletions token-core/tcx-migration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ base64 = "=0.13.1"
base58 = "=0.2.0"
parking_lot = "=0.12.1"
uuid = { version = "=1.2.2", features = ["serde", "v4"] }
serde-aux = {version = "=4.5.0"}
67 changes: 58 additions & 9 deletions token-core/tcx-migration/src/migration.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use anyhow::anyhow;
use serde::{Deserialize, Serialize};
use serde::de::DeserializeOwned;
use serde::{de, Deserialize, Deserializer, Serialize};
use serde_json::Value;
use std::collections::BTreeMap as Map;
use std::str::FromStr;
use tcx_common::{FromHex, ToHex};
use tcx_constants::{coin_info_from_param, CurveType};
Expand Down Expand Up @@ -93,6 +96,7 @@ pub struct EOSKeyPath {
pub struct LegacyKeystore {
pub version: i32,
pub id: String,
#[serde(alias = "Crypto", alias = "crypto")]
pub crypto: Crypto,
#[serde(skip_serializing_if = "Option::is_none")]
pub enc_mnemonic: Option<EncPair>,
Expand All @@ -107,6 +111,19 @@ pub struct LegacyKeystore {
pub key_path_privates: Option<Vec<EOSKeyPath>>,
}

fn case_insensitive<'de, T, D>(deserializer: D) -> std::result::Result<T, D::Error>
where
T: DeserializeOwned,
D: Deserializer<'de>,
{
let map = Map::<String, Value>::deserialize(deserializer)?;
let lower = map
.into_iter()
.map(|(k, v)| (k.to_lowercase(), v))
.collect();
T::deserialize(Value::Object(lower)).map_err(de::Error::custom)
}

impl LegacyKeystore {
pub fn new_v3(private_key: &[u8], password: &str) -> Result<Self> {
let crypto = Crypto::new(password, private_key);
Expand Down Expand Up @@ -318,7 +335,7 @@ mod tests {

fn v44_bitcoin_mnemonic_1() -> (&'static str, &'static str) {
(
include_str!("../tests/fixtures/02a55ab6-554a-4e78-bc26-6a7acced7e5e.json"),
include_str!("../tests/fixtures/02a55ab6-554a-4e78-bc26-6a7acced7e5e.json"),
"7f3ebdfe19a22e6a64be6834f61f9c79e9502a60e6b22f89654b1daae19ad2bf0c7556398713452d8bbecaae9cec1dbf116cfea89a15d4b1c23570fb95a34443",
)
}
Expand All @@ -329,7 +346,7 @@ mod tests {

fn v3_eos_private_key() -> (&'static str, &'static str) {
(include_str!("../tests/fixtures/42c275c6-957a-49e8-9eb3-43c21cbf583f.json"),
"6ecb361252e42b4f4e2dcd2e0c3331e8a58cb1522aee0dfad6d950b681d48fc78f11a3b12c05a56899339eef24109ea0c7c0287a1427232f04be9004fbf8f7b4")
"6ecb361252e42b4f4e2dcd2e0c3331e8a58cb1522aee0dfad6d950b681d48fc78f11a3b12c05a56899339eef24109ea0c7c0287a1427232f04be9004fbf8f7b4")
}

fn v3_eth_private_key() -> (&'static str, &'static str) {
Expand All @@ -339,10 +356,17 @@ mod tests {
)
}

fn v3_eth_keystore_capital_crypto_field() -> (&'static str, &'static str) {
(
include_str!("../tests/fixtures/045861fe-0e9b-4069-92aa-0ac03cad55e1.json"),
"d71eb325f9c20b9d84cbf44bb9a952d9a27f92672ea04de44843ca6dca3214de81ed31e0d784cd8389231057f99f9bb7caecefb578b349a355c320cd47fbb6f2",
)
}

fn v3_eth_mnemonic() -> (&'static str, &'static str) {
(
include_str!("../tests/fixtures/175169f7-5a35-4df7-93c1-1ff612168e71.json"),
"3223cd3abf2422d0ad3503f73aaa6e7e36a555385c6825b383908c1e8acf5e9d9a4c751809473c75599a632fe5b1437f51a3a848e054d9c170f8c3b5c5701b8b",
include_str!("../tests/fixtures/175169f7-5a35-4df7-93c1-1ff612168e71.json"),
"3223cd3abf2422d0ad3503f73aaa6e7e36a555385c6825b383908c1e8acf5e9d9a4c751809473c75599a632fe5b1437f51a3a848e054d9c170f8c3b5c5701b8b",
)
}

Expand All @@ -352,15 +376,15 @@ mod tests {

fn ios_metadata() -> (&'static str, &'static str) {
(
include_str!("../tests/fixtures/5991857a-2488-4546-b730-463a5f84ea6a"),
"3223cd3abf2422d0ad3503f73aaa6e7e36a555385c6825b383908c1e8acf5e9d9a4c751809473c75599a632fe5b1437f51a3a848e054d9c170f8c3b5c5701b8b",
include_str!("../tests/fixtures/5991857a-2488-4546-b730-463a5f84ea6a"),
"3223cd3abf2422d0ad3503f73aaa6e7e36a555385c6825b383908c1e8acf5e9d9a4c751809473c75599a632fe5b1437f51a3a848e054d9c170f8c3b5c5701b8b",
)
}

fn metadata_missing_password_hint() -> (&'static str, &'static str) {
(
include_str!("../tests/fixtures/6f8c2912-ebe8-4359-90f4-f1d1f1af1e4d"),
"3223cd3abf2422d0ad3503f73aaa6e7e36a555385c6825b383908c1e8acf5e9d9a4c751809473c75599a632fe5b1437f51a3a848e054d9c170f8c3b5c5701b8b",
include_str!("../tests/fixtures/6f8c2912-ebe8-4359-90f4-f1d1f1af1e4d"),
"3223cd3abf2422d0ad3503f73aaa6e7e36a555385c6825b383908c1e8acf5e9d9a4c751809473c75599a632fe5b1437f51a3a848e054d9c170f8c3b5c5701b8b",
)
}

Expand Down Expand Up @@ -467,6 +491,31 @@ mod tests {
assert_eq!("0x41983f2e3Af196C1Df429A3fF5cDECC45c82c600", acc.address);
}

#[test]
fn test_v3_ethereum_keystore_capital_crypto_field() {
let (keystore_str, derived_key) = v3_eth_keystore_capital_crypto_field();
let ks = LegacyKeystore::from_json_str(keystore_str).unwrap();

//password Insecure Pa55w0rd
let key = Key::DerivedKey(derived_key.to_owned());
let mut keystore = ks.migrate(&key, &IdentityNetwork::Testnet).unwrap();

assert_eq!(keystore.derivable(), false);
assert_eq!(keystore.id(), "045861fe-0e9b-4069-92aa-0ac03cad55e1");

let coin_info = CoinInfo {
coin: "ETHEREUM".to_string(),
derivation_path: "".to_string(),
curve: CurveType::SECP256k1,
network: "".to_string(),
seg_wit: "".to_string(),
};

keystore.unlock(&key).unwrap();
let acc = keystore.derive_coin::<EthAddress>(&coin_info).unwrap();
assert_eq!("0x41983f2e3Af196C1Df429A3fF5cDECC45c82c600", acc.address);
}

#[test]
fn test_v3_ethereum_mnemonic() {
let (keystore_str, derived_key) = v3_eth_mnemonic();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"Crypto": {
"cipher": "aes-128-ctr",
"cipherparams": {
"iv": "a322450a5d78b355d3f10d32424bdeb7"
},
"ciphertext": "7323633304b6e10fce17725b2f6ff8190b8e2f1c4fdb29904802e8eb9cb1ac6b",
"kdf": "pbkdf2",
"kdfparams": {
"c": 65535,
"dklen": 32,
"prf": "hmac-sha256",
"salt": "51bcbf8d464d96fca108a6bd7779381076a3f5a6ca5242eb12c8c219f1015767"
},
"mac": "cf81fa8f858554a21d00a376923138e727567f686f30f77fe3bba31b40a91c56"
},
"id": "045861fe-0e9b-4069-92aa-0ac03cad55e1",
"version": 3,
"address": "41983f2e3af196c1df429a3ff5cdecc45c82c600",
"imTokenMeta": {
"backup": [],
"chainType": "ETHEREUM",
"mode": "NORMAL",
"name": "ETH-Wallet-2",
"passwordHint": "",
"source": "KEYSTORE",
"timestamp": 1519611469,
"walletType": "V3"
}
}
9 changes: 8 additions & 1 deletion token-core/tcx/tests/export_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,14 @@ pub fn test_export_private_key() {

// test export as mainnet
assert_eq!(pks[idx], export_result.private_key);
remove_created_wallet(&import_result.id);
let delete_param = WalletKeyParam {
id: import_result.id.to_string(),
key: Some(api::wallet_key_param::Key::Password(
TEST_PASSWORD.to_string(),
)),
};
call_api("delete_keystore", delete_param).unwrap();
// remove_created_wallet(&import_result.id);
}
})
}
Expand Down
22 changes: 7 additions & 15 deletions token-core/tcx/tests/other_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,22 +200,14 @@ pub fn test_keystore_exists() {
assert!(result.is_exists);
assert_eq!(result.id, wallet.id);

let wallet = import_default_wallet();
let param: ExistsMnemonicParam = ExistsMnemonicParam {
mnemonic: format!(
"{}",
" inject kidney empty canal shadow pact comfort wife crush horse wife sketch "
)
.to_string(), //Badly formatted mnemonic
let delete_param = WalletKeyParam {
id: wallet.id.to_string(),
key: Some(api::wallet_key_param::Key::Password(
TEST_PASSWORD.to_string(),
)),
};

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

remove_created_wallet(&wallet.id);
call_api("delete_keystore", delete_param).unwrap();
// remove_created_wallet(&wallet.id);
})
}

Expand Down

0 comments on commit b50364a

Please sign in to comment.