diff --git a/packages/crypto/lib/src/crypto/bip39.rs b/packages/crypto/lib/src/crypto/bip39.rs index 168738c92b..14b1deb59d 100644 --- a/packages/crypto/lib/src/crypto/bip39.rs +++ b/packages/crypto/lib/src/crypto/bip39.rs @@ -10,13 +10,8 @@ use zeroize::Zeroize; pub enum Bip39Error { #[error("Invalid phrase")] InvalidPhrase, -} - -#[wasm_bindgen] -#[derive(Copy, Clone)] -pub enum PhraseSize { - N12 = 12, - N24 = 24, + #[error("Invalid phrase size! Must be 12 or 24!")] + InvalidPhraseSize, } #[wasm_bindgen] @@ -27,15 +22,16 @@ pub struct Mnemonic { #[wasm_bindgen] impl Mnemonic { #[wasm_bindgen(constructor)] - pub fn new(size: PhraseSize) -> Mnemonic { + pub fn new(size: u8) -> Result { let mnemonic_type = match size { - PhraseSize::N12 => MnemonicType::Words12, - PhraseSize::N24 => MnemonicType::Words24, + 12 => MnemonicType::Words12, + 24 => MnemonicType::Words24, + _ => return Err(Bip39Error::InvalidPhraseSize.to_string()), }; let mnemonic = M::new(mnemonic_type, Language::English); - Mnemonic { mnemonic } + Ok(Mnemonic { mnemonic }) } pub fn validate(phrase: &str) -> bool { @@ -88,13 +84,13 @@ mod tests { #[wasm_bindgen_test] fn can_generate_mnemonic_from_size() { - let mnemonic = Mnemonic::new(PhraseSize::N12); + let mnemonic = Mnemonic::new(12).unwrap(); let phrase = mnemonic.phrase(); let words: Vec<&str> = phrase.split(' ').collect(); assert_eq!(words.iter().len(), 12); - let mnemonic = Mnemonic::new(PhraseSize::N24); + let mnemonic = Mnemonic::new(24).unwrap(); let phrase = mnemonic.phrase(); let words: Vec<&str> = phrase.split(' ').collect(); @@ -141,14 +137,14 @@ mod tests { #[wasm_bindgen_test] fn can_generate_word_list_from_mnemonic() { - let mnemonic = Mnemonic::new(PhraseSize::N12); + let mnemonic = Mnemonic::new(12).unwrap(); let words = mnemonic .to_words() .expect("Should return a VecStringPointer containing the words"); assert_eq!(words.strings.len(), 12); - let mnemonic = Mnemonic::new(PhraseSize::N24); + let mnemonic = Mnemonic::new(24).unwrap(); let words = mnemonic .to_words() .expect("Should return a VecStringPointer containing the words"); diff --git a/packages/sdk/src/mnemonic.ts b/packages/sdk/src/mnemonic.ts index c875d02ff2..108653bfc1 100644 --- a/packages/sdk/src/mnemonic.ts +++ b/packages/sdk/src/mnemonic.ts @@ -1,12 +1,14 @@ import { Mnemonic as MnemonicWasm, - PhraseSize, StringPointer, readVecStringPointer, readVecU8Pointer, } from "@namada/crypto"; -export { PhraseSize } from "@namada/crypto"; +export enum PhraseSize { + N12 = 12, + N24 = 24, +} /** * Class for accessing mnemonic functionality from wasm