mnemonic: set phrase size on import, paste on confirmation

This commit is contained in:
ardocrat 2024-06-12 12:13:54 +03:00
parent 22342c8b8a
commit 9a13276198
3 changed files with 36 additions and 15 deletions

View file

@ -203,12 +203,23 @@ impl WalletCreation {
}); });
}); });
ui.add_space(4.0); ui.add_space(4.0);
} else { } else if step == Step::ConfirmMnemonic {
ui.add_space(4.0);
// Show next step or paste button.
if step_available { if step_available {
ui.add_space(4.0);
self.next_step_button_ui(ui, step, on_create); self.next_step_button_ui(ui, step, on_create);
ui.add_space(4.0); } else {
let paste_text = format!("{} {}", CLIPBOARD_TEXT, t!("paste").to_uppercase());
View::button(ui, paste_text, Colors::white_or_black(false), || {
let data = ZeroingString::from(cb.get_string_from_buffer().trim());
self.mnemonic_setup.mnemonic.import_text(&data, true);
});
} }
ui.add_space(4.0);
} else if step_available {
ui.add_space(4.0);
self.next_step_button_ui(ui, step, on_create);
ui.add_space(4.0);
} }
ui.add_space(4.0); ui.add_space(4.0);
} }
@ -229,7 +240,7 @@ impl WalletCreation {
let p_t = format!("{} {}", CLIPBOARD_TEXT, t!("paste").to_uppercase()); let p_t = format!("{} {}", CLIPBOARD_TEXT, t!("paste").to_uppercase());
View::button(ui, p_t, Colors::white_or_black(false), || { View::button(ui, p_t, Colors::white_or_black(false), || {
let data = ZeroingString::from(cb.get_string_from_buffer().trim()); let data = ZeroingString::from(cb.get_string_from_buffer().trim());
self.mnemonic_setup.mnemonic.import_text(&data); self.mnemonic_setup.mnemonic.import_text(&data, false);
}); });
} }
} }

View file

@ -384,7 +384,7 @@ impl MnemonicSetup {
self.camera_content.clear_state(); self.camera_content.clear_state();
match &result { match &result {
QrScanResult::Text(text) => { QrScanResult::Text(text) => {
self.mnemonic.import_text(text); self.mnemonic.import_text(text, false);
if self.mnemonic.is_valid_phrase() { if self.mnemonic.is_valid_phrase() {
modal.close(); modal.close();
return; return;

View file

@ -106,18 +106,28 @@ impl Mnemonic {
} }
/// Set words from provided text if possible. /// Set words from provided text if possible.
pub fn import_text(&mut self, text: &ZeroingString) { pub fn import_text(&mut self, text: &ZeroingString, confirmation: bool) {
if self.mode != PhraseMode::Import {
return;
}
let words_split = text.trim().split(" "); let words_split = text.trim().split(" ");
let count = words_split.clone().count(); let count = words_split.clone().count();
if PhraseSize::is_correct_count(count) { if let Some(size) = PhraseSize::type_for_value(count) {
if self.size == PhraseSize::type_for_value(count).unwrap() { if !confirmation {
let mut words = vec![]; self.size = size;
words_split.enumerate().for_each(|(i, word)| { } else if self.size != size {
words.insert(i, word.to_string()) return;
}); }
let mut words = vec![];
words_split.enumerate().for_each(|(i, word)| {
if confirmation && !self.is_valid_word(&word.to_string(), i) {
words = vec![];
return;
}
words.insert(i, word.to_string())
});
if confirmation {
if !words.is_empty() {
self.confirm_words = words;
}
} else {
self.words = words; self.words = words;
} }
} }