Skip to content

Commit f793c1a

Browse files
Merge pull request #141 from klever-io/feat/export-get-path-by-chain
export get path by chain
2 parents 80309b3 + 224c733 commit f793c1a

4 files changed

Lines changed: 37 additions & 6 deletions

File tree

Cargo.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/kos-mobile/src/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ fn validate_mnemonic(mnemonic: String) -> bool {
154154
kos::crypto::mnemonic::validate_mnemonic(mnemonic.as_str()).is_ok()
155155
}
156156

157+
#[uniffi::export]
158+
fn get_path_by_chain(chain_id: u32, index: u32, use_legacy_path: bool) -> Result<String, KOSError> {
159+
let chain = get_chain_by(chain_id)?;
160+
let path = chain.get_path(index, use_legacy_path);
161+
Ok(path)
162+
}
163+
157164
#[uniffi::export]
158165
fn generate_wallet_from_mnemonic(
159166
mnemonic: String,
@@ -663,4 +670,16 @@ mod tests {
663670
"The supported chains should not be empty"
664671
);
665672
}
673+
674+
#[test]
675+
fn should_get_path_by_chain() {
676+
let path = get_path_by_chain(38, 0, false).unwrap();
677+
assert_eq!(path, "m/44'/690'/0'/0'/0'");
678+
679+
let path = get_path_by_chain(27, 0, false).unwrap();
680+
assert_eq!(path, "");
681+
682+
let path = get_path_by_chain(27, 1, false).unwrap();
683+
assert_eq!(path, "//0///");
684+
}
666685
}

packages/kos-web/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub enum Error {
1111
// JSON serialization error
1212
JSONSerde(String),
1313
// UnsupportedChain,
14-
UnsupportedChain(&'static str),
14+
UnsupportedChain(String),
1515
// InvalidMnemonic,
1616
InvalidMnemonic(&'static str),
1717
// InvalidPath,

packages/kos-web/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ pub fn is_chain_supported(chain: u32) -> bool {
7171
kos::chains::is_chain_supported(chain)
7272
}
7373

74+
#[wasm_bindgen(js_name = "getPathByChain")]
75+
pub fn get_path_by_chain(
76+
chain_id: u32,
77+
index: u32,
78+
use_legacy_path: bool,
79+
) -> Result<String, Error> {
80+
let chain = kos::chains::get_chain_by_id(chain_id)
81+
.ok_or(Error::UnsupportedChain(format!("{}", chain_id)))?;
82+
let path = chain.get_path(index, use_legacy_path);
83+
Ok(path)
84+
}
85+
7486
#[wasm_bindgen]
7587
#[derive(Debug, Clone)]
7688
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]

0 commit comments

Comments
 (0)