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

Commit

Permalink
Merge pull request #54 from dj8yfo/extract_constants
Browse files Browse the repository at this point in the history
chore: Extract constants
  • Loading branch information
dj8yfo authored Apr 17, 2024
2 parents 906de2c + 53a0976 commit f3c7ec7
Show file tree
Hide file tree
Showing 32 changed files with 365 additions and 179 deletions.
16 changes: 16 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import? 'local.just'

cleanup:
#!/usr/bin/env bash
sudo rm -rf target
Expand All @@ -8,6 +10,12 @@ cleanup:
exit 3;
fi
fmt:
cargo fmt --all

unit_tests:
RUSTFLAGS='-D warnings' bash local_unit_tests.sh

build_all:
#!/usr/bin/env bash
bash local_test_helper.sh -c build_all
Expand All @@ -25,6 +33,14 @@ pull_dev_images:
docker pull ghcr.io/ledgerhq/ledger-app-builder/ledger-app-builder:latest
docker pull ghcr.io/ledgerhq/ledger-app-builder/ledger-app-dev-tools:latest

run_builder:
# docker command to build
docker run --rm -ti --privileged -v "/dev/bus/usb:/dev/bus/usb" -v "$(realpath ./):/app" ghcr.io/ledgerhq/ledger-app-builder/ledger-app-builder:latest

run_builder_with_local_sdk:
# docker command to build with local sdk folder (relative path via volume)
docker run --rm -ti --privileged -v "/dev/bus/usb:/dev/bus/usb" -v "$(realpath ./):/app" -v "$(realpath ../ledger-device-rust-sdk):/sdk" ghcr.io/ledgerhq/ledger-app-builder/ledger-app-builder:latest

run_speculos_nanos:
docker run --rm -p 5000:5000 -p 5001:5001 -v '/dev/bus/usb:/dev/bus/usb' \
-v "$(realpath ./):/app" -it --name \
Expand Down
10 changes: 6 additions & 4 deletions near_gas/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use numtoa::NumToA;
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Ord, Eq)]
pub struct NearGas(u64);

/// A buffer, large enough to fit all [NearGas] representation values into
pub type GasBuffer = Buffer<30>;

/// Gas is a type for storing amount of gas.
type Gas = u64;

Expand Down Expand Up @@ -68,7 +71,7 @@ impl NearGas {
self.0
}

