ui: port confirm component to be awaitable, reduce callback soup
What changed, and why it matters
This commit is a routine internal code cleanup in the BitBox02 hardware wallet firmware. It rewrites the user confirmation dialog so it uses Rust's async/await style instead of manual callbacks. There is no indication this fixes a security bug or introduces a vulnerability. It is a refactoring change.
No security action required. Treat as normal refactoring review; verify async/await behavior and screensaver inhibition in existing UI tests.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch ports the confirm UI component from a callback-based API (confirm_create + closure) to an awaitable async function. It introduces a ScreensaverInhibitor RAII guard to disable the screensaver while a confirmation dialog is active, replacing the previous option_no_screensaver helper. The new implementation uses a RefCell<SharedState> with an optional Waker and result to bridge a C callback into a Rust future. The callback is guaranteed to be called once by the underlying C code. The change touches the real UI implementation, a stub, and a C unit-test stub, keeping behavior equivalent.
Changed components
src/rust/bitbox02-rust/src/workflow/confirm.rssrc/rust/bitbox02/src/screen_saver.rssrc/rust/bitbox02/src/ui/ui.rssrc/rust/bitbox02/src/ui/ui_stub.rssrc/rust/bitbox02/src/ui/ui_stub_c_unit_tests.rsInspect captured patch +73 / −48
diff --git a/src/rust/bitbox02-rust/src/workflow/confirm.rs b/src/rust/bitbox02-rust/src/workflow/confirm.rs
index 5d87738..dd53b23 100644
--- a/src/rust/bitbox02-rust/src/workflow/confirm.rs
+++ b/src/rust/bitbox02-rust/src/workflow/confirm.rs
@@ -1,23 +1,14 @@
// SPDX-License-Identifier: Apache-2.0
-use crate::bb02_async::option_no_screensaver;
-
pub use bitbox02::ui::{ConfirmParams as Params, Font};
pub struct UserAbort;
-/// Returns true if the user accepts, false if the user rejects.
+/// Returns `Ok(())` if the user accepts, `Err(UserAbort)` if the user rejects.
pub async fn confirm(params: &Params<'_>) -> Result<(), UserAbort> {
- let result = core::cell::RefCell::new(None as Option<Result<(), UserAbort>>);
-
- // The component will set the result when the user accepted/rejected.
- let mut component = bitbox02::ui::confirm_create(params, |accepted| {
- *result.borrow_mut() = if accepted {
- Some(Ok(()))
- } else {
- Some(Err(UserAbort))
- };
- });
- component.screen_stack_push();
- option_no_screensaver(&result).await
+ if bitbox02::ui::confirm(params).await {
+ Ok(())
+ } else {
+ Err(UserAbort)
+ }
}
diff --git a/src/rust/bitbox02/src/screen_saver.rs b/src/rust/bitbox02/src/screen_saver.rs
index 5c18bf9..5a26a4f 100644
--- a/src/rust/bitbox02/src/screen_saver.rs
+++ b/src/rust/bitbox02/src/screen_saver.rs
@@ -13,3 +13,20 @@ pub fn screen_saver_disable() {
bitbox02_sys::screen_saver_disable();
}
}
+
+pub struct ScreensaverInhibitor {
+ __private: (), // prevents direct init
+}
+
+impl ScreensaverInhibitor {
+ pub fn new() -> Self {
+ screen_saver_disable();
+ Self { __private: () }
+ }
+}
+
+impl Drop for ScreensaverInhibitor {
+ fn drop(&mut self) {
+ screen_saver_enable();
+ }
+}
diff --git a/src/rust/bitbox02/src/ui/ui.rs b/src/rust/bitbox02/src/ui/ui.rs
index ec7a8e6..70ab1fb 100644
--- a/src/rust/bitbox02/src/ui/ui.rs
+++ b/src/rust/bitbox02/src/ui/ui.rs
@@ -113,22 +113,32 @@ where
}
}
-/// Creates a user confirmation dialog screen.
-/// `result` - will be asynchronously set to `Some(bool)` once the user accets or rejects.
-pub fn confirm_create<'a, F>(params: &ConfirmParams, result_callback: F) -> Component<'a>
-where
- // Callback must outlive component.
- F: FnMut(bool) + 'a,
-{
- unsafe extern "C" fn c_callback<F2>(result: bool, user_data: *mut c_void)
- where
- F2: FnMut(bool),
- {
- // The callback is dropped afterwards. This is safe because
- // this C callback is guaranteed to be called only once.
- let mut callback = unsafe { Box::from_raw(user_data as *mut F2) };
- callback(result);
+/// Returns true if the user accepts, false if the user rejects.
+pub async fn confirm(params: &ConfirmParams<'_>) -> bool {
+ let _no_screensaver = crate::screen_saver::ScreensaverInhibitor::new();
+
+ // Shared between the async context and the c callback
+ struct SharedState {
+ waker: Option<Waker>,
+ result: Option<bool>,
}
+ let shared_state = Box::new(RefCell::new(SharedState {
+ waker: None,
+ result: None,
+ }));
+ let shared_state_ptr = shared_state.as_ref() as *const RefCell<SharedState> as *mut c_void;
+
+ unsafe extern "C" fn callback(result: bool, user_data: *mut c_void) {
+ let shared_state = unsafe { &*(user_data as *mut RefCell<SharedState>) };
+ let mut shared_state = shared_state.borrow_mut();
+ if shared_state.result.is_none() {
+ shared_state.result = Some(result);
+ if let Some(waker) = shared_state.waker.as_ref() {
+ waker.wake_by_ref();
+ }
+ }
+ }
+
let mut title_scratch = Vec::new();
let mut body_scratch = Vec::new();
let component = unsafe {
@@ -136,17 +146,34 @@ where
¶ms
.to_c_params(&mut title_scratch, &mut body_scratch)
.data,
- Some(c_callback::<F>),
- // passed to the C callback as `user_data`
- Box::into_raw(Box::new(result_callback)) as *mut _,
+ Some(callback),
+ shared_state_ptr, // passed to callback as `user_data`.
)
};
- Component {
+
+ let mut component = Component {
component,
is_pushed: false,
on_drop: None,
_p: PhantomData,
- }
+ };
+ component.screen_stack_push();
+
+ core::future::poll_fn({
+ let shared_state = &shared_state;
+ move |cx| {
+ let mut shared_state = shared_state.borrow_mut();
+
+ if let Some(result) = shared_state.result {
+ Poll::Ready(result)
+ } else {
+ // Store the waker so the callback can wake up this task
+ shared_state.waker = Some(cx.waker().clone());
+ Poll::Pending
+ }
+ }
+ })
+ .await
}
pub fn screen_process() {
diff --git a/src/rust/bitbox02/src/ui/ui_stub.rs b/src/rust/bitbox02/src/ui/ui_stub.rs
index caaaaad..6bfa539 100644
--- a/src/rust/bitbox02/src/ui/ui_stub.rs
+++ b/src/rust/bitbox02/src/ui/ui_stub.rs
@@ -49,10 +49,7 @@ where
panic!("not used");
}
-pub fn confirm_create<'a, F>(_params: &ConfirmParams, _result_callback: F) -> Component<'a>
-where
- F: FnMut(bool) + 'a,
-{
+pub async fn confirm(_params: &ConfirmParams<'_>) -> bool {
panic!("not used");
}
diff --git a/src/rust/bitbox02/src/ui/ui_stub_c_unit_tests.rs b/src/rust/bitbox02/src/ui/ui_stub_c_unit_tests.rs
index 531ed9f..ec15044 100644
--- a/src/rust/bitbox02/src/ui/ui_stub_c_unit_tests.rs
+++ b/src/rust/bitbox02/src/ui/ui_stub_c_unit_tests.rs
@@ -55,20 +55,13 @@ where
}
}
-pub fn confirm_create<'a, F>(params: &ConfirmParams, mut result_callback: F) -> Component<'a>
-where
- F: FnMut(bool) + 'a,
-{
+pub async fn confirm(params: &ConfirmParams<'_>) -> bool {
crate::print_stdout(&format!(
"CONFIRM SCREEN START\nTITLE: {}\nBODY: {}\nCONFIRM SCREEN END\n",
params.title, params.body
));
- result_callback(true);
- Component {
- is_pushed: false,
- _p: PhantomData,
- }
+ true
}
pub fn screen_process() {}
Why this scored 12/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.