Skip to content

Commit

Permalink
Add "choice" use case to NBGL lib.
Browse files Browse the repository at this point in the history
  • Loading branch information
agrojean-ledger committed Jul 26, 2024
1 parent 1e7b585 commit 7798a1d
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 0 deletions.
51 changes: 51 additions & 0 deletions ledger_device_sdk/examples/nbgl_choice.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#![no_std]
#![no_main]

// Force boot section to be embedded in
use ledger_device_sdk as _;

use include_gif::include_gif;
use ledger_device_sdk::io::*;
use ledger_device_sdk::nbgl::{init_comm, NbglChoice, NbglGlyph};
use ledger_secure_sdk_sys::*;

#[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> ! {
exit_app(1);
}

#[no_mangle]
extern "C" fn sample_main() {
unsafe {
nbgl_refreshReset();
}

let mut comm = Comm::new();
// Initialize reference to Comm instance for NBGL
// API calls.
init_comm(&mut comm);

// Load glyph from 64x64 4bpp gif file with include_gif macro. Creates an NBGL compatible glyph.
const WARNING: NbglGlyph =
NbglGlyph::from_include(include_gif!("icons/Warning_64px.gif", NBGL));

let back_to_safety = NbglChoice::new().glyph(&WARNING)
.status_text(Some("Transaction rejected"),None)
.show(
"Security risk detected",
"It may not be safe to sign this transaction. To continue, you'll need to review the risk.",
"Back to safety",
"Review risk",
);

if !back_to_safety {
NbglChoice::new()
.status_text(Some("Transaction confirmed"), Some("Transaction rejected"))
.show(
"The transaction cannot be trusted",
"Your Ledger cannot decode this transaction. If you sign it, you could be authorizing malicious actions that can drain your wallet.\n\nLearn more: ledger.com/e8",
"I accept the risk",
"Reject transaction"
);
}
}
Binary file added ledger_device_sdk/icons/Warning_64px.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
86 changes: 86 additions & 0 deletions ledger_device_sdk/src/nbgl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,92 @@ impl<'a> NbglAddressReview<'a> {
}
}

/// A wrapper around the synchronous NBGL ux_sync_status C API binding.
/// Draws a generic choice page, described in a centered info (with configurable icon),
/// thanks to a button and a footer at the bottom of the page.
pub struct NbglChoice<'a> {
glyph: Option<&'a NbglGlyph<'a>>,
confirmed_text: Option<CString>,
cancelled_text: Option<CString>,
}

impl<'a> NbglChoice<'a> {
pub fn new() -> NbglChoice<'a> {
NbglChoice {
glyph: None,
confirmed_text: None,
cancelled_text: None,
}
}

pub fn glyph(self, glyph: &'a NbglGlyph) -> NbglChoice<'a> {
NbglChoice {
glyph: Some(glyph),
..self
}
}

pub fn status_text(
self,
confirmed: Option<&'a str>,
cancelled: Option<&'a str>,
) -> NbglChoice<'a> {
let confirmed_text = confirmed.map(|s| CString::new(s).unwrap());
let cancelled_text = cancelled.map(|s| CString::new(s).unwrap());
NbglChoice {
confirmed_text,
cancelled_text,
..self
}
}

pub fn show(
self,
message: &str,
sub_message: &str,
confirm_text: &str,
cancel_text: &str,
) -> bool {
unsafe {
let icon: nbgl_icon_details_t = match self.glyph {
Some(g) => g.into(),
None => nbgl_icon_details_t::default(),
};
let message = CString::new(message).unwrap();
let sub_message = CString::new(sub_message).unwrap();
let confirm_text = CString::new(confirm_text).unwrap();
let cancel_text = CString::new(cancel_text).unwrap();

let sync_ret = ux_sync_choice(
&icon as *const nbgl_icon_details_t,
message.as_ptr() as *const c_char,
sub_message.as_ptr() as *const c_char,
confirm_text.as_ptr() as *const c_char,
cancel_text.as_ptr() as *const c_char,
);

// Return true if the user approved the transaction, false otherwise.
match sync_ret {
ledger_secure_sdk_sys::UX_SYNC_RET_APPROVED => {
if let Some(text) = self.confirmed_text {
ledger_secure_sdk_sys::ux_sync_status(text.as_ptr() as *const c_char, true);
}
return true;
}
_ => {
if let Some(text) = self.cancelled_text {
ledger_secure_sdk_sys::ux_sync_status(
text.as_ptr() as *const c_char,
false,
);
}
return false;
}
}
}
}
}

#[derive(Copy, Clone)]
pub enum TuneIndex {
Reserved,
Expand Down

0 comments on commit 7798a1d

Please sign in to comment.