hal: add show_and_confirm_mnemonic to HAL
What changed, and why it matters
This commit is a straightforward internal code refactor. It moves the 'show and confirm mnemonic' feature into a hardware abstraction layer (HAL) so that unit tests can mock it more easily. There is no change to user-facing behavior, no fix for a bug, and no security-relevant change visible in the code.
No security action required. Treat as normal maintenance/testability refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change adds show_and_confirm_mnemonic as a default method on the Ui trait in the Rust HAL. The default implementation delegates to the existing workflow::mnemonic::show_and_confirm_mnemonic. Call sites in bip85.rs and show_mnemonic.rs are updated from mnemonic::show_and_confirm_mnemonic(hal, &words) to hal.ui().show_and_confirm_mnemonic(&words). The standalone function’s signature is narrowed from &mut impl Hal to &mut impl Ui, and the C unit-test stub is updated accordingly. The behavior of the real implementation is unchanged.
Changed components
src/rust/bitbox02-rust/src/workflow.rssrc/rust/bitbox02-rust/src/workflow/mnemonic.rssrc/rust/bitbox02-rust/src/hww/api/bip85.rssrc/rust/bitbox02-rust/src/hww/api/show_mnemonic.rssrc/rust/bitbox02-rust/src/workflow/mnemonic_c_unit_tests.rsInspect captured patch +26 / −13
diff --git a/src/rust/bitbox02-rust/src/hww/api/bip85.rs b/src/rust/bitbox02-rust/src/hww/api/bip85.rs
index b3a3a4a..f019043 100644
--- a/src/rust/bitbox02-rust/src/hww/api/bip85.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/bip85.rs
@@ -44,7 +44,7 @@ pub async fn process(
/// https://github.com/bitcoin/bips/blob/master/bip-0085.mediawiki#bip39.
async fn process_bip39(hal: &mut impl crate::hal::Hal) -> Result<(), Error> {
use crate::workflow::trinary_choice::TrinaryChoice;
- use crate::workflow::{mnemonic, trinary_input_string};
+ use crate::workflow::trinary_input_string;
hal.ui()
.confirm(&confirm::Params {
@@ -123,7 +123,7 @@ async fn process_bip39(hal: &mut impl crate::hal::Hal) -> Result<(), Error> {
let mnemonic = keystore::bip85_bip39(num_words, index)?;
let words: Vec<&str> = mnemonic.split(' ').collect();
- mnemonic::show_and_confirm_mnemonic(hal, &words).await?;
+ hal.ui().show_and_confirm_mnemonic(&words).await?;
hal.ui().status("Finished", true).await;
diff --git a/src/rust/bitbox02-rust/src/hww/api/show_mnemonic.rs b/src/rust/bitbox02-rust/src/hww/api/show_mnemonic.rs
index 65b88d7..a7a07a4 100644
--- a/src/rust/bitbox02-rust/src/hww/api/show_mnemonic.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/show_mnemonic.rs
@@ -20,7 +20,7 @@ use crate::pb;
use pb::response::Response;
use crate::hal::Ui;
-use crate::workflow::{confirm, mnemonic, unlock};
+use crate::workflow::{confirm, unlock};
use crate::keystore;
@@ -55,7 +55,7 @@ pub async fn process(hal: &mut impl crate::hal::Hal) -> Result<Response, Error>
let words: Vec<&str> = mnemonic_sentence.split(' ').collect();
- mnemonic::show_and_confirm_mnemonic(hal, &words).await?;
+ hal.ui().show_and_confirm_mnemonic(&words).await?;
bitbox02::memory::set_initialized().or(Err(Error::Memory))?;
diff --git a/src/rust/bitbox02-rust/src/workflow.rs b/src/rust/bitbox02-rust/src/workflow.rs
index 9901e49..863de64 100644
--- a/src/rust/bitbox02-rust/src/workflow.rs
+++ b/src/rust/bitbox02-rust/src/workflow.rs
@@ -79,6 +79,20 @@ pub trait Workflows {
choices: &[&str],
title: &str,
) -> Result<u8, cancel::Error>;
+
+ /// Display the mnemonic words and have the user confirm them in a multiple-choice quiz.
+ ///
+ /// The default implementation is implemented in terms of `self.show_mnemonic()`,
+ /// `self.quiz_mnemonic_word()`, etc.
+ ///
+ /// This function is defined in the HAL so unit tests can easily mock it. Real implementations
+ /// should leave the default implementation.
+ async fn show_and_confirm_mnemonic(&mut self, words: &[&str]) -> Result<(), cancel::Error>
+ where
+ Self: Sized,
+ {
+ mnemonic::show_and_confirm_mnemonic(self, words).await
+ }
}
pub struct RealWorkflows;
diff --git a/src/rust/bitbox02-rust/src/workflow/mnemonic.rs b/src/rust/bitbox02-rust/src/workflow/mnemonic.rs
index 7835bbb..e1fc57f 100644
--- a/src/rust/bitbox02-rust/src/workflow/mnemonic.rs
+++ b/src/rust/bitbox02-rust/src/workflow/mnemonic.rs
@@ -111,10 +111,10 @@ pub async fn confirm_word(choices: &[&str], title: &str) -> Result<u8, CancelErr
}
pub async fn show_and_confirm_mnemonic(
- hal: &mut impl crate::hal::Hal,
+ hal_ui: &mut impl crate::hal::Ui,
words: &[&str],
) -> Result<(), CancelError> {
- hal.ui()
+ hal_ui
.confirm(&confirm::Params {
title: "",
body: &format!("{} words follow", words.len()),
@@ -125,11 +125,10 @@ pub async fn show_and_confirm_mnemonic(
.map_err(|_| CancelError::Cancelled)?;
// Part 1) Scroll through words
- hal.ui().show_mnemonic(words).await?;
+ hal_ui.show_mnemonic(words).await?;
// Can only succeed due to `accept_only`.
- let _ = hal
- .ui()
+ let _ = hal_ui
.confirm(&confirm::Params {
title: "",
body: "Please confirm\neach word",
@@ -147,10 +146,10 @@ pub async fn show_and_confirm_mnemonic(
choices.push("Back to\nrecovery words");
let back_idx = (choices.len() - 1) as u8;
loop {
- match hal.ui().quiz_mnemonic_word(&choices, &title).await? {
+ match hal_ui.quiz_mnemonic_word(&choices, &title).await? {
selected_idx if selected_idx == correct_idx => break,
- selected_idx if selected_idx == back_idx => hal.ui().show_mnemonic(words).await?,
- _ => hal.ui().status("Incorrect word\nTry again", false).await,
+ selected_idx if selected_idx == back_idx => hal_ui.show_mnemonic(words).await?,
+ _ => hal_ui.status("Incorrect word\nTry again", false).await,
}
}
}
diff --git a/src/rust/bitbox02-rust/src/workflow/mnemonic_c_unit_tests.rs b/src/rust/bitbox02-rust/src/workflow/mnemonic_c_unit_tests.rs
index 6fd74e6..5e31a05 100644
--- a/src/rust/bitbox02-rust/src/workflow/mnemonic_c_unit_tests.rs
+++ b/src/rust/bitbox02-rust/src/workflow/mnemonic_c_unit_tests.rs
@@ -26,7 +26,7 @@ pub async fn confirm_word(_choices: &[&str], _title: &str) -> Result<u8, CancelE
}
pub async fn show_and_confirm_mnemonic(
- _hal: &mut impl crate::hal::Hal,
+ _ui: &mut impl crate::hal::Ui,
words: &[&str],
) -> Result<(), CancelError> {
for word in words.iter() {
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.