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 #8 from dj8yfo/delete_key_action
Browse files Browse the repository at this point in the history
feat: `DeleteKey` action
  • Loading branch information
dj8yfo authored Jan 25, 2024
2 parents c84b4c6 + 734c815 commit 6f21b56
Show file tree
Hide file tree
Showing 32 changed files with 296 additions and 160 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["yhql", "agrojean-ledger", "dj8yf0μl"]
edition = "2021"

[dependencies]
ledger_device_sdk = "1.4.0"
ledger_device_sdk = "1.4.3"
ledger_secure_sdk_sys = "1.0.2"
include_gif = "1.0.1"
hex = { version = "0.4.3", default-features = false, features = ["serde"] }
Expand Down
2 changes: 1 addition & 1 deletion src/app_ui/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
* limitations under the License.
*****************************************************************************/

use crate::AppSW;
use crate::utils::crypto;
use crate::utils::types::fmt_buffer::FmtBuffer;
use crate::AppSW;
use ledger_device_sdk::ui::bitmaps::{CROSSMARK, EYE, VALIDATE_14};
use ledger_device_sdk::ui::gadgets::{Field, MultiFieldReview};

Expand Down
3 changes: 1 addition & 2 deletions src/app_ui/fields_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ use ledger_device_sdk::ui::gadgets::Field;

use crate::utils::types::capped_string::ElipsisFields;



pub struct FieldsWriter<'a, const N: usize> {
buffer: [Field<'a>; N],
used: usize,
}

#[derive(Debug)]
pub struct FieldsOverflow;

