Skip to content

Commit 465b4da

Browse files
committed
Merge branch 'ui-traits'
2 parents 67add36 + d8d7933 commit 465b4da

File tree

4 files changed

+35
-26
lines changed

4 files changed

+35
-26
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ pub mod noise;
1717

1818
use alloc::vec::Vec;
1919

20+
use crate::workflow::RealWorkflows;
21+
2022
const OP_UNLOCK: u8 = b'u';
2123
const OP_ATTESTATION: u8 = b'a';
2224

@@ -113,14 +115,16 @@ fn api_attestation(usb_in: &[u8]) -> Vec<u8> {
113115
/// `usb_in` - api request bytes.
114116
/// Returns the usb response bytes.
115117
pub async fn process_packet(usb_in: Vec<u8>) -> Vec<u8> {
118+
let workflows = &mut RealWorkflows;
119+
116120
match usb_in.split_first() {
117121
Some((&OP_UNLOCK, b"")) => return api_unlock().await,
118122
Some((&OP_ATTESTATION, rest)) => return api_attestation(rest),
119123
_ => (),
120124
}
121125

122126
let mut out = [OP_STATUS_SUCCESS].to_vec();
123-
match noise::process(usb_in, &mut out).await {
127+
match noise::process(workflows, usb_in, &mut out).await {
124128
Ok(()) => out,
125129
Err(noise::Error) => [OP_STATUS_FAILURE].to_vec(),
126130
}

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use pb::request::Request;
4646
use pb::response::Response;
4747
use prost::Message;
4848

49-
use crate::workflow::{RealWorkflows, Workflows};
49+
use crate::workflow::Workflows;
5050

5151
/// Encodes a protobuf Response message.
5252
pub fn encode(response: Response) -> Vec<u8> {
@@ -159,8 +159,10 @@ fn can_call(request: &Request) -> bool {
159159
}
160160

161161
/// Handle a protobuf api call.
162-
async fn process_api(request: &Request) -> Result<Response, Error> {
163-
let workflows = &mut RealWorkflows;
162+
async fn process_api<W: Workflows>(
163+
workflows: &mut W,
164+
request: &Request,
165+
) -> Result<Response, Error> {
164166
match request {
165167
Request::Reboot(ref request) => system::reboot(request).await,
166168
Request::DeviceInfo(_) => device_info::process(),
@@ -213,7 +215,7 @@ async fn process_api(request: &Request) -> Result<Response, Error> {
213215
///
214216
/// `input` is a hww.proto Request message, protobuf encoded.
215217
/// Returns a protobuf encoded hww.proto Response message.
216-
pub async fn process(input: Vec<u8>) -> Vec<u8> {
218+
pub async fn process<W: Workflows>(workflows: &mut W, input: Vec<u8>) -> Vec<u8> {
217219
let request = match decode(&input[..]) {
218220
Ok(request) => request,
219221
Err(err) => return encode(make_error(err)),
@@ -222,7 +224,7 @@ pub async fn process(input: Vec<u8>) -> Vec<u8> {
222224
return encode(make_error(Error::InvalidState));
223225
}
224226

225-
match process_api(&request).await {
227+
match process_api(workflows, &request).await {
226228
Ok(response) => encode(response),
227229
Err(error) => encode(make_error(error)),
228230
}

src/rust/bitbox02-rust/src/hww/noise.rs

Lines changed: 8 additions & 4 deletions
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 crate::workflow::pairing;
15+
use crate::workflow::{pairing, Workflows};
1616
use alloc::vec::Vec;
1717
use bitbox02::memory;
1818
use core::cell::RefCell;
@@ -75,7 +75,11 @@ pub fn decrypt(msg: &[u8]) -> Result<Vec<u8>, Error> {
7575
/// Returns Err if anything goes wrong:
7676
/// - Invalid OP-code
7777
/// - Noise message in the wrong state (e.g. handshake before init, etc.).
78-
pub(crate) async fn process(usb_in: Vec<u8>, usb_out: &mut Vec<u8>) -> Result<(), Error> {
78+
pub(crate) async fn process<W: Workflows>(
79+
workflows: &mut W,
80+
usb_in: Vec<u8>,
81+
usb_out: &mut Vec<u8>,
82+
) -> Result<(), Error> {
7983
match usb_in.split_first() {
8084
Some((&OP_I_CAN_HAS_HANDSHAEK, b"")) => {
8185
// The previous screen was "See the BitBoxApp".
@@ -117,7 +121,7 @@ pub(crate) async fn process(usb_in: Vec<u8>, usb_out: &mut Vec<u8>) -> Result<()
117121
let state = NOISE_STATE.0.borrow();
118122
state.get_handshake_hash()?
119123
};
120-
match pairing::confirm(&hash).await {
124+
match pairing::confirm(workflows, &hash).await {
121125
Ok(()) => {
122126
let mut state = NOISE_STATE.0.borrow_mut();
123127
state.set_pairing_verified()?;
@@ -138,7 +142,7 @@ pub(crate) async fn process(usb_in: Vec<u8>, usb_out: &mut Vec<u8>) -> Result<()
138142
}
139143
Some((&OP_NOISE_MSG, encrypted_msg)) => {
140144
let decrypted_msg = decrypt(encrypted_msg)?;
141-
let response = super::api::process(decrypted_msg).await;
145+
let response = super::api::process(workflows, decrypted_msg).await;
142146
encrypt(&response, usb_out)?;
143147
Ok(())
144148
}

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

Lines changed: 15 additions & 16 deletions
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 crate::workflow::confirm;
15+
use crate::workflow::{confirm, Workflows};
1616
pub use confirm::UserAbort;
1717

1818
use alloc::string::String;
@@ -32,43 +32,42 @@ pub fn format_hash(hash: &[u8; 32]) -> String {
3232
)
3333
}
3434

35-
pub async fn confirm(hash: &[u8; 32]) -> Result<(), UserAbort> {
35+
pub async fn confirm<W: Workflows>(workflows: &mut W, hash: &[u8; 32]) -> Result<(), UserAbort> {
3636
let params = confirm::Params {
3737
title: "Pairing code",
3838
body: &format_hash(hash),
3939
font: confirm::Font::Monogram5X9,
4040
..Default::default()
4141
};
4242

43-
confirm::confirm(&params).await
43+
workflows.confirm(&params).await
4444
}
4545

4646
#[cfg(test)]
4747
mod tests {
4848
use super::*;
4949

5050
use crate::bb02_async::block_on;
51+
use crate::workflow::testing::{Screen, TestingWorkflows};
5152
use bitbox02::testing::{mock, Data};
5253

5354
use alloc::boxed::Box;
5455

5556
#[test]
5657
fn test_confirm() {
57-
static mut CONFIRMED: bool = false;
58-
mock(Data {
59-
ui_confirm_create: Some(Box::new(|params| {
60-
assert_eq!(params.title, "Pairing code");
61-
assert_eq!(params.body, "LEUJX W53W2\n3I5DY SP5E2");
62-
unsafe {
63-
CONFIRMED = true;
64-
}
65-
true
66-
})),
58+
let mut mock_workflows = TestingWorkflows::new();
6759

68-
..Default::default()
69-
});
7060
assert!(block_on(confirm(
61+
&mut mock_workflows,
7162
b"\x59\x28\x9b\xdb\xbb\xb6\xb6\x8e\x8f\x12\x7f\x49\xa5\x25\xb0\x30\x13\x50\x0b\x3c\x1a\xf2\x62\x6f\x40\x07\xeb\xe4\x4f\x09\xc8\x6b")).is_ok());
72-
assert!(unsafe { CONFIRMED });
63+
64+
assert_eq!(
65+
mock_workflows.screens,
66+
vec![Screen::Confirm {
67+
title: "Pairing code".into(),
68+
body: "LEUJX W53W2\n3I5DY SP5E2".into(),
69+
longtouch: false,
70+
},]
71+
);
7372
}
7473
}

0 commit comments

Comments
 (0)