From ea73f5d3df000670610e435b4278ed6e44861fe3 Mon Sep 17 00:00:00 2001 From: Pedro Camboim Date: Fri, 22 Dec 2023 11:08:05 -0300 Subject: [PATCH] chore: get trx legacy path --- packages/kos-sdk/src/chain.rs | 4 ++-- packages/kos-sdk/src/chains/bitcoin/mod.rs | 4 ++-- packages/kos-sdk/src/chains/default/mod.rs | 2 +- packages/kos-sdk/src/chains/ethereum/mod.rs | 4 ++-- packages/kos-sdk/src/chains/klever/mod.rs | 2 +- packages/kos-sdk/src/chains/polygon/mod.rs | 6 +++--- packages/kos-sdk/src/chains/tron/mod.rs | 14 +++++++++----- packages/kos-sdk/src/wallet.rs | 8 ++++---- 8 files changed, 24 insertions(+), 20 deletions(-) diff --git a/packages/kos-sdk/src/chain.rs b/packages/kos-sdk/src/chain.rs index 87156a1..6688c10 100644 --- a/packages/kos-sdk/src/chain.rs +++ b/packages/kos-sdk/src/chain.rs @@ -48,9 +48,9 @@ macro_rules! createChains { } } - pub fn get_path(&self, index: u32) -> Result { + pub fn get_path(&self, index: u32, is_legacy: Option) -> Result { match self { - $(Chain::$name => $name::get_path(index),)* + $(Chain::$name => $name::get_path(index, is_legacy),)* } } diff --git a/packages/kos-sdk/src/chains/bitcoin/mod.rs b/packages/kos-sdk/src/chains/bitcoin/mod.rs index 34e3012..71caf4b 100644 --- a/packages/kos-sdk/src/chains/bitcoin/mod.rs +++ b/packages/kos-sdk/src/chains/bitcoin/mod.rs @@ -105,7 +105,7 @@ impl BTC { } #[wasm_bindgen(js_name = "getPath")] - pub fn get_path(index: u32) -> Result { + pub fn get_path(index: u32, is_legacy: Option) -> Result { Ok(format!("m/84'/{}'/0'/0/{}", BIP44_PATH, index)) } @@ -402,7 +402,7 @@ mod tests { ]; for (index, expected_addr) in v { - let path = BTC::get_path(index).unwrap(); + let path = BTC::get_path(index, None).unwrap(); let kp = BTC::keypair_from_mnemonic(DEFAULT_MNEMONIC, &path, None).unwrap(); let addr = BTC::get_address_from_keypair(&kp).unwrap(); diff --git a/packages/kos-sdk/src/chains/default/mod.rs b/packages/kos-sdk/src/chains/default/mod.rs index 61d77c9..2f2d4cd 100644 --- a/packages/kos-sdk/src/chains/default/mod.rs +++ b/packages/kos-sdk/src/chains/default/mod.rs @@ -48,7 +48,7 @@ impl NONE { } #[wasm_bindgen(js_name = "getPath")] - pub fn get_path(_index: u32) -> Result { + pub fn get_path(_index: u32, _is_legacy: Option ) -> Result { Err(Error::UnsupportedChain("NONE")) } diff --git a/packages/kos-sdk/src/chains/ethereum/mod.rs b/packages/kos-sdk/src/chains/ethereum/mod.rs index d6e9af4..44df6da 100644 --- a/packages/kos-sdk/src/chains/ethereum/mod.rs +++ b/packages/kos-sdk/src/chains/ethereum/mod.rs @@ -87,7 +87,7 @@ impl ETH { } #[wasm_bindgen(js_name = "getPath")] - pub fn get_path(index: u32) -> Result { + pub fn get_path(index: u32, is_legacy: Option) -> Result { Ok(format!("m/44'/{}'/0'/0/{}", BIP44_PATH, index)) } @@ -465,7 +465,7 @@ mod tests { ]; for (index, expected_addr) in v { - let path = ETH::get_path(index).unwrap(); + let path = ETH::get_path(index, None).unwrap(); let kp = ETH::keypair_from_mnemonic(DEFAULT_MNEMONIC, &path, None).unwrap(); let addr = ETH::get_address_from_keypair(&kp).unwrap(); diff --git a/packages/kos-sdk/src/chains/klever/mod.rs b/packages/kos-sdk/src/chains/klever/mod.rs index 3c247f4..1b96b37 100644 --- a/packages/kos-sdk/src/chains/klever/mod.rs +++ b/packages/kos-sdk/src/chains/klever/mod.rs @@ -69,7 +69,7 @@ impl KLV { } #[wasm_bindgen(js_name = "getPath")] - pub fn get_path(index: u32) -> Result { + pub fn get_path(index: u32, is_legacy: Option) -> Result { Ok(format!("m/44'/{}'/0'/0'/{}'", BIP44_PATH, index)) } diff --git a/packages/kos-sdk/src/chains/polygon/mod.rs b/packages/kos-sdk/src/chains/polygon/mod.rs index 4c31c75..5396154 100644 --- a/packages/kos-sdk/src/chains/polygon/mod.rs +++ b/packages/kos-sdk/src/chains/polygon/mod.rs @@ -93,8 +93,8 @@ impl MATIC { } #[wasm_bindgen(js_name = "getPath")] - pub fn get_path(index: u32) -> Result { - ETH::get_path(index) + pub fn get_path(index: u32, is_legacy: Option) -> Result { + ETH::get_path(index, is_legacy) } #[wasm_bindgen(js_name = "signDigest")] @@ -237,7 +237,7 @@ mod tests { ]; for (index, expected_addr) in v { - let path = MATIC::get_path(index).unwrap(); + let path = MATIC::get_path(index, None).unwrap(); let kp = MATIC::keypair_from_mnemonic(DEFAULT_MNEMONIC, &path, None).unwrap(); let addr = MATIC::get_address_from_keypair(&kp).unwrap(); diff --git a/packages/kos-sdk/src/chains/tron/mod.rs b/packages/kos-sdk/src/chains/tron/mod.rs index 03a3236..704f2d4 100644 --- a/packages/kos-sdk/src/chains/tron/mod.rs +++ b/packages/kos-sdk/src/chains/tron/mod.rs @@ -79,9 +79,13 @@ impl TRX { } #[wasm_bindgen(js_name = "getPath")] - pub fn get_path(index: u32) -> Result { - // use account 0 index X - Ok(format!("m/44'/{}'/0'/0/{}", BIP44_PATH, index)) + pub fn get_path(index: u32, is_legacy: Option) -> Result { + if let Some(legacy) = is_legacy { + Ok(format!("m/44'/{}'/{}'", BIP44_PATH, index)) + } else { + // use account 0 index X + Ok(format!("m/44'/{}'/0'/0/{}", BIP44_PATH, index)) + } } #[wasm_bindgen(js_name = "signDigest")] @@ -324,7 +328,7 @@ mod tests { #[test] fn test_address_from_mnemonic() { - let path = TRX::get_path(0).unwrap(); + let path = TRX::get_path(0, None).unwrap(); let kp = TRX::keypair_from_mnemonic("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about", &path, None).unwrap(); let address = TRX::get_address_from_keypair(&kp).unwrap(); @@ -389,7 +393,7 @@ mod tests { ]; for (index, expected_addr) in v { - let path = TRX::get_path(index).unwrap(); + let path = TRX::get_path(index, None).unwrap(); let kp = TRX::keypair_from_mnemonic(default_mnemonic, &path, None).unwrap(); let addr = TRX::get_address_from_keypair(&kp).unwrap(); diff --git a/packages/kos-sdk/src/wallet.rs b/packages/kos-sdk/src/wallet.rs index d3a4bcc..28f850d 100644 --- a/packages/kos-sdk/src/wallet.rs +++ b/packages/kos-sdk/src/wallet.rs @@ -227,7 +227,7 @@ impl Wallet { index: u32, password: Option, ) -> Result { - let path = chain.get_path(index)?; + let path = chain.get_path(index, None)?; let mut wallet = Wallet::from_mnemonic(chain, mnemonic, path, password)?; wallet.index = Some(index); @@ -474,7 +474,7 @@ mod tests { let mut w1 = Wallet::from_mnemonic( Chain::KLV, "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about".to_string(), - Chain::KLV.get_path(0).unwrap(), + Chain::KLV.get_path(0, None).unwrap(), None, ).unwrap(); @@ -501,7 +501,7 @@ mod tests { let mut w1 = Wallet::from_mnemonic( Chain::KLV, "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about".to_string(), - Chain::KLV.get_path(0).unwrap(), + Chain::KLV.get_path(0, None).unwrap(), None, ).unwrap(); @@ -576,7 +576,7 @@ qeVTAAAA let w1 = Wallet::from_mnemonic( Chain::KLV, "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about".to_string(), - Chain::KLV.get_path(0).unwrap(), + Chain::KLV.get_path(0, None).unwrap(), None, ).unwrap();