Browse Source

Commonize GuiMixin for keepkey and trezor

283
Neil Booth 9 years ago
parent
commit
42719cd0a9
  1. 66
      plugins/keepkey/keepkey.py
  2. 57
      plugins/trezor/gui_mixin.py
  3. 64
      plugins/trezor/trezor.py

66
plugins/keepkey/keepkey.py

@ -1,5 +1,4 @@
from binascii import unhexlify
from sys import stderr
import electrum
from electrum import bitcoin
@ -11,6 +10,7 @@ from electrum.plugins import BasePlugin, hook
from electrum.transaction import deserialize, is_extended_pubkey
from electrum.wallet import BIP32_Hardware_Wallet
from electrum.util import print_error
from plugins.trezor.gui_mixin import GuiMixin
try:
from keepkeylib.client import types
@ -23,10 +23,6 @@ except ImportError:
import keepkeylib.ckd_public as ckd_public
def log(msg):
stderr.write("%s\n" % msg)
stderr.flush()
def give_error(message):
print_error(message)
raise Exception(message)
@ -246,63 +242,11 @@ class KeepKeyPlugin(BasePlugin):
tx.deserialize()
return self.electrum_tx_to_txtype(tx)
class KeepKeyGuiMixin(object):
def __init__(self, *args, **kwargs):
super(KeepKeyGuiMixin, self).__init__(*args, **kwargs)
def callback_ButtonRequest(self, msg):
if msg.code == 3:
message = "Confirm transaction outputs on KeepKey device to continue"
elif msg.code == 8:
message = "Confirm transaction fee on KeepKey device to continue"
elif msg.code == 7:
message = "Confirm message to sign on KeepKey device to continue"
elif msg.code == 10:
message = "Confirm address on KeepKey device to continue"
else:
message = "Check KeepKey device to continue"
cancel_callback=self.cancel if msg.code in [3, 8] else None
self.handler.show_message(message, cancel_callback)
return proto.ButtonAck()
def callback_PinMatrixRequest(self, msg):
if msg.type == 1:
desc = 'current PIN'
elif msg.type == 2:
desc = 'new PIN'
elif msg.type == 3:
desc = 'new PIN again'
else:
desc = 'PIN'
pin = self.handler.get_pin("Please enter KeepKey %s" % desc)
if not pin:
return proto.Cancel()
return proto.PinMatrixAck(pin=pin)
def callback_PassphraseRequest(self, req):
msg = _("Please enter your KeepKey passphrase.")
passphrase = self.handler.get_passphrase(msg)
if passphrase is None:
return proto.Cancel()
return proto.PassphraseAck(passphrase=passphrase)
def callback_WordRequest(self, msg):
#TODO
log("Enter one word of mnemonic: ")
word = raw_input()
return proto.WordAck(word=word)
if KEEPKEY:
class QtGuiKeepKeyClient(ProtocolMixin, KeepKeyGuiMixin, BaseClient):
class QtGuiKeepKeyClient(ProtocolMixin, GuiMixin, BaseClient):
protocol = proto
device = 'KeepKey'
def call_raw(self, msg):
try:
resp = BaseClient.call_raw(self, msg)

57
plugins/trezor/gui_mixin.py

