Skip to content

Commit

Permalink
Fixed error management in ui_display_tx
Browse files Browse the repository at this point in the history
  • Loading branch information
kingofpayne committed Nov 23, 2023
1 parent 96fa745 commit 1425aa7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 38 deletions.
80 changes: 43 additions & 37 deletions src/app_ui/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,56 +19,62 @@ use core::str::from_utf8;

use crate::handlers::sign_tx::Tx;
use crate::utils::concatenate;
use crate::AppSW;
use ledger_device_ui_sdk::bitmaps::{CROSSMARK, EYE, VALIDATE_14};
use ledger_device_ui_sdk::ui::{Field, MultiFieldReview};
use numtoa::NumToA;

const MAX_COIN_LENGTH: usize = 10;

pub fn ui_display_tx(tx: &Tx) -> bool {
/// Displays a transaction and returns true if user approved it.
///
/// This method can return [`AppSW::TxDisplayFail`] error if the coin name length is too long.
///
/// # Arguments
///
/// * `tx` - Transaction to be displayed for validation
pub fn ui_display_tx(tx: &Tx) -> Result<bool, AppSW> {
// Generate string for amount
let mut numtoa_buf = [0u8; 20];
let mut value_buf = [0u8; 20 + MAX_COIN_LENGTH + 1];

if let Ok(value_str) = concatenate(
let value_str = concatenate(
&[tx.coin, &" ", tx.value.numtoa_str(10, &mut numtoa_buf)],
&mut value_buf,
) {
// Generate destination address string in hexadecimal format.
let mut to_str = [0u8; 42];
to_str[..2].copy_from_slice("0x".as_bytes());
hex::encode_to_slice(tx.to, &mut to_str[2..]).unwrap();
)
.map_err(|_| AppSW::TxDisplayFail)?; // Fails if value_buf is too small

// Define transaction review fields
let my_fields = [
Field {
name: "Amount",
value: value_str,
},
Field {
name: "Destination",
value: core::str::from_utf8(&to_str).unwrap(),
},
Field {
name: "Memo",
value: tx.memo,
},
];
// Generate destination address string in hexadecimal format.
let mut to_str = [0u8; 42];
to_str[..2].copy_from_slice("0x".as_bytes());
hex::encode_to_slice(tx.to, &mut to_str[2..]).unwrap();

// Create transaction review
let my_review = MultiFieldReview::new(
&my_fields,
&["Review ", "Transaction"],
Some(&EYE),
"Approve",
Some(&VALIDATE_14),
"Reject",
Some(&CROSSMARK),
);
// Define transaction review fields
let my_fields = [
Field {
name: "Amount",
value: value_str,
},
Field {
name: "Destination",
value: core::str::from_utf8(&to_str).unwrap(),
},
Field {
name: "Memo",
value: tx.memo,
},
];

my_review.show()
} else {
// Coin name too long, concatenation buffer was too small.
return false;
}
// Create transaction review
let my_review = MultiFieldReview::new(
&my_fields,
&["Review ", "Transaction"],
Some(&EYE),
"Approve",
Some(&VALIDATE_14),
"Reject",
Some(&CROSSMARK),
);

Ok(my_review.show())
}
2 changes: 1 addition & 1 deletion src/handlers/sign_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub fn handler_sign_tx(
// Display transaction. If user approves
// the transaction, sign it. Otherwise,
// return a "deny" status word.
if ui_display_tx(&tx) {
if ui_display_tx(&tx)? {
return compute_signature_and_append(comm, ctx);
} else {
return Err(AppSW::Deny);
Expand Down

0 comments on commit 1425aa7

Please sign in to comment.