impl<'a, const N: usize> FieldsWriter<'a, N> {
Expand Down
14 changes: 14 additions & 0 deletions src/app_ui/sign/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use numtoa::NumToA;

mod create_account;
mod delete_account;
mod delete_key;
mod transfer;

pub fn ui_display_transfer(
Expand Down Expand Up @@ -47,6 +48,19 @@ pub fn ui_display_delete_account(
ui_display_common(&mut writer, ordinal, total_actions)
}

pub fn ui_display_delete_key(
delete_key: &parsing::types::DeleteKey,
ordinal: u32,
total_actions: u32,
) -> bool {
let mut field_context: delete_key::FieldsContext = delete_key::FieldsContext::new();
let mut writer: FieldsWriter<'_, 2> = FieldsWriter::new();

delete_key::format(delete_key, &mut field_context, &mut writer);

ui_display_common(&mut writer, ordinal, total_actions)
}

pub fn ui_display_common<const N: usize>(
writer: &mut FieldsWriter<'_, N>,
ordinal: u32,
Expand Down
18 changes: 7 additions & 11 deletions src/app_ui/sign/action/create_account.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
use crate::parsing;
use ledger_device_sdk::ui::gadgets::Field;

use crate::{
app_ui::fields_writer::FieldsWriter,
utils::types::capped_string::ElipsisFields,
};
use crate::{app_ui::fields_writer::FieldsWriter, utils::types::capped_string::ElipsisFields};

pub fn format(
_create_account: &parsing::types::CreateAccount,
writer: &'_ mut FieldsWriter<'_, 1>,
) {
match writer.push_fields(ElipsisFields::one(Field {
name: "Action type:",
value: "Create Account",
})) {
Ok(..) => {}
Err(_err) => panic!("wrong total fields in tx prefix FieldsWriter"),
}
writer
.push_fields(ElipsisFields::one(Field {
name: "Action type:",
value: "Create Account",
}))
.unwrap();
}
23 changes: 8 additions & 15 deletions src/app_ui/sign/action/delete_account.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
use crate::parsing;
use ledger_device_sdk::ui::gadgets::Field;

use crate::{
app_ui::fields_writer::FieldsWriter,
utils::types::capped_string::ElipsisFields,
};
use crate::{app_ui::fields_writer::FieldsWriter, utils::types::capped_string::ElipsisFields};

pub fn format<'b, 'a: 'b>(
delete_account: &'a parsing::types::DeleteAccount,
writer: &'_ mut FieldsWriter<'b, 3>,
) {
match writer.push_fields(ElipsisFields::one(Field {
name: "Action type:",
value: "Delete Account",
})) {
Ok(..) => {}
Err(_err) => panic!("wrong total fields in tx prefix FieldsWriter"),
}
writer
.push_fields(ElipsisFields::one(Field {
name: "Action type:",
value: "Delete Account",
}))
.unwrap();

let beneficiary_id = delete_account.beneficiary_id.ui_fields("Beneficiary");
match writer.push_fields(beneficiary_id) {
Ok(..) => {}
Err(_err) => panic!("wrong total fields in tx prefix FieldsWriter"),
}
writer.push_fields(beneficiary_id).unwrap();
}
68 changes: 68 additions & 0 deletions src/app_ui/sign/action/delete_key.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use ledger_device_sdk::ui::gadgets::Field;

use crate::{
app_ui::fields_writer::FieldsWriter,
parsing::{self, types::tx_public_key::TxPublicKey},
utils::types::{capped_string::ElipsisFields, fmt_buffer::FmtBuffer},
};

pub struct FieldsContext {
buffer: FmtBuffer<100>,
}

impl FieldsContext {
pub fn new() -> Self {
Self {
buffer: FmtBuffer::new(),
}
}

fn format_public_key(&mut self, public_key: &TxPublicKey) {
match public_key {
TxPublicKey::ED25519(arr) => {
let mut tmp_buf = [0u8; 50];
// NOTE: expecting `tmp_buf` to be always large enough : 1.4 * 32
let len = bs58::encode(arr).onto(&mut tmp_buf[..]).unwrap();
// expecting `bs58` to always produce correct strings
// https://docs.rs/bs58/0.5.0/src/bs58/encode.rs.html#201
let bs58_str = core::str::from_utf8(&tmp_buf[..len]).unwrap();

self.buffer.write_str("ed25519:");
self.buffer.write_str(bs58_str);
}
TxPublicKey::SECP256K1(arr) => {
let mut tmp_buf = [0u8; 90];

// expecting `tmp_buf` to be always large enough: 1.4 * 64
let len = bs58::encode(arr).onto(&mut tmp_buf[..]).unwrap();
// expecting `bs58` to always produce correct strings
// https://docs.rs/bs58/0.5.0/src/bs58/encode.rs.html#201
let bs58_str = core::str::from_utf8(&tmp_buf[..len]).unwrap();

self.buffer.write_str("secp256k1:");
self.buffer.write_str(bs58_str);
}
}
}
}

pub fn format<'b, 'a: 'b>(
delete_key: &parsing::types::DeleteKey,
field_context: &'a mut FieldsContext,
writer: &'_ mut FieldsWriter<'b, 2>,
) {
field_context.format_public_key(&delete_key.public_key);
writer
.push_fields(ElipsisFields::one(Field {
name: "Action type:",
value: "Delete Key",
}))
.unwrap();

writer
.push_fields(ElipsisFields::one(Field {
name: "Public Key:",
value: field_context.buffer.as_str(),
}))
.unwrap();
}
32 changes: 13 additions & 19 deletions src/app_ui/sign/action/transfer.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use crate::parsing::{self, types::action::ONE_NEAR};
use ledger_device_sdk::ui::gadgets::Field;

use crate::{
app_ui::fields_writer::FieldsWriter,
utils::types::capped_string::ElipsisFields,
};
use crate::{app_ui::fields_writer::FieldsWriter, utils::types::capped_string::ElipsisFields};

pub struct FieldsContext {
pub float_buffer: dtoa::Buffer,
Expand All @@ -15,7 +12,6 @@ impl FieldsContext {
Self {
float_buffer: dtoa::Buffer::new(),
}

}
}

Expand All @@ -24,21 +20,19 @@ pub fn format<'b, 'a: 'b>(
field_context: &'a mut FieldsContext,
writer: &'_ mut FieldsWriter<'b, 2>,
) {
match writer.push_fields(ElipsisFields::one(Field {
name: "Action type:",
value: "Transfer",
})) {
Ok(..) => {}
Err(_err) => panic!("wrong total fields in tx prefix FieldsWriter"),
}
writer
.push_fields(ElipsisFields::one(Field {
name: "Action type:",
value: "Transfer",
}))
.unwrap();

let deposit = (transfer.deposit as f64) / (ONE_NEAR as f64);
let printed = field_context.float_buffer.format(deposit);
match writer.push_fields(ElipsisFields::one(Field {
name: "Amount (NEAR)",
value: printed,
})) {
Ok(..) => {}
Err(_err) => panic!("wrong total fields in tx prefix FieldsWriter"),
}
writer
.push_fields(ElipsisFields::one(Field {
name: "Amount (NEAR)",
value: printed,
}))
.unwrap();
}
26 changes: 11 additions & 15 deletions src/app_ui/sign/transaction_prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,26 @@ use crate::{
};