@ -0,0 +1,57 @@
from sys import stderr
from electrum.i18n import _
class GuiMixin(object):
# Requires: self.protcol, self.device
def __init__(self, *args, **kwargs):
super(GuiMixin, self).__init__(*args, **kwargs)
def callback_ButtonRequest(self, msg):
if msg.code == 3:
message = _("Confirm transaction outputs on %s device to continue")
elif msg.code == 8:
message = _("Confirm transaction fee on %s device to continue")
elif msg.code == 7:
message = _("Confirm message to sign on %s device to continue")
elif msg.code == 10:
message = _("Confirm address on %s device to continue")
else:
message = _("Check %s device to continue")
if msg.code in [3, 8] and hasattr(self, 'cancel'):
cancel_callback = self.cancel
else:
cancel_callback = None
self.handler.show_message(message % self.device, cancel_callback)
return self.protocol.ButtonAck()
def callback_PinMatrixRequest(self, msg):
if msg.type == 1:
msg = _("Please enter %s current PIN")
elif msg.type == 2:
msg = _("Please enter %s new PIN")
elif msg.type == 3:
msg = _("Please enter %s new PIN again")
else:
msg = _("Please enter %s PIN")
pin = self.handler.get_pin(msg % self.device)
if not pin:
return self.protocol.Cancel()
return self.protocol.PinMatrixAck(pin=pin)
def callback_PassphraseRequest(self, req):
msg = _("Please enter your %s passphrase")
passphrase = self.handler.get_passphrase(msg % self.device)
if passphrase is None:
return self.protocol.Cancel()
return self.protocol.PassphraseAck(passphrase=passphrase)
def callback_WordRequest(self, msg):
#TODO
stderr.write("Enter one word of mnemonic:\n")
stderr.flush()
word = raw_input()
return self.protocol.WordAck(word=word)

64
plugins/trezor/trezor.py

@ -1,5 +1,4 @@
from binascii import unhexlify
from sys import stderr
import electrum
from electrum import bitcoin
@ -11,6 +10,7 @@ from electrum.plugins import BasePlugin, hook
from electrum.transaction import deserialize, is_extended_pubkey
from electrum.wallet import BIP32_Hardware_Wallet
from electrum.util import print_error
from plugins.trezor.gui_mixin import GuiMixin
try:
from trezorlib.client import types
@ -23,10 +23,6 @@ except ImportError:
import trezorlib.ckd_public as ckd_public
def log(msg):
stderr.write("%s\n" % msg)
stderr.flush()
def give_error(message):
print_error(message)
raise Exception(message)
@ -229,61 +225,11 @@ class TrezorPlugin(BasePlugin):
tx.deserialize()
return self.electrum_tx_to_txtype(tx)
class TrezorGuiMixin(object):
def __init__(self, *args, **kwargs):
super(TrezorGuiMixin, self).__init__(*args, **kwargs)
def callback_ButtonRequest(self, msg):
if msg.code == 3:
message = "Confirm transaction outputs on Trezor device to continue"
elif msg.code == 8:
message = "Confirm transaction fee on Trezor device to continue"
elif msg.code == 7:
message = "Confirm message to sign on Trezor device to continue"
elif msg.code == 10:
message = "Confirm address on Trezor device to continue"
else:
message = "Check Trezor device to continue"
self.handler.show_message(message)
return proto.ButtonAck()
def callback_PinMatrixRequest(self, msg):
if msg.type == 1:
desc = 'current PIN'
elif msg.type == 2:
desc = 'new PIN'
elif msg.type == 3:
desc = 'new PIN again'
else:
desc = 'PIN'
pin = self.handler.get_pin("Please enter Trezor %s" % desc)
if not pin:
return proto.Cancel()
return proto.PinMatrixAck(pin=pin)
def callback_PassphraseRequest(self, req):
msg = _("Please enter your Trezor passphrase.")
passphrase = self.handler.get_passphrase(msg)
if passphrase is None:
return proto.Cancel()
return proto.PassphraseAck(passphrase=passphrase)
def callback_WordRequest(self, msg):
#TODO
log("Enter one word of mnemonic: ")
word = raw_input()
return proto.WordAck(word=word)
if TREZOR:
class QtGuiTrezorClient(ProtocolMixin, TrezorGuiMixin, BaseClient):
class QtGuiTrezorClient(ProtocolMixin, GuiMixin, BaseClient):
protocol = proto
device = 'Trezor'
def call_raw(self, msg):
try:
resp = BaseClient.call_raw(self, msg)

Loading…
Cancel
Save