use crate::{ChineseIdiomList, IdiomMnemonic, IdiomMnemonicSize};
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
#[cfg(feature = "rand")]
pub fn generate(idiom_count: u32) -> Result<String, JsValue> {
let size = match idiom_count {
12 => IdiomMnemonicSize::Idioms12,
15 => IdiomMnemonicSize::Idioms15,
18 => IdiomMnemonicSize::Idioms18,
21 => IdiomMnemonicSize::Idioms21,
24 => IdiomMnemonicSize::Idioms24,
_ => {
return Err(JsValue::from_str(
"Invalid idiom count. Must be 12, 15, 18, 21, or 24",
))
}
};
let idiom_mnemonic =
IdiomMnemonic::generate(size).map_err(|e| JsValue::from_str(&format!("{}", e)))?;
Ok(idiom_mnemonic.phrase())
}
#[wasm_bindgen]
pub fn to_seed(phrase: &str, passphrase: Option<String>) -> Result<Vec<u8>, JsValue> {
let idiom_mnemonic =
IdiomMnemonic::from_phrase(phrase).map_err(|e| JsValue::from_str(&format!("{}", e)))?;
let pass = passphrase.as_deref().unwrap_or("");
let seed = idiom_mnemonic.to_seed(pass);
Ok(seed.to_vec())
}
#[wasm_bindgen]
pub fn validate(phrase: &str) -> bool {
IdiomMnemonic::validate(phrase)
}
#[wasm_bindgen]
pub fn idiom_to_index(idiom: &str) -> Option<usize> {
ChineseIdiomList::idiom_to_index(idiom)
}
#[wasm_bindgen]
pub fn index_to_idiom(idx: usize) -> Option<&'static str> {
ChineseIdiomList::index_to_idiom(idx)
}
#[wasm_bindgen]
pub fn get_pinyin(idx: usize) -> Option<&'static str> {
ChineseIdiomList::get_pinyin(idx)
}
#[wasm_bindgen]
pub fn idiom_count() -> usize {
ChineseIdiomList::len()
}