Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

feat: improve near amount display #41

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
7 changes: 0 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ include_gif = "1.0.1"
hex = { version = "0.4.3", default-features = false, features = ["serde"] }
bs58 = { version = "0.5.0", default-features = false }
numtoa = "0.2.4"
dtoa = "1.0.9"

[profile.release]
opt-level = 'z'
Expand Down
18 changes: 8 additions & 10 deletions src/app_ui/sign/common/action/function_call_common.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
parsing::{self, types::ONE_NEAR},
utils::types::elipsis_fields::ElipsisFields,
parsing::{self},
utils::types::{elipsis_fields::ElipsisFields, fmt_buffer::FmtBuffer},
};
use ledger_device_sdk::ui::gadgets::Field;
use numtoa::NumToA;
Expand All @@ -10,15 +10,15 @@ use crate::app_ui::fields_writer::FieldsWriter;
pub struct FieldsContext {
pub method_name_display_buf: [u8; 20],
pub gas_buf: [u8; 20],
pub float_buffer: dtoa::Buffer,
pub deposit_buffer: FmtBuffer<30>,
}

impl FieldsContext {
pub fn new() -> Self {
Self {
method_name_display_buf: [0u8; 20],
gas_buf: [0u8; 20],
float_buffer: dtoa::Buffer::new(),
deposit_buffer: FmtBuffer::new(),
}
}
}
Expand Down Expand Up @@ -49,12 +49,10 @@ pub fn format<'b, 'a: 'b, const N: usize>(
}))
.unwrap();

let deposit_amount = (func_call_common.deposit as f64) / (ONE_NEAR as f64);
let printed_amount = field_context.float_buffer.format(deposit_amount);
func_call_common
.deposit
.display_as_buffer(&mut field_context.deposit_buffer);
writer
.push_fields(ElipsisFields::one(Field {
name: "Deposit (NEAR)",
value: printed_amount,
}))
.push_fields(field_context.deposit_buffer.ui_field("Deposit"))
.unwrap();
}
15 changes: 5 additions & 10 deletions src/app_ui/sign/common/action/function_call_permission.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
app_ui::fields_writer::FieldsWriter,
parsing::{self, types::ONE_NEAR},
parsing,
utils::types::{elipsis_fields::ElipsisFields, fmt_buffer::FmtBuffer},
};

Expand All @@ -11,7 +11,7 @@ pub struct FieldsContext {
pub num_buf: [u8; 10],
pub receiver_display_buf: [u8; 20],
pub method_names_display_buf: [u8; 20],
pub allowance_str: FmtBuffer<30>,
pub allowance_buffer: FmtBuffer<30>,
}

impl FieldsContext {
Expand All @@ -20,7 +20,7 @@ impl FieldsContext {
num_buf: [0u8; 10],
receiver_display_buf: [0u8; 20],
method_names_display_buf: [0u8; 20],
allowance_str: FmtBuffer::new(),
allowance_buffer: FmtBuffer::new(),
}
}
}
Expand All @@ -32,13 +32,8 @@ pub fn format<'b, 'a: 'b>(
) {
let allowance = match function_call_perm.allowance {
Some(allowance) => {
let mut float_buffer = dtoa::Buffer::new();
let allowance = (allowance as f64) / (ONE_NEAR as f64);
field_context
.allowance_str
.write_str(float_buffer.format(allowance));
field_context.allowance_str.write_str(" NEAR");
field_context.allowance_str.as_str()
allowance.display_as_buffer(&mut field_context.allowance_buffer);
field_context.allowance_buffer.as_str()
}
None => "Unlimited NEAR",
};
Expand Down
18 changes: 8 additions & 10 deletions src/app_ui/sign/common/action/stake.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
use crate::{
parsing::{self, types::ONE_NEAR},
parsing::{self},
sign_ui::common::tx_public_key_context,
utils::types::elipsis_fields::ElipsisFields,
utils::types::{elipsis_fields::ElipsisFields, fmt_buffer::FmtBuffer},
};
use ledger_device_sdk::ui::gadgets::Field;

use crate::app_ui::fields_writer::FieldsWriter;

pub struct FieldsContext {
pub float_buffer: dtoa::Buffer,
pub stake_buffer: FmtBuffer<30>,
pub pub_key_context: tx_public_key_context::FieldsContext,
}

impl FieldsContext {
pub fn new() -> Self {
Self {
float_buffer: dtoa::Buffer::new(),
stake_buffer: FmtBuffer::new(),
pub_key_context: tx_public_key_context::FieldsContext::new(),
}
}
Expand All @@ -26,8 +26,6 @@ pub fn format<'b, 'a: 'b>(
field_context: &'a mut FieldsContext,
writer: &'_ mut FieldsWriter<'b, 3>,
) {
let stake_amount = (stake.stake as f64) / (ONE_NEAR as f64);
let printed_amount = field_context.float_buffer.format(stake_amount);
field_context
.pub_key_context
.format_public_key(&stake.public_key);
Expand All @@ -38,11 +36,11 @@ pub fn format<'b, 'a: 'b>(
}))
.unwrap();

