Skip to content

Commit

Permalink
Add MicroSD PSBT Setting in Advanced
Browse files Browse the repository at this point in the history
  • Loading branch information
newtonick committed Dec 9, 2022
1 parent bcdc07a commit 2e5993d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 28 deletions.
50 changes: 26 additions & 24 deletions src/seedsigner/hardware/microsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from seedsigner.models.singleton import Singleton
from seedsigner.models.threads import BaseThread
from seedsigner.models.settings import Settings
from seedsigner.models.settings import Settings, SettingsConstants
#from seedsigner.views.view import MicroSDToastView
from seedsigner.gui.screens.screen import MicroSDToastScreen

Expand Down Expand Up @@ -73,27 +73,29 @@ def find_psbt_files(self):
self.psbt_files = []
# only populate psbt files from the microsd in seedsigner-os
if Settings.HOSTNAME == Settings.SEEDSIGNER_OS:
for filepath in sorted(glob.glob(MicroSD.MOUNT_LOCATION + '*')):
if os.path.isfile(filepath):
with open(filepath, 'rb') as psbt_file:
file_header = psbt_file.read(MicroSD.MAGIC_MAX_LENGTH)
# only populate psbt files if setting is enabled
if Settings.get_instance().get_value(SettingsConstants.SETTING__MICROSD_PSBT) == SettingsConstants.OPTION__ENABLED:
for filepath in sorted(glob.glob(MicroSD.MOUNT_LOCATION + '*')):
if os.path.isfile(filepath):
with open(filepath, 'rb') as psbt_file:
file_header = psbt_file.read(MicroSD.MAGIC_MAX_LENGTH)

# binary psbt file check
if file_header.startswith(MicroSD.MAGIC):
self.psbt_files.append({
"name": os.path.splitext(os.path.basename(filepath))[0],
"filename": os.path.basename(filepath),
"filepath": filepath,
"type": "binary"
})
# base64 psbt file check
elif file_header.startswith(MicroSD.MAGIC_BASE64):
self.psbt_files.append({
"name": os.path.splitext(os.path.basename(filepath))[0],
"filename": os.path.basename(filepath),
"filepath": filepath,
"type": "base64"
})

# binary psbt file check
if file_header.startswith(MicroSD.MAGIC):
self.psbt_files.append({
"name": os.path.splitext(os.path.basename(filepath))[0],
"filename": os.path.basename(filepath),
"filepath": filepath,
"type": "binary"
})
# base64 psbt file check
elif file_header.startswith(MicroSD.MAGIC_BASE64):
self.psbt_files.append({
"name": os.path.splitext(os.path.basename(filepath))[0],
"filename": os.path.basename(filepath),
"filepath": filepath,
"type": "base64"
})

# sort the list by name of file without extension
self.psbt_files = sorted(self.psbt_files, key=lambda d: d['name'])
# sort the list by name of file without extension
self.psbt_files = sorted(self.psbt_files, key=lambda d: d['name'])
8 changes: 7 additions & 1 deletion src/seedsigner/models/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,14 @@ def set_value(self, attr_name: str, value: any):
print(f"Removed {self.SETTINGS_FILENAME}")
except:
print(f"{self.SETTINGS_FILENAME} not found to be removed")

self._data[attr_name] = value

# Special handling for MicroSD PSBT setting
if attr_name == SettingsConstants.SETTING__MICROSD_PSBT:
from seedsigner.hardware.microsd import MicroSD
MicroSD.get_instance().find_psbt_files()

self.save()


Expand Down
7 changes: 7 additions & 0 deletions src/seedsigner/models/settings_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ def map_network_to_embit(cls, network) -> str:
SETTING__CAMERA_ROTATION = "camera_rotation"
SETTING__COMPACT_SEEDQR = "compact_seedqr"
SETTING__BIP85_CHILD_SEEDS = "bip85_child_seeds"
SETTING__MICROSD_PSBT = "microsd_psbt"
SETTING__PRIVACY_WARNINGS = "privacy_warnings"
SETTING__DIRE_WARNINGS = "dire_warnings"
SETTING__PARTNER_LOGOS = "partner_logos"
Expand Down Expand Up @@ -446,6 +447,12 @@ class SettingsDefinition:
visibility=SettingsConstants.VISIBILITY__ADVANCED,
default_value=SettingsConstants.OPTION__DISABLED),

SettingsEntry(category=SettingsConstants.CATEGORY__FEATURES,
attr_name=SettingsConstants.SETTING__MICROSD_PSBT,
display_name="MicroSD PSBT",
visibility=SettingsConstants.VISIBILITY__ADVANCED,
default_value=SettingsConstants.OPTION__DISABLED),

SettingsEntry(category=SettingsConstants.CATEGORY__FEATURES,
attr_name=SettingsConstants.SETTING__PRIVACY_WARNINGS,
display_name="Show privacy warnings",
Expand Down
24 changes: 21 additions & 3 deletions src/seedsigner/views/psbt_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ def run(self):

# Read PSBT file select if it exists
psbt_file = self.controller.microsd.psbt_files[selected_menu_num]
tx = PSBT.PSBT
tx = PSBT

if os.path.exists(psbt_file["filepath"]):
with open(psbt_file["filepath"], 'rb') as f:
data = f.read()

if psbt_file["type"] == "binary":
tx = psbt.PSBT.parse(data)
tx = PSBT.parse(data)
elif psbt_file["type"] == "base64":
raw = a2b_base64(data)
tx = psbt.PSBT.parse(raw)
tx = PSBT.parse(raw)
self.controller.psbt = tx
self.controller.psbt_parser = None
self.controller.psbt_file = psbt_file
Expand Down Expand Up @@ -573,6 +573,24 @@ def run(self):
signed_filepath = MicroSD.MOUNT_LOCATION + signed_filename
increment = 0

# verify microsd directory exists and is writable, display message warning on failure
selected_menu_num = 0
while (not os.path.exists(MicroSD.MOUNT_LOCATION) or not os.access(MicroSD.MOUNT_LOCATION, os.W_OK)):
selected_menu_num = WarningScreen(
status_headline="MicroSD Card Missing!",
text="MicroSD Card is required to write out signed PSBT file.",
button_data=["Continue", "Exit"],
).display()

# if users chooses to exit, consider the PSBT workflow complete
if selected_menu_num == 1:
# We're done with this PSBT. Remove all related data
self.controller.psbt = None
self.controller.psbt_parser = None
self.controller.psbt_seed = None
self.controller.psbt_file = None
return Destination(MainMenuView)

# if signed filename already exists on disk, add incremented number to the end of the file name
while os.path.exists(signed_filepath):
increment += 1
Expand Down

0 comments on commit 2e5993d

Please sign in to comment.