Skip to content

Commit

Permalink
feat: add scan_keystores
Browse files Browse the repository at this point in the history
  • Loading branch information
XuNeal committed Jan 12, 2024
1 parent 3f45039 commit af06009
Show file tree
Hide file tree
Showing 13 changed files with 428 additions and 76 deletions.
21 changes: 14 additions & 7 deletions token-core/tcx-keystore/src/keystore/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ pub(crate) mod tests {
{
"id": "7719d1e3-3f67-439f-a18e-d9ae413e00e1",
"version": 12000,
"fingerprint": "efbe00a55ddd4c5350e295a9533d28f93cac001bfdad8cf4275140461ea03e9e",
"sourceFingerprint": "0xefbe00a55ddd4c5350e295a9533d28f93cac001bfdad8cf4275140461ea03e9e",
"crypto": {
"cipher": "aes-128-ctr",
"cipherparams": {
Expand All @@ -591,6 +591,11 @@ pub(crate) mod tests {
},
"mac": "56af7c5faf0a791cbb4911c4c20070156e4ad0a03f8253b2a2fb005a68d7a026"
},
"encOriginal":
{
"encStr":"ba382601567c543984778a7914d7bfb2462098a8680f36edd7ceaa1a5039e1ca",
"nonce":"d117ae86c627850341f1a5d6bd9cd855"
},
"identity":{
"encAuthKey":
{
Expand All @@ -607,20 +612,19 @@ pub(crate) mod tests {
"timestamp": 1575605134,
"source": "MNEMONIC",
"network":"MAINNET"
},
"curve": null
}
}
"#;

static PK_KEYSTORE_JSON: &'static str = r#"
{"id":"89e6fc5d-ac9a-46ab-b53f-342a80f3d28b","version":12001,
"fingerprint":"4fc213ddcb6fa44a2e2f4c83d67502f88464e6ee","crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"c0ecc72839f8a02cc37eb7b0dd0b93ba"},"ciphertext":"1239e5807e19f95d86567f81c162c69a5f4564ea17f487669a277334f4dcc7dc","kdf":"pbkdf2","kdfparams":{"c":1024,"prf":"hmac-sha256","dklen":32,"salt":"3c9df9eb95a014c77bbc8b9a06f4f14e0d08170dea71189c7cf377a3b2099404"},"mac":"909a6bfe1ad031901e80927b847a8fa8407fdcde56cfa374f7a732fb3b3a882d"},"identity":{"encAuthKey":{"encStr":"ba382601567c543984778a7914d7bfb2462098a8680f36edd7ceaa1a5039e1ca","nonce":"d117ae86c627850341f1a5d6bd9cd855"},"encKey":"ef806a542bcc30da7ce60fc37bd6cc91619b482f6f070af3a9d7b042087886f3","identifier":"im14x5GXsdME4JsrHYe2wvznqRz4cUhx2pA4HPf","ipfsId":"QmWqwovhrZBMmo32BzY83ZMEBQaP7YRMqXNmMc8mgrpzs6"},"imTokenMeta":{"name":"Unknown","passwordHint":"","timestamp":1576733295,"source":"PRIVATE","network":"MAINNET"},
"curve": null
"sourceFingerprint":"4fc213ddcb6fa44a2e2f4c83d67502f88464e6ee","crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"c0ecc72839f8a02cc37eb7b0dd0b93ba"},"ciphertext":"1239e5807e19f95d86567f81c162c69a5f4564ea17f487669a277334f4dcc7dc","kdf":"pbkdf2","kdfparams":{"c":1024,"prf":"hmac-sha256","dklen":32,"salt":"3c9df9eb95a014c77bbc8b9a06f4f14e0d08170dea71189c7cf377a3b2099404"},"mac":"909a6bfe1ad031901e80927b847a8fa8407fdcde56cfa374f7a732fb3b3a882d"},"identity":{"encAuthKey":{"encStr":"ba382601567c543984778a7914d7bfb2462098a8680f36edd7ceaa1a5039e1ca","nonce":"d117ae86c627850341f1a5d6bd9cd855"},"encKey":"ef806a542bcc30da7ce60fc37bd6cc91619b482f6f070af3a9d7b042087886f3","identifier":"im14x5GXsdME4JsrHYe2wvznqRz4cUhx2pA4HPf","ipfsId":"QmWqwovhrZBMmo32BzY83ZMEBQaP7YRMqXNmMc8mgrpzs6"},"encOriginal":{"encStr":"ba382601567c543984778a7914d7bfb2462098a8680f36edd7ceaa1a5039e1ca","nonce":"d117ae86c627850341f1a5d6bd9cd855"}, "imTokenMeta":{"name":"Unknown","passwordHint":"","timestamp":1576733295,"source":"PRIVATE","network":"MAINNET"},
"curve": "secp256k1"
}
"#;

static INVALID_PK_KEYSTORE_JSON: &'static str = r#"
{"id":"89e6fc5d-ac9a-46ab-b53f-342a80f3d28b","version":10001,"fingerprint":"4fc213ddcb6fa44a2e2f4c83d67502f88464e6ee","crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"c0ecc72839f8a02cc37eb7b0dd0b93ba"},"ciphertext":"1239e5807e19f95d86567f81c162c69a5f4564ea17f487669a277334f4dcc7dc","kdf":"pbkdf2","kdfparams":{"c":1024,"prf":"hmac-sha256","dklen":32,"salt":"3c9df9eb95a014c77bbc8b9a06f4f14e0d08170dea71189c7cf377a3b2099404"},"mac":"909a6bfe1ad031901e80927b847a8fa8407fdcde56cfa374f7a732fb3b3a882d"},"identity":{"encAuthKey":{"encStr":"ba382601567c543984778a7914d7bfb2462098a8680f36edd7ceaa1a5039e1ca","nonce":"d117ae86c627850341f1a5d6bd9cd855"},"encKey":"ef806a542bcc30da7ce60fc37bd6cc91619b482f6f070af3a9d7b042087886f3","identifier":"im14x5GXsdME4JsrHYe2wvznqRz4cUhx2pA4HPf","ipfsId":"QmWqwovhrZBMmo32BzY83ZMEBQaP7YRMqXNmMc8mgrpzs6"},"imTokenMeta":{"name":"Unknown","passwordHint":"","timestamp":1576733295,"source":"PRIVATE","network":"MAINNET"}}
{"id":"89e6fc5d-ac9a-46ab-b53f-342a80f3d28b","version":10001,"sourceFingerprint":"4fc213ddcb6fa44a2e2f4c83d67502f88464e6ee","crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"c0ecc72839f8a02cc37eb7b0dd0b93ba"},"ciphertext":"1239e5807e19f95d86567f81c162c69a5f4564ea17f487669a277334f4dcc7dc","kdf":"pbkdf2","kdfparams":{"c":1024,"prf":"hmac-sha256","dklen":32,"salt":"3c9df9eb95a014c77bbc8b9a06f4f14e0d08170dea71189c7cf377a3b2099404"},"mac":"909a6bfe1ad031901e80927b847a8fa8407fdcde56cfa374f7a732fb3b3a882d"},"identity":{"encAuthKey":{"encStr":"ba382601567c543984778a7914d7bfb2462098a8680f36edd7ceaa1a5039e1ca","nonce":"d117ae86c627850341f1a5d6bd9cd855"},"encKey":"ef806a542bcc30da7ce60fc37bd6cc91619b482f6f070af3a9d7b042087886f3","identifier":"im14x5GXsdME4JsrHYe2wvznqRz4cUhx2pA4HPf","ipfsId":"QmWqwovhrZBMmo32BzY83ZMEBQaP7YRMqXNmMc8mgrpzs6"},"encOriginal":{"encStr":"ba382601567c543984778a7914d7bfb2462098a8680f36edd7ceaa1a5039e1ca","nonce":"d117ae86c627850341f1a5d6bd9cd855"}, "imTokenMeta":{"name":"Unknown","passwordHint":"","timestamp":1576733295,"source":"PRIVATE","network":"MAINNET"}}
"#;

static OLD_KEYSTORE_JSON: &'static str = r#"
Expand Down Expand Up @@ -844,7 +848,10 @@ pub(crate) mod tests {
HdKeystore::from_mnemonic(TEST_MNEMONIC, TEST_PASSWORD, Metadata::default()).unwrap();
let keystore = Hd(hd_store);
assert!(keystore.derivable());
assert_eq!(keystore.fingerprint(), "0x1468dba9");
assert_eq!(
keystore.fingerprint(),
"0x1468dba9c246fe22183c056540ab4d8b04553217"
);

let meta = Metadata {
name: "test_create".to_string(),
Expand Down
6 changes: 3 additions & 3 deletions token-core/tcx-keystore/src/keystore/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,21 +216,21 @@ mod tests {
)
.unwrap();
let fingerprint = fingerprint_from_private_key(&pk_data).unwrap();
assert_eq!(fingerprint, "0x1468dba9");
assert_eq!(fingerprint, "0x1468dba9c246fe22183c056540ab4d8b04553217");

let pk_data = &Vec::<u8>::from_hex(
"257cd2f8eb13f6930ecb95ac7736dd25e65d231ce1a3b1669e51f6737350b43e",
)
.unwrap();
let fingerprint = fingerprint_from_private_key(&pk_data).unwrap();
assert_eq!(fingerprint, "0xf6f23259");
assert_eq!(fingerprint, "0xf6f232595e79dd9723aa4e840d548e792d44aea6");

let pk_data = &Vec::<u8>::from_hex(
"ad87a08796efbdd9276e2ca5a10f938937cb5d2b7d5f698c06a94d8eeed3f600257cd2f8eb13f6930ecb95ac7736dd25e65d231ce1a3b1669e51f6737350b43e",
)
.unwrap();
let fingerprint = fingerprint_from_private_key(&pk_data).unwrap();
assert_eq!(fingerprint, "0x404ba38b");
assert_eq!(fingerprint, "0x404ba38b37b9c682526621118094a43220a95bd6");
}

#[test]
Expand Down
33 changes: 25 additions & 8 deletions token-core/tcx-migration/src/keystore_upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,10 @@ mod tests {
let upgraded = upgrade_keystore.upgrade(&key, &IdentityNetwork::Mainnet);

assert!(!upgrade_keystore.need_upgrade());
assert_eq!(upgraded.err().unwrap().to_string(), "invalid version");
assert_eq!(
upgraded.err().unwrap().to_string(),
"upgrade wrong version keystore"
);
}
}

Expand Down Expand Up @@ -464,7 +467,7 @@ mod tests {
"keyHash":"4fc213ddcb6fa44a2e2f4c83d67502f88464e6ee",
"crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"c0ecc72839f8a02cc37eb7b0dd0b93ba"},"ciphertext":"1239e5807e19f95d86567f81c162c69a5f4564ea17f487669a277334f4dcc7dc","kdf":"pbkdf2",
"kdfparams":{"c":1024,"prf":"hmac-sha256","dklen":32,"salt":"3c9df9eb95a014c77bbc8b9a06f4f14e0d08170dea71189c7cf377a3b2099404"},"mac":"909a6bfe1ad031901e80927b847a8fa8407fdcde56cfa374f7a732fb3b3a882d"},
"activeAccounts": [{"address": "t3qdyntx5snnwgmjkp2ztd6tf6hhcmurxfj53zylrqyympwvzvbznx6vnvdqloate5eviphnzrkupno4wheesa", "curve": "BLS"}],
"activeAccounts": [{"address": "t3qdyntx5snnwgmjkp2ztd6tf6hhcmurxfj53zylrqyympwvzvbznx6vnvdqloate5eviphnzrkupno4wheesa", "curve": "BLS", "coin": "FILECOIN"}],
"imTokenMeta":{"name":name.to_string(),"passwordHint":"","timestamp":1576733295,"source":source.to_string()}}
)
}
Expand All @@ -477,7 +480,7 @@ mod tests {
"keyHash":"4fc213ddcb6fa44a2e2f4c83d67502f88464e6ee",
"crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"c0ecc72839f8a02cc37eb7b0dd0b93ba"},"ciphertext":"1239e5807e19f95d86567f81c162c69a5f4564ea17f487669a277334f4dcc7dc","kdf":"pbkdf2",
"kdfparams":{"c":1024,"prf":"hmac-sha256","dklen":32,"salt":"3c9df9eb95a014c77bbc8b9a06f4f14e0d08170dea71189c7cf377a3b2099404"},"mac":"909a6bfe1ad031901e80927b847a8fa8407fdcde56cfa374f7a732fb3b3a882d"},
"activeAccounts": [{"address": "", "curve": "SECP256k1"}],
"activeAccounts": [{"address": "", "curve": "SECP256k1", "coin": "BITCOINCASH"}],
"imTokenMeta":{"name":name.to_string(),"passwordHint":"","timestamp":1576733295,"source":source.to_string()}}
)
}
Expand All @@ -490,7 +493,7 @@ mod tests {
"keyHash":"4fc213ddcb6fa44a2e2f4c83d67502f88464e6ee",
"crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"c0ecc72839f8a02cc37eb7b0dd0b93ba"},"ciphertext":"1239e5807e19f95d86567f81c162c69a5f4564ea17f487669a277334f4dcc7dc","kdf":"pbkdf2",
"kdfparams":{"c":1024,"prf":"hmac-sha256","dklen":32,"salt":"3c9df9eb95a014c77bbc8b9a06f4f14e0d08170dea71189c7cf377a3b2099404"},"mac":"909a6bfe1ad031901e80927b847a8fa8407fdcde56cfa374f7a732fb3b3a882d"},
"activeAccounts": [{"address": "", "curve": "ED25519"}],
"activeAccounts": [{"address": "", "curve": "ED25519", "coin": "TEZOS"}],
"imTokenMeta":{"name":name.to_string(),"passwordHint":"","timestamp":1576733295,"source":source.to_string()}}
)
}
Expand All @@ -501,9 +504,20 @@ mod tests {
"id":"89e6fc5d-ac9a-46ab-b53f-342a80f3d28b",
"version":version,
"keyHash":"4fc213ddcb6fa44a2e2f4c83d67502f88464e6ee",
"crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"c0ecc72839f8a02cc37eb7b0dd0b93ba"},"ciphertext":"1239e5807e19f95d86567f81c162c69a5f4564ea17f487669a277334f4dcc7dc","kdf":"pbkdf2",
"kdfparams":{"c":1024,"prf":"hmac-sha256","dklen":32,"salt":"3c9df9eb95a014c77bbc8b9a06f4f14e0d08170dea71189c7cf377a3b2099404"},"mac":"909a6bfe1ad031901e80927b847a8fa8407fdcde56cfa374f7a732fb3b3a882d"},
"activeAccounts": [{"address": "", "curve": "SubSr25519"}],
"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(),"passwordHint":"","timestamp":1576733295,"source":source.to_string()}}
)
}
Expand Down Expand Up @@ -532,7 +546,10 @@ mod tests {
super::KeystoreUpgrade::new(pk_json_with_subsr25519(11001, "PRIVATE", "vvvvvv"));

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

Expand Down
12 changes: 6 additions & 6 deletions token-core/tcx-migration/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,11 +516,11 @@ mod tests {
#[test]
fn test_original_after_migrated_wif() {
let json_str =
include_str!("../../test-data/wallets-ios-2_14_1/f3615a56-cb03-4aa4-a893-89944e49920d");
include_str!("../../test-data/wallets-ios-2_14_1/9f4acb4a-7431-4c7d-bd25-a19656a86ea0");
let old_ks = LegacyKeystore::from_json_str(json_str).unwrap();
let ks = old_ks
.migrate(
&Key::DerivedKey("0xa5b0cb9cb0536d6ec6ab21da77415bd59aff62c44c1da40d377c4faf2a44608693a72efb4079f57a5dca710ecff75dc5b54beb4ad6d9f9d47b63583810b50c61".to_string()),
&Key::Password(TEST_PASSWORD.to_string()),
&IdentityNetwork::Mainnet,
)
.unwrap();
Expand Down Expand Up @@ -586,21 +586,21 @@ mod tests {
#[test]
fn test_original_after_migrated_cosmos() {
let json_str =
include_str!("../../test-data/wallets-ios-2_14_1/f3615a56-cb03-4aa4-a893-89944e49920d");
include_str!("../../test-data/wallets-ios-2_14_1/ac59ccc1-285b-47a7-92f5-a6c432cee21a");
let old_ks = LegacyKeystore::from_json_str(json_str).unwrap();
let ks = old_ks
.migrate(
&Key::DerivedKey("0x79c74b67fc73a255bc66afc1e7c25867a19e6d2afa5b8e3107a472de13201f1924fed05e811e7f5a4c3e72a8a6e047a80393c215412bde239ec7ded520896630".to_string()),
&Key::Password(TEST_PASSWORD.to_string()),
&IdentityNetwork::Mainnet,
)
.unwrap();
let ori = ks.backup(TEST_PASSWORD).unwrap();

assert_eq!(
ori,
"4b8e7a47497d810cd11f209b8ce9d3b0eec34e85dc8bad5d12cb602425dd3d6b"
"inject kidney empty canal shadow pact comfort wife crush horse wife sketch"
);
}

// TODO: add cosmos testcase
// TODO: add eos testcase
}
5 changes: 5 additions & 0 deletions token-core/tcx-proto/src/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ message MigrateKeystoreParam {
KeystoreResult keystore = 3;
}

message ScanKeystoresResult {
repeated KeystoreResult hdKeystores = 1;
repeated ImportPrivateKeyResult privateKeyKeystores = 2;
}

message LegacyKeystoreResult {
string id = 1;
string name = 2;
Expand Down
8 changes: 8 additions & 0 deletions token-core/tcx/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,14 @@ pub struct MigrateKeystoreResult {
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct ScanKeystoresResult {
#[prost(message, repeated, tag = "1")]
pub hd_keystores: ::prost::alloc::vec::Vec<KeystoreResult>,
#[prost(message, repeated, tag = "2")]
pub private_key_keystores: ::prost::alloc::vec::Vec<ImportPrivateKeyResult>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct LegacyKeystoreResult {
#[prost(string, tag = "1")]
pub id: ::prost::alloc::string::String,
Expand Down
Loading

0 comments on commit af06009

Please sign in to comment.