stake
.stake
.display_as_buffer(&mut field_context.stake_buffer);
writer
.push_fields(ElipsisFields::one(Field {
name: "Stake (NEAR)",
value: printed_amount,
}))
.push_fields(field_context.stake_buffer.ui_field("Stake"))
.unwrap();

writer
Expand Down
19 changes: 9 additions & 10 deletions src/app_ui/sign/common/action/transfer.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use crate::{
parsing::{self, types::ONE_NEAR},
utils::types::elipsis_fields::ElipsisFields,
parsing::{self},
utils::types::{elipsis_fields::ElipsisFields, fmt_buffer::FmtBuffer},
};
use ledger_device_sdk::ui::gadgets::Field;

use crate::app_ui::fields_writer::FieldsWriter;

pub struct FieldsContext {
pub float_buffer: dtoa::Buffer,
pub amount_buffer: FmtBuffer<30>,
}

impl FieldsContext {
pub fn new() -> Self {
Self {
float_buffer: dtoa::Buffer::new(),
amount_buffer: FmtBuffer::new(),
}
}
}
Expand All @@ -30,12 +30,11 @@ pub fn format<'b, 'a: 'b>(
}))
.unwrap();

let deposit = (transfer.deposit as f64) / (ONE_NEAR as f64);
let printed = field_context.float_buffer.format(deposit);
transfer
.deposit
.display_as_buffer(&mut field_context.amount_buffer);