pub fn display_as_buffer(&self, result: &mut Buffer<30>) {
pub fn display_as_buffer(&self, result: &mut GasBuffer) {
if *self == NearGas::from_gas(0) {
result.write_str("0 Tgas");
} else if *self < NearGas::from_ggas(1) {
Expand Down Expand Up @@ -115,8 +118,7 @@ impl BorshDeserialize for NearGas {

#[cfg(test)]
mod test {
use crate::NearGas;
use fmt_buffer::Buffer;
use crate::{GasBuffer, NearGas};

#[test]
fn test_display() {
Expand Down Expand Up @@ -146,7 +148,7 @@ mod test {
"1000000.5 Tgas",
),
] {
let mut buffer: Buffer<30> = Buffer::new();
let mut buffer: GasBuffer = GasBuffer::new();
near_gas.display_as_buffer(&mut buffer);

assert_eq!(buffer.as_str(), expected_display);
Expand Down
12 changes: 8 additions & 4 deletions near_token/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ type Balance = u128;
const ONE_MILLINEAR: u128 = 10_u128.pow(21);
use numtoa::NumToA;

/// A buffer, large enough to fit all [NearToken] representation values into
pub type TokenBuffer = Buffer<30>;

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub struct NearToken(pub Balance);

Expand Down Expand Up @@ -45,7 +48,7 @@ impl NearToken {
self.0
}

pub fn display_as_buffer(&self, result: &mut Buffer<30>) {
pub fn display_as_buffer(&self, result: &mut TokenBuffer) {
if *self == NearToken::from_yoctonear(0) {
result.write_str("0 NEAR");
} else if *self == NearToken::from_yoctonear(1) {
Expand Down Expand Up @@ -96,8 +99,9 @@ impl BorshDeserialize for NearToken {
}
#[cfg(test)]
mod tests {
use crate::TokenBuffer;

use super::NearToken;
use fmt_buffer::Buffer;

#[test]
fn test_display() {
Expand All @@ -107,7 +111,7 @@ mod tests {
(0, "0 NEAR"),
(1, "1 yoctoNEAR"),
] {
let mut buffer: Buffer<30> = Buffer::new();
let mut buffer: TokenBuffer = TokenBuffer::new();

let token = NearToken::from_yoctonear(integer);

Expand All @@ -127,7 +131,7 @@ mod tests {
(1100, "1.10 NEAR"),
(1000 * 1_000_000, "1000000.00 NEAR"),
] {
let mut buffer: Buffer<30> = Buffer::new();
let mut buffer: TokenBuffer = TokenBuffer::new();

let token = NearToken::from_millinear(integer_millis);

Expand Down
60 changes: 60 additions & 0 deletions src/app_ui/aliases.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use fmt_buffer::Buffer;

use crate::utils::types::{capped_string::CappedString, hex_display::HexDisplay};

/// A capped string for storing
/// https://docs.rs/near-account-id/1.0.0/near_account_id/struct.AccountId.html
/// where all bytes after 64-byte prefix are truncated and displayed as `... N bytes` ellipsis
///
/// 64 is enough to show implicit account ID-s and most of
/// practical named account ID-s
pub type CappedAccountId = CappedString<64>;

/// A buffer, large enough to contain string representation
/// of u64
pub type U64Buffer = [u8; 20];
/// A buffer, large enough to contain string representation
/// of u32
pub type U32Buffer = [u8; 10];

/// Type, which is used for displaying full `args` field of
/// https://docs.rs/near-primitives/0.21.2/near_primitives/action/struct.FunctionCallAction.html
/// or prefix of `args` with suffix truncated and displayed as `... N bytes` ellipsis,
/// displayed in hexadecimal form, when `args` are considered just arbitrary bytes.
///
/// Current parameter value is limited by `nanos` capabilities.
/// The parameter value can be made larger with `#[cfg(target_os = "...")]`
/// for other platforms
/// see https://github.com/dj8yfo/app-near-rs/pull/45/files
pub type FnCallHexDisplay = HexDisplay<200>;

/// Type, which is used for displaying full `args` field of
/// https://docs.rs/near-primitives/0.21.2/near_primitives/action/struct.FunctionCallAction.html
/// or prefix of `args` with suffix truncated and displayed as `... N bytes` ellipsis,
/// displayed as string, when `args` is considered to be a valid utf-8 string
///
/// Current parameter value is limited by `nanos` capabilities.
/// The parameter value can be made larger with `#[cfg(target_os = "...")]`
/// for other platforms
/// see https://github.com/dj8yfo/app-near-rs/pull/45/files
pub type FnCallCappedString = CappedString<200>;

/// Type, which is used for displaying full `method_names` field of
/// https://docs.rs/near-primitives/0.21.2/near_primitives/account/struct.FunctionCallPermission.html
/// or prefix of `method_names` with suffix truncated and displayed as `... N bytes` ellipsis
///
/// Current parameter value is limited by `nanos` capabilities.
/// The parameter value can be made larger with `#[cfg(target_os = "...")]`
/// for other platforms
/// see https://github.com/dj8yfo/app-near-rs/pull/45/files
pub type MethodNamesBuffer = Buffer<210>;

/// Type, which is used for displaying full `message` field of
/// https://docs.rs/near-ledger/0.5.0/near_ledger/struct.NEP413Payload.html
/// or prefix of `message` with suffix truncated and displayed as `... N bytes` ellipsis
///
/// Current parameter value is limited by `nanos` capabilities.
/// The parameter value can be made larger with `#[cfg(target_os = "...")]`
/// for other platforms
/// see https://github.com/dj8yfo/app-near-rs/pull/45/files
pub type NEP413CappedString = CappedString<400>;
7 changes: 4 additions & 3 deletions src/app_ui/sign/common/action/add_key_common.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::app_ui::aliases::U64Buffer;
use crate::{
app_ui::fields_writer::FieldsWriter, parsing, sign_ui::common::tx_public_key_context,
utils::types::elipsis_fields::ElipsisFields,
Expand All @@ -7,14 +8,14 @@ use ledger_device_sdk::ui::gadgets::Field;
use numtoa::NumToA;

pub struct FieldsContext {
pub num_buf: [u8; 20],
pub num_buf: U64Buffer,
pub pub_key_context: tx_public_key_context::FieldsContext,
}

impl FieldsContext {
pub fn new() -> Self {
Self {
num_buf: [0u8; 20],
num_buf: U64Buffer::default(),
pub_key_context: tx_public_key_context::FieldsContext::new(),
}
}
Expand All @@ -36,7 +37,7 @@ pub fn format<'b, 'a: 'b, const N: usize>(

writer.push_fields(ElipsisFields::one(Field {
name: "Public Key",
value: field_context.pub_key_context.buffer.as_str(),
value: field_context.pub_key_context.as_str(),
}));

writer.push_fields(ElipsisFields::one(Field {
Expand Down
4 changes: 3 additions & 1 deletion src/app_ui/sign/common/action/create_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ use ledger_device_sdk::ui::gadgets::Field;

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

/// action type (1)
const MAX_FIELDS: usize = 1;
pub fn format(
_create_account: &parsing::types::CreateAccount,
writer: &'_ mut FieldsWriter<'_, 1>,
writer: &'_ mut FieldsWriter<'_, MAX_FIELDS>,
) {
writer.push_fields(ElipsisFields::one(Field {
name: "Action type",
Expand Down
14 changes: 10 additions & 4 deletions src/app_ui/sign/common/action/delete_account.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
use crate::{parsing, utils::types::elipsis_fields::ElipsisFields};
use crate::{
parsing,
utils::types::elipsis_fields::{ElipsisFields, EllipsisBuffer},
};
use ledger_device_sdk::ui::gadgets::Field;

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

/// action type (1) + Beneficiary `EllipsisFields` (1-2)
const MAX_FIELDS: usize = 3;

pub struct FieldsContext {
pub beneficiary_display_buf: [u8; 20],
pub beneficiary_display_buf: EllipsisBuffer,
}

impl FieldsContext {
pub fn new() -> Self {
Self {
beneficiary_display_buf: [0u8; 20],
beneficiary_display_buf: EllipsisBuffer::default(),
}
}
}
pub fn format<'b, 'a: 'b>(
delete_account: &'a mut parsing::types::DeleteAccount,
field_context: &'a mut FieldsContext,
writer: &'_ mut FieldsWriter<'b, 3>,
writer: &'_ mut FieldsWriter<'b, MAX_FIELDS>,
) {
writer.push_fields(ElipsisFields::one(Field {
name: "Action type",
Expand Down
7 changes: 5 additions & 2 deletions src/app_ui/sign/common/action/delete_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ use crate::{
utils::types::elipsis_fields::ElipsisFields,
};

/// action type(1) + Public Key (1)
const MAX_FIELDS: usize = 2;

pub fn format<'b, 'a: 'b>(
delete_key: &parsing::types::DeleteKey,
field_context: &'a mut tx_public_key_context::FieldsContext,
writer: &'_ mut FieldsWriter<'b, 2>,
writer: &'_ mut FieldsWriter<'b, MAX_FIELDS>,
) {
field_context.format_public_key(&delete_key.public_key);
writer.push_fields(ElipsisFields::one(Field {
Expand All @@ -18,6 +21,6 @@ pub fn format<'b, 'a: 'b>(

writer.push_fields(ElipsisFields::one(Field {
name: "Public Key",
value: field_context.buffer.as_str(),
value: field_context.as_str(),
}));
}
5 changes: 4 additions & 1 deletion src/app_ui/sign/common/action/deploy_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ use ledger_device_sdk::ui::gadgets::Field;

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

/// action type (1) + Contract SHA256 (1)
const MAX_FIELDS: usize = 2;

pub fn format<'b>(
deploy_contract: &'b parsing::types::DeployContract,
writer: &'_ mut FieldsWriter<'b, 2>,
writer: &'_ mut FieldsWriter<'b, MAX_FIELDS>,
) {
writer.push_fields(ElipsisFields::one(Field {
name: "Action type",
Expand Down
17 changes: 11 additions & 6 deletions src/app_ui/sign/common/action/function_call_bin.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
use crate::app_ui::aliases::FnCallHexDisplay;
use crate::app_ui::fields_writer::FieldsWriter;
use crate::utils::types::elipsis_fields::ElipsisFields;
use crate::utils::types::hex_display::HexDisplay;
use crate::utils::types::elipsis_fields::{ElipsisFields, EllipsisBuffer};

pub struct FieldsContext {
pub args_display_buf: [u8; 20],
pub args_display_buf: EllipsisBuffer,
}

impl FieldsContext {
pub fn new() -> Self {
Self {
args_display_buf: [0u8; 20],
args_display_buf: EllipsisBuffer::default(),
}
}
}

/// action type (1) + Method Name `ElipsisFields` (1-2) +
/// Gas (1) + Deposit (1) + Args Binary `ElipsisFields` (1-2)
const MAX_FIELDS: usize = 7;

pub fn format<'b, 'a: 'b>(
args: &'b HexDisplay<200>,
args: &'b FnCallHexDisplay,
field_context: &'a mut FieldsContext,
writer: &'_ mut FieldsWriter<'b, 7>,
writer: &'_ mut FieldsWriter<'b, MAX_FIELDS>,
) {
let args_fields =
ElipsisFields::from_hex_display(args, "Args Binary", &mut field_context.args_display_buf);
Expand Down
19 changes: 10 additions & 9 deletions src/app_ui/sign/common/action/function_call_common.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
use crate::{
parsing::{self},
utils::types::elipsis_fields::ElipsisFields,
parsing,
utils::types::elipsis_fields::{ElipsisFields, EllipsisBuffer},
};
use fmt_buffer::Buffer;
use ledger_device_sdk::ui::gadgets::Field;
use near_gas::GasBuffer;
use near_token::TokenBuffer;

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

pub struct FieldsContext {
pub method_name_display_buf: [u8; 20],
pub gas_buf: Buffer<30>,
pub deposit_buffer: Buffer<30>,
pub method_name_display_buf: EllipsisBuffer,
pub gas_buf: GasBuffer,
pub deposit_buffer: TokenBuffer,
}

impl FieldsContext {
pub fn new() -> Self {
Self {
method_name_display_buf: [0u8; 20],
gas_buf: Buffer::new(),
deposit_buffer: Buffer::new(),
method_name_display_buf: EllipsisBuffer::default(),
gas_buf: GasBuffer::new(),
deposit_buffer: TokenBuffer::new(),
}
}
}
Expand Down
Loading

0 comments on commit f3c7ec7

Please sign in to comment.