partial progress on implementing more ui functionality

this commit might need to be backed out but i need to abort work
on this due to other more urgent issues
This commit is contained in:
bunnie 2025-06-07 01:33:13 +08:00
parent 10957971bc
commit a1d3292190
3 changed files with 187 additions and 132 deletions

View file

@ -7,13 +7,10 @@ use std::sync::{
atomic::{AtomicBool, Ordering},
};
use gam::TextEntryPayload;
use locales::t;
use num_traits::*;
use passwords::PasswordGenerator;
use pddb::BasisRetentionPolicy;
#[cfg(feature = "vaultperf")]
use perflib::*;
use persistent_store::store::OPENSK2_DICT;
use vault::env::xous::U2F_APP_DICT;
use vault::{
@ -26,24 +23,12 @@ use crate::storage::{self, PasswordRecord, StorageContent};
use crate::totp::TotpAlgorithm;
use crate::{ItemLists, SelectedEntry, VaultMode};
use crate::{ListItem, ListKey, storage::TotpRecord};
#[cfg(feature = "vaultperf")]
const FILE_ID_APPS_VAULT_SRC_ACTIONS: u32 = 1;
const VAULT_PASSWORD_REC_VERSION: u32 = 1;
const VAULT_TOTP_REC_VERSION: u32 = 1;
/// time allowed between dialog box swaps for background operations to redraw
#[cfg(feature = "ux-swap-delay")]
const SWAP_DELAY_MS: usize = 300;
#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
pub enum ActionOp {
/// Menu items
MenuAddnew,
MenuEditStage2,
MenuDeleteStage2,
MenuClose,
MenuUnlockBasis,
MenuManageBasis,
/// Internal ops
UpdateMode,
UpdateOneItem,
@ -69,14 +54,6 @@ pub struct ActionManager<'a> {
opensk_mutex: Arc<Mutex<i32>>,
mode_cache: VaultMode,
main_conn: xous::CID,
#[cfg(feature = "vaultperf")]
perfbuf: xous::MemoryRange,
#[cfg(feature = "vaultperf")]
pm: PerfMgr<'a>,
#[cfg(feature = "vaultperf")]
pid: u32,
// this is necessary to keep rustc quiet when not using `vaultperf` build option...
phantom: core::marker::PhantomData<&'a u32>,
}
impl<'a> ActionManager<'a> {
pub fn new(
@ -89,20 +66,6 @@ impl<'a> ActionManager<'a> {
let xns = xous_names::XousNames::new().unwrap();
let storage_manager = storage::Manager::new(&xns);
// notes: to use vault as the performance manager, build with `cargo xtask perf-image vault --feature
// vaultperf`. this will override shellchat as the performance manager, while enabling all the
// other performance reporting agents
#[cfg(feature = "vaultperf")]
let perfbuf = xous::syscall::map_memory(
None,
None,
BUFLEN,
xous::MemoryFlags::R | xous::MemoryFlags::W | xous::MemoryFlags::RESERVE,
)
.expect("couldn't map in the performance buffer");
#[cfg(feature = "vaultperf")]
let pm = build_perf_mgr(perfbuf.as_mut_ptr());
let mc = (*mode.lock().unwrap()).clone();
ActionManager {
modals: modals::Modals::new(&xns).unwrap(),
@ -119,13 +82,6 @@ impl<'a> ActionManager<'a> {
action_active,
opensk_mutex,
main_conn,
#[cfg(feature = "vaultperf")]
perfbuf,
#[cfg(feature = "vaultperf")]
pm,
#[cfg(feature = "vaultperf")]
pid: xous::process::id() as u32,
phantom: core::marker::PhantomData,
}
}
@ -507,89 +463,42 @@ impl<'a> ActionManager<'a> {
let choice = match entry.mode {
VaultMode::Password => Some(storage::ContentKind::Password),
VaultMode::Totp => Some(storage::ContentKind::TOTP),
VaultMode::Fido => None,
};
if choice.is_none() {
// we're dealing with FIDO stuff, use the custom code path
let dictionary = match usize::from_str_radix(entry.key_guid.as_str(), 10) {
Ok(fido_key) => {
if vault::ctap::storage::key::CREDENTIALS.contains(&fido_key) {
persistent_store::store::OPENSK2_DICT // heuristic: all fido2 keys are simple integers
} else {
U2F_APP_DICT
}
// we're deleting either a password, or a totp
let choice = choice.unwrap();
let guid = entry.key_guid.as_str();
if entry.mode == VaultMode::Password {
// self.modals.show_notification(&format!("deleting key {}", guid), None).ok();
// if it's a password, we have to pull the full record, and then reconstitute the
// item_lists index key so we can remove it from the UX cache
let storage = self.storage.borrow_mut();
let pw: storage::PasswordRecord = match storage.get_record(&choice, guid) {
Ok(record) => record,
Err(error) => {
self.report_err(t!("vault.error.internal_error", locales::LANG), Some(error));
return;
}
Err(_) => U2F_APP_DICT, // u2f keys are long hex strings
};
match self.pddb.borrow().get(
dictionary,
entry.key_guid.as_str(),
None,
false,
false,
None,
None::<fn()>,
) {
Ok(candidate) => {
let attr = candidate.attributes().expect("couldn't get key attributes");
match self.pddb.borrow().delete_key(
dictionary,
entry.key_guid.as_str(),
Some(&attr.basis),
) {
Ok(_) => {
self.modals
.show_notification(t!("vault.completed", locales::LANG), None)
.ok();
}
Err(e) => {
self.report_err(t!("vault.error.internal_error", locales::LANG), Some(e))
}
}
}
Err(e) => {
self.report_err(t!("vault.error.not_found", locales::LANG), Some(e));
}
}
} else {
// we're deleting either a password, or a totp
let choice = choice.unwrap();
let guid = entry.key_guid.as_str();
if entry.mode == VaultMode::Password {
// self.modals.show_notification(&format!("deleting key {}", guid), None).ok();
// if it's a password, we have to pull the full record, and then reconstitute the
// item_lists index key so we can remove it from the UX cache
let storage = self.storage.borrow_mut();
let pw: storage::PasswordRecord = match storage.get_record(&choice, guid) {
Ok(record) => record,
Err(error) => {
self.report_err(t!("vault.error.internal_error", locales::LANG), Some(error));
return;
}
};
let mut desc = String::with_capacity(256);
make_pw_name(&pw.description, &pw.username, &mut desc);
let key = ListKey::key_from_parts(&desc, guid);
assert!(
self.item_lists.lock().unwrap().remove(entry.mode, key).is_some(),
"requested to delete item, but it wasn't found!"
);
/*
if self.item_lists.lock().unwrap().pw.remove(&key).is_some() {
self.modals.show_notification(&format!("deleted UX {}", &key), None).ok();
} else {
self.modals.show_notification(&format!("could not delete UX {}", &key), None).ok();
}; */
}
match self.storage.borrow_mut().delete(choice, guid) {
Ok(_) => self
.modals
.show_notification(t!("vault.completed", locales::LANG), None)
.ok()
.unwrap(),
Err(e) => self.report_err(t!("vault.error.internal_error", locales::LANG), Some(e)),
let mut desc = String::with_capacity(256);
make_pw_name(&pw.description, &pw.username, &mut desc);
let key = ListKey::key_from_parts(&desc, guid);
assert!(
self.item_lists.lock().unwrap().remove(entry.mode, key).is_some(),
"requested to delete item, but it wasn't found!"
);
/*
if self.item_lists.lock().unwrap().pw.remove(&key).is_some() {
self.modals.show_notification(&format!("deleted UX {}", &key), None).ok();
} else {
self.modals.show_notification(&format!("could not delete UX {}", &key), None).ok();
}; */
}
match self.storage.borrow_mut().delete(choice, guid) {
Ok(_) => {
self.modals.show_notification(t!("vault.completed", locales::LANG), None).ok().unwrap()
}
Err(e) => self.report_err(t!("vault.error.internal_error", locales::LANG), Some(e)),
}
self.pddb.borrow().sync().ok();
}

View file

@ -7,6 +7,9 @@ mod ux;
use ux::*;
mod itemcache;
use itemcache::*;
// mod actions;
// use actions::ActionOp;
// mod storage;
mod totp;
pub(crate) const SERVER_NAME_VAULT2: &str = "_Vault2_";
@ -47,8 +50,11 @@ fn main() -> ! {
let mode = Arc::new(Mutex::new(VaultMode::Totp));
let allow_totp_rendering = Arc::new(AtomicBool::new(true));
let item_lists = Arc::new(Mutex::new(ItemLists::new()));
let action_active = Arc::new(AtomicBool::new(false));
// Protects access to the openSK PDDB entries from simultaneous readout on the UX while OpenSK is updating
let opensk_mutex = Arc::new(Mutex::new(0));
let mut vault_ui = VaultUi::new(&xns, conn, item_lists.clone());
let mut vault_ui = VaultUi::new(&xns, conn, item_lists.clone(), mode.clone());
// spawn the TOTP pumper
let pump_sid = xous::create_server().unwrap();
@ -59,6 +65,105 @@ fn main() -> ! {
let kbd = cramium_api::keyboard::Keyboard::new(&xns).unwrap();
kbd.register_listener(SERVER_NAME_VAULT2, VaultOp::KeyPress.to_u32().unwrap() as usize);
// spawn the actions server. This is responsible for grooming the UX elements. It
// has to be in its own thread because it uses blocking modal calls that would cause
// redraws of the background list to block/fail.
let actions_sid = xous::create_server().unwrap();
/*
let _ = thread::spawn({
let main_conn = conn.clone();
let sid = actions_sid.clone();
let mode = mode.clone();
let item_lists = item_lists.clone();
let action_active = action_active.clone();
let opensk_mutex = opensk_mutex.clone();
move || {
let mut manager =
crate::actions::ActionManager::new(main_conn, mode, item_lists, action_active, opensk_mutex);
loop {
let msg = xous::receive_message(sid).unwrap();
let opcode: Option<ActionOp> = FromPrimitive::from_usize(msg.body.id());
log::debug!("{:?}", opcode);
match opcode {
Some(ActionOp::MenuAddnew) => {
manager.activate();
manager.menu_addnew(); // this is responsible for updating the item cache
manager.deactivate();
}
Some(ActionOp::MenuDeleteStage2) => {
let buffer =
unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
let entry = buffer.to_original::<SelectedEntry, _>().unwrap();
manager.activate();
manager.menu_delete(entry);
manager.retrieve_db();
manager.deactivate();
}
Some(ActionOp::MenuEditStage2) => {
let buffer =
unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
let entry = buffer.to_original::<SelectedEntry, _>().unwrap();
manager.activate();
manager.menu_edit(entry); // this is responsible for updating the item cache
manager.deactivate();
}
Some(ActionOp::MenuUnlockBasis) => {
manager.activate();
manager.unlock_basis();
manager.item_lists.lock().unwrap().clear(VaultMode::Password); // clear the cached item list for passwords (totp/fido are not cached and don't need clearing)
manager.retrieve_db();
manager.deactivate();
}
Some(ActionOp::MenuManageBasis) => {
manager.activate();
manager.manage_basis();
manager.item_lists.lock().unwrap().clear(VaultMode::Password); // clear the cached item list for passwords
manager.retrieve_db();
manager.deactivate();
}
Some(ActionOp::MenuClose) => {
// dummy activate/de-activate cycle because we have to trigger a redraw of the
// underlying UX
manager.activate();
manager.deactivate();
}
Some(ActionOp::UpdateOneItem) => {
let buffer =
unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
let entry = buffer.to_original::<SelectedEntry, _>().unwrap();
manager.activate();
manager.update_db_entry(entry);
manager.deactivate();
}
Some(ActionOp::UpdateMode) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
// the password DBs are now not shared between modes, so no need to re-retrieve it.
if manager.is_db_empty() {
manager.retrieve_db();
}
xous::return_scalar(msg.sender, 1).unwrap();
}),
Some(ActionOp::ReloadDb) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
manager.retrieve_db();
xous::return_scalar(msg.sender, 1).unwrap();
}),
Some(ActionOp::Quit) => {
break;
}
None => {
log::error!("msg could not be decoded {:?}", msg);
}
#[cfg(feature = "vault-testing")]
Some(ActionOp::GenerateTests) => {
manager.populate_tests();
manager.retrieve_db();
}
}
}
xous::destroy_server(sid).ok();
}
});
*/
let actions_conn = xous::connect(actions_sid).unwrap();
// kickstart the pumper
xous::send_message(pump_conn, xous::Message::new_scalar(PumpOp::Pump.to_usize().unwrap(), 0, 0, 0, 0))
.expect("couldn't start the pumper");
@ -69,18 +174,29 @@ fn main() -> ! {
match FromPrimitive::from_usize(msg.body.id()) {
Some(VaultOp::Redraw) => {
log::debug!("Got redraw");
match *mode.lock().unwrap() {
VaultMode::Totp => {
vault_ui.redraw_totp();
}
_ => {
unimplemented!()
}
}
vault_ui.redraw();
}
Some(VaultOp::KeyPress) => xous::msg_scalar_unpack!(msg, k1, _k2, _k3, _k4, {
let k = char::from_u32(k1 as u32).unwrap_or('\u{0000}');
log::info!("got key {}", k);
log::info!("key {}", k);
match k {
'↓' => {
vault_ui.nav(NavDir::Down);
}
'↑' => {
vault_ui.nav(NavDir::Up);
}
'←' => {
vault_ui.nav(NavDir::PageUp);
}
'→' => {
vault_ui.nav(NavDir::PageDown);
}
_ => {
log::info!("unhandled key {}", k);
}
}
vault_ui.redraw();
}),
_ => {
log::error!("Got unknown message");

View file

@ -152,6 +152,36 @@ impl VaultUi {
self.gfx.draw_rectangle(timer_remaining).ok();
}
VaultMode::Password => {
/*
Password UI --
Home menu:
> Search by QR code
> Search by text entry
> Enter new by QR code
> Enter new by text entry
Prefix entered:
---------------------
| Selected Domain |
| Selected Username |
---------------------
> Domain short 1
> Domain short 2
> Domain short 3
> Domain short 4
> search prefix <
- Scrolling down to search prefix and hitting home brings up keyboard UI
- Left-right would page through prefixed entries
- Up down would page through just the current page of entries (allowing selection of search prefix region)
- Selecting search prefix brings up the alphabetic entry UI
- Pressing in on scroll button raises a new menu for "autotype password" + "delete passsword" + "edit password" options
- Pressing on circle middle button just does autotype
*/
todo!()
}
}