hw plugins: adapt trezor_qt_pinmatrix.py to qt6
This fixes the pinmatrix dialog (used by trezor one, keepkey, safet), which was previously segfaulting. follow-up https://github.com/spesmilo/electrum/pull/9189
This commit is contained in:
@@ -17,37 +17,21 @@
|
|||||||
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
|
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
|
||||||
|
|
||||||
import math
|
import math
|
||||||
import sys
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
try:
|
from PyQt6.QtCore import QRegularExpression, Qt
|
||||||
from PyQt5.QtCore import QT_VERSION_STR, QRegExp, Qt
|
from PyQt6.QtGui import QRegularExpressionValidator
|
||||||
from PyQt5.QtGui import QRegExpValidator
|
from PyQt6.QtWidgets import (
|
||||||
from PyQt5.QtWidgets import (
|
QGridLayout,
|
||||||
QApplication,
|
QHBoxLayout,
|
||||||
QGridLayout,
|
QLabel,
|
||||||
QHBoxLayout,
|
QLineEdit,
|
||||||
QLabel,
|
QPushButton,
|
||||||
QLineEdit,
|
QSizePolicy,
|
||||||
QPushButton,
|
QVBoxLayout,
|
||||||
QSizePolicy,
|
QWidget,
|
||||||
QVBoxLayout,
|
)
|
||||||
QWidget,
|
|
||||||
)
|
|
||||||
except Exception:
|
|
||||||
from PyQt4.QtCore import QT_VERSION_STR, SIGNAL, QObject, QRegExp, Qt # noqa: I
|
|
||||||
from PyQt4.QtGui import ( # noqa: I
|
|
||||||
QApplication,
|
|
||||||
QGridLayout,
|
|
||||||
QHBoxLayout,
|
|
||||||
QLabel,
|
|
||||||
QLineEdit,
|
|
||||||
QPushButton,
|
|
||||||
QRegExpValidator,
|
|
||||||
QSizePolicy,
|
|
||||||
QVBoxLayout,
|
|
||||||
QWidget,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class PinButton(QPushButton):
|
class PinButton(QPushButton):
|
||||||
@@ -56,12 +40,7 @@ class PinButton(QPushButton):
|
|||||||
self.password = password
|
self.password = password
|
||||||
self.encoded_value = encoded_value
|
self.encoded_value = encoded_value
|
||||||
|
|
||||||
if QT_VERSION_STR >= "5":
|
self.clicked.connect(self._pressed)
|
||||||
self.clicked.connect(self._pressed)
|
|
||||||
elif QT_VERSION_STR >= "4":
|
|
||||||
QObject.connect(self, SIGNAL("clicked()"), self._pressed)
|
|
||||||
else:
|
|
||||||
raise RuntimeError("Unsupported Qt version")
|
|
||||||
|
|
||||||
def _pressed(self) -> None:
|
def _pressed(self) -> None:
|
||||||
self.password.setText(self.password.text() + str(self.encoded_value))
|
self.password.setText(self.password.text() + str(self.encoded_value))
|
||||||
@@ -81,21 +60,14 @@ class PinMatrixWidget(QWidget):
|
|||||||
super(PinMatrixWidget, self).__init__(parent)
|
super(PinMatrixWidget, self).__init__(parent)
|
||||||
|
|
||||||
self.password = QLineEdit()
|
self.password = QLineEdit()
|
||||||
self.password.setValidator(QRegExpValidator(QRegExp("[1-9]+"), None))
|
self.password.setValidator(QRegularExpressionValidator(QRegularExpression("[1-9]+"), None))
|
||||||
self.password.setEchoMode(QLineEdit.Password)
|
self.password.setEchoMode(QLineEdit.EchoMode.Password)
|
||||||
|
|
||||||
if QT_VERSION_STR >= "5":
|
self.password.textChanged.connect(self._password_changed)
|
||||||
self.password.textChanged.connect(self._password_changed)
|
|
||||||
elif QT_VERSION_STR >= "4":
|
|
||||||
QObject.connect(
|
|
||||||
self.password, SIGNAL("textChanged(QString)"), self._password_changed
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
raise RuntimeError("Unsupported Qt version")
|
|
||||||
|
|
||||||
self.strength = QLabel()
|
self.strength = QLabel()
|
||||||
self.strength.setMinimumWidth(75)
|
self.strength.setMinimumWidth(75)
|
||||||
self.strength.setAlignment(Qt.AlignCenter)
|
self.strength.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
||||||
self._set_strength(0)
|
self._set_strength(0)
|
||||||
|
|
||||||
grid = QGridLayout()
|
grid = QGridLayout()
|
||||||
@@ -103,8 +75,8 @@ class PinMatrixWidget(QWidget):
|
|||||||
for y in range(3)[::-1]:
|
for y in range(3)[::-1]:
|
||||||
for x in range(3):
|
for x in range(3):
|
||||||
button = PinButton(self.password, x + y * 3 + 1)
|
button = PinButton(self.password, x + y * 3 + 1)
|
||||||
button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
|
button.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
|
||||||
button.setFocusPolicy(Qt.NoFocus)
|
button.setFocusPolicy(Qt.FocusPolicy.NoFocus)
|
||||||
grid.addWidget(button, 3 - y, x)
|
grid.addWidget(button, 3 - y, x)
|
||||||
|
|
||||||
hbox = QHBoxLayout()
|
hbox = QHBoxLayout()
|
||||||
@@ -141,36 +113,3 @@ class PinMatrixWidget(QWidget):
|
|||||||
|
|
||||||
def get_value(self) -> str:
|
def get_value(self) -> str:
|
||||||
return self.password.text()
|
return self.password.text()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
"""
|
|
||||||
Demo application showing PinMatrix widget in action
|
|
||||||
"""
|
|
||||||
app = QApplication(sys.argv)
|
|
||||||
|
|
||||||
matrix = PinMatrixWidget()
|
|
||||||
|
|
||||||
def clicked() -> None:
|
|
||||||
print("PinMatrix value is", matrix.get_value())
|
|
||||||
print("Possible button combinations:", matrix.get_strength())
|
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
ok = QPushButton("OK")
|
|
||||||
if QT_VERSION_STR >= "5":
|
|
||||||
ok.clicked.connect(clicked)
|
|
||||||
elif QT_VERSION_STR >= "4":
|
|
||||||
QObject.connect(ok, SIGNAL("clicked()"), clicked)
|
|
||||||
else:
|
|
||||||
raise RuntimeError("Unsupported Qt version")
|
|
||||||
|
|
||||||
vbox = QVBoxLayout()
|
|
||||||
vbox.addWidget(matrix)
|
|
||||||
vbox.addWidget(ok)
|
|
||||||
|
|
||||||
w = QWidget()
|
|
||||||
w.setLayout(vbox)
|
|
||||||
w.move(100, 100)
|
|
||||||
w.show()
|
|
||||||
|
|
||||||
app.exec_()
|
|
||||||
|
|||||||
@@ -15,8 +15,10 @@ from electrum.i18n import _
|
|||||||
from electrum.plugin import hook
|
from electrum.plugin import hook
|
||||||
from electrum.logging import Logger
|
from electrum.logging import Logger
|
||||||
|
|
||||||
from ..hw_wallet.qt import QtHandlerBase, QtPluginBase
|
from electrum.plugins.hw_wallet.qt import QtHandlerBase, QtPluginBase
|
||||||
from ..hw_wallet.plugin import only_hook_if_libraries_available
|
from electrum.plugins.hw_wallet.trezor_qt_pinmatrix import PinMatrixWidget
|
||||||
|
from electrum.plugins.hw_wallet.plugin import only_hook_if_libraries_available
|
||||||
|
|
||||||
from .keepkey import KeepKeyPlugin, TIM_NEW, TIM_RECOVER, TIM_MNEMONIC, TIM_PRIVKEY
|
from .keepkey import KeepKeyPlugin, TIM_NEW, TIM_RECOVER, TIM_MNEMONIC, TIM_PRIVKEY
|
||||||
|
|
||||||
from electrum.gui.qt.wizard.wallet import WCScriptAndDerivation, WCHWUnlock, WCHWXPub, WalletWizardComponent
|
from electrum.gui.qt.wizard.wallet import WCScriptAndDerivation, WCHWUnlock, WCHWXPub, WalletWizardComponent
|
||||||
@@ -318,7 +320,6 @@ class Plugin(KeepKeyPlugin, QtPlugin):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def pin_matrix_widget_class(self):
|
def pin_matrix_widget_class(self):
|
||||||
from keepkeylib.qt.pinmatrix import PinMatrixWidget
|
|
||||||
return PinMatrixWidget
|
return PinMatrixWidget
|
||||||
|
|
||||||
@hook
|
@hook
|
||||||
|
|||||||
@@ -15,8 +15,10 @@ from electrum.i18n import _
|
|||||||
from electrum.plugin import hook
|
from electrum.plugin import hook
|
||||||
from electrum.logging import Logger
|
from electrum.logging import Logger
|
||||||
|
|
||||||
from ..hw_wallet.qt import QtHandlerBase, QtPluginBase
|
from electrum.plugins.hw_wallet.qt import QtHandlerBase, QtPluginBase
|
||||||
from ..hw_wallet.plugin import only_hook_if_libraries_available
|
from electrum.plugins.hw_wallet.trezor_qt_pinmatrix import PinMatrixWidget
|
||||||
|
from electrum.plugins.hw_wallet.plugin import only_hook_if_libraries_available
|
||||||
|
|
||||||
from .safe_t import SafeTPlugin, TIM_NEW, TIM_RECOVER, TIM_MNEMONIC, TIM_PRIVKEY
|
from .safe_t import SafeTPlugin, TIM_NEW, TIM_RECOVER, TIM_MNEMONIC, TIM_PRIVKEY
|
||||||
|
|
||||||
from electrum.gui.qt.wizard.wallet import WCScriptAndDerivation, WCHWUnlock, WCHWXPub, WalletWizardComponent
|
from electrum.gui.qt.wizard.wallet import WCScriptAndDerivation, WCHWUnlock, WCHWXPub, WalletWizardComponent
|
||||||
@@ -194,7 +196,6 @@ class Plugin(SafeTPlugin, QtPlugin):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def pin_matrix_widget_class(self):
|
def pin_matrix_widget_class(self):
|
||||||
from safetlib.qt.pinmatrix import PinMatrixWidget
|
|
||||||
return PinMatrixWidget
|
return PinMatrixWidget
|
||||||
|
|
||||||
@hook
|
@hook
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ from electrum.plugin import hook
|
|||||||
from electrum.keystore import ScriptTypeNotSupported
|
from electrum.keystore import ScriptTypeNotSupported
|
||||||
|
|
||||||
from electrum.plugins.hw_wallet.qt import QtHandlerBase, QtPluginBase
|
from electrum.plugins.hw_wallet.qt import QtHandlerBase, QtPluginBase
|
||||||
|
from electrum.plugins.hw_wallet.trezor_qt_pinmatrix import PinMatrixWidget
|
||||||
from electrum.plugins.hw_wallet.plugin import only_hook_if_libraries_available, OutdatedHwFirmwareException
|
from electrum.plugins.hw_wallet.plugin import only_hook_if_libraries_available, OutdatedHwFirmwareException
|
||||||
|
|
||||||
from electrum.gui.qt.util import (WindowModalDialog, WWLabel, Buttons, CancelButton,
|
from electrum.gui.qt.util import (WindowModalDialog, WWLabel, Buttons, CancelButton,
|
||||||
@@ -462,7 +463,6 @@ class Plugin(TrezorPlugin, QtPlugin):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def pin_matrix_widget_class(self):
|
def pin_matrix_widget_class(self):
|
||||||
from trezorlib.qt.pinmatrix import PinMatrixWidget
|
|
||||||
return PinMatrixWidget
|
return PinMatrixWidget
|
||||||
|
|
||||||
@hook
|
@hook
|
||||||
|
|||||||
Reference in New Issue
Block a user