Skip to content

Commit d7cfe68

Browse files
committed
hal: add show_and_confirm_mnemonic to HAL
The default implementation is in terms of other HAL/UI functions. For unit tests (e.g. in show_mnemonic.rs or bip85.rs), it is useful to be able to skip over the implementation details, so the tests don't have to mock selecting the right words in the quiz.
1 parent e74e3b8 commit d7cfe68

File tree

5 files changed

+26
-13
lines changed

5 files changed

+26
-13
lines changed

src/rust/bitbox02-rust/src/hww/api/bip85.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub async fn process(
4444
/// https://github.com/bitcoin/bips/blob/master/bip-0085.mediawiki#bip39.
4545
async fn process_bip39(hal: &mut impl crate::hal::Hal) -> Result<(), Error> {
4646
use crate::workflow::trinary_choice::TrinaryChoice;
47-
use crate::workflow::{mnemonic, trinary_input_string};
47+
use crate::workflow::trinary_input_string;
4848

4949
hal.ui()
5050
.confirm(&confirm::Params {
@@ -123,7 +123,7 @@ async fn process_bip39(hal: &mut impl crate::hal::Hal) -> Result<(), Error> {
123123

124124
let mnemonic = keystore::bip85_bip39(num_words, index)?;
125125
let words: Vec<&str> = mnemonic.split(' ').collect();
126-
mnemonic::show_and_confirm_mnemonic(hal, &words).await?;
126+
hal.ui().show_and_confirm_mnemonic(&words).await?;
127127

128128
hal.ui().status("Finished", true).await;
129129

src/rust/bitbox02-rust/src/hww/api/show_mnemonic.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::pb;
2020
use pb::response::Response;
2121

2222
use crate::hal::Ui;
23-
use crate::workflow::{confirm, mnemonic, unlock};
23+
use crate::workflow::{confirm, unlock};
2424

2525
use crate::keystore;
2626

@@ -55,7 +55,7 @@ pub async fn process(hal: &mut impl crate::hal::Hal) -> Result<Response, Error>
5555

5656
let words: Vec<&str> = mnemonic_sentence.split(' ').collect();
5757

58-
mnemonic::show_and_confirm_mnemonic(hal, &words).await?;
58+
hal.ui().show_and_confirm_mnemonic(&words).await?;
5959

6060
bitbox02::memory::set_initialized().or(Err(Error::Memory))?;
6161

src/rust/bitbox02-rust/src/workflow.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,20 @@ pub trait Workflows {
7979
choices: &[&str],
8080
title: &str,
8181
) -> Result<u8, cancel::Error>;
82+
83+
/// Display the mnemonic words and have the user confirm them in a multiple-choice quiz.
84+
///
85+
/// The default implementation is implemented in terms of `self.show_mnemonic()`,
86+
/// `self.quiz_mnemonic_word()`, etc.
87+
///
88+
/// This function is defined in the HAL so unit tests can easily mock it. Real implementations
89+
/// should leave the default implementation.
90+
async fn show_and_confirm_mnemonic(&mut self, words: &[&str]) -> Result<(), cancel::Error>
91+
where
92+
Self: Sized,
93+
{
94+
mnemonic::show_and_confirm_mnemonic(self, words).await
95+
}
8296
}
8397

8498
pub struct RealWorkflows;

src/rust/bitbox02-rust/src/workflow/mnemonic.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ pub async fn confirm_word(choices: &[&str], title: &str) -> Result<u8, CancelErr
111111
}
112112

113113
pub async fn show_and_confirm_mnemonic(
114-
hal: &mut impl crate::hal::Hal,
114+
hal_ui: &mut impl crate::hal::Ui,
115115
words: &[&str],
116116
) -> Result<(), CancelError> {
117-
hal.ui()
117+
hal_ui
118118
.confirm(&confirm::Params {
119119
title: "",
120120
body: &format!("{} words follow", words.len()),
@@ -125,11 +125,10 @@ pub async fn show_and_confirm_mnemonic(
125125
.map_err(|_| CancelError::Cancelled)?;
126126

127127
// Part 1) Scroll through words
128-
hal.ui().show_mnemonic(words).await?;
128+
hal_ui.show_mnemonic(words).await?;
129129

130130
// Can only succeed due to `accept_only`.
131-
let _ = hal
132-
.ui()
131+
let _ = hal_ui
133132
.confirm(&confirm::Params {
134133
title: "",
135134
body: "Please confirm\neach word",
@@ -147,10 +146,10 @@ pub async fn show_and_confirm_mnemonic(
147146
choices.push("Back to\nrecovery words");
148147
let back_idx = (choices.len() - 1) as u8;
149148
loop {
150-
match hal.ui().quiz_mnemonic_word(&choices, &title).await? {
149+
match hal_ui.quiz_mnemonic_word(&choices, &title).await? {
151150
selected_idx if selected_idx == correct_idx => break,
152-
selected_idx if selected_idx == back_idx => hal.ui().show_mnemonic(words).await?,
153-
_ => hal.ui().status("Incorrect word\nTry again", false).await,
151+
selected_idx if selected_idx == back_idx => hal_ui.show_mnemonic(words).await?,
152+
_ => hal_ui.status("Incorrect word\nTry again", false).await,
154153
}
155154
}
156155
}

src/rust/bitbox02-rust/src/workflow/mnemonic_c_unit_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub async fn confirm_word(_choices: &[&str], _title: &str) -> Result<u8, CancelE
2626
}
2727

2828
pub async fn show_and_confirm_mnemonic(
29-
_hal: &mut impl crate::hal::Hal,
29+
_ui: &mut impl crate::hal::Ui,
3030
words: &[&str],
3131
) -> Result<(), CancelError> {
3232
for word in words.iter() {

0 commit comments

Comments
 (0)