Skip to content

Commit

Permalink
Merge pull request #76 from LedgerHQ/y333/use_sync_nbgl_homeandsettings
Browse files Browse the repository at this point in the history
Use async NbglHomeAndSettings and stop redrawing screen for every received…
  • Loading branch information
yogh333 authored Sep 25, 2024
2 parents bddcf06 + 52811eb commit 2fe39d2
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 36 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
name = "app-boilerplate-rust"
version = "1.5.1"
version = "1.6.0"
authors = ["yhql", "agrojean-ledger"]
edition = "2021"

[dependencies]
ledger_device_sdk = { version="1.15.2" }
ledger_device_sdk = { version="1.16.0" }
include_gif = "1.2.0"
serde = {version="1.0.192", default_features = false, features = ["derive"]}
serde-json-core = { git = "https://github.com/rust-embedded-community/serde-json-core"}
Expand Down
9 changes: 6 additions & 3 deletions src/app_ui/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*****************************************************************************/

use include_gif::include_gif;
use ledger_device_sdk::io::{Comm, Event};
use ledger_device_sdk::io::Comm;

#[cfg(not(any(target_os = "stax", target_os = "flex")))]
use ledger_device_sdk::ui::{
Expand All @@ -29,7 +29,10 @@ use crate::settings::Settings;
#[cfg(any(target_os = "stax", target_os = "flex"))]
use ledger_device_sdk::nbgl::{NbglGlyph, NbglHomeAndSettings};

#[cfg(not(any(target_os = "stax", target_os = "flex")))]
use crate::Instruction;
#[cfg(not(any(target_os = "stax", target_os = "flex")))]
use ledger_device_sdk::io::Event;

// use ledger_device_sdk::nvm::*;

Expand Down Expand Up @@ -70,12 +73,13 @@ pub fn ui_menu_main(comm: &mut Comm) -> Event<Instruction> {
}

#[cfg(any(target_os = "stax", target_os = "flex"))]
pub fn ui_menu_main(_: &mut Comm) -> Event<Instruction> {
pub fn ui_menu_main(_: &mut Comm) -> NbglHomeAndSettings {
// Load glyph from 64x64 4bpp gif file with include_gif macro. Creates an NBGL compatible glyph.
const FERRIS: NbglGlyph = NbglGlyph::from_include(include_gif!("crab_64x64.gif", NBGL));

let settings_strings = [["Display Memo", "Allow display of transaction memo."]];
let mut settings: Settings = Default::default();

// Display the home screen.
NbglHomeAndSettings::new()
.glyph(&FERRIS)
Expand All @@ -85,5 +89,4 @@ pub fn ui_menu_main(_: &mut Comm) -> Event<Instruction> {
env!("CARGO_PKG_VERSION"),
env!("CARGO_PKG_AUTHORS"),
)
.show()
}
7 changes: 7 additions & 0 deletions src/handlers/sign_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ use ledger_device_sdk::ecc::{Secp256k1, SeedDerive};
use ledger_device_sdk::hash::{sha3::Keccak256, HashInit};
use ledger_device_sdk::io::Comm;

#[cfg(any(target_os = "stax", target_os = "flex"))]
use ledger_device_sdk::nbgl::NbglHomeAndSettings;

use serde::Deserialize;
use serde_json_core::from_slice;

Expand All @@ -42,6 +45,8 @@ pub struct TxContext {
raw_tx: Vec<u8>,
path: Bip32Path,
review_finished: bool,
#[cfg(any(target_os = "stax", target_os = "flex"))]
pub home: NbglHomeAndSettings,
}

// Implement constructor for TxInfo with default values
Expand All @@ -52,6 +57,8 @@ impl TxContext {
raw_tx: Vec::new(),
path: Default::default(),
review_finished: false,
#[cfg(any(target_os = "stax", target_os = "flex"))]
home: Default::default(),
}
}
// Get review status
Expand Down
68 changes: 40 additions & 28 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@ use handlers::{
get_version::handler_get_version,
sign_tx::{handler_sign_tx, TxContext},
};
use ledger_device_sdk::io::{ApduHeader, Comm, Event, Reply, StatusWords};
use ledger_device_sdk::io::{ApduHeader, Comm, Reply, StatusWords};
#[cfg(feature = "pending_review_screen")]
#[cfg(not(any(target_os = "stax", target_os = "flex")))]
use ledger_device_sdk::ui::gadgets::display_pending_review;

#[cfg(not(any(target_os = "stax", target_os = "flex")))]
use ledger_device_sdk::io::Event;

ledger_device_sdk::set_panic!(ledger_device_sdk::exiting_panic);

// Required for using String, Vec, format!...
Expand Down Expand Up @@ -129,7 +132,7 @@ impl TryFrom<ApduHeader> for Instruction {
}

#[cfg(any(target_os = "stax", target_os = "flex"))]
fn show_status_if_needed(ins: &Instruction, tx_ctx: &TxContext, status: &AppSW) {
fn show_status_and_home_if_needed(ins: &Instruction, tx_ctx: &mut TxContext, status: &AppSW) {
let (show_status, status_type) = match (ins, status) {
(Instruction::GetPubkey { display: true }, AppSW::Deny | AppSW::Ok) => {
(true, StatusType::Address)
Expand All @@ -145,6 +148,9 @@ fn show_status_if_needed(ins: &Instruction, tx_ctx: &TxContext, status: &AppSW)
NbglReviewStatus::new()
.status_type(status_type)
.show(success);

// call home.show_and_return() to show home and setting screen
tx_ctx.home.show_and_return();
}
}

Expand All @@ -155,38 +161,44 @@ extern "C" fn sample_main() {
// BadCla status word.
let mut comm = Comm::new().set_expected_cla(0xe0);

// Initialize reference to Comm instance for NBGL
// API calls.
let mut tx_ctx = TxContext::new();

#[cfg(any(target_os = "stax", target_os = "flex"))]
init_comm(&mut comm);
{
// Initialize reference to Comm instance for NBGL
// API calls.
init_comm(&mut comm);
tx_ctx.home = ui_menu_main(&mut comm);
tx_ctx.home.show_and_return();
}

// Developer mode / pending review popup
// must be cleared with user interaction
#[cfg(feature = "pending_review_screen")]
#[cfg(not(any(target_os = "stax", target_os = "flex")))]
#[cfg(feature = "pending_review_screen")]
display_pending_review(&mut comm);

let mut tx_ctx = TxContext::new();

loop {
// Wait for either a specific button push to exit the app
// or an APDU command
if let Event::Command(ins) = ui_menu_main(&mut comm) {
let result = handle_apdu(&mut comm, &ins, &mut tx_ctx);
let _status: AppSW = match result {
Ok(()) => {
comm.reply_ok();
AppSW::Ok
}
Err(sw) => {
comm.reply(sw);
sw
}
};

#[cfg(any(target_os = "stax", target_os = "flex"))]
show_status_if_needed(&ins, &tx_ctx, &_status);
}
#[cfg(any(target_os = "stax", target_os = "flex"))]
let ins: Instruction = comm.next_command();

#[cfg(not(any(target_os = "stax", target_os = "flex")))]
let ins = if let Event::Command(ins) = ui_menu_main(&mut comm) {
ins
} else {
continue;
};

let _status = match handle_apdu(&mut comm, &ins, &mut tx_ctx) {
Ok(()) => {
comm.reply_ok();
AppSW::Ok
}
Err(sw) => {
comm.reply(sw);
sw
}
};
#[cfg(any(target_os = "stax", target_os = "flex"))]
show_status_and_home_if_needed(&ins, &mut tx_ctx, &_status);
}
}

Expand Down
Binary file modified tests/snapshots/flex/test_app_mainmenu/00002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/snapshots/nanosp/test_app_mainmenu/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/snapshots/nanox/test_app_mainmenu/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/snapshots/stax/test_app_mainmenu/00002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2fe39d2

Please sign in to comment.