diff --git a/src/app_ui/sign.rs b/src/app_ui/sign.rs index 80e831a8..a7ada44d 100644 --- a/src/app_ui/sign.rs +++ b/src/app_ui/sign.rs @@ -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 { // 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()) } diff --git a/src/handlers/sign_tx.rs b/src/handlers/sign_tx.rs index 70bbac2c..b232f520 100644 --- a/src/handlers/sign_tx.rs +++ b/src/handlers/sign_tx.rs @@ -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);