Skip to content

Commit

Permalink
add transaction cancelled message
Browse files Browse the repository at this point in the history
  • Loading branch information
ryankurte committed Mar 21, 2023
1 parent 2e1ca93 commit d2492b2
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 21 deletions.
18 changes: 12 additions & 6 deletions fw/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ fn handle_btn<RNG: RngCore + CryptoRng>(
}
})
}
UiState::Complete(ref mut a) => {
UiState::Message(ref mut a) => {
a.update(btn).map_exit(|_| {
// Reset engine on exit
// Reset engine on message clear
engine.reset()
})
}
Expand All @@ -215,7 +215,7 @@ fn handle_btn<RNG: RngCore + CryptoRng>(
UiState::KeyRequest(..)
| UiState::TxRequest(..)
| UiState::Progress(..)
| UiState::Complete(..)
| UiState::Message(..)
if r.is_exit() =>
{
ui.state = UiState::Menu;
Expand Down Expand Up @@ -352,9 +352,15 @@ fn handle_apdu<RNG: RngCore + CryptoRng>(
render = true;
}

// Update to complete when transaction is complete
State::Complete if !ui.state.is_complete() => {
ui.state = UiState::Complete(Complete::new());
// Set complete message when transaction is complete
State::Complete if !ui.state.is_message() => {
ui.state = UiState::Message(Message::new("Transaction Complete"));
render = true;
}

// Set cancelled message when transaction is aborted
State::Deny if !ui.state.is_message() => {
ui.state = UiState::Message(Message::new("Transaction Cancelled"));
render = true;
}

Expand Down
12 changes: 7 additions & 5 deletions fw/src/ui/complete.rs → fw/src/ui/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ use ledger_mob_core::engine::{Driver, Engine};
use super::{clear_screen, UiResult};

#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Complete;
pub struct Message {
value: &'static str,
}

impl Complete {
pub fn new() -> Self {
Self
impl Message {
pub fn new(value: &'static str) -> Self {
Self { value }
}

pub fn update(&mut self, btn: &ButtonEvent) -> UiResult<bool> {
Expand All @@ -35,7 +37,7 @@ impl Complete {
clear_screen();

// Render transaction information
"Transaction Complete".place(Location::Middle, Layout::Centered, false);
self.value.place(Location::Middle, Layout::Centered, false);

// Update screen
screen_util::screen_update();
Expand Down
14 changes: 7 additions & 7 deletions fw/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ pub use approver::*;
mod progress;
pub use progress::*;

mod complete;
pub use complete::*;
mod message;
pub use message::*;

mod tx_blind_approver;
pub use tx_blind_approver::*;
Expand Down Expand Up @@ -65,8 +65,8 @@ pub enum UiState {
/// Progress indicator
Progress(Progress),

/// Transaction complete
Complete(Complete),
/// Messages (transaction complete, rejected, etc.)
Message(Message),
}

impl UiState {
Expand All @@ -87,8 +87,8 @@ impl UiState {
matches!(self, UiState::Progress(..))
}

pub fn is_complete(&self) -> bool {
matches!(self, UiState::Complete(..))
pub fn is_message(&self) -> bool {
matches!(self, UiState::Message(..))
}

#[cfg(feature = "ident")]
Expand Down Expand Up @@ -118,7 +118,7 @@ impl Ui {
#[cfg(feature = "ident")]
UiState::IdentRequest(a) => a.render(engine),
UiState::Progress(a) => a.render(engine),
UiState::Complete(a) => a.render(engine),
UiState::Message(a) => a.render(engine),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub enum Error<E: Display + Debug> {
Transport(E),

/// Invalid transaction state
#[error("Invalid transaction state (actual: {0}, expected: {1}")]
#[error("Invalid transaction state (actual: {0}, expected: {1})")]
InvalidState(TxState, TxState),

/// Unexpected APDU response
Expand Down
7 changes: 5 additions & 2 deletions lib/src/tx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,14 @@ impl<T: Exchange + Send + Sync> TransactionHandle<T> {

debug!("awaiting tx approval (state: {:?})", r);

// Handle responses, waiting for `Ready` or `Error` states
// Handle responses, waiting for `Ready`, `Denied` or `Error` states
match r {
Ok(v) if v.state == TxState::Pending => (),
Ok(v) if v.state == TxState::Ready => return Ok(()),
Ok(v) if v.state == TxState::TxDenied => return Err(Error::UserDenied),
Ok(v) if v.state == TxState::Error => return Err(Error::Engine(0)),
_ => (),
Ok(v) => return Err(Error::InvalidState(v.state, TxState::Pending)),
Err(_) => (),
}

// Sleep while we wait
Expand Down

0 comments on commit d2492b2

Please sign in to comment.