Skip to content

Commit

Permalink
Keep using CStringHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
yogh333 committed May 28, 2024
1 parent 937982b commit 6bfff8e
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions ledger_device_sdk/src/nbgl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,17 @@ pub struct NbglHomeAndSettings<'a> {
}

impl<'a> NbglHomeAndSettings<'a> {
pub fn new(app_name: &'a str, version: &'a str, author: &'a str) -> NbglHomeAndSettings<'a> {
pub fn new() -> NbglHomeAndSettings<'a> {
NbglHomeAndSettings {
glyph: None,
info_contents: [
CStr::from_bytes_until_nul(app_name.as_bytes())
CStr::from_bytes_until_nul("Rust App\0".as_bytes())
.unwrap()
.as_ptr(),
CStr::from_bytes_until_nul(version.as_bytes())
CStr::from_bytes_until_nul("0.0.0\0".as_bytes())
.unwrap()
.as_ptr(),
CStr::from_bytes_until_nul(author.as_bytes())
CStr::from_bytes_until_nul("Ledger\0".as_bytes())
.unwrap()
.as_ptr(),
],
Expand All @@ -221,6 +221,22 @@ impl<'a> NbglHomeAndSettings<'a> {
}
}

pub fn infos(
self,
app_name: &'a str,
version: &'a str,
author: &'a str,
) -> NbglHomeAndSettings<'a> {
NbglHomeAndSettings {
info_contents: [
self.c_string_helper.to_cstring(app_name).unwrap().as_ptr() as *const c_char,
self.c_string_helper.to_cstring(version).unwrap().as_ptr() as *const c_char,
self.c_string_helper.to_cstring(author).unwrap().as_ptr() as *const c_char,
],
..self
}
}

pub fn settings(
self,
nvm_data: &'a mut AtomicStorage<[u8; SETTINGS_SIZE]>,
Expand Down Expand Up @@ -461,15 +477,13 @@ impl<'a, const MAX_FIELD_NUMBER: usize, const STRING_BUFFER_SIZE: usize>
/// Used to display address confirmation screens.
pub struct NbglAddressReview<'a> {
glyph: Option<&'a NbglGlyph<'a>>,
verify_str: *const c_char,
verify_str: &'a str,
}

impl<'a> NbglAddressReview<'a> {
pub fn new() -> NbglAddressReview<'a> {
NbglAddressReview {
verify_str: CStr::from_bytes_until_nul("Verify address\0".as_bytes())
.unwrap()
.as_ptr(),
verify_str: "Verify address",
glyph: None,
}
}
Expand All @@ -482,24 +496,15 @@ impl<'a> NbglAddressReview<'a> {
}

pub fn verify_str(self, verify_str: &'a str) -> NbglAddressReview<'a> {
NbglAddressReview {
verify_str: CStr::from_bytes_until_nul(verify_str.as_bytes())
.unwrap()
.as_ptr(),
..self
}
NbglAddressReview { verify_str, ..self }
}

pub fn show(&mut self, address: &str) -> bool {
unsafe {
// Create CStringHelper instance and convert the address and verify_str into c strings.
//let c_string_helper = CStringHelper::<128>::new();
//let c_addr_str = c_string_helper.to_cstring(address).unwrap();
//let c_verif_str = c_string_helper.to_cstring(self.verify_str).unwrap();

let addr = CStr::from_bytes_until_nul(address.as_bytes())
.unwrap()
.as_ptr();
let c_string_helper = CStringHelper::<128>::new();
let c_addr_str = c_string_helper.to_cstring(address).unwrap();
let c_verif_str = c_string_helper.to_cstring(self.verify_str).unwrap();

let icon = if self.glyph.is_some() {
&self.glyph.unwrap().into() as *const nbgl_icon_details_t
Expand All @@ -509,10 +514,10 @@ impl<'a> NbglAddressReview<'a> {

// Show the address confirmation on the device.
let sync_ret = ux_sync_addressReview(
addr,
c_addr_str.as_ptr() as *const c_char,
core::ptr::null(),
icon,
self.verify_str,
c_verif_str.as_ptr() as *const c_char,
core::ptr::null(),
);

Expand Down

0 comments on commit 6bfff8e

Please sign in to comment.