diff --git a/services/modals/src/api.rs b/services/modals/src/api.rs index 1725ffddf..bb570e57c 100644 --- a/services/modals/src/api.rs +++ b/services/modals/src/api.rs @@ -43,6 +43,14 @@ pub struct ManagedNotification { pub qrtext: Option>, } +#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)] +pub struct ManagedBip39 { + pub token: [u32; 4], + pub bip39_data: [u8; 32], + pub bip39_len: u32, + pub caption: Option>, +} + #[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)] #[cfg(feature = "ditherpunk")] pub struct ManagedImage { @@ -73,6 +81,10 @@ pub struct DynamicNotification { pub text: Option>, } +/// API note: enums with explicit numbers may not have their numbers re-ordered, especially +/// not for aesthetic reasons! This is because when we assign numbers to enums, something else +/// is explicitly depending on that number in a way that will break if you change it (e.g. +/// FFI ABIs) #[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] pub(crate) enum Opcode { // these are blocking calls @@ -82,6 +94,10 @@ pub(crate) enum Opcode { PromptWithMultiResponse = 1, /// simple notification Notification = 2, + /// bip39 coded notification + Bip39 = 31, // ---- note op number + Bip39Input = 32, // ----- note op number + Bip39Return = 33, // ----- note op number /// display an image #[cfg(feature = "ditherpunk")] Image = 3, diff --git a/services/modals/src/lib.rs b/services/modals/src/lib.rs index a8888b448..495ea93f7 100644 --- a/services/modals/src/lib.rs +++ b/services/modals/src/lib.rs @@ -2,6 +2,7 @@ pub mod api; use api::*; +#[cfg(feature = "ditherpunk")] pub mod tests; use bit_field::BitField; @@ -192,6 +193,61 @@ impl Modals { Ok(()) } + /// this blocks until the notification has been acknowledged. It will attempt to render up to 256 bits + /// of `data` in bip39 format. Data must conform to the codeable lengths by BIP39, or else the routine + /// will return immediately with an `InvalidString` error without showing any dialog box. + pub fn show_bip39( + &self, + caption: Option<&str>, + data: &Vec::, + ) -> Result<(), xous::Error> { + match data.len() { + 16 | 20 | 24 | 28 | 32 => (), + _ => return Err(xous::Error::InvalidString) + } + self.lock(); + let mut bip39_data = [0u8; 32]; + for (&s, d) in data.iter().zip(bip39_data.iter_mut()) { + *d = s; + } + let spec = ManagedBip39 { + token: self.token, + caption: if let Some(c) = caption {Some(xous_ipc::String::from_str(c))} else {None}, + bip39_data, + bip39_len: if data.len() <= 32 {data.len() as u32} else {32}, + }; + let buf = Buffer::into_buf(spec).or(Err(xous::Error::InternalError))?; + buf.lend(self.conn, Opcode::Bip39.to_u32().unwrap()) + .or(Err(xous::Error::InternalError))?; + self.unlock(); + Ok(()) + } + + pub fn input_bip39( + &self, + prompt: Option<&str>, + ) -> Result, xous::Error> { + self.lock(); + let spec = ManagedBip39 { + token: self.token, + caption: if let Some(c) = prompt {Some(xous_ipc::String::from_str(c))} else {None}, + bip39_data: [0u8; 32], + bip39_len: 0, + }; + let mut buf = Buffer::into_buf(spec).or(Err(xous::Error::InternalError))?; + buf.lend_mut(self.conn, Opcode::Bip39Input.to_u32().unwrap()) + .or(Err(xous::Error::InternalError))?; + let result = buf.to_original::().or(Err(xous::Error::InternalError))?; + self.unlock(); + if result.bip39_len == 0 { + Err(xous::Error::InvalidString) + } else { + Ok( + result.bip39_data[..result.bip39_len as usize].to_vec() + ) + } + } + /// this blocks until the image has been dismissed. #[cfg(feature = "ditherpunk")] pub fn show_image(&self, img: &Img) -> Result<(), xous::Error> { diff --git a/services/modals/src/main.rs b/services/modals/src/main.rs index c35c20711..2f8ed0066 100644 --- a/services/modals/src/main.rs +++ b/services/modals/src/main.rs @@ -32,6 +32,7 @@ use gam::Bitmap; use xous::{msg_blocking_scalar_unpack, msg_scalar_unpack, send_message, Message}; use xous_ipc::Buffer; +use locales::t; use gam::modal::*; #[cfg(feature = "tts")] @@ -55,10 +56,15 @@ enum RendererState { RunText(ManagedPromptWithTextResponse), RunProgress(ManagedProgress), RunNotification(ManagedNotification), + RunBip39(ManagedBip39), + RunBip39Input(ManagedBip39), RunDynamicNotification(DynamicNotification), #[cfg(feature="ditherpunk")] RunImage(ManagedImage), } + +const DEFAULT_STYLE: GlyphStyle = GlyphStyle::Regular; + fn main () -> ! { #[cfg(not(feature="ditherpunk"))] wrapped_main(); @@ -126,7 +132,7 @@ fn wrapped_main() -> ! { ActionType::TextEntry(text_action.clone()), Some("Placeholder"), None, - GlyphStyle::Regular, + DEFAULT_STYLE, 8, ); renderer_modal.spawn_helper( @@ -255,6 +261,42 @@ fn wrapped_main() -> ! { ) .expect("couldn't initiate UX op"); } + Some(Opcode::Bip39) => { + let spec = { + let buffer = + unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; + buffer.to_original::().unwrap() + }; + if spec.token != token_lock.unwrap_or(default_nonce) { + log::warn!("Attempt to access modals without a mutex lock. Ignoring."); + continue; + } + op = RendererState::RunBip39(spec); + dr = Some(msg); + send_message( + renderer_cid, + Message::new_scalar(Opcode::InitiateOp.to_usize().unwrap(), 0, 0, 0, 0), + ) + .expect("couldn't initiate UX op"); + } + Some(Opcode::Bip39Input) => { + let spec = { + let buffer = + unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; + buffer.to_original::().unwrap() + }; + if spec.token != token_lock.unwrap_or(default_nonce) { + log::warn!("Attempt to access modals without a mutex lock. Ignoring."); + continue; + } + op = RendererState::RunBip39Input(spec); + dr = Some(msg); + send_message( + renderer_cid, + Message::new_scalar(Opcode::InitiateOp.to_usize().unwrap(), 0, 0, 0, 0), + ) + .expect("couldn't initiate UX op"); + } #[cfg(feature="ditherpunk")] Some(Opcode::Image) => { let spec = { @@ -439,7 +481,7 @@ fn wrapped_main() -> ! { false, None, true, - None, + Some(DEFAULT_STYLE), ); renderer_modal.activate(); log::debug!("should be active!"); @@ -467,7 +509,61 @@ fn wrapped_main() -> ! { false, None, true, + Some(DEFAULT_STYLE), + ); + renderer_modal.activate(); + } + RendererState::RunBip39(config) => { + let notification = gam::modal::Notification::new( + renderer_cid, + Opcode::NotificationReturn.to_u32().unwrap(), + ); + let mut text = String::new(); + if let Some(c) = config.caption { + text.push_str(c.as_str().unwrap()); + text.push_str("\n\n"); + } + + let phrase = renderer_modal.gam.bytes_to_bip39(&config.bip39_data[..config.bip39_len as usize].to_vec()) + .unwrap_or(vec![t!("bip39.invalid_bytes", xous::LANG).to_string()]); + + for word in phrase { + text.push_str(&word); + text.push_str(" "); + } + + #[cfg(feature = "tts")] + tts.tts_simple(&text).unwrap(); + renderer_modal.modify( + Some(ActionType::Notification(notification)), + Some(&text), + false, None, + true, + Some(GlyphStyle::Bold), + ); + renderer_modal.activate(); + } + RendererState::RunBip39Input(config) => { + let b39input = gam::modal::Bip39Entry::new( + false, + renderer_cid, + Opcode::Bip39Return.to_u32().unwrap(), + ); + let mut text = String::new(); + if let Some(c) = config.caption { + text.push_str(c.as_str().unwrap()); + } + + #[cfg(feature = "tts")] + tts.tts_simple(&text).unwrap(); + renderer_modal.modify( + Some(ActionType::Bip39Entry(b39input)), + Some(&text), + false, + None, + true, + Some(GlyphStyle::Bold), ); renderer_modal.activate(); } @@ -485,7 +581,7 @@ fn wrapped_main() -> ! { true, None, true, - None, + Some(DEFAULT_STYLE), ); renderer_modal.activate(); } @@ -510,7 +606,7 @@ fn wrapped_main() -> ! { false, None, true, - None, + Some(DEFAULT_STYLE), ); renderer_modal.activate(); } @@ -538,7 +634,7 @@ fn wrapped_main() -> ! { false, None, true, - None, + Some(DEFAULT_STYLE), ); renderer_modal.activate(); } @@ -565,7 +661,7 @@ fn wrapped_main() -> ! { false, None, true, - None, + Some(DEFAULT_STYLE), ); renderer_modal.activate(); } @@ -594,7 +690,7 @@ fn wrapped_main() -> ! { config.title.is_none(), Some(&bot_text), config.text.is_none(), - None, + Some(DEFAULT_STYLE), ); renderer_modal.activate(); } @@ -701,7 +797,7 @@ fn wrapped_main() -> ! { }), Some(Opcode::NotificationReturn) => { match op { - RendererState::RunNotification(_) => { + RendererState::RunNotification(_) | RendererState::RunBip39(_) => { op = RendererState::None; dr.take(); // unblocks the caller, but without any response data token_lock = next_lock(&mut work_queue); @@ -718,6 +814,38 @@ fn wrapped_main() -> ! { } } }, + Some(Opcode::Bip39Return) => match op { + RendererState::RunBip39Input(_config) => { + let buf = + unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; + let b39 = buf + .to_original::() + .unwrap(); + if let Some(mut origin) = dr.take() { + let mut response = unsafe { + Buffer::from_memory_message_mut( + origin.body.memory_message_mut().unwrap(), + ) + }; + let mut spec = response.to_original::().unwrap(); + spec.bip39_data[..b39.len as usize].copy_from_slice(&b39.data[..b39.len as usize]); + spec.bip39_len = b39.len; + + response.replace(spec).unwrap(); + op = RendererState::None; + } else { + log::error!("Ux routine returned but no origin was recorded"); + panic!("Ux routine returned but no origin was recorded"); + } + } + RendererState::None => { + log::warn!("Text entry detected a fat finger event, ignoring.") + } + _ => { + log::error!("UX return opcode does not match our current operation in flight. This is a serious internal error."); + panic!("UX return opcode does not match our current operation in flight. This is a serious internal error."); + } + }, #[cfg(feature="ditherpunk")] Some(Opcode::ImageReturn) => { match op { diff --git a/services/modals/src/tests.rs b/services/modals/src/tests.rs index 668073554..0c5086b41 100644 --- a/services/modals/src/tests.rs +++ b/services/modals/src/tests.rs @@ -25,6 +25,22 @@ pub fn spawn_test() { let modals = Modals::new(&xns).unwrap(); let tt = ticktimer_server::Ticktimer::new().unwrap(); + // -2. bip39 display test + let refnum = 0b00000110001101100111100111001010000110110010100010110101110011111101101010011100000110000110101100110110011111100010011100011110u128; + let refvec = refnum.to_be_bytes().to_vec(); + modals.show_bip39(Some("Some bip39 words"), &refvec) + .expect("couldn't show bip39 words"); + + // -1. bip39 input test + log::info!("type these words: alert record income curve mercy tree heavy loan hen recycle mean devote"); + match modals.input_bip39(Some("Input BIP39 words")) { + Ok(data) => { + log::info!("got bip39 input: {:x?}", data); + log::info!("reference: 0x063679ca1b28b5cfda9c186b367e271e"); + } + Err(e) => log::error!("couldn't get input: {:?}", e), + } + // 0. multi-modal test log::info!( "modal data: {:#?}",