Skip to content

Commit 1163156

Browse files
committed
Use sync NbglHomeAndSettings and stop redraw screen for each received APDU
1 parent bddcf06 commit 1163156

File tree

5 files changed

+63
-33
lines changed

5 files changed

+63
-33
lines changed

Cargo.lock

Lines changed: 4 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ authors = ["yhql", "agrojean-ledger"]
55
edition = "2021"
66

77
[dependencies]
8-
ledger_device_sdk = { version="1.15.2" }
8+
ledger_device_sdk = { version="1.15.6" }
99
include_gif = "1.2.0"
1010
serde = {version="1.0.192", default_features = false, features = ["derive"]}
1111
serde-json-core = { git = "https://github.com/rust-embedded-community/serde-json-core"}
@@ -36,4 +36,8 @@ icon = "crab_14x14.gif"
3636
icon = "crab_32x32.gif"
3737

3838
[package.metadata.ledger.flex]
39-
icon = "crab_40x40.gif"
39+
icon = "crab_40x40.gif"
40+
41+
[patch.crates-io]
42+
ledger_device_sdk = { git = "https://github.com/LedgerHQ/ledger-device-rust-sdk.git", branch = "y333/new_nbgl" }
43+
include_gif = { git = "https://github.com/LedgerHQ/ledger-device-rust-sdk.git", branch = "y333/new_nbgl" }

src/app_ui/menu.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use crate::settings::Settings;
2929
#[cfg(any(target_os = "stax", target_os = "flex"))]
3030
use ledger_device_sdk::nbgl::{NbglGlyph, NbglHomeAndSettings};
3131

32+
#[cfg(not(any(target_os = "stax", target_os = "flex")))]
3233
use crate::Instruction;
3334

3435
// use ledger_device_sdk::nvm::*;
@@ -70,20 +71,20 @@ pub fn ui_menu_main(comm: &mut Comm) -> Event<Instruction> {
7071
}
7172

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

7778
let settings_strings = [["Display Memo", "Allow display of transaction memo."]];
7879
let mut settings: Settings = Default::default();
7980
// Display the home screen.
80-
NbglHomeAndSettings::new()
81+
let home = NbglHomeAndSettings::new()
8182
.glyph(&FERRIS)
8283
.settings(settings.get_mut_ref(), &settings_strings)
8384
.infos(
8485
"Boilerplate",
8586
env!("CARGO_PKG_VERSION"),
8687
env!("CARGO_PKG_AUTHORS"),
87-
)
88-
.show()
88+
);
89+
home
8990
}

src/handlers/sign_tx.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ use ledger_device_sdk::ecc::{Secp256k1, SeedDerive};
2222
use ledger_device_sdk::hash::{sha3::Keccak256, HashInit};
2323
use ledger_device_sdk::io::Comm;
2424

25+
#[cfg(any(target_os = "stax", target_os = "flex"))]
26+
use ledger_device_sdk::nbgl::NbglHomeAndSettings;
27+
2528
use serde::Deserialize;
2629
use serde_json_core::from_slice;
2730

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

4752
// Implement constructor for TxInfo with default values
@@ -52,6 +57,8 @@ impl TxContext {
5257
raw_tx: Vec::new(),
5358
path: Default::default(),
5459
review_finished: false,
60+
#[cfg(any(target_os = "stax", target_os = "flex"))]
61+
home: NbglHomeAndSettings::new(),
5562
}
5663
}
5764
// Get review status

src/main.rs

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl TryFrom<ApduHeader> for Instruction {
129129
}
130130

131131
#[cfg(any(target_os = "stax", target_os = "flex"))]
132-
fn show_status_if_needed(ins: &Instruction, tx_ctx: &TxContext, status: &AppSW) {
132+
fn show_status_if_needed(ins: &Instruction, tx_ctx: &mut TxContext, status: &AppSW) {
133133
let (show_status, status_type) = match (ins, status) {
134134
(Instruction::GetPubkey { display: true }, AppSW::Deny | AppSW::Ok) => {
135135
(true, StatusType::Address)
@@ -145,6 +145,9 @@ fn show_status_if_needed(ins: &Instruction, tx_ctx: &TxContext, status: &AppSW)
145145
NbglReviewStatus::new()
146146
.status_type(status_type)
147147
.show(success);
148+
149+
// call display() to show home and setting screen
150+
tx_ctx.home.show_and_return();
148151
}
149152
}
150153

@@ -155,25 +158,19 @@ extern "C" fn sample_main() {
155158
// BadCla status word.
156159
let mut comm = Comm::new().set_expected_cla(0xe0);
157160

158-
// Initialize reference to Comm instance for NBGL
159-
// API calls.
160-
#[cfg(any(target_os = "stax", target_os = "flex"))]
161-
init_comm(&mut comm);
162-
163-
// Developer mode / pending review popup
164-
// must be cleared with user interaction
165-
#[cfg(feature = "pending_review_screen")]
166-
#[cfg(not(any(target_os = "stax", target_os = "flex")))]
167-
display_pending_review(&mut comm);
168-
169161
let mut tx_ctx = TxContext::new();
170162

171-
loop {
172-
// Wait for either a specific button push to exit the app
173-
// or an APDU command
174-
if let Event::Command(ins) = ui_menu_main(&mut comm) {
175-
let result = handle_apdu(&mut comm, &ins, &mut tx_ctx);
176-
let _status: AppSW = match result {
163+
#[cfg(any(target_os = "stax", target_os = "flex"))]
164+
{
165+
// Initialize reference to Comm instance for NBGL
166+
// API calls.
167+
init_comm(&mut comm);
168+
tx_ctx.home = ui_menu_main(&mut comm);
169+
tx_ctx.home.show_and_return();
170+
171+
loop {
172+
let ins: Instruction = comm.next_command();
173+
let status = match handle_apdu(&mut comm, &ins, &mut tx_ctx) {
177174
Ok(()) => {
178175
comm.reply_ok();
179176
AppSW::Ok
@@ -183,9 +180,33 @@ extern "C" fn sample_main() {
183180
sw
184181
}
185182
};
183+
show_status_if_needed(&ins, &mut tx_ctx, &status);
184+
}
185+
}
186186

187-
#[cfg(any(target_os = "stax", target_os = "flex"))]
188-
show_status_if_needed(&ins, &tx_ctx, &_status);
187+
#[cfg(not(any(target_os = "stax", target_os = "flex")))]
188+
{
189+
// Developer mode / pending review popup
190+
// must be cleared with user interaction
191+
#[cfg(feature = "pending_review_screen")]
192+
display_pending_review(&mut comm);
193+
194+
loop {
195+
// Wait for either a specific button push to exit the app
196+
// or an APDU command
197+
if let Event::Command(ins) = ui_menu_main(&mut comm) {
198+
let result = handle_apdu(&mut comm, &ins, &mut tx_ctx);
199+
let _status: AppSW = match result {
200+
Ok(()) => {
201+
comm.reply_ok();
202+
AppSW::Ok
203+
}
204+
Err(sw) => {
205+
comm.reply(sw);
206+
sw
207+
}
208+
};
209+
}
189210
}
190211
}
191212
}

0 commit comments

Comments
 (0)