Skip to content

Commit

Permalink
chore: get trx legacy path
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrxlz committed Dec 22, 2023
1 parent bc29fef commit ea73f5d
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 20 deletions.
4 changes: 2 additions & 2 deletions packages/kos-sdk/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ macro_rules! createChains {
}
}

pub fn get_path(&self, index: u32) -> Result<String, Error> {
pub fn get_path(&self, index: u32, is_legacy: Option<bool>) -> Result<String, Error> {
match self {
$(Chain::$name => $name::get_path(index),)*
$(Chain::$name => $name::get_path(index, is_legacy),)*
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/kos-sdk/src/chains/bitcoin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl BTC {
}

#[wasm_bindgen(js_name = "getPath")]
pub fn get_path(index: u32) -> Result<String, Error> {
pub fn get_path(index: u32, is_legacy: Option<bool>) -> Result<String, Error> {
Ok(format!("m/84'/{}'/0'/0/{}", BIP44_PATH, index))
}

Expand Down Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion packages/kos-sdk/src/chains/default/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl NONE {
}

#[wasm_bindgen(js_name = "getPath")]
pub fn get_path(_index: u32) -> Result<String, Error> {
pub fn get_path(_index: u32, _is_legacy: Option<bool> ) -> Result<String, Error> {
Err(Error::UnsupportedChain("NONE"))
}

Expand Down
4 changes: 2 additions & 2 deletions packages/kos-sdk/src/chains/ethereum/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl ETH {
}

#[wasm_bindgen(js_name = "getPath")]
pub fn get_path(index: u32) -> Result<String, Error> {
pub fn get_path(index: u32, is_legacy: Option<bool>) -> Result<String, Error> {
Ok(format!("m/44'/{}'/0'/0/{}", BIP44_PATH, index))
}

Expand Down Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion packages/kos-sdk/src/chains/klever/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl KLV {
}

#[wasm_bindgen(js_name = "getPath")]
pub fn get_path(index: u32) -> Result<String, Error> {
pub fn get_path(index: u32, is_legacy: Option<bool>) -> Result<String, Error> {
Ok(format!("m/44'/{}'/0'/0'/{}'", BIP44_PATH, index))
}

Expand Down
6 changes: 3 additions & 3 deletions packages/kos-sdk/src/chains/polygon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ impl MATIC {
}

#[wasm_bindgen(js_name = "getPath")]
pub fn get_path(index: u32) -> Result<String, Error> {
ETH::get_path(index)
pub fn get_path(index: u32, is_legacy: Option<bool>) -> Result<String, Error> {
ETH::get_path(index, is_legacy)
}

#[wasm_bindgen(js_name = "signDigest")]
Expand Down Expand Up @@ -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();

Expand Down
14 changes: 9 additions & 5 deletions packages/kos-sdk/src/chains/tron/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,13 @@ impl TRX {
}

#[wasm_bindgen(js_name = "getPath")]
pub fn get_path(index: u32) -> Result<String, Error> {
// use account 0 index X
Ok(format!("m/44'/{}'/0'/0/{}", BIP44_PATH, index))
pub fn get_path(index: u32, is_legacy: Option<bool>) -> Result<String, Error> {
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")]
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand Down
8 changes: 4 additions & 4 deletions packages/kos-sdk/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ impl Wallet {
index: u32,
password: Option<String>,
) -> Result<Wallet, Error> {
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);

Expand Down Expand Up @@ -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();

Expand All @@ -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();

Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit ea73f5d

Please sign in to comment.