Skip to content

Commit

Permalink
fix: metadata missing password_hint error (#88)
Browse files Browse the repository at this point in the history
* fix: metadata missing password_hint error

* feat: make the password_hint is option
  • Loading branch information
XuNeal committed Apr 18, 2024
1 parent 8e76d89 commit 7799ba6
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 11 deletions.
2 changes: 1 addition & 1 deletion token-core/tcx-keystore/src/keystore/hd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ mod tests {
let meta = Metadata::default();
let expected = Metadata {
name: String::from("Unknown"),
password_hint: String::new(),
password_hint: None,
timestamp: metadata_default_time(),
source: Source::Mnemonic,
network: IdentityNetwork::Mainnet,
Expand Down
7 changes: 4 additions & 3 deletions token-core/tcx-keystore/src/keystore/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ impl fmt::Display for IdentityNetwork {
#[serde(rename_all = "camelCase")]
pub struct Metadata {
pub name: String,
pub password_hint: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub password_hint: Option<String>,
#[serde(default = "metadata_default_time")]
pub timestamp: i64,
#[serde(default = "metadata_default_source")]
Expand Down Expand Up @@ -208,7 +209,7 @@ impl Default for Metadata {
fn default() -> Self {
Metadata {
name: String::from("Unknown"),
password_hint: String::new(),
password_hint: None,
timestamp: metadata_default_time(),
source: Source::Mnemonic,
network: IdentityNetwork::Mainnet,
Expand Down Expand Up @@ -845,7 +846,7 @@ pub(crate) mod tests {

let meta = Metadata {
name: "test_create".to_string(),
password_hint: TEST_PASSWORD.to_string(),
password_hint: Some(TEST_PASSWORD.to_string()),
source: Source::Private,
..Metadata::default()
};
Expand Down
35 changes: 35 additions & 0 deletions token-core/tcx-migration/src/keystore_upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,30 @@ mod tests {
)
}

fn pk_json_without_password_hint(version: i64, source: &str, name: &str) -> Value {
json!(
{
"id":"89e6fc5d-ac9a-46ab-b53f-342a80f3d28b",
"version":version,
"keyHash":"4fc213ddcb6fa44a2e2f4c83d67502f88464e6ee",
"crypto": {
"cipher": "aes-128-ctr",
"cipherparams": { "iv": "31deaa1033b2a55cc39916b347453649" },
"ciphertext": "c52bbc9842da2e9afdf8b905585db5ea4b86920f54f76e5ce686586604a29c3cc152ba9f309d2a1995cddb0ecafbe4d1f43ad033d1e0f5a02e68eda7c2f71687",
"kdf": "pbkdf2",
"kdfparams": {
"c": 1,
"prf": "hmac-sha256",
"dklen": 32,
"salt": "87436212538c3d747df0988246078a308bed9245388badf88dbeb21806e6dd49"
},
"mac": "53af7ee52b69782e775a46def178d0b629c94b04e70fbddc5adafa437b6144e6"
},
"activeAccounts": [{"address": "", "curve": "SubSr25519", "coin": "POLKADOT"}],
"imTokenMeta":{"name":name.to_string(),"timestamp":1576733295,"source":source.to_string()}}
)
}

#[test]
fn test_migration_curve() {
let key = Key::Password("imtoken1".to_owned());
Expand Down Expand Up @@ -553,6 +577,17 @@ mod tests {
.unwrap();
assert_eq!(upgraded.store().curve, Some(CurveType::SR25519));

let upgrade_keystore =
super::KeystoreUpgrade::new(pk_json_without_password_hint(11001, "PRIVATE", "vvvvvv"));

let upgraded = upgrade_keystore
.upgrade(
&Key::Password(TEST_PASSWORD.to_string()),
&IdentityNetwork::Testnet,
)
.unwrap();
assert_eq!(upgraded.store().curve, Some(CurveType::SR25519));

let upgrade_keystore =
super::KeystoreUpgrade::new(pk_json_with_secp256k1(11001, "PRIVATE", "vvvvvv"));

Expand Down
36 changes: 35 additions & 1 deletion token-core/tcx-migration/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct OldMetadata {
pub chain_type: Option<String>,
pub chain: Option<String>,
pub network: Option<String>,
pub password_hint: String,
pub password_hint: Option<String>,
pub timestamp: NumberOrNumberStr,
pub source: Option<String>,
pub seg_wit: Option<String>,
Expand Down Expand Up @@ -357,6 +357,13 @@ mod tests {
)
}

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

#[test]
fn test_eos_private_key() {
let (keystore_str, derived_key) = v3_eos_private_key();
Expand Down Expand Up @@ -501,6 +508,33 @@ mod tests {
seg_wit: "".to_string(),
};

keystore.unlock(&key).unwrap();
let acc = keystore.derive_coin::<EthAddress>(&coin_info).unwrap();
assert_eq!("0x6031564e7b2F5cc33737807b2E58DaFF870B590b", acc.address);
assert_eq!(
keystore.meta().password_hint,
Some("ios_password_hint".to_string())
);
}

#[test]
fn test_metadata_missing_password_hint() {
let (keystore_str, derived_key) = metadata_missing_password_hint();
let ks = LegacyKeystore::from_json_str(keystore_str).unwrap();
//password imtoken1
let key = Key::DerivedKey(derived_key.to_owned());
let mut keystore = ks.migrate(&key, &IdentityNetwork::Testnet).unwrap();

assert_eq!(keystore.derivable(), true);
assert_eq!(keystore.id(), "6f8c2912-ebe8-4359-90f4-f1d1f1af1e4d");
let coin_info = CoinInfo {
coin: "ETHEREUM".to_string(),
derivation_path: "m/44'/60'/0'/0/0".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!("0x6031564e7b2F5cc33737807b2E58DaFF870B590b", acc.address);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
"mode": "normal",
"chain": "ETHEREUM",
"segWit": "NONE",
"passwordHint": "",
"passwordHint": "ios_password_hint",
"backup": [],
"source": "RECOVERED_IDENTITY",
"version": "iOS-2.14.0.1742",
"name": "ETH",
"network": "MAINNET",
"timestamp": "1695800371.215641"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"id": "6f8c2912-ebe8-4359-90f4-f1d1f1af1e4d",
"crypto": {
"cipher": "aes-128-ctr",
"cipherparams": {
"iv": "3a442e8b02843edf71b8d3a9c9da2c3b"
},
"ciphertext": "fcbcceae1d239f9575c55f4c4f81eeba44a6ad9d948f544af2ffee9efef2c038",
"kdf": "pbkdf2",
"kdfparams": {
"c": 65535,
"dklen": 32,
"prf": "hmac-sha256",
"salt": "fa141145a343d9b6c7e2f12e0e56d564bc4d1b46cd48e8f7d898779e06357f1f"
},
"mac": "50ee3b40129c5f18f9ff6982db0eb18504ea2e8f3d96e4ac062b4eb5849cf011"
},
"version": 3,
"address": "6031564e7b2f5cc33737807b2e58daff870b590b",
"encMnemonic": {
"encStr": "267bda938e4edbf7c420e89c59c6862f9127c7275d012b1b607f9e91ddb94574e81e94f6d8155e3c85ede03f584e09916122f03c72b67a1f96ddbf291beb46894d9a02d30170a9444692",
"nonce": "3cfe9f0b32b5d592e5fab54bd28863cd"
},
"mnemonicPath": "m/44'/60'/0'/0/0",
"imTokenMeta": {
"mode": "normal",
"chain": "ETHEREUM",
"segWit": "NONE",
"backup": [],
"source": "RECOVERED_IDENTITY",
"version": "iOS-2.14.0.1742",
"name": "ETH",
"network": "MAINNET",
"timestamp": "1695800371.215641"
}
}
6 changes: 3 additions & 3 deletions token-core/tcx/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ fn import_private_key_internal(

let meta = Metadata {
name: param.name.to_string(),
password_hint: param.password_hint.to_string(),
password_hint: Some(param.password_hint.to_string()),
source: meta_source,
identified_chain_types: Some(decoded_ret.chain_types.clone()),
..Metadata::default()
Expand Down Expand Up @@ -480,7 +480,7 @@ pub fn create_keystore(data: &[u8]) -> Result<Vec<u8>> {

let meta = Metadata {
name: param.name,
password_hint: param.password_hint,
password_hint: Some(param.password_hint),
source: Source::NewMnemonic,
network: IdentityNetwork::from_str(&param.network)?,
..Metadata::default()
Expand Down Expand Up @@ -531,7 +531,7 @@ pub fn import_mnemonic(data: &[u8]) -> Result<Vec<u8>> {

let meta = Metadata {
name: param.name,
password_hint: param.password_hint,
password_hint: Some(param.password_hint),
source: Source::Mnemonic,
..Metadata::default()
};
Expand Down
2 changes: 1 addition & 1 deletion token-core/tcx/tests/import_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn test_create_keystore() {
run_test(|| {
let param = CreateKeystoreParam {
password: TEST_PASSWORD.to_string(),
password_hint: "".to_string(),
password_hint: "hint".to_string(),
name: "aaa".to_string(),
network: IdentityNetwork::Mainnet.to_string(),
};
Expand Down

0 comments on commit 7799ba6

Please sign in to comment.