Skip to content

Commit

Permalink
rust: allow restricting get_bip39_wordlist to subset
Browse files Browse the repository at this point in the history
This will allow to set the autocomplete keyboard to a subset of the
BIP39 wordlist to allow entering the last word of a 12/18 word
mnemonic using the keyboard, similar to how the 24th word can be
entered using a menu.

The keyboard is needed for 12/18 words as there are too many
candidates to show in a simple menu.
  • Loading branch information
benma committed Oct 27, 2023
1 parent c9a3edc commit 3f83629
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/rust/bitbox02-rust/src/workflow/mnemonic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ pub async fn get() -> Result<zeroize::Zeroizing<String>, CancelError> {
status(&format!("Enter {} words", num_words), true).await;

// Provide all bip39 words to restrict the keyboard entry.
let bip39_wordlist = bitbox02::keystore::get_bip39_wordlist().unwrap();
let bip39_wordlist = bitbox02::keystore::get_bip39_wordlist(None);

let mut word_idx: usize = 0;
let mut entered_words = vec![zeroize::Zeroizing::new(String::new()); num_words];
Expand Down
29 changes: 19 additions & 10 deletions src/rust/bitbox02/src/keystore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,25 @@ impl Drop for Bip39Wordlist {
}
}

pub fn get_bip39_wordlist() -> Result<Bip39Wordlist, ()> {
let mut result = Bip39Wordlist(vec![core::ptr::null(); BIP39_WORDLIST_LEN as usize]);
for i in 0..BIP39_WORDLIST_LEN {
let mut word_ptr: *mut u8 = core::ptr::null_mut();
match unsafe { bitbox02_sys::keystore_get_bip39_word(i, &mut word_ptr) } {
false => return Err(()),
true => result.0[i as usize] = word_ptr,
}
}
Ok(result)
/// If indices is None, all BIP39 English words are returned, otherwise only the words of the given
/// indices in the BIP39 English wordlist.
pub fn get_bip39_wordlist(indices: Option<&[u16]>) -> Bip39Wordlist {
let indices = match indices {
Some(indices) => indices.to_vec(),
None => (0..BIP39_WORDLIST_LEN).collect(),
};
Bip39Wordlist(
indices
.into_iter()
.map(|i| {
let mut word_ptr: *mut u8 = core::ptr::null_mut();
match unsafe { bitbox02_sys::keystore_get_bip39_word(i, &mut word_ptr) } {
false => panic!("get_bip39_wordlist"),
true => word_ptr as _,
}
})
.collect(),
)
}

pub fn secp256k1_pubkey_compressed_to_uncompressed(
Expand Down

0 comments on commit 3f83629

Please sign in to comment.