Skip to content

Commit

Permalink
fix: don't export wasm-bindgen mnemonic size enum
Browse files Browse the repository at this point in the history
  • Loading branch information
jurevans committed Jan 7, 2025
1 parent c0c2051 commit 00b1175
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
26 changes: 11 additions & 15 deletions packages/crypto/lib/src/crypto/bip39.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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<Mnemonic, String> {
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 {
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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");
Expand Down
6 changes: 2 additions & 4 deletions packages/sdk/src/mnemonic.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import {
Mnemonic as MnemonicWasm,
PhraseSize,
StringPointer,
readVecStringPointer,
readVecU8Pointer,
} from "@namada/crypto";

export { PhraseSize } from "@namada/crypto";
export { PhraseSize } from "@namada/types";

/**
* Class for accessing mnemonic functionality from wasm
Expand All @@ -22,7 +20,7 @@ export class Mnemonic {
* @param [size] Mnemonic length
* @returns An array of words
*/
generate(size: PhraseSize = PhraseSize.N12): string[] {
generate(size: 12 | 24 = 12): string[] {
const mnemonic = new MnemonicWasm(size);

const vecStringPointer = mnemonic.to_words();
Expand Down
4 changes: 4 additions & 0 deletions packages/types/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum PhraseSize {
N12 = 12,
N24 = 24,
}
1 change: 1 addition & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./account";
export * from "./chain";
export * from "./constants";
export * from "./events";
export * from "./namada";
export * from "./proposals";
Expand Down

0 comments on commit 00b1175

Please sign in to comment.