pwsave improve exception handling

This commit is contained in:
scgbckbone 2024-01-18 12:15:13 +01:00 committed by doc-hex
parent 2c46a22679
commit b1a7d2b1b2
2 changed files with 48 additions and 40 deletions

View File

@ -5,7 +5,7 @@
import stash, ujson, ngu, pyb, os, aes256ctr
from files import CardSlot, CardMissingError, needs_microsd
from ux import ux_dramatic_pause, ux_confirm, ux_show_story
from utils import xfp2str
from utils import xfp2str, problem_file_line
from menu import MenuItem, MenuSystem
@ -59,38 +59,30 @@ class PassphraseSaver:
fd.write(msg)
async def delete(self, idx):
try:
with CardSlot() as card:
self._calc_key(card)
data = self._read(card)
with CardSlot() as card:
self._calc_key(card)
data = self._read(card)
try:
del data[idx]
except IndexError: pass
try:
del data[idx]
except IndexError: pass
await self._save(card, data)
if not data:
return True # is empty
except CardMissingError:
await needs_microsd()
await self._save(card, data)
if not data:
return True # is empty
async def append(self, xfp, bip39pw):
from glob import dis
dis.fullscreen('Reading...')
try:
with CardSlot() as card:
self._calc_key(card)
data = self._read(card)
with CardSlot() as card:
self._calc_key(card)
data = self._read(card)
to_add = dict(xfp=xfp, pw=bip39pw)
if to_add not in data:
dis.fullscreen('Saving...')
data.append(to_add)
await self._save(card, data)
except CardMissingError:
await needs_microsd()
to_add = dict(xfp=xfp, pw=bip39pw)
if to_add not in data:
dis.fullscreen('Saving...')
data.append(to_add)
await self._save(card, data)
class PassphraseSaverMenu(MenuSystem):
@ -160,20 +152,28 @@ class PassphraseSaverMenu(MenuSystem):
pw_saver, i = item.arg
if await ux_confirm("Delete saved passphrase?"):
dis.fullscreen("Wait...")
is_empty = await pw_saver.delete(i)
the_ux.pop()
if not is_empty:
m = the_ux.top_of_stack()
m.update_contents()
else:
# remove .tmp.tmp file after last passphrase
# is deleted
with CardSlot() as card:
f_path = pw_saver.filename(card)
os.remove(f_path)
try:
is_empty = await pw_saver.delete(i)
the_ux.pop()
m = the_ux.top_of_stack()
m.update_contents()
if not is_empty:
m = the_ux.top_of_stack()
m.update_contents()
else:
# remove .tmp.tmp file after last passphrase
# is deleted
with CardSlot() as card:
f_path = pw_saver.filename(card)
os.remove(f_path)
the_ux.pop()
m = the_ux.top_of_stack()
m.update_contents()
except CardMissingError:
await needs_microsd()
except Exception as e:
await ux_show_story(
title="ERROR",
msg='Delete failed!\n\n%s\n%s' % (e, problem_file_line(e))
)
@classmethod
def construct(cls):

View File

@ -1265,7 +1265,15 @@ class PassphraseMenu(MenuSystem):
await set_ephemeral_seed(nv, summarize_ux=False, bip39pw=pp_sofar,
meta="BIP-39 Passphrase on [%s]" % parent_xfp_str)
if ch == '1':
await PassphraseSaver().append(xfp, pp_sofar)
try:
await PassphraseSaver().append(xfp, pp_sofar)
except CardMissingError:
await needs_microsd()
except Exception as e:
await ux_show_story(
title="ERROR",
msg='Save failed!\n\n%s\n%s' % (e, problem_file_line(e))
)
goto_top_menu()