Skip to content

Commit

Permalink
satochip: fixes and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
accumulator committed Nov 13, 2024
1 parent b06e332 commit f3a230d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
39 changes: 23 additions & 16 deletions electrum/plugins/satochip/qt.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
from functools import partial
from os import urandom
import textwrap
import threading

from electrum.i18n import _
from electrum.logging import get_logger
from electrum.keystore import bip39_is_checksum_valid
from electrum.util import UserFacingException
from electrum.simple_config import SimpleConfig
from electrum.gui.qt.util import (EnterButton, Buttons, CloseButton, icon_path,
OkButton, CancelButton, WindowModalDialog, WWLabel, PasswordLineEdit)
from electrum.gui.qt.qrcodewidget import QRDialog
from electrum.gui.qt.wizard.wallet import (WCHaveSeed, WCEnterExt, WCScriptAndDerivation,
WCHWUnlock, WCHWXPub, WalletWizardComponent, QENewWalletWizard)
WCHWUnlock, WCHWXPub, WalletWizardComponent, QENewWalletWizard)
from electrum.plugin import hook
from electrum.plugins.hw_wallet.qt import QtHandlerBase, QtPluginBase

from PyQt6.QtGui import QPixmap
from PyQt6.QtCore import Qt, pyqtSignal
from PyQt6.QtWidgets import (QPushButton, QLabel, QVBoxLayout, QHBoxLayout,
QWidget, QGridLayout, QComboBox, QLineEdit, QTextEdit, QTabWidget)

from functools import partial
from os import urandom
import textwrap
import threading
QWidget, QGridLayout, QComboBox, QLineEdit, QTabWidget)

# satochip
from .satochip import SatochipPlugin
from ..hw_wallet.qt import QtHandlerBase, QtPluginBase

# pysatochip
from pysatochip.CardConnector import UnexpectedSW12Error, CardError, CardNotPresentError, WrongPinError
Expand Down Expand Up @@ -49,6 +50,7 @@
_("If set, you will need your passphrase along with your BIP39 seed to restore your wallet from a backup. "),
]


class Plugin(SatochipPlugin, QtPluginBase):
icon_unpaired = "satochip_unpaired.png"
icon_paired = "satochip.png"
Expand Down Expand Up @@ -104,7 +106,8 @@ def extend_wizard(self, wizard: 'QENewWalletWizard'):
},
'satochip_have_seed': {
'gui': WCHaveSeed,
'next': lambda d: 'satochip_have_ext' if wizard.wants_ext(d) else 'satochip_import_seed'
'next': lambda d: 'satochip_have_ext' if wizard.wants_ext(d) else 'satochip_import_seed',
'params': {'seed_options': ['ext', 'bip39']}
},
'satochip_have_ext': {
'gui': WCEnterExt,
Expand Down Expand Up @@ -255,10 +258,14 @@ def _change_card_label():

def show_values(self, client):
_logger.info("Show value!")
is_ok = client.verify_PIN()
if not is_ok:
msg = f"action cancelled by user"
self.window.show_error(msg)
try:
is_ok = client.verify_PIN()
if not is_ok:
msg = f"action cancelled by user"
self.window.show_error(msg)
return
except UserFacingException as e:
self.window.show_error(str(e))
return

sw_rel = 'v' + str(SATOCHIP_PROTOCOL_MAJOR_VERSION) + \
Expand Down Expand Up @@ -412,7 +419,7 @@ def reset_seed_dialog(self, msg):
parent = self.top_level_window()
d = WindowModalDialog(parent, _("Enter PIN"))
pw = QLineEdit()
pw.setEchoMode(2)
pw.setEchoMode(QLineEdit.EchoMode.Password)
pw.setMinimumWidth(200)

vbox = QVBoxLayout()
Expand Down Expand Up @@ -645,7 +652,6 @@ def change_card_label_dialog(self, client, msg):
parent = self.top_level_window()
d = WindowModalDialog(parent, _("Enter Label"))
pw = QLineEdit()
pw.setEchoMode(0)
pw.setMinimumWidth(200)

vbox = QVBoxLayout()
Expand Down Expand Up @@ -929,6 +935,7 @@ def apply(self):
# Import seed wizard #
##########################


class WCSeedMessage(WalletWizardComponent):
def __init__(self, parent, wizard):
WalletWizardComponent.__init__(self, parent, wizard, title=_('Satochip needs a seed'))
Expand Down
7 changes: 5 additions & 2 deletions electrum/plugins/satochip/satochip.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from ..hw_wallet import HW_PluginBase, HardwareClientBase

# pysatochip
from pysatochip.CardConnector import CardConnector
from pysatochip.CardConnector import CardConnector, UninitializedSeedError
from pysatochip.CardConnector import CardNotPresentError, UnexpectedSW12Error, WrongPinError, PinBlockedError, PinRequiredError
from pysatochip.Satochip2FA import Satochip2FA, SERVER_LIST

Expand Down Expand Up @@ -163,7 +163,10 @@ def get_xpub(self, bip32_path, xtype):
# bip32_path is of the form 44'/0'/1'
_logger.info(f"[SatochipClient] get_xpub(): bip32_path={bip32_path}")
(depth, bytepath) = bip32path2bytes(bip32_path)
(childkey, childchaincode) = self.cc.card_bip32_get_extendedkey(bytepath)
try:
(childkey, childchaincode) = self.cc.card_bip32_get_extendedkey(bytepath)
except UninitializedSeedError as e:
raise UserFacingException(str(e))
if depth == 0: # masterkey
fingerprint = bytes([0, 0, 0, 0])
child_number = bytes([0, 0, 0, 0])
Expand Down

0 comments on commit f3a230d

Please sign in to comment.