Skip to content

Commit

Permalink
fix: fix nervos and eth derive account error
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoguang1010 committed Jan 23, 2024
1 parent a099805 commit b05721a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
19 changes: 11 additions & 8 deletions imkey-core/ikc-wallet/coin-ckb/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl CkbAddress {
Ok(address)
}

pub fn get_enc_xpub(network: Network, path: &str) -> Result<String> {
pub fn get_enc_xpub(network: &str, path: &str) -> Result<String> {
let xpub = Self::get_xpub(network, path)?;
let key = ikc_common::XPUB_COMMON_KEY_128.read();
let iv = ikc_common::XPUB_COMMON_IV.read();
Expand All @@ -99,7 +99,7 @@ impl CkbAddress {
Ok(base64::encode(&encrypted))
}

pub fn get_xpub(network: Network, path: &str) -> Result<String> {
pub fn get_xpub(network: &str, path: &str) -> Result<String> {
//path check
check_path_validity(path)?;

Expand All @@ -121,9 +121,14 @@ impl CkbAddress {

//get parent public key fingerprint
let parent_chain_code = ChainCode::from(hex::decode(parent_chain_code)?.as_slice());
let network = match network.to_uppercase().as_str() {
"MAINNET" => Network::Bitcoin,
"TESTNET" => Network::Testnet,
_ => Network::Testnet,
};
let parent_ext_pub_key = ExtendedPubKey {
network: network,
depth: 0 as u8,
network,
depth: 0u8,
parent_fingerprint: Fingerprint::default(),
child_number: ChildNumber::from_normal_idx(0).unwrap(),
public_key: parent_pub_key_obj,
Expand Down Expand Up @@ -247,16 +252,14 @@ mod tests {
*XPUB_COMMON_KEY_128.write() = "4A2B655485ABBAB54BD30298BB0A5B55".to_string();
*XPUB_COMMON_IV.write() = "73518399CB98DCD114D873E06EBF4BCC".to_string();

let version: Network = Network::Bitcoin;
let path: &str = "m/44'/309'/0'";
let get_xpub_result = CkbAddress::get_xpub(version, path);
let get_xpub_result = CkbAddress::get_xpub("MAINNET", path);
assert!(get_xpub_result.is_ok());
let xpub = get_xpub_result.ok().unwrap();
assert_eq!("xpub6CyvXfYwHJjJ9syYjG7qZMva1yMx93SUmqUcKHvoReUadCzqJA8mMXrrXQjRvzveuahgdQmCsdsuiCkMRsLec63DW83Wwu5UqKJQmsonKpo", xpub);

let version: Network = Network::Bitcoin;
let path: &str = "m/44'/309'/0'";
let get_enc_xpub_result = CkbAddress::get_enc_xpub(version, path);
let get_enc_xpub_result = CkbAddress::get_enc_xpub("MAINNET", path);
let enc_xpub = get_enc_xpub_result.ok().unwrap();
assert_eq!("xkpmspgDJhDx7nBk/se+P0CRS/fbPKYivQiqocPEdXFIsNLCHy5lZEc59LGLbsFFamWX7j8TUs5ugKynEign+y0hDQIhm3y1PypmU+frhoWckc7vgkdzbd9xGdMTVv7J+JW4Zlenksb8a9UNkRfrJg==", enc_xpub);
}
Expand Down
5 changes: 2 additions & 3 deletions imkey-core/ikc/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub(crate) fn derive_accounts(data: &[u8]) -> Result<Vec<u8>> {
let public_key = uncompress_pubkey_2_compress(&public_key[..130]);
account_rsp.public_key = format!("0x{}", public_key);
account_rsp.address = EthAddress::get_address(&derivation.path)?;
CosmosAddress::get_xpub(&account_path)?
EthAddress::get_xpub(&account_path)?
}
"COSMOS" => {
let public_key = CosmosAddress::get_pub_key(&derivation.path)?;
Expand Down Expand Up @@ -114,8 +114,7 @@ pub(crate) fn derive_accounts(data: &[u8]) -> Result<Vec<u8>> {
account_rsp.public_key = format!("0x{}", public_key);
account_rsp.address =
CkbAddress::get_address(&derivation.network, &derivation.path)?;
let network = Network::from_str(&derivation.network.to_lowercase())?;
CkbAddress::get_xpub(network, &account_path)?
CkbAddress::get_xpub(&derivation.network, &account_path)?
}
"EOS" => {
let public_key = EosPubkey::get_sub_pubkey(&derivation.path)?;
Expand Down
20 changes: 19 additions & 1 deletion imkey-core/ikc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,15 @@ mod tests {
curve: "secp256k1".to_string(),
bech32_prefix: "".to_string(),
},
Derivation {
chain_type: "NERVOS".to_string(),
path: "m/44'/309'/0'/0/0".to_string(),
network: "MAINNET".to_string(),
seg_wit: "".to_string(),
chain_id: "".to_string(),
curve: "secp256k1".to_string(),
bech32_prefix: "".to_string(),
},
];
let param = DeriveAccountsParam { derivations };
let action: ImkeyAction = ImkeyAction {
Expand Down Expand Up @@ -712,6 +721,15 @@ mod tests {
derived_accounts.accounts[13].public_key
);
assert_eq!("xpub6Bmkv3mmRZZWoFSBdj9vDMqR2PCPSP6DEj8u3bBuv44g3Ncnro6cPVqZAw6wTEcxHQuodkuJG4EmAinqrrRXGsN3HHnRRMtAvzfYTiBATV1", derived_accounts.accounts[13].extended_public_key);
assert_eq!("MwDMFXVWDEuWvBogeW1v/MOMFDnGnnflm2JAPvJaJZO4HXp8fCsWETA7u8MzOW3KaPksglpUHLN3xkDr2QWMEQq0TewFZoZ3KsjmLW0KGMRN7XQKqo/omkSEsPfalVnp9Zxm2lpxVmIacqvlernVSg==", derived_accounts.accounts[0].encrypted_extended_public_key);

assert_eq!(
"ckb1qyqtr684u76tu7r8efkd24hw8922xfvhnazst8nagx",
derived_accounts.accounts[14].address
);
assert_eq!(
"0x03554851980004ff256888612bf0d64d9b1002bf82331450fd5a7405d1b23cc5bd",
derived_accounts.accounts[14].public_key
);
assert_eq!("xpub6CyvXfYwHJjJ9syYjG7qZMva1yMx93SUmqUcKHvoReUadCzqJA8mMXrrXQjRvzveuahgdQmCsdsuiCkMRsLec63DW83Wwu5UqKJQmsonKpo", derived_accounts.accounts[14].extended_public_key);
}
}
8 changes: 1 addition & 7 deletions imkey-core/ikc/src/nervos_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,9 @@ use coin_ckb::address::CkbAddress;
use ikc_common::path::get_account_path;

pub fn get_address(param: &AddressParam) -> Result<Vec<u8>> {
let network = match param.network.as_ref() {
"MAINNET" => Network::Bitcoin,
"TESTNET" => Network::Testnet,
_ => Network::Testnet,
};

let address = CkbAddress::get_address(param.network.as_ref(), param.path.as_ref())?;
let account_path = get_account_path(&param.path)?;
let enc_xpub = CkbAddress::get_enc_xpub(network, &account_path)?;
let enc_xpub = CkbAddress::get_enc_xpub(&param.network, &account_path)?;

let address_message = BtcForkWallet {
path: param.path.to_owned(),
Expand Down

0 comments on commit b05721a

Please sign in to comment.