pub fn ui_display(transaction_prefix: &parsing::types::TransactionPrefix) -> bool {
#[cfg(feature = "speculos")]
transaction_prefix.debug_print();

let mut field_writer: FieldsWriter<'_, 5> = FieldsWriter::new();
let signer_id = transaction_prefix.signer_id.ui_fields("Signer Id");
match field_writer.push_fields(signer_id) {
Ok(..) => {}
Err(_err) => panic!("wrong total fields in tx prefix FieldsWriter"),
}
field_writer.push_fields(signer_id).unwrap();

let receiver_id = transaction_prefix.receiver_id.ui_fields("Receiver Id");
match field_writer.push_fields(receiver_id) {
Ok(..) => {}
Err(_err) => panic!("wrong total fields in tx prefix FieldsWriter"),
}
field_writer.push_fields(receiver_id).unwrap();
let mut numtoa_buf = [0u8; 10];

let num_actions_str = transaction_prefix
.number_of_actions
.numtoa_str(10, &mut numtoa_buf);
match field_writer.push_fields(ElipsisFields::one(Field {
name: "Total actions:",
value: num_actions_str,
})) {
Ok(..) => {}
Err(_err) => panic!("wrong total fields in tx prefix FieldsWriter"),
}
field_writer
.push_fields(ElipsisFields::one(Field {
name: "Total actions:",
value: num_actions_str,
}))
.unwrap();

let my_review = MultiFieldReview::new(
field_writer.get_fields(),
Expand Down
2 changes: 1 addition & 1 deletion src/app_ui/sign/widgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use ledger_device_sdk::ui::bitmaps::WARNING;

use ledger_device_sdk::ui::gadgets::clear_screen;
use ledger_device_sdk::ui::layout::{StringPlace, Location, Layout};
use ledger_device_sdk::ui::layout::{Layout, Location, StringPlace};
use ledger_device_sdk::ui::screen_util::screen_update;

pub fn display_receiving() {
Expand Down
1 change: 0 additions & 1 deletion src/handlers/get_public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use crate::utils::crypto;
use crate::AppSW;
use ledger_device_sdk::io::Comm;


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)?;
Expand Down
1 change: 0 additions & 1 deletion src/handlers/get_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use ledger_device_sdk::io;
#[cfg(feature = "speculos")]
use ledger_device_sdk::testing;


pub fn handler(comm: &mut io::Comm) -> Result<(), AppSW> {
#[cfg(feature = "speculos")]
testing::debug_print("enter `handler_get_version` fn\n");
Expand Down
1 change: 0 additions & 1 deletion src/handlers/get_wallet_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::utils::crypto;
use crate::AppSW;
use ledger_device_sdk::io::Comm;


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)?;
Expand Down
4 changes: 3 additions & 1 deletion src/handlers/sign_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ const MAX_TRANSACTION_LEN: usize = 534;

pub struct Signature(pub [u8; 64]);

pub mod transfer;
pub mod create_account;
pub mod delete_account;
pub mod delete_key;
pub mod transfer;

fn popup_transaction_prefix(stream: &mut HashingStream<SingleTxStream<'_>>) -> Result<u32, AppSW> {
let mut tx_prefix = parsing::types::TransactionPrefix {
Expand Down Expand Up @@ -65,6 +66,7 @@ fn popup_action(
Action::Transfer => transfer::handle(stream, ordinal_action, total_actions),
Action::CreateAccount => create_account::handle(stream, ordinal_action, total_actions),
Action::DeleteAccount => delete_account::handle(stream, ordinal_action, total_actions),
Action::DeleteKey => delete_key::handle(stream, ordinal_action, total_actions),
_ => unimplemented!(),
}
}
Expand Down
22 changes: 16 additions & 6 deletions src/handlers/sign_tx/create_account.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
use crate::{AppSW, parsing::{HashingStream, SingleTxStream, types::CreateAccount, borsh::BorshDeserialize}};
use crate::sign_ui;
use crate::{
parsing::{borsh::BorshDeserialize, types::CreateAccount, HashingStream, SingleTxStream},
AppSW,
};

pub fn handle(stream: &mut HashingStream<SingleTxStream<'_>>, ordinal_action: u32, total_actions: u32) -> Result<(), AppSW> {
let create_account = CreateAccount::deserialize_reader(stream).map_err(|_err| AppSW::TxParsingFail)?;
pub fn handle(
stream: &mut HashingStream<SingleTxStream<'_>>,
ordinal_action: u32,
total_actions: u32,
) -> Result<(), AppSW> {
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, ordinal_action + 1, total_actions) {
if !sign_ui::action::ui_display_create_account(
&create_account,
ordinal_action + 1,
total_actions,
) {
return Err(AppSW::Deny);
}
Ok(())

}
Loading

0 comments on commit 6f21b56

Please sign in to comment.