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);
} else {
} else if step == Step::ConfirmMnemonic {
ui.add_space(4.0);
// Show next step or paste button.
if step_available {
ui.add_space(4.0);
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);
}
@ -229,7 +240,7 @@ impl WalletCreation {
let p_t = format!("{} {}", CLIPBOARD_TEXT, t!("paste").to_uppercase());
View::button(ui, p_t, Colors::white_or_black(false), || {
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();
match &result {
QrScanResult::Text(text) => {
self.mnemonic.import_text(text);
self.mnemonic.import_text(text, false);
if self.mnemonic.is_valid_phrase() {
modal.close();
return;

View file

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