Skip to content

Commit 5b75afd

Browse files
committed
workflows: add menu and trinary_choice to the Workflows trait
1 parent 01b624c commit 5b75afd

File tree

4 files changed

+86
-33
lines changed

4 files changed

+86
-33
lines changed

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

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ pub async fn process<W: Workflows>(
4646
/// Derives and displays a BIP-39 seed according to BIP-85:
4747
/// https://github.com/bitcoin/bips/blob/master/bip-0085.mediawiki#bip39.
4848
async fn process_bip39<W: Workflows>(workflows: &mut W) -> Result<(), Error> {
49-
use crate::workflow::trinary_choice::{choose, TrinaryChoice};
50-
use crate::workflow::{menu, mnemonic, trinary_input_string};
49+
use crate::workflow::trinary_choice::TrinaryChoice;
50+
use crate::workflow::{mnemonic, trinary_input_string};
5151

5252
workflows
5353
.confirm(&confirm::Params {
@@ -68,7 +68,10 @@ async fn process_bip39<W: Workflows>(workflows: &mut W) -> Result<(), Error> {
6868
})
6969
.await?;
7070

71-
let num_words: u32 = match choose("How many words?", "12", "18", "24").await {
71+
let num_words: u32 = match workflows
72+
.trinary_choice("How many words?", "12", "18", "24")
73+
.await
74+
{
7275
TrinaryChoice::TRINARY_CHOICE_LEFT => 12,
7376
TrinaryChoice::TRINARY_CHOICE_MIDDLE => 18,
7477
TrinaryChoice::TRINARY_CHOICE_RIGHT => 24,
@@ -79,32 +82,34 @@ async fn process_bip39<W: Workflows>(workflows: &mut W) -> Result<(), Error> {
7982
.await;
8083

8184
// Pick index. The first few are quick-access. "More" leads to a full number input keyboard.
82-
let index: u32 =
83-
match menu::pick(&["0", "1", "2", "3", "4", "More"], Some("Select index")).await? {
84-
i @ 0..=4 => i.into(),
85-
5 => {
86-
let number_string = workflows
87-
.enter_string(
88-
&trinary_input_string::Params {
89-
title: "Enter index",
90-
number_input: true,
91-
longtouch: true,
92-
..Default::default()
93-
},
94-
trinary_input_string::CanCancel::Yes,
95-
"",
96-
)
97-
.await?;
98-
match number_string.as_str().parse::<u32>() {
99-
Ok(i) if i < util::bip32::HARDENED => i,
100-
_ => {
101-
workflows.status("Invalid index", false).await;
102-
return Err(Error::InvalidInput);
103-
}
85+
let index: u32 = match workflows
86+
.menu(&["0", "1", "2", "3", "4", "More"], Some("Select index"))
87+
.await?
88+
{
89+
i @ 0..=4 => i.into(),
90+
5 => {
91+
let number_string = workflows
92+
.enter_string(
93+
&trinary_input_string::Params {
94+
title: "Enter index",
95+
number_input: true,
96+
longtouch: true,
97+
..Default::default()
98+
},
99+
trinary_input_string::CanCancel::Yes,
100+
"",
101+
)
102+
.await?;
103+
match number_string.as_str().parse::<u32>() {
104+
Ok(i) if i < util::bip32::HARDENED => i,
105+
_ => {
106+
workflows.status("Invalid index", false).await;
107+
return Err(Error::InvalidInput);
104108
}
105109
}
106-
6.. => panic!("bip85 error"),
107-
};
110+
}
111+
6.. => panic!("bip85 error"),
112+
};
108113

109114
workflows.status(&format!("Index: {}", index), true).await;
110115

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ pub trait Workflows {
5858
) -> Result<zeroize::Zeroizing<String>, trinary_input_string::Error>;
5959

6060
async fn insert_sdcard(&mut self) -> Result<(), sdcard::UserAbort>;
61+
62+
async fn menu(&mut self, words: &[&str], title: Option<&str>) -> Result<u8, menu::CancelError>;
63+
64+
async fn trinary_choice(
65+
&mut self,
66+
message: &str,
67+
label_left: &str,
68+
label_middle: &str,
69+
label_right: &str,
70+
) -> trinary_choice::TrinaryChoice;
6171
}
6272

6373
pub struct RealWorkflows;
@@ -106,4 +116,20 @@ impl Workflows for RealWorkflows {
106116
async fn insert_sdcard(&mut self) -> Result<(), sdcard::UserAbort> {
107117
sdcard::sdcard().await
108118
}
119+
120+
#[inline(always)]
121+
async fn menu(&mut self, words: &[&str], title: Option<&str>) -> Result<u8, menu::CancelError> {
122+
menu::pick(words, title).await
123+
}
124+
125+
#[inline(always)]
126+
async fn trinary_choice(
127+
&mut self,
128+
message: &str,
129+
label_left: &str,
130+
label_middle: &str,
131+
label_right: &str,
132+
) -> trinary_choice::TrinaryChoice {
133+
trinary_choice::choose(message, label_left, label_middle, label_right).await
134+
}
109135
}

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub use super::cancel::Error as CancelError;
1616
use super::cancel::{cancel, set_result, with_cancel};
1717
use super::confirm;
1818
use super::menu;
19-
use super::trinary_choice::{choose, TrinaryChoice};
19+
use super::trinary_choice::TrinaryChoice;
2020
use super::trinary_input_string;
2121
use super::Workflows;
2222

@@ -224,8 +224,8 @@ async fn get_24th_word<W: Workflows>(
224224
choices.len() - 1
225225
};
226226
loop {
227-
match super::menu::pick(&as_str_vec(&choices), Some(title)).await {
228-
Err(super::menu::CancelError::Cancelled) => return Err(CancelError::Cancelled),
227+
match workflows.menu(&as_str_vec(&choices), Some(title)).await {
228+
Err(menu::CancelError::Cancelled) => return Err(CancelError::Cancelled),
229229
Ok(choice_idx) if choice_idx as usize == none_of_them_idx => {
230230
let params = confirm::Params {
231231
title: "",
@@ -301,7 +301,10 @@ async fn get_12th_18th_word<W: Workflows>(
301301
pub async fn get<W: Workflows>(
302302
workflows: &mut W,
303303
) -> Result<zeroize::Zeroizing<String>, CancelError> {
304-
let num_words: usize = match choose("How many words?", "12", "18", "24").await {
304+
let num_words: usize = match workflows
305+
.trinary_choice("How many words?", "12", "18", "24")
306+
.await
307+
{
305308
TrinaryChoice::TRINARY_CHOICE_LEFT => 12,
306309
TrinaryChoice::TRINARY_CHOICE_MIDDLE => 18,
307310
TrinaryChoice::TRINARY_CHOICE_RIGHT => 24,
@@ -378,7 +381,8 @@ pub async fn get<W: Workflows>(
378381
} else {
379382
// In all other words, we give the choice between editing the previous word and
380383
// cancelling.
381-
match menu::pick(&["Edit previous word", "Cancel restore"], Some("Choose"))
384+
match workflows
385+
.menu(&["Edit previous word", "Cancel restore"], Some("Choose"))
382386
.await
383387
{
384388
Err(menu::CancelError::Cancelled) => {

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use super::{confirm, sdcard, transaction, trinary_input_string, Workflows};
15+
use super::{confirm, menu, sdcard, transaction, trinary_choice, trinary_input_string, Workflows};
1616

1717
use alloc::boxed::Box;
1818
use alloc::string::String;
@@ -137,6 +137,24 @@ impl Workflows for TestingWorkflows<'_> {
137137
async fn insert_sdcard(&mut self) -> Result<(), sdcard::UserAbort> {
138138
Ok(())
139139
}
140+
141+
async fn menu(
142+
&mut self,
143+
_words: &[&str],
144+
_title: Option<&str>,
145+
) -> Result<u8, menu::CancelError> {
146+
todo!("not used in unit tests yet");
147+
}
148+
149+
async fn trinary_choice(
150+
&mut self,
151+
_message: &str,
152+
_label_left: &str,
153+
_label_middle: &str,
154+
_label_right: &str,
155+
) -> trinary_choice::TrinaryChoice {
156+
todo!("not used in unit tests yet");
157+
}
140158
}
141159

142160
impl<'a> TestingWorkflows<'a> {

0 commit comments

Comments
 (0)