ui/trinary_choice: make choices optional
What changed, and why it matters
This commit is a routine user-interface refactor. It changes a three-choice picker so that one or more of the three buttons can be left out, instead of always requiring all three. The immediate use case mentioned by the developer is to remove the '18 words' option from a backup-restore screen, leaving only '12' and '24'. There is no security bug being fixed here.
No security action required. Review as normal UI refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change makes the left, middle, and right labels of trinary_choice optional (Option<&str> in Rust, nullable const char* in C). Existing call sites that still want three choices pass Some(…) for all labels. The C implementation now checks each label for NULL before creating and positioning the corresponding button. A minor cleanup in menu_create replaces a closure with a function pointer. No cryptographic, memory-safety, or authentication logic is modified.
Changed components
src/rust/bitbox02-rust/src/hww/api/bip85.rssrc/rust/bitbox02-rust/src/workflow.rssrc/rust/bitbox02-rust/src/workflow/mnemonic.rssrc/rust/bitbox02-rust/src/workflow/testing.rssrc/rust/bitbox02-rust/src/workflow/trinary_choice.rssrc/rust/bitbox02/src/ui/ui.rssrc/rust/bitbox02/src/ui/ui_stub.rssrc/rust/bitbox02/src/ui/ui_stub_c_unit_tests.rssrc/ui/components/trinary_choice.cInspect captured patch +61 / −43
diff --git a/src/rust/bitbox02-rust/src/hww/api/bip85.rs b/src/rust/bitbox02-rust/src/hww/api/bip85.rs
index b5bf593..6708713 100644
--- a/src/rust/bitbox02-rust/src/hww/api/bip85.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/bip85.rs
@@ -67,7 +67,7 @@ async fn process_bip39(hal: &mut impl crate::hal::Hal) -> Result<(), Error> {
let num_words: u32 = match hal
.ui()
- .trinary_choice("How many words?", "12", "18", "24")
+ .trinary_choice("How many words?", Some("12"), Some("18"), Some("24"))
.await
{
TrinaryChoice::TRINARY_CHOICE_LEFT => 12,
diff --git a/src/rust/bitbox02-rust/src/workflow.rs b/src/rust/bitbox02-rust/src/workflow.rs
index 0ca1d21..826441b 100644
--- a/src/rust/bitbox02-rust/src/workflow.rs
+++ b/src/rust/bitbox02-rust/src/workflow.rs
@@ -64,9 +64,9 @@ pub trait Workflows {
async fn trinary_choice(
&mut self,
message: &str,
- label_left: &str,
- label_middle: &str,
- label_right: &str,
+ label_left: Option<&str>,
+ label_middle: Option<&str>,
+ label_right: Option<&str>,
) -> trinary_choice::TrinaryChoice;
}
@@ -126,9 +126,9 @@ impl Workflows for RealWorkflows {
async fn trinary_choice(
&mut self,
message: &str,
- label_left: &str,
- label_middle: &str,
- label_right: &str,
+ label_left: Option<&str>,
+ label_middle: Option<&str>,
+ label_right: Option<&str>,
) -> trinary_choice::TrinaryChoice {
trinary_choice::choose(message, label_left, label_middle, label_right).await
}
diff --git a/src/rust/bitbox02-rust/src/workflow/mnemonic.rs b/src/rust/bitbox02-rust/src/workflow/mnemonic.rs
index 24e82da..33efffe 100644
--- a/src/rust/bitbox02-rust/src/workflow/mnemonic.rs
+++ b/src/rust/bitbox02-rust/src/workflow/mnemonic.rs
@@ -306,7 +306,7 @@ pub async fn get(
) -> Result<zeroize::Zeroizing<String>, CancelError> {
let num_words: usize = match hal
.ui()
- .trinary_choice("How many words?", "12", "18", "24")
+ .trinary_choice("How many words?", Some("12"), Some("18"), Some("24"))
.await
{
TrinaryChoice::TRINARY_CHOICE_LEFT => 12,
diff --git a/src/rust/bitbox02-rust/src/workflow/testing.rs b/src/rust/bitbox02-rust/src/workflow/testing.rs
index eb0a597..7f0a2ae 100644
--- a/src/rust/bitbox02-rust/src/workflow/testing.rs
+++ b/src/rust/bitbox02-rust/src/workflow/testing.rs
@@ -149,9 +149,9 @@ impl Workflows for TestingWorkflows<'_> {
async fn trinary_choice(
&mut self,
_message: &str,
- _label_left: &str,
- _label_middle: &str,
- _label_right: &str,
+ _label_left: Option<&str>,
+ _label_middle: Option<&str>,
+ _label_right: Option<&str>,
) -> trinary_choice::TrinaryChoice {
todo!("not used in unit tests yet");
}
diff --git a/src/rust/bitbox02-rust/src/workflow/trinary_choice.rs b/src/rust/bitbox02-rust/src/workflow/trinary_choice.rs
index 9e823cf..26cc9e0 100644
--- a/src/rust/bitbox02-rust/src/workflow/trinary_choice.rs
+++ b/src/rust/bitbox02-rust/src/workflow/trinary_choice.rs
@@ -22,9 +22,9 @@ pub use bitbox02::ui::TrinaryChoice;
pub async fn choose(
message: &str,
- label_left: &str,
- label_middle: &str,
- label_right: &str,
+ label_left: Option<&str>,
+ label_middle: Option<&str>,
+ label_right: Option<&str>,
) -> TrinaryChoice {
let result = RefCell::new(None as Option<TrinaryChoice>);
diff --git a/src/rust/bitbox02/src/ui/ui.rs b/src/rust/bitbox02/src/ui/ui.rs
index 6f16742..1f4175b 100644
--- a/src/rust/bitbox02/src/ui/ui.rs
+++ b/src/rust/bitbox02/src/ui/ui.rs
@@ -287,7 +287,7 @@ pub fn menu_create(params: MenuParams<'_>) -> Component<'_> {
// copied in C
title
.as_ref()
- .map_or_else(|| core::ptr::null(), |title| title.as_ptr()),
+ .map_or_else(core::ptr::null, |title| title.as_ptr()),
continue_on_last_cb,
continue_on_last_cb_param,
cancel_cb,
@@ -318,9 +318,9 @@ pub fn menu_create(params: MenuParams<'_>) -> Component<'_> {
pub fn trinary_choice_create<'a>(
message: &'a str,
- label_left: &'a str,
- label_middle: &'a str,
- label_right: &'a str,
+ label_left: Option<&'a str>,
+ label_middle: Option<&'a str>,
+ label_right: Option<&'a str>,
chosen_callback: TrinaryChoiceCb,
) -> Component<'a> {
unsafe extern "C" fn c_chosen_cb(choice: TrinaryChoice, param: *mut c_void) {
@@ -329,12 +329,26 @@ pub fn trinary_choice_create<'a>(
}
let chosen_cb_param = Box::into_raw(Box::new(chosen_callback)) as *mut c_void;
+
+ let label_left = label_left.map(|label| crate::util::str_to_cstr_vec(label).unwrap());
+ let label_middle = label_middle.map(|label| crate::util::str_to_cstr_vec(label).unwrap());
+ let label_right = label_right.map(|label| crate::util::str_to_cstr_vec(label).unwrap());
+
let component = unsafe {
bitbox02_sys::trinary_choice_create(
crate::util::str_to_cstr_vec(message).unwrap().as_ptr(), // copied in C
- crate::util::str_to_cstr_vec(label_left).unwrap().as_ptr(), // copied in C
- crate::util::str_to_cstr_vec(label_middle).unwrap().as_ptr(), // copied in C
- crate::util::str_to_cstr_vec(label_right).unwrap().as_ptr(), // copied in C
+ // copied in C
+ label_left
+ .as_ref()
+ .map_or_else(core::ptr::null, |label| label.as_ptr()),
+ // copied in C
+ label_middle
+ .as_ref()
+ .map_or_else(core::ptr::null, |label| label.as_ptr()),
+ // copied in C
+ label_right
+ .as_ref()
+ .map_or_else(core::ptr::null, |label| label.as_ptr()),
Some(c_chosen_cb as _),
chosen_cb_param,
core::ptr::null_mut(), // parent component, there is no parent.
diff --git a/src/rust/bitbox02/src/ui/ui_stub.rs b/src/rust/bitbox02/src/ui/ui_stub.rs
index 0321ed2..d683d51 100644
--- a/src/rust/bitbox02/src/ui/ui_stub.rs
+++ b/src/rust/bitbox02/src/ui/ui_stub.rs
@@ -90,9 +90,9 @@ pub fn menu_create(_params: MenuParams<'_>) -> Component<'_> {
pub fn trinary_choice_create<'a>(
_message: &'a str,
- _label_left: &'a str,
- _label_middle: &'a str,
- _label_right: &'a str,
+ _label_left: Option<&'a str>,
+ _label_middle: Option<&'a str>,
+ _label_right: Option<&'a str>,
_chosen_callback: TrinaryChoiceCb,
) -> Component<'a> {
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 046bf82..acc76c5 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
@@ -117,9 +117,9 @@ pub fn menu_create(_params: MenuParams<'_>) -> Component<'_> {
pub fn trinary_choice_create<'a>(
_message: &'a str,
- _label_left: &'a str,
- _label_middle: &'a str,
- _label_right: &'a str,
+ _label_left: Option<&'a str>,
+ _label_middle: Option<&'a str>,
+ _label_right: Option<&'a str>,
_chosen_callback: TrinaryChoiceCb,
) -> Component<'a> {
panic!("not implemented")
diff --git a/src/ui/components/trinary_choice.c b/src/ui/components/trinary_choice.c
index 357e678..88d65cb 100644
--- a/src/ui/components/trinary_choice.c
+++ b/src/ui/components/trinary_choice.c
@@ -92,23 +92,27 @@ component_t* trinary_choice_create(
ui_util_add_sub_component(component, label_create(message, NULL, CENTER, component));
}
- component_t* button_left =
- button_create(label_left, bottom_slider, 0, _left_selected, component);
- ui_util_add_sub_component(component, button_left);
-
- component_t* button_middle =
- button_create(label_middle, bottom_slider, 0, _middle_selected, component);
- ui_util_add_sub_component(component, button_middle);
-
- component_t* button_right =
- button_create(label_right, bottom_slider, 0, _right_selected, component);
- ui_util_add_sub_component(component, button_right);
+ if (label_left != NULL) {
+ component_t* button_left =
+ button_create(label_left, bottom_slider, 0, _left_selected, component);
+ ui_util_add_sub_component(component, button_left);
+ ui_util_position_left_bottom_offset(component, button_left, 0, 0);
+ }
- ui_util_position_left_bottom_offset(component, button_left, 0, 0);
- ui_util_position_left_bottom_offset(
- component, button_middle, SCREEN_WIDTH / 2 - button_middle->dimension.width / 2, 0);
- ui_util_position_left_bottom_offset(
- component, button_right, SCREEN_WIDTH - button_right->dimension.width, 0);
+ if (label_middle != NULL) {
+ component_t* button_middle =
+ button_create(label_middle, bottom_slider, 0, _middle_selected, component);
+ ui_util_add_sub_component(component, button_middle);
+ ui_util_position_left_bottom_offset(
+ component, button_middle, SCREEN_WIDTH / 2 - button_middle->dimension.width / 2, 0);
+ }
+ if (label_right != NULL) {
+ component_t* button_right =
+ button_create(label_right, bottom_slider, 0, _right_selected, component);
+ ui_util_add_sub_component(component, button_right);
+ ui_util_position_left_bottom_offset(
+ component, button_right, SCREEN_WIDTH - button_right->dimension.width, 0);
+ }
return component;
}
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.