writer
.push_fields(ElipsisFields::one(Field {
name: "Amount (NEAR)",
value: printed,
}))
.push_fields(field_context.amount_buffer.ui_field("Amount"))
.unwrap();
}
3 changes: 0 additions & 3 deletions src/app_ui/sign/nep413/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ fn format<'b, 'a: 'b>(
}
}
pub fn ui_display(payload: &Payload) -> bool {
#[cfg(feature = "speculos")]
payload.debug_print();

let mut field_writer: FieldsWriter<'_, 7> = FieldsWriter::new();
let mut field_context: FieldsContext = FieldsContext::new();
format(payload, &mut field_context, &mut field_writer);
Expand Down
3 changes: 0 additions & 3 deletions src/app_ui/sign/transaction/prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ fn format<'b, 'a: 'b>(
.unwrap();
}
pub fn ui_display(prefix: &parsing::types::transaction::prefix::Prefix) -> bool {
#[cfg(feature = "speculos")]
prefix.debug_print();

let mut field_writer: FieldsWriter<'_, 5> = FieldsWriter::new();
let mut field_context: FieldsContext = FieldsContext::new();
format(prefix, &mut field_context, &mut field_writer);
Expand Down
6 changes: 0 additions & 6 deletions src/handlers/common/action/add_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ pub fn handle(
) -> Result<(), AppSW> {
let add_key_common = AddKey::deserialize_reader(stream).map_err(|_err| AppSW::TxParsingFail)?;

#[cfg(feature = "speculos")]
add_key_common.debug_print();

match add_key_common.access_key.permission {
AccessKeyPermission::FunctionCall => handle_function_call(&add_key_common, stream, params),
AccessKeyPermission::FullAccess => {
Expand All @@ -35,9 +32,6 @@ pub fn handle_function_call(
) -> Result<(), AppSW> {
let mut function_call_perm = FunctionCallPermission::new();

#[cfg(feature = "speculos")]
function_call_perm.debug_print();

function_call_perm
.deserialize_reader_in_place(stream)
.map_err(|_err| AppSW::TxParsingFail)?;
Expand Down
3 changes: 0 additions & 3 deletions src/handlers/common/action/create_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ pub fn handle(
let create_account =
CreateAccount::deserialize_reader(stream).map_err(|_err| AppSW::TxParsingFail)?;

#[cfg(feature = "speculos")]
create_account.debug_print();

if !sign_ui::action::ui_display_create_account(&create_account, params) {
return Err(AppSW::Deny);
}
Expand Down
3 changes: 0 additions & 3 deletions src/handlers/common/action/delete_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ pub fn handle(
.deserialize_reader_in_place(stream)
.map_err(|_err| AppSW::TxParsingFail)?;

#[cfg(feature = "speculos")]
delete_account.debug_print();

if !sign_ui::action::ui_display_delete_account(&delete_account, params) {
return Err(AppSW::Deny);
}
Expand Down
3 changes: 0 additions & 3 deletions src/handlers/common/action/delete_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ pub fn handle(
) -> Result<(), AppSW> {
let delete_key = DeleteKey::deserialize_reader(stream).map_err(|_err| AppSW::TxParsingFail)?;

#[cfg(feature = "speculos")]
delete_key.debug_print();

if !sign_ui::action::ui_display_delete_key(&delete_key, params) {
return Err(AppSW::Deny);
}
Expand Down
3 changes: 0 additions & 3 deletions src/handlers/common/action/deploy_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ pub fn handle(
let deploy_contract =
DeployContract::deserialize_reader(stream).map_err(|_err| AppSW::TxParsingFail)?;

#[cfg(feature = "speculos")]
deploy_contract.debug_print();

if !sign_ui::action::ui_display_deploy_contract(&deploy_contract, params) {
return Err(AppSW::Deny);
}
Expand Down
3 changes: 0 additions & 3 deletions src/handlers/common/action/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ pub fn handle(
) -> Result<(), AppSW> {
let stake = Stake::deserialize_reader(stream).map_err(|_err| AppSW::TxParsingFail)?;

#[cfg(feature = "speculos")]
stake.debug_print();

if !sign_ui::action::ui_display_stake(&stake, params) {
return Err(AppSW::Deny);
}
Expand Down
3 changes: 0 additions & 3 deletions src/handlers/common/action/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ pub fn handle(
) -> Result<(), AppSW> {
let transfer = Transfer::deserialize_reader(stream).map_err(|_err| AppSW::TxParsingFail)?;

#[cfg(feature = "speculos")]
transfer.debug_print();

if !sign_ui::action::ui_display_transfer(&transfer, params) {
return Err(AppSW::Deny);
}
Expand Down
6 changes: 0 additions & 6 deletions src/handlers/get_public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,12 @@ pub fn handler(comm: &mut Comm, display: bool) -> Result<(), AppSW> {
let data = comm.get_data().map_err(|_| AppSW::WrongApduLength)?;
let path = crypto::PathBip32::parse(data).map_err(|_| AppSW::Bip32PathParsingFail)?;

#[cfg(feature = "speculos")]
path.debug_print();

let pk = crypto::bip32_derive(&path.0)
.public_key()
.map_err(|_| AppSW::KeyDeriveFail)?;

let pk = crypto::PublicKeyBe::from_little_endian(pk);

#[cfg(feature = "speculos")]
pk.debug_print()?;

if display && !address::ui_display_pk_base58(&pk)? {
return Err(AppSW::Deny);
}
Expand Down
6 changes: 0 additions & 6 deletions src/handlers/get_wallet_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,12 @@ pub fn handler(comm: &mut Comm) -> Result<(), AppSW> {
let data = comm.get_data().map_err(|_| AppSW::WrongApduLength)?;
let path = crypto::PathBip32::parse(data).map_err(|_| AppSW::Bip32PathParsingFail)?;

#[cfg(feature = "speculos")]
path.debug_print();

let pk = crypto::bip32_derive(&path.0)
.public_key()
.map_err(|_| AppSW::KeyDeriveFail)?;

let pk = crypto::PublicKeyBe::from_little_endian(pk);

#[cfg(feature = "speculos")]
pk.debug_print()?;

if !address::ui_display_hex(&pk)? {
return Err(AppSW::Deny);
}
Expand Down
3 changes: 0 additions & 3 deletions src/handlers/sign_nep366_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ pub fn handler(mut stream: SingleTxStream<'_>) -> Result<Signature, AppSW> {
let path = <crypto::PathBip32 as BorshDeserialize>::deserialize_reader(&mut stream)
.map_err(|_| AppSW::Bip32PathParsingFail)?;

#[cfg(feature = "speculos")]
path.debug_print();

let mut stream = HashingStream::new(stream)?;

let msg_discriminant = MessageDiscriminant::new_on_chain(NEP_366_META_TRANSACTIONS).unwrap();
Expand Down
3 changes: 0 additions & 3 deletions src/handlers/sign_nep413_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ pub fn handler(mut stream: SingleTxStream<'_>) -> Result<Signature, AppSW> {
let path = <crypto::PathBip32 as BorshDeserialize>::deserialize_reader(&mut stream)
.map_err(|_| AppSW::Bip32PathParsingFail)?;

#[cfg(feature = "speculos")]
path.debug_print();

let mut stream = HashingStream::new(stream)?;

let msg_discriminant = MessageDiscriminant::new_off_chain(413).unwrap();
Expand Down
Loading
Loading