Skip to content

Commit

Permalink
chore: add checksum to the eth address returned by the scan_legacy_ke…
Browse files Browse the repository at this point in the history
…ystores interface
  • Loading branch information
xiaoguang1010 committed Apr 8, 2024
1 parent 4fc0dc9 commit a0d163f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 29 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.

24 changes: 7 additions & 17 deletions imkey-core/ikc/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,25 +209,15 @@ pub(crate) fn get_extended_public_keys(data: &[u8]) -> Result<Vec<u8>> {
let extended_public_key = match public_key_derivation.chain_type.as_str() {
"BITCOIN" | "LITCOIN" | "BITCOINCASH" => {
BtcAddress::get_xpub(Network::Bitcoin, public_key_derivation.path.as_str())?
},
"ETHEREUM" => {
EthAddress::get_xpub(public_key_derivation.path.as_str())?
},
"COSMOS" => {
CosmosAddress::get_xpub(public_key_derivation.path.as_str())?
},
}
"ETHEREUM" => EthAddress::get_xpub(public_key_derivation.path.as_str())?,
"COSMOS" => CosmosAddress::get_xpub(public_key_derivation.path.as_str())?,
"FILECOIN" => {
FilecoinAddress::get_xpub("MAINNET", public_key_derivation.path.as_str())?
},
"TRON" => {
TronAddress::get_xpub(public_key_derivation.path.as_str())?
},
"EOS" => {
EosPubkey::get_xpub(public_key_derivation.path.as_str())?
},
"NERVOS" => {
CkbAddress::get_xpub("MAINNET", public_key_derivation.path.as_str())?
},
}
"TRON" => TronAddress::get_xpub(public_key_derivation.path.as_str())?,
"EOS" => EosPubkey::get_xpub(public_key_derivation.path.as_str())?,
"NERVOS" => CkbAddress::get_xpub("MAINNET", public_key_derivation.path.as_str())?,
_ => return Err(anyhow!("unsupported_chain_type")),
};
extended_public_keys.push(extended_public_key);
Expand Down
7 changes: 0 additions & 7 deletions imkey-core/ikc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1752,11 +1752,4 @@ mod tests {
assert_eq!("success", bind_result.bind_status);
}
}

#[test]
fn test1(){
let req_hex = "1218696d6b65795f636f6d6d616e645f646174615f6572726f72";
let res = ErrorResponse::decode(hex::decode(req_hex).unwrap().as_slice()).unwrap();
println!("{:?}",res);
}
}
2 changes: 1 addition & 1 deletion token-core/tcx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ hex = "=0.4.3"
base64 = "=0.13.1"
base58 = "=0.2.0"
parking_lot = "=0.12.1"

ethereum-types = "=0.14.0"
strum = { version = "=0.25.0", features = ["derive"] }

[lib]
Expand Down
27 changes: 23 additions & 4 deletions token-core/tcx/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::filemanager::{cache_keystore, KEYSTORE_MAP, WALLET_FILE_DIR};
use crate::filemanager::{flush_keystore, LEGACY_WALLET_FILE_DIR};
use crate::handler::{encode_message, encrypt_xpub};
use anyhow::anyhow;
use ethereum_types::H160;
use prost::Message;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
Expand All @@ -18,6 +19,7 @@ use std::path::Path;
use std::str::FromStr;
use tcx_common::{FromHex, ToHex};
use tcx_constants::coin_info::get_xpub_prefix;
use tcx_eth::address::to_checksum;
use tcx_keystore::keystore::IdentityNetwork;
use tcx_keystore::Metadata;
use tcx_keystore::{Keystore, Source};
Expand Down Expand Up @@ -418,9 +420,15 @@ fn parse_legacy_kesytore(contents: String) -> Result<LegacyKeystoreResult> {
} else {
"".to_string()
};

let mut address = legacy_keystore.address.unwrap_or("".to_string());
if chain_type.eq("ETHEREUM") && !address.eq("") {
address = to_checksum(&H160::from_slice(&hex::decode(&address)?), None);
}

let account = AccountResponse {
chain_type,
address: legacy_keystore.address.unwrap_or("".to_string()),
address,
path,
curve: "secp256k1".to_string(),
public_key,
Expand Down Expand Up @@ -472,9 +480,14 @@ fn parse_tcx_keystore(v: &Value) -> Result<LegacyKeystoreResult> {
("".to_string(), "".to_string())
};

let mut address = legacy_account.address.to_string();
if legacy_account.coin.eq("ETHEREUM") {
address = to_checksum(&H160::from_slice(&hex::decode(&address)?), None);
}

account_responses.push(AccountResponse {
chain_type: legacy_account.coin.to_string(),
address: legacy_account.address.to_string(),
address,
path: legacy_account.derivation_path.to_string(),
curve: mapping_curve_name(legacy_account.curve.as_str()).to_string(),
public_key,
Expand Down Expand Up @@ -723,7 +736,10 @@ mod tests {
.unwrap();
assert_eq!(keystore.id, "60573d8d-8e83-45c3-85a5-34fbb2aad5e1");
let account = keystore.accounts.first().unwrap();
assert_eq!(account.address, "02c98f4ed8c8aab1aaba46539e45070a02e416c0");
assert_eq!(
account.address,
"0x02c98F4ED8c8aAb1aABA46539e45070a02e416C0"
);
assert_eq!(account.chain_type, "ETHEREUM");
assert_eq!(account.curve, "secp256k1");
assert_eq!(account.path, "");
Expand All @@ -737,7 +753,10 @@ mod tests {
.unwrap();
assert_eq!(keystore.id, "792a0051-16d7-44a7-921a-9b4a0c893b8f");
let account = keystore.accounts.first().unwrap();
assert_eq!(account.address, "7152bcad819b084d57179e293d2765ffa0109e04");
assert_eq!(
account.address,
"0x7152bcad819b084d57179e293D2765fFa0109E04"
);
assert_eq!(account.chain_type, "ETHEREUM");
assert_eq!(account.curve, "secp256k1");
assert_eq!(account.path, "m/44'/60'/0'/0/1");
Expand Down

0 comments on commit a0d163f

Please sign in to comment.