From 9a132761982cf9ac80d5acaacb75174d8b3904c6 Mon Sep 17 00:00:00 2001 From: ardocrat Date: Wed, 12 Jun 2024 12:13:54 +0300 Subject: [PATCH] mnemonic: set phrase size on import, paste on confirmation --- src/gui/views/wallets/creation/creation.rs | 19 +++++++++++--- src/gui/views/wallets/creation/mnemonic.rs | 2 +- src/wallet/mnemonic.rs | 30 ++++++++++++++-------- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/gui/views/wallets/creation/creation.rs b/src/gui/views/wallets/creation/creation.rs index 0bd285d..cef6ac6 100644 --- a/src/gui/views/wallets/creation/creation.rs +++ b/src/gui/views/wallets/creation/creation.rs @@ -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); }); } } diff --git a/src/gui/views/wallets/creation/mnemonic.rs b/src/gui/views/wallets/creation/mnemonic.rs index 91a1725..8a078b6 100644 --- a/src/gui/views/wallets/creation/mnemonic.rs +++ b/src/gui/views/wallets/creation/mnemonic.rs @@ -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; diff --git a/src/wallet/mnemonic.rs b/src/wallet/mnemonic.rs index 54da957..1ab4124 100644 --- a/src/wallet/mnemonic.rs +++ b/src/wallet/mnemonic.rs @@ -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; } }