Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
agrojean-ledger committed Nov 15, 2023
1 parent b134372 commit b57d5bd
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 17 deletions.
10 changes: 3 additions & 7 deletions src/app_ui/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,13 @@ use nanos_ui::ui::{Field, MultiFieldReview};
const DISPLAY_ADDR_BYTES_LEN: usize = 20;

pub fn ui_display_pk(addr: &[u8]) -> Result<bool, Reply> {
let addr_hex_str_buf =
to_hex_all_caps(&addr[addr.len() - DISPLAY_ADDR_BYTES_LEN as usize..])
.map_err(|_| Reply(SW_DISPLAY_ADDRESS_FAIL))?;
let addr_hex_str_buf = to_hex_all_caps(&addr[addr.len() - DISPLAY_ADDR_BYTES_LEN as usize..])
.map_err(|_| Reply(SW_DISPLAY_ADDRESS_FAIL))?;
let addr_hex_str = from_utf8(&addr_hex_str_buf[..DISPLAY_ADDR_BYTES_LEN * 2])
.map_err(|_| Reply(SW_DISPLAY_ADDRESS_FAIL))?;

let mut addr_hex_str_with_prefix_buf = [0u8; DISPLAY_ADDR_BYTES_LEN * 2 + 2];
concatenate(
&["0x", &addr_hex_str],
&mut addr_hex_str_with_prefix_buf,
);
concatenate(&["0x", &addr_hex_str], &mut addr_hex_str_with_prefix_buf);
let addr_hex_str_with_prefix =
from_utf8(&addr_hex_str_with_prefix_buf).map_err(|_| Reply(SW_DISPLAY_ADDRESS_FAIL))?;

Expand Down
3 changes: 2 additions & 1 deletion src/app_ui/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ pub fn ui_display_tx(tx: &Tx) -> Result<bool, Reply> {
from_utf8(&addr_with_prefix_buf).map_err(|_| Reply(SW_TX_DISPLAY_FAIL))?;

// Format memo
let memo_str = from_utf8(&tx.memo[..tx.memo_len as usize]).map_err(|_| Reply(SW_TX_DISPLAY_FAIL))?;
let memo_str =
from_utf8(&tx.memo[..tx.memo_len as usize]).map_err(|_| Reply(SW_TX_DISPLAY_FAIL))?;

// Define transaction review fields
let my_fields = [
Expand Down
10 changes: 5 additions & 5 deletions src/handlers/sign_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*****************************************************************************/
use crate::app_ui::sign::ui_display_tx;
use crate::utils::{read_bip32_path, varint_read, slice_or_err, MAX_ALLOWED_PATH_LEN};
use crate::utils::{read_bip32_path, slice_or_err, varint_read, MAX_ALLOWED_PATH_LEN};
use crate::{SW_DENY, SW_TX_HASH_FAIL, SW_TX_PARSING_FAIL, SW_TX_SIGN_FAIL, SW_WRONG_TX_LENGTH};
use nanos_sdk::bindings::{
cx_hash_no_throw, cx_hash_t, cx_keccak_init_no_throw, cx_sha3_t, CX_LAST, CX_OK,
Expand Down Expand Up @@ -50,15 +50,15 @@ impl<'a> TryFrom<&'a [u8]> for Tx<'a> {
let value = u64::from_be_bytes(slice_or_err(raw_tx, 28, 8)?.try_into().map_err(|_| ())?);
// Memo length
let (memo_len_u64, memo_len_size) = varint_read(&raw_tx[36..])?;
let memo_len = memo_len_u64 as usize;
let memo_len = memo_len_u64 as usize;
// Memo
let memo = slice_or_err(raw_tx, 36 + memo_len_size, memo_len)?;

// Check memo ASCII encoding
if !memo[..memo_len].iter().all(|&byte| byte.is_ascii()) {
return Err(());
}

Ok(Tx {
nonce,
value,
Expand Down Expand Up @@ -148,7 +148,7 @@ fn compute_signature_and_append(comm: &mut Comm, ctx: &mut TxContext) -> Result<
let mut message_hash: [u8; 32] = [0u8; 32];

unsafe {
if cx_keccak_init_no_throw(&mut keccak256, 256) != CX_OK {
if cx_keccak_init_no_throw(&mut keccak256, 256) != CX_OK {
return Err(Reply(SW_TX_HASH_FAIL));
}
if cx_hash_no_throw(
Expand Down
8 changes: 4 additions & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ pub fn slice_or_err(slice: &[u8], start: usize, len: usize) -> Result<&[u8], ()>
}

/// Read a varint from a slice
pub fn varint_read(input: &[u8]) -> Result<(u64, usize), ()> {
pub fn varint_read(input: &[u8]) -> Result<(u64, usize), ()> {
let mut bytes = [0u8; 8];
let int_length: usize;

if input.is_empty() {
return Err(());
}

let prefix = input[0];

if prefix == 0xFD {
Expand All @@ -122,11 +122,11 @@ pub fn varint_read(input: &[u8]) -> Result<(u64, usize), ()> {
}
int_length = 8;
} else {
return Ok((u64::from(prefix), 1));
return Ok((u64::from(prefix), 1));
}

let buf = slice_or_err(input, 1, int_length)?;
bytes[..int_length].copy_from_slice(buf);
let result = u64::from_le_bytes(bytes);
Ok((result, int_length + 1))
}
}

0 comments on commit b57d5bd

Please sign in to comment.