Skip to content

Commit

Permalink
*Improve reliability of processes when connecting over PN532 NFC
Browse files Browse the repository at this point in the history
*Improve error messages
  • Loading branch information
3rdIteration committed Dec 23, 2023
1 parent de5199f commit e148d60
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 33 deletions.
57 changes: 37 additions & 20 deletions src/seedsigner/helpers/seedkeeper_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,40 @@
from os import urandom

def init_satochip(parentObject):
from seedsigner.models.settings import Settings, SettingsConstants, SettingsDefinition

parentObject.loading_screen = LoadingScreenThread(text="Searching for Card")
parentObject.loading_screen.start()

# Spam connecting for 10 seconds to give the user time to insert the card
# Spam connecting for 5 seconds to give the user time to insert the card
status = None
time_end = time.time() + 10
time_end = time.time() + 5
while time.time() < time_end:
try:
Satochip_Connector = CardConnector()
time.sleep(0.1) # give some time to initialize reader...
time.sleep(1) # give some time to initialize reader...
status = Satochip_Connector.card_get_status()
break

print("Found Card:")
print(status[3])

if (Satochip_Connector.needs_secure_channel):
print("Initiating Secure Channel")
Satochip_Connector.card_initiate_secure_channel()
print("Secure Channel Initialised")

if len(status[3]) > 0: #Sometimes it's possible to end up with an invalid of zero length here...
break
except Exception as e:
print(e)
time.sleep(0.1) # Sleep for 100ms

status = None # Reset this every loop...

parentObject.loading_screen.stop()

if not status:

parentObject.run_screen(
WarningScreen,
title="Unable to Connect",
Expand All @@ -39,39 +54,32 @@ def init_satochip(parentObject):
return None

status = Satochip_Connector.card_get_status()
print("Found Card:")
print(status[3])

if (Satochip_Connector.needs_secure_channel):
print("Initiating Secure Channel")
Satochip_Connector.card_initiate_secure_channel()

if status[3]['setup_done']:
ret = seed_screens.SeedAddPassphraseScreen(title="Card PIN").display()

if ret == RET_CODE__BACK_BUTTON:
card_pin = seed_screens.SeedAddPassphraseScreen(title="Card PIN").display()
if card_pin == RET_CODE__BACK_BUTTON:
return None

Satochip_Connector.set_pin(0, list(bytes(ret, "utf-8")))
Satochip_Connector.set_pin(0, list(bytes(card_pin, "utf-8")))

try:
print("Verifying PIN")
(response, sw1, sw2) = Satochip_Connector.card_verify_PIN()
if sw1 == 0x90 and sw2 == 0x00:
print("Pin Correct")
pass #Pin is correct
else:
# Pin is not incorrect, but isn't valid either (doesn't increment the failed pin counter)
print("Failure: Invalid PIN")
parentObject.run_screen(
WarningScreen,
title="Invalid PIN",
title="Failure",
status_headline=None,
text=f"Invalid PIN entered, select another and try again.",
text=f"Failed, Code:" + str(sw1) + " " + str(sw2),
show_back_button=True,
)
return None

except RuntimeError as e: #Incorrect PIN
print("RunTimeError")
print(e) #
status = Satochip_Connector.card_get_status()
pin_tries_left = status[3]['PIN0_remaining_tries']
Expand All @@ -92,6 +100,16 @@ def init_satochip(parentObject):
show_back_button=True,
)
return None

except Exception as e:
parentObject.run_screen(
WarningScreen,
title="Failed",
status_headline=None,
text=str(e),
show_back_button=True,
)
return None
else:
print("Card Needs Initial Setup")
parentObject.run_screen(
Expand All @@ -102,7 +120,6 @@ def init_satochip(parentObject):
show_back_button=True,
)


ret = seed_screens.SeedAddPassphraseScreen(title="Card PIN").display()

if ret == RET_CODE__BACK_BUTTON:
Expand Down
20 changes: 8 additions & 12 deletions src/seedsigner/views/seed_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,12 @@ class SeedKeeperSelectView(View):
def run(self):
try:
Satochip_Connector = seedkeeper_utils.init_satochip(self)

if not Satochip_Connector:
return Destination(BackStackView)

headers = Satochip_Connector.seedkeeper_list_secret_headers()

print(headers)
headers_parsed = []
button_data = []
for header in headers:
Expand All @@ -240,23 +239,23 @@ def run(self):
title="No Secrets to Load",
status_headline=None,
text=f"No BIP39 Secrets to Load from Seedkeeper",
show_back_button=True,
show_back_button=False,
)
return Destination(BackStackView)

selected_menu_num = self.run_screen(
ButtonListScreen,
title="Select Secret",
is_button_text_centered=False,
button_data=button_data
button_data=button_data,
show_back_button=True,
)

print(type(headers_parsed[selected_menu_num][0]))
if selected_menu_num == RET_CODE__BACK_BUTTON:
return Destination(BackStackView)

secret_dict = Satochip_Connector.seedkeeper_export_secret(headers_parsed[selected_menu_num][0], None)

print(secret_dict)

secret_dict['secret'] = unhexlify(secret_dict['secret'])[1:].decode().rstrip("\x00")

bip39_secret = secret_dict['secret']
Expand All @@ -266,21 +265,18 @@ def run(self):
secret_mnemonic = bip39_secret[:secret_size]
secret_passphrase = bip39_secret[secret_size+1:]

print("BIP39-Mnemonic:", secret_mnemonic, "BIP39-Passphrase:", secret_passphrase)

except Exception as e:
print(e)
self.run_screen(
WarningScreen,
title="Unknown Error...",
title="Error",
status_headline=None,
text=f"Load from Seedkeeper Failed",
text=str(e),
show_back_button=True,
)
return Destination(BackStackView)

mnemonic = secret_mnemonic.split(" ")
print(len(mnemonic))
self.controller.storage.init_pending_mnemonic(num_words=len(mnemonic))
for i, word in enumerate(mnemonic):
self.controller.storage.update_pending_mnemonic(word, i)
Expand Down
2 changes: 1 addition & 1 deletion src/seedsigner/views/settings_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from seedsigner.gui.components import SeedSignerIconConstants
from seedsigner.hardware.microsd import MicroSD

from .view import View, Destination, MainMenuView
from .view import View, Destination, MainMenuView, BackStackView

from seedsigner.gui.screens import (RET_CODE__BACK_BUTTON, ButtonListScreen, settings_screens)
from seedsigner.models.settings import Settings, SettingsConstants, SettingsDefinition
Expand Down

0 comments on commit e148d60

Please sign in to comment.