diff --git a/imkey-core/ikc-wallet/coin-ckb/src/address.rs b/imkey-core/ikc-wallet/coin-ckb/src/address.rs index a968b5d2..879225fa 100644 --- a/imkey-core/ikc-wallet/coin-ckb/src/address.rs +++ b/imkey-core/ikc-wallet/coin-ckb/src/address.rs @@ -88,7 +88,7 @@ impl CkbAddress { Ok(address) } - pub fn get_enc_xpub(network: Network, path: &str) -> Result { + pub fn get_enc_xpub(network: &str, path: &str) -> Result { let xpub = Self::get_xpub(network, path)?; let key = ikc_common::XPUB_COMMON_KEY_128.read(); let iv = ikc_common::XPUB_COMMON_IV.read(); @@ -99,7 +99,7 @@ impl CkbAddress { Ok(base64::encode(&encrypted)) } - pub fn get_xpub(network: Network, path: &str) -> Result { + pub fn get_xpub(network: &str, path: &str) -> Result { //path check check_path_validity(path)?; @@ -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, @@ -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); } diff --git a/imkey-core/ikc/src/handler.rs b/imkey-core/ikc/src/handler.rs index e6e868be..760cac34 100644 --- a/imkey-core/ikc/src/handler.rs +++ b/imkey-core/ikc/src/handler.rs @@ -70,7 +70,7 @@ pub(crate) fn derive_accounts(data: &[u8]) -> Result> { 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)?; @@ -114,8 +114,7 @@ pub(crate) fn derive_accounts(data: &[u8]) -> Result> { 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)?; diff --git a/imkey-core/ikc/src/lib.rs b/imkey-core/ikc/src/lib.rs index f2b86549..a5dc6038 100644 --- a/imkey-core/ikc/src/lib.rs +++ b/imkey-core/ikc/src/lib.rs @@ -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 { @@ -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); } } diff --git a/imkey-core/ikc/src/nervos_address.rs b/imkey-core/ikc/src/nervos_address.rs index 0d4c70b4..7431f412 100644 --- a/imkey-core/ikc/src/nervos_address.rs +++ b/imkey-core/ikc/src/nervos_address.rs @@ -6,15 +6,9 @@ use coin_ckb::address::CkbAddress; use ikc_common::path::get_account_path; pub fn get_address(param: &AddressParam) -> Result> { - 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(¶m.path)?; - let enc_xpub = CkbAddress::get_enc_xpub(network, &account_path)?; + let enc_xpub = CkbAddress::get_enc_xpub(¶m.network, &account_path)?; let address_message = BtcForkWallet { path: param.path.to_owned(),