Merge pull request #2 from wozz/trezor_plugin

Trezor plugin
This commit is contained in:
m0mchil 2014-08-03 09:38:18 +03:00
commit c3f0490711
1 changed files with 50 additions and 14 deletions

View File

@ -1,16 +1,17 @@
from PyQt4.Qt import QMessageBox, QDialog, QVBoxLayout, QLabel from PyQt4.Qt import QMessageBox, QDialog, QVBoxLayout, QLabel, QThread, SIGNAL, QObject
from binascii import unhexlify from binascii import unhexlify
from struct import pack from struct import pack
from sys import stderr from sys import stderr
from time import sleep
from gui.qt.password_dialog import make_password_dialog, run_password_dialog from electrum_gui.qt.password_dialog import make_password_dialog, run_password_dialog
from gui.qt.util import ok_cancel_buttons from electrum_gui.qt.util import ok_cancel_buttons
from lib.account import BIP32_Account from electrum.account import BIP32_Account
from lib.bitcoin import EncodeBase58Check from electrum.bitcoin import EncodeBase58Check
from lib.i18n import _ from electrum.i18n import _
from lib.plugins import BasePlugin from electrum.plugins import BasePlugin
from lib.transaction import deserialize from electrum.transaction import deserialize
from lib.wallet import NewWallet from electrum.wallet import NewWallet
try: try:
@ -27,6 +28,7 @@ def log(msg):
stderr.write("%s\n" % msg) stderr.write("%s\n" % msg)
stderr.flush() stderr.flush()
gl_emitter = QObject()
class Plugin(BasePlugin): class Plugin(BasePlugin):
@ -173,7 +175,12 @@ class TrezorWallet(NewWallet):
inputs = self.tx_inputs(tx) inputs = self.tx_inputs(tx)
outputs = self.tx_outputs(tx) outputs = self.tx_outputs(tx)
signed_tx = self.get_client().sign_tx('Bitcoin', inputs, outputs)[1] try:
signed_tx = self.get_client().sign_tx('Bitcoin', inputs, outputs)[1]
except Exception, e:
raise e
finally:
gl_emitter.emit(SIGNAL('trezor_done'))
values = [i['value'] for i in tx.inputs] values = [i['value'] for i in tx.inputs]
raw = signed_tx.encode('hex') raw = signed_tx.encode('hex')
tx.update(raw) tx.update(raw)
@ -280,10 +287,14 @@ class TrezorQtGuiMixin(object):
super(TrezorQtGuiMixin, self).__init__(*args, **kwargs) super(TrezorQtGuiMixin, self).__init__(*args, **kwargs)
def callback_ButtonRequest(self, msg): def callback_ButtonRequest(self, msg):
i = QMessageBox.question(None, _('Trezor'), _("Please check request on Trezor's screen"), _('OK'), _('Cancel')) if msg.code == 3:
if i == 0: message = "Confirm transaction outputs on Trezor device to continue"
return proto.ButtonAck() elif msg.code == 8:
return proto.Cancel() message = "Confirm transaction fee on Trezor device to continue"
else:
message = "Check Trezor device to continue"
twd.start(message)
return proto.ButtonAck()
def callback_PinMatrixRequest(self, msg): def callback_PinMatrixRequest(self, msg):
if msg.type == 1: if msg.type == 1:
@ -339,6 +350,28 @@ class TrezorQtGuiMixin(object):
if not d.exec_(): return if not d.exec_(): return
return str(matrix.get_value()) return str(matrix.get_value())
class TrezorWaitingDialog(QThread):
def __init__(self):
QThread.__init__(self)
self.waiting = False
def start(self, message):
self.d = QDialog()
self.d.setModal(True)
self.d.setWindowTitle('Please Check Trezor Device')
l = QLabel(message)
vbox = QVBoxLayout(self.d)
vbox.addWidget(l)
self.d.show()
if not self.waiting:
self.waiting = True
self.d.connect(gl_emitter, SIGNAL('trezor_done'), self.stop)
def stop(self):
self.d.hide()
self.waiting = False
if TREZOR: if TREZOR:
class QtGuiTrezorClient(ProtocolMixin, TrezorQtGuiMixin, BaseClient): class QtGuiTrezorClient(ProtocolMixin, TrezorQtGuiMixin, BaseClient):
def call_raw(self, msg): def call_raw(self, msg):
@ -349,3 +382,6 @@ if TREZOR:
raise raise
return resp return resp
twd = TrezorWaitingDialog()