From 6bfff8e541fa305ed644beb4eb3584eaf90b0e88 Mon Sep 17 00:00:00 2001 From: GroM Date: Tue, 28 May 2024 16:42:54 +0200 Subject: [PATCH] Keep using CStringHelper --- ledger_device_sdk/src/nbgl.rs | 51 +++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/ledger_device_sdk/src/nbgl.rs b/ledger_device_sdk/src/nbgl.rs index 94ecf01e..75d29507 100644 --- a/ledger_device_sdk/src/nbgl.rs +++ b/ledger_device_sdk/src/nbgl.rs @@ -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(), ], @@ -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]>, @@ -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, } } @@ -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 @@ -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(), );