2014-08-17 04:41:42 -07:00
|
|
|
from PyQt4.Qt import QMessageBox, QDialog, QVBoxLayout, QLabel, QThread, SIGNAL, QGridLayout, QInputDialog, QPushButton
|
2014-08-03 14:37:26 -07:00
|
|
|
import PyQt4.QtCore as QtCore
|
2014-07-10 12:44:28 -07:00
|
|
|
from binascii import unhexlify
|
|
|
|
from struct import pack
|
|
|
|
from sys import stderr
|
2014-08-02 20:18:02 -07:00
|
|
|
from time import sleep
|
2014-08-03 16:10:20 -07:00
|
|
|
from base64 import b64encode, b64decode
|
2015-03-05 08:09:39 -08:00
|
|
|
import unicodedata
|
2015-04-04 06:13:56 -07:00
|
|
|
import threading
|
2014-07-10 12:44:28 -07:00
|
|
|
|
2014-08-31 06:33:20 -07:00
|
|
|
import electrum
|
2014-08-02 11:52:28 -07:00
|
|
|
from electrum.account import BIP32_Account
|
2014-08-18 15:41:10 -07:00
|
|
|
from electrum.bitcoin import EncodeBase58Check, public_key_to_bc_address, bc_address_to_hash_160
|
2014-08-02 11:52:28 -07:00
|
|
|
from electrum.i18n import _
|
2015-04-18 05:59:46 -07:00
|
|
|
from electrum.plugins import BasePlugin, hook, always_hook, run_hook
|
2014-08-02 11:52:28 -07:00
|
|
|
from electrum.transaction import deserialize
|
2015-01-04 15:33:10 -08:00
|
|
|
from electrum.wallet import BIP32_HD_Wallet
|
2015-05-26 00:08:54 -07:00
|
|
|
from electrum.util import print_error, print_msg
|
2015-04-04 06:13:56 -07:00
|
|
|
from electrum.wallet import pw_decode, bip32_private_derivation, bip32_root
|
2014-07-10 12:44:28 -07:00
|
|
|
|
2015-03-14 04:28:19 -07:00
|
|
|
from electrum_gui.qt.util import *
|
2014-07-10 12:44:28 -07:00
|
|
|
|
|
|
|
try:
|
|
|
|
from trezorlib.client import types
|
|
|
|
from trezorlib.client import proto, BaseClient, ProtocolMixin
|
2014-07-18 06:40:31 -07:00
|
|
|
from trezorlib.qt.pinmatrix import PinMatrixWidget
|
2014-07-10 12:44:28 -07:00
|
|
|
from trezorlib.transport import ConnectionError
|
2014-07-21 10:41:43 -07:00
|
|
|
from trezorlib.transport_hid import HidTransport
|
2014-07-10 12:44:28 -07:00
|
|
|
TREZOR = True
|
2014-07-18 06:40:31 -07:00
|
|
|
except ImportError:
|
|
|
|
TREZOR = False
|
2014-07-10 12:44:28 -07:00
|
|
|
|
|
|
|
def log(msg):
|
|
|
|
stderr.write("%s\n" % msg)
|
|
|
|
stderr.flush()
|
|
|
|
|
2014-08-22 04:24:29 -07:00
|
|
|
def give_error(message):
|
2014-09-10 01:16:07 -07:00
|
|
|
print_error(message)
|
2014-08-22 04:24:29 -07:00
|
|
|
raise Exception(message)
|
|
|
|
|
2015-03-05 08:09:39 -08:00
|
|
|
|
2014-07-10 12:44:28 -07:00
|
|
|
class Plugin(BasePlugin):
|
|
|
|
|
2014-08-31 06:33:20 -07:00
|
|
|
def __init__(self, config, name):
|
|
|
|
BasePlugin.__init__(self, config, name)
|
2014-07-10 12:44:28 -07:00
|
|
|
self._is_available = self._init()
|
2014-08-17 04:41:42 -07:00
|
|
|
self._requires_settings = True
|
2014-07-11 14:28:26 -07:00
|
|
|
self.wallet = None
|
2015-06-09 00:58:40 -07:00
|
|
|
self.handler = None
|
2015-06-28 01:20:54 -07:00
|
|
|
self.client = None
|
|
|
|
self.transport = None
|
2015-05-23 01:38:19 -07:00
|
|
|
|
2015-05-24 11:37:05 -07:00
|
|
|
def constructor(self, s):
|
|
|
|
return TrezorWallet(s)
|
2014-07-10 12:44:28 -07:00
|
|
|
|
|
|
|
def _init(self):
|
|
|
|
return TREZOR
|
|
|
|
|
|
|
|
def is_available(self):
|
2015-01-31 09:09:50 -08:00
|
|
|
if not self._is_available:
|
|
|
|
return False
|
|
|
|
if not self.wallet:
|
|
|
|
return False
|
|
|
|
if self.wallet.storage.get('wallet_type') != 'trezor':
|
|
|
|
return False
|
|
|
|
return True
|
2014-07-10 12:44:28 -07:00
|
|
|
|
2014-08-17 04:41:42 -07:00
|
|
|
def requires_settings(self):
|
|
|
|
return self._requires_settings
|
|
|
|
|
2014-07-11 14:28:26 -07:00
|
|
|
def set_enabled(self, enabled):
|
|
|
|
self.wallet.storage.put('use_' + self.name, enabled)
|
|
|
|
|
|
|
|
def is_enabled(self):
|
|
|
|
if not self.is_available():
|
|
|
|
return False
|
2015-01-31 09:09:50 -08:00
|
|
|
if self.wallet.has_seed():
|
|
|
|
return False
|
|
|
|
return True
|
2014-07-11 14:28:26 -07:00
|
|
|
|
2015-06-28 01:20:54 -07:00
|
|
|
def compare_version(self, major, minor=0, patch=0):
|
|
|
|
features = self.get_client().features
|
2015-06-28 12:55:01 -07:00
|
|
|
v = [features.major_version, features.minor_version, features.patch_version]
|
|
|
|
self.print_error('firmware version', v)
|
|
|
|
return cmp(v, [major, minor, patch])
|
2015-06-28 01:20:54 -07:00
|
|
|
|
|
|
|
def atleast_version(self, major, minor=0, patch=0):
|
|
|
|
return self.compare_version(major, minor, patch) >= 0
|
|
|
|
|
|
|
|
def get_client(self):
|
|
|
|
if not TREZOR:
|
|
|
|
give_error('please install github.com/trezor/python-trezor')
|
|
|
|
|
|
|
|
if not self.client or self.client.bad:
|
2015-06-28 10:59:27 -07:00
|
|
|
d = HidTransport.enumerate()
|
|
|
|
if not d:
|
2015-06-28 01:20:54 -07:00
|
|
|
give_error('Could not connect to your Trezor. Please verify the cable is connected and that no other app is using it.')
|
2015-06-28 10:59:27 -07:00
|
|
|
self.transport = HidTransport(d[0])
|
2015-06-28 01:20:54 -07:00
|
|
|
self.client = QtGuiTrezorClient(self.transport)
|
|
|
|
self.client.handler = self.handler
|
|
|
|
self.client.set_tx_api(self)
|
|
|
|
self.client.bad = False
|
|
|
|
if not self.atleast_version(1, 2, 1):
|
2015-06-28 12:55:01 -07:00
|
|
|
self.client = None
|
2015-06-28 01:20:54 -07:00
|
|
|
give_error('Outdated Trezor firmware. Please update the firmware from https://www.mytrezor.com')
|
|
|
|
return self.client
|
2015-04-18 05:59:46 -07:00
|
|
|
|
2014-09-12 10:58:59 -07:00
|
|
|
@hook
|
|
|
|
def close_wallet(self):
|
|
|
|
print_error("trezor: clear session")
|
2015-06-28 01:20:54 -07:00
|
|
|
if self.client:
|
|
|
|
self.client.clear_session()
|
|
|
|
self.client.transport.close()
|
|
|
|
self.client = None
|
2015-04-18 05:59:46 -07:00
|
|
|
self.wallet = None
|
|
|
|
|
2015-05-26 00:08:54 -07:00
|
|
|
@hook
|
2015-06-10 13:08:19 -07:00
|
|
|
def cmdline_load_wallet(self, wallet):
|
|
|
|
self.wallet = wallet
|
|
|
|
self.wallet.plugin = self
|
|
|
|
if self.handler is None:
|
|
|
|
self.handler = TrezorCmdLineHandler()
|
2015-05-26 00:08:54 -07:00
|
|
|
|
2014-08-31 02:42:40 -07:00
|
|
|
@hook
|
2015-06-09 00:58:40 -07:00
|
|
|
def load_wallet(self, wallet, window):
|
2015-06-10 03:40:08 -07:00
|
|
|
self.print_error("load_wallet")
|
2015-04-18 05:59:46 -07:00
|
|
|
self.wallet = wallet
|
2015-06-09 00:58:40 -07:00
|
|
|
self.window = window
|
|
|
|
self.wallet.plugin = self
|
|
|
|
|
|
|
|
if self.handler is None:
|
|
|
|
self.handler = TrezorQtHandler(self.window.app)
|
|
|
|
|
2015-06-28 12:55:01 -07:00
|
|
|
try:
|
|
|
|
self.get_client().ping('t')
|
|
|
|
except BaseException as e:
|
|
|
|
QMessageBox.information(self.window, _('Error'), _("Trezor device not detected.\nContinuing in watching-only mode." + '\n\nReason:\n' + str(e)), _('OK'))
|
|
|
|
self.wallet.force_watching_only = True
|
|
|
|
return
|
|
|
|
|
|
|
|
if self.wallet.addresses() and not self.wallet.check_proper_device():
|
|
|
|
QMessageBox.information(self.window, _('Error'), _("This wallet does not match your Trezor device"), _('OK'))
|
2015-01-30 23:35:07 -08:00
|
|
|
self.wallet.force_watching_only = True
|
2014-07-11 14:28:26 -07:00
|
|
|
|
2015-06-11 00:36:19 -07:00
|
|
|
@hook
|
|
|
|
def installwizard_load_wallet(self, wallet, window):
|
|
|
|
self.load_wallet(wallet, window)
|
|
|
|
|
2014-08-31 02:42:40 -07:00
|
|
|
@hook
|
2014-07-11 12:53:28 -07:00
|
|
|
def installwizard_restore(self, wizard, storage):
|
2014-08-26 08:58:17 -07:00
|
|
|
if storage.get('wallet_type') != 'trezor':
|
|
|
|
return
|
2015-01-30 23:35:07 -08:00
|
|
|
seed = wizard.enter_seed_dialog("Enter your Trezor seed", None, func=lambda x:True)
|
|
|
|
if not seed:
|
2014-08-19 02:03:24 -07:00
|
|
|
return
|
2015-01-30 23:35:07 -08:00
|
|
|
wallet = TrezorWallet(storage)
|
|
|
|
self.wallet = wallet
|
2015-04-18 05:59:46 -07:00
|
|
|
passphrase = self.handler.get_passphrase(_("Please enter your Trezor passphrase.") + '\n' + _("Press OK if you do not use one."))
|
2015-03-05 08:09:39 -08:00
|
|
|
if passphrase is None:
|
|
|
|
return
|
2015-01-30 23:35:07 -08:00
|
|
|
password = wizard.password_dialog()
|
|
|
|
wallet.add_seed(seed, password)
|
2015-03-05 08:09:39 -08:00
|
|
|
wallet.add_cosigner_seed(seed, 'x/', password, passphrase)
|
2015-01-30 23:35:07 -08:00
|
|
|
wallet.create_main_account(password)
|
|
|
|
# disable trezor plugin
|
|
|
|
self.set_enabled(False)
|
2014-07-11 12:53:28 -07:00
|
|
|
return wallet
|
|
|
|
|
2014-08-31 02:42:40 -07:00
|
|
|
@hook
|
2015-02-08 03:28:10 -08:00
|
|
|
def receive_menu(self, menu, addrs):
|
2015-06-28 01:20:54 -07:00
|
|
|
if not self.wallet.is_watching_only() and self.atleast_version(1, 3) and len(addrs) == 1:
|
|
|
|
menu.addAction(_("Show on TREZOR"), lambda: self.show_address(addrs[0]))
|
|
|
|
|
|
|
|
def show_address(self, address):
|
|
|
|
if not self.wallet.check_proper_device():
|
|
|
|
give_error('Wrong device or password')
|
|
|
|
try:
|
|
|
|
address_path = self.wallet.address_id(address)
|
|
|
|
address_n = self.get_client().expand_path(address_path)
|
|
|
|
except Exception, e:
|
|
|
|
give_error(e)
|
|
|
|
try:
|
|
|
|
self.get_client().get_address('Bitcoin', address_n, True)
|
|
|
|
except Exception, e:
|
|
|
|
give_error(e)
|
|
|
|
finally:
|
|
|
|
self.handler.stop()
|
2014-07-10 12:44:28 -07:00
|
|
|
|
2014-08-17 04:41:42 -07:00
|
|
|
def settings_widget(self, window):
|
|
|
|
return EnterButton(_('Settings'), self.settings_dialog)
|
|
|
|
|
|
|
|
def settings_dialog(self):
|
2015-06-28 01:20:54 -07:00
|
|
|
get_label = lambda: self.get_client().features.label
|
2014-08-17 04:41:42 -07:00
|
|
|
update_label = lambda: current_label_label.setText("Label: %s" % get_label())
|
|
|
|
|
|
|
|
d = QDialog()
|
|
|
|
layout = QGridLayout(d)
|
|
|
|
layout.addWidget(QLabel("Trezor Options"),0,0)
|
|
|
|
layout.addWidget(QLabel("ID:"),1,0)
|
2015-06-28 01:20:54 -07:00
|
|
|
layout.addWidget(QLabel(" %s" % self.get_client().get_device_id()),1,1)
|
2014-08-17 04:41:42 -07:00
|
|
|
|
|
|
|
def modify_label():
|
|
|
|
response = QInputDialog().getText(None, "Set New Trezor Label", "New Trezor Label: (upon submission confirm on Trezor)")
|
|
|
|
if not response[1]:
|
|
|
|
return
|
|
|
|
new_label = str(response[0])
|
2015-04-18 05:59:46 -07:00
|
|
|
self.handler.show_message("Please confirm label change on Trezor")
|
2015-06-28 01:20:54 -07:00
|
|
|
status = self.get_client().apply_settings(label=new_label)
|
2015-04-18 05:59:46 -07:00
|
|
|
self.handler.stop()
|
2014-08-17 04:41:42 -07:00
|
|
|
update_label()
|
|
|
|
|
|
|
|
current_label_label = QLabel()
|
|
|
|
update_label()
|
|
|
|
change_label_button = QPushButton("Modify")
|
|
|
|
change_label_button.clicked.connect(modify_label)
|
|
|
|
layout.addWidget(current_label_label,3,0)
|
|
|
|
layout.addWidget(change_label_button,3,1)
|
|
|
|
|
|
|
|
if d.exec_():
|
2014-09-10 14:12:28 -07:00
|
|
|
return True
|
2014-08-17 04:41:42 -07:00
|
|
|
else:
|
2014-09-10 14:12:28 -07:00
|
|
|
return False
|
2014-08-17 04:41:42 -07:00
|
|
|
|
2015-07-01 23:53:17 -07:00
|
|
|
def sign_transaction(self, tx, prev_tx, xpub_path):
|
|
|
|
self.prev_tx = prev_tx
|
|
|
|
self.xpub_path = xpub_path
|
2015-06-28 01:20:54 -07:00
|
|
|
client = self.get_client()
|
2015-07-01 23:53:17 -07:00
|
|
|
inputs = self.tx_inputs(tx, True)
|
2015-06-28 01:20:54 -07:00
|
|
|
outputs = self.tx_outputs(tx)
|
2015-07-01 23:53:17 -07:00
|
|
|
#try:
|
|
|
|
signed_tx = client.sign_tx('Bitcoin', inputs, outputs)[1]
|
|
|
|
#except Exception, e:
|
|
|
|
# give_error(e)
|
|
|
|
#finally:
|
|
|
|
self.handler.stop()
|
|
|
|
|
2015-06-28 01:20:54 -07:00
|
|
|
#values = [i['value'] for i in tx.inputs]
|
|
|
|
raw = signed_tx.encode('hex')
|
|
|
|
tx.update(raw)
|
|
|
|
#for i, txinput in enumerate(tx.inputs):
|
|
|
|
# txinput['value'] = values[i]
|
|
|
|
|
2015-07-01 23:53:17 -07:00
|
|
|
def tx_inputs(self, tx, for_sig=False):
|
2015-06-28 01:20:54 -07:00
|
|
|
inputs = []
|
|
|
|
for txinput in tx.inputs:
|
|
|
|
txinputtype = types.TxInputType()
|
|
|
|
if ('is_coinbase' in txinput and txinput['is_coinbase']):
|
|
|
|
prev_hash = "\0"*32
|
|
|
|
prev_index = 0xffffffff # signed int -1
|
|
|
|
else:
|
2015-07-01 23:53:17 -07:00
|
|
|
|
|
|
|
if for_sig:
|
|
|
|
x_pubkey = txinput['x_pubkeys'][0]
|
|
|
|
xpub, s = BIP32_Account.parse_xpubkey(x_pubkey)
|
|
|
|
xpub_n = self.get_client().expand_path(self.xpub_path[xpub])
|
|
|
|
txinputtype.address_n.extend(xpub_n + s)
|
2015-06-28 01:20:54 -07:00
|
|
|
|
|
|
|
prev_hash = unhexlify(txinput['prevout_hash'])
|
|
|
|
prev_index = txinput['prevout_n']
|
|
|
|
|
|
|
|
txinputtype.prev_hash = prev_hash
|
|
|
|
txinputtype.prev_index = prev_index
|
|
|
|
|
|
|
|
if 'scriptSig' in txinput:
|
|
|
|
script_sig = txinput['scriptSig'].decode('hex')
|
|
|
|
txinputtype.script_sig = script_sig
|
|
|
|
|
|
|
|
if 'sequence' in txinput:
|
|
|
|
sequence = txinput['sequence']
|
|
|
|
txinputtype.sequence = sequence
|
|
|
|
|
|
|
|
inputs.append(txinputtype)
|
|
|
|
#TODO P2SH
|
|
|
|
return inputs
|
|
|
|
|
|
|
|
def tx_outputs(self, tx):
|
|
|
|
outputs = []
|
|
|
|
|
|
|
|
for type, address, amount in tx.outputs:
|
|
|
|
assert type == 'address'
|
|
|
|
txoutputtype = types.TxOutputType()
|
|
|
|
if self.wallet.is_change(address):
|
|
|
|
address_path = self.wallet.address_id(address)
|
|
|
|
address_n = self.get_client().expand_path(address_path)
|
|
|
|
txoutputtype.address_n.extend(address_n)
|
|
|
|
else:
|
|
|
|
txoutputtype.address = address
|
|
|
|
txoutputtype.amount = amount
|
|
|
|
addrtype, hash_160 = bc_address_to_hash_160(address)
|
|
|
|
if addrtype == 0:
|
|
|
|
txoutputtype.script_type = types.PAYTOADDRESS
|
|
|
|
elif addrtype == 5:
|
|
|
|
txoutputtype.script_type = types.PAYTOSCRIPTHASH
|
|
|
|
else:
|
|
|
|
raise BaseException('addrtype')
|
|
|
|
outputs.append(txoutputtype)
|
|
|
|
|
|
|
|
return outputs
|
|
|
|
|
|
|
|
def electrum_tx_to_txtype(self, tx):
|
|
|
|
t = types.TransactionType()
|
|
|
|
d = deserialize(tx.raw)
|
|
|
|
t.version = d['version']
|
|
|
|
t.lock_time = d['lockTime']
|
|
|
|
inputs = self.tx_inputs(tx)
|
|
|
|
t.inputs.extend(inputs)
|
|
|
|
for vout in d['outputs']:
|
|
|
|
o = t.bin_outputs.add()
|
|
|
|
o.amount = vout['value']
|
|
|
|
o.script_pubkey = vout['scriptPubKey'].decode('hex')
|
|
|
|
return t
|
|
|
|
|
|
|
|
def get_tx(self, tx_hash):
|
2015-07-01 23:53:17 -07:00
|
|
|
tx = self.prev_tx[tx_hash]
|
2015-06-28 01:20:54 -07:00
|
|
|
tx.deserialize()
|
|
|
|
return self.electrum_tx_to_txtype(tx)
|
|
|
|
|
|
|
|
|
2014-07-10 12:44:28 -07:00
|
|
|
|
2015-01-30 23:35:07 -08:00
|
|
|
|
2015-01-04 15:33:10 -08:00
|
|
|
class TrezorWallet(BIP32_HD_Wallet):
|
2014-08-20 12:01:30 -07:00
|
|
|
wallet_type = 'trezor'
|
2015-01-04 15:33:10 -08:00
|
|
|
root_derivation = "m/44'/0'"
|
2014-07-10 12:44:28 -07:00
|
|
|
|
|
|
|
def __init__(self, storage):
|
2015-01-04 15:33:10 -08:00
|
|
|
BIP32_HD_Wallet.__init__(self, storage)
|
2014-07-18 06:47:09 -07:00
|
|
|
self.mpk = None
|
2014-07-10 12:44:28 -07:00
|
|
|
self.device_checked = False
|
2015-06-28 01:20:54 -07:00
|
|
|
self.proper_device = False
|
2015-01-30 23:35:07 -08:00
|
|
|
self.force_watching_only = False
|
2014-07-10 12:44:28 -07:00
|
|
|
|
|
|
|
def get_action(self):
|
|
|
|
if not self.accounts:
|
|
|
|
return 'create_accounts'
|
|
|
|
|
2014-09-09 05:22:06 -07:00
|
|
|
def can_import(self):
|
|
|
|
return False
|
|
|
|
|
2015-04-04 06:13:56 -07:00
|
|
|
def can_sign_xpubkey(self, x_pubkey):
|
|
|
|
xpub, sequence = BIP32_Account.parse_xpubkey(x_pubkey)
|
|
|
|
return xpub in self.master_public_keys.values()
|
|
|
|
|
2014-09-09 05:30:57 -07:00
|
|
|
def can_export(self):
|
|
|
|
return False
|
|
|
|
|
2014-07-10 12:44:28 -07:00
|
|
|
def can_create_accounts(self):
|
|
|
|
return True
|
|
|
|
|
2014-08-19 01:36:55 -07:00
|
|
|
def can_change_password(self):
|
|
|
|
return False
|
|
|
|
|
2014-07-10 12:44:28 -07:00
|
|
|
def is_watching_only(self):
|
2015-01-30 23:35:07 -08:00
|
|
|
return self.force_watching_only
|
2014-07-10 12:44:28 -07:00
|
|
|
|
|
|
|
def get_client(self):
|
2015-06-28 01:20:54 -07:00
|
|
|
return self.plugin.get_client()
|
2015-02-14 04:13:59 -08:00
|
|
|
|
2014-07-10 12:44:28 -07:00
|
|
|
def address_id(self, address):
|
|
|
|
account_id, (change, address_index) = self.get_address_index(address)
|
2014-08-28 08:38:56 -07:00
|
|
|
return "44'/0'/%s'/%d/%d" % (account_id, change, address_index)
|
2014-07-10 12:44:28 -07:00
|
|
|
|
2014-08-20 12:01:30 -07:00
|
|
|
def create_main_account(self, password):
|
|
|
|
self.create_account('Main account', None) #name, empty password
|
2014-07-10 12:44:28 -07:00
|
|
|
|
2015-01-30 23:35:07 -08:00
|
|
|
def mnemonic_to_seed(self, mnemonic, passphrase):
|
|
|
|
# trezor uses bip39
|
|
|
|
import pbkdf2, hashlib, hmac
|
|
|
|
PBKDF2_ROUNDS = 2048
|
2015-03-05 08:09:39 -08:00
|
|
|
mnemonic = unicodedata.normalize('NFKD', ' '.join(mnemonic.split()))
|
|
|
|
passphrase = unicodedata.normalize('NFKD', passphrase)
|
2015-01-30 23:35:07 -08:00
|
|
|
return pbkdf2.PBKDF2(mnemonic, 'mnemonic' + passphrase, iterations = PBKDF2_ROUNDS, macmodule = hmac, digestmodule = hashlib.sha512).read(64)
|
|
|
|
|
2014-08-20 12:01:30 -07:00
|
|
|
def derive_xkeys(self, root, derivation, password):
|
2015-01-30 23:35:07 -08:00
|
|
|
x = self.master_private_keys.get(root)
|
|
|
|
if x:
|
|
|
|
root_xprv = pw_decode(x, password)
|
|
|
|
xprv, xpub = bip32_private_derivation(root_xprv, root, derivation)
|
|
|
|
return xpub, xprv
|
|
|
|
else:
|
|
|
|
derivation = derivation.replace(self.root_name,"44'/0'/")
|
|
|
|
xpub = self.get_public_key(derivation)
|
|
|
|
return xpub, None
|
2014-07-10 12:44:28 -07:00
|
|
|
|
|
|
|
def get_public_key(self, bip32_path):
|
2015-06-28 01:20:54 -07:00
|
|
|
address_n = self.plugin.get_client().expand_path(bip32_path)
|
|
|
|
node = self.plugin.get_client().get_public_node(address_n).node
|
2014-08-08 09:29:08 -07:00
|
|
|
xpub = "0488B21E".decode('hex') + chr(node.depth) + self.i4b(node.fingerprint) + self.i4b(node.child_num) + node.chain_code + node.public_key
|
2014-07-10 12:44:28 -07:00
|
|
|
return EncodeBase58Check(xpub)
|
|
|
|
|
|
|
|
def get_master_public_key(self):
|
|
|
|
if not self.mpk:
|
|
|
|
self.mpk = self.get_public_key("44'/0'")
|
|
|
|
return self.mpk
|
|
|
|
|
|
|
|
def i4b(self, x):
|
2014-08-20 12:18:16 -07:00
|
|
|
return pack('>I', x)
|
2014-07-10 12:44:28 -07:00
|
|
|
|
|
|
|
def add_keypairs(self, tx, keypairs, password):
|
|
|
|
#do nothing - no priv keys available
|
|
|
|
pass
|
|
|
|
|
2014-08-03 16:10:20 -07:00
|
|
|
def decrypt_message(self, pubkey, message, password):
|
2014-09-03 01:45:41 -07:00
|
|
|
raise BaseException( _('Decrypt method is not implemented in Trezor') )
|
|
|
|
#address = public_key_to_bc_address(pubkey.decode('hex'))
|
|
|
|
#address_path = self.address_id(address)
|
|
|
|
#address_n = self.get_client().expand_path(address_path)
|
|
|
|
#try:
|
|
|
|
# decrypted_msg = self.get_client().decrypt_message(address_n, b64decode(message))
|
|
|
|
#except Exception, e:
|
|
|
|
# give_error(e)
|
|
|
|
#finally:
|
2015-04-04 06:13:56 -07:00
|
|
|
# twd.stop()
|
2014-09-03 01:45:41 -07:00
|
|
|
#return str(decrypted_msg)
|
2014-08-03 16:10:20 -07:00
|
|
|
|
2014-08-03 14:37:26 -07:00
|
|
|
def sign_message(self, address, message, password):
|
2014-08-26 03:38:05 -07:00
|
|
|
if not self.check_proper_device():
|
|
|
|
give_error('Wrong device or password')
|
2014-08-03 14:37:26 -07:00
|
|
|
try:
|
|
|
|
address_path = self.address_id(address)
|
2015-06-28 01:20:54 -07:00
|
|
|
address_n = self.plugin.get_client().expand_path(address_path)
|
2014-08-03 14:37:26 -07:00
|
|
|
except Exception, e:
|
2014-08-22 04:24:29 -07:00
|
|
|
give_error(e)
|
2014-08-03 14:37:26 -07:00
|
|
|
try:
|
2015-06-28 01:20:54 -07:00
|
|
|
msg_sig = self.plugin.get_client().sign_message('Bitcoin', address_n, message)
|
2014-08-03 14:37:26 -07:00
|
|
|
except Exception, e:
|
2014-08-22 04:24:29 -07:00
|
|
|
give_error(e)
|
2014-08-03 14:37:26 -07:00
|
|
|
finally:
|
2015-04-18 05:59:46 -07:00
|
|
|
self.plugin.handler.stop()
|
2014-08-03 14:37:26 -07:00
|
|
|
b64_msg_sig = b64encode(msg_sig.signature)
|
|
|
|
return str(b64_msg_sig)
|
|
|
|
|
2014-10-30 13:04:10 -07:00
|
|
|
def sign_transaction(self, tx, password):
|
2015-07-01 23:53:17 -07:00
|
|
|
if tx.is_complete():
|
|
|
|
return
|
2014-07-10 12:44:28 -07:00
|
|
|
if not self.check_proper_device():
|
2014-08-22 04:24:29 -07:00
|
|
|
give_error('Wrong device or password')
|
2015-07-01 23:53:17 -07:00
|
|
|
# previous transactions used as inputs
|
|
|
|
prev_tx = {}
|
|
|
|
# path of the xpubs that are involved
|
|
|
|
xpub_path = {}
|
|
|
|
for txin in tx.inputs:
|
|
|
|
tx_hash = txin['prevout_hash']
|
|
|
|
prev_tx[tx_hash] = self.transactions[tx_hash]
|
|
|
|
address = txin['address']
|
|
|
|
address_path = self.address_id(address)
|
|
|
|
account_id, (change, address_index) = self.get_address_index(address)
|
|
|
|
|
|
|
|
for x_pubkey in txin['x_pubkeys']:
|
|
|
|
xpub, s = BIP32_Account.parse_xpubkey(x_pubkey)
|
|
|
|
xpub_path[xpub] = "44'/0'/%s'"%account_id
|
|
|
|
|
|
|
|
self.plugin.sign_transaction(tx, prev_tx, xpub_path)
|
2014-07-10 12:44:28 -07:00
|
|
|
|
|
|
|
def check_proper_device(self):
|
|
|
|
self.get_client().ping('t')
|
|
|
|
if not self.device_checked:
|
2014-09-10 01:09:13 -07:00
|
|
|
address = self.addresses(False)[0]
|
2014-07-10 12:44:28 -07:00
|
|
|
address_id = self.address_id(address)
|
|
|
|
n = self.get_client().expand_path(address_id)
|
|
|
|
device_address = self.get_client().get_address('Bitcoin', n)
|
|
|
|
self.device_checked = True
|
|
|
|
|
|
|
|
if device_address != address:
|
|
|
|
self.proper_device = False
|
|
|
|
else:
|
|
|
|
self.proper_device = True
|
|
|
|
|
|
|
|
return self.proper_device
|
|
|
|
|
|
|
|
|
2015-04-04 06:13:56 -07:00
|
|
|
class TrezorGuiMixin(object):
|
2014-07-10 12:44:28 -07:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2015-04-04 06:13:56 -07:00
|
|
|
super(TrezorGuiMixin, self).__init__(*args, **kwargs)
|
2014-07-10 12:44:28 -07:00
|
|
|
|
|
|
|
def callback_ButtonRequest(self, msg):
|
2014-08-02 20:18:02 -07:00
|
|
|
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"
|
2014-08-03 14:37:26 -07:00
|
|
|
elif msg.code == 7:
|
|
|
|
message = "Confirm message to sign on Trezor device to continue"
|
2015-02-08 03:28:10 -08:00
|
|
|
elif msg.code == 10:
|
|
|
|
message = "Confirm address on Trezor device to continue"
|
2014-08-02 20:18:02 -07:00
|
|
|
else:
|
|
|
|
message = "Check Trezor device to continue"
|
2015-04-18 05:59:46 -07:00
|
|
|
self.handler.show_message(message)
|
2014-08-02 20:18:02 -07:00
|
|
|
return proto.ButtonAck()
|
2014-07-10 12:44:28 -07:00
|
|
|
|
|
|
|
def callback_PinMatrixRequest(self, msg):
|
|
|
|
if msg.type == 1:
|
2014-08-20 11:00:43 -07:00
|
|
|
desc = 'current PIN'
|
2014-07-10 12:44:28 -07:00
|
|
|
elif msg.type == 2:
|
|
|
|
desc = 'new PIN'
|
|
|
|
elif msg.type == 3:
|
|
|
|
desc = 'new PIN again'
|
|
|
|
else:
|
|
|
|
desc = 'PIN'
|
2015-04-18 05:59:46 -07:00
|
|
|
pin = self.handler.get_pin("Please enter Trezor %s" % desc)
|
2014-07-10 12:44:28 -07:00
|
|
|
if not pin:
|
|
|
|
return proto.Cancel()
|
|
|
|
return proto.PinMatrixAck(pin=pin)
|
|
|
|
|
2015-03-05 08:09:39 -08:00
|
|
|
def callback_PassphraseRequest(self, req):
|
|
|
|
msg = _("Please enter your Trezor passphrase.")
|
2015-04-18 05:59:46 -07:00
|
|
|
passphrase = self.handler.get_passphrase(msg)
|
2015-03-05 08:09:39 -08:00
|
|
|
if passphrase is None:
|
2014-07-10 12:44:28 -07:00
|
|
|
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)
|
|
|
|
|
2015-04-04 06:13:56 -07:00
|
|
|
|
2015-05-26 00:08:54 -07:00
|
|
|
class TrezorCmdLineHandler:
|
|
|
|
|
|
|
|
def get_passphrase(self, msg):
|
2015-06-10 13:08:19 -07:00
|
|
|
import getpass
|
2015-05-26 00:08:54 -07:00
|
|
|
print_msg(msg)
|
2015-06-10 13:08:19 -07:00
|
|
|
return getpass.getpass('')
|
2015-05-26 00:08:54 -07:00
|
|
|
|
|
|
|
def get_pin(self, msg):
|
2015-06-10 13:08:19 -07:00
|
|
|
t = { 'a':'7', 'b':'8', 'c':'9', 'd':'4', 'e':'5', 'f':'6', 'g':'1', 'h':'2', 'i':'3'}
|
|
|
|
print_msg(msg)
|
|
|
|
print_msg("a b c\nd e f\ng h i\n-----")
|
|
|
|
o = raw_input()
|
|
|
|
return ''.join(map(lambda x: t[x], o))
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def show_message(self, msg):
|
2015-05-26 00:08:54 -07:00
|
|
|
print_msg(msg)
|
|
|
|
|
|
|
|
|
2015-04-04 06:13:56 -07:00
|
|
|
class TrezorQtHandler:
|
|
|
|
|
|
|
|
def __init__(self, win):
|
|
|
|
self.win = win
|
|
|
|
self.win.connect(win, SIGNAL('trezor_done'), self.dialog_stop)
|
|
|
|
self.win.connect(win, SIGNAL('message_dialog'), self.message_dialog)
|
|
|
|
self.win.connect(win, SIGNAL('pin_dialog'), self.pin_dialog)
|
|
|
|
self.win.connect(win, SIGNAL('passphrase_dialog'), self.passphrase_dialog)
|
|
|
|
self.done = threading.Event()
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.win.emit(SIGNAL('trezor_done'))
|
|
|
|
|
|
|
|
def show_message(self, msg):
|
|
|
|
self.message = msg
|
|
|
|
self.win.emit(SIGNAL('message_dialog'))
|
|
|
|
|
|
|
|
def get_pin(self, msg):
|
|
|
|
self.done.clear()
|
|
|
|
self.message = msg
|
|
|
|
self.win.emit(SIGNAL('pin_dialog'))
|
|
|
|
self.done.wait()
|
|
|
|
return self.response
|
|
|
|
|
|
|
|
def get_passphrase(self, msg):
|
|
|
|
self.done.clear()
|
|
|
|
self.message = msg
|
|
|
|
self.win.emit(SIGNAL('passphrase_dialog'))
|
|
|
|
self.done.wait()
|
|
|
|
return self.passphrase
|
|
|
|
|
|
|
|
def pin_dialog(self):
|
2014-07-10 12:44:28 -07:00
|
|
|
d = QDialog(None)
|
|
|
|
d.setModal(1)
|
|
|
|
d.setWindowTitle(_("Enter PIN"))
|
2014-09-13 15:14:01 -07:00
|
|
|
d.setWindowFlags(d.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
|
2014-07-10 12:44:28 -07:00
|
|
|
matrix = PinMatrixWidget()
|
|
|
|
vbox = QVBoxLayout()
|
2015-04-04 06:13:56 -07:00
|
|
|
vbox.addWidget(QLabel(self.message))
|
2014-07-10 12:44:28 -07:00
|
|
|
vbox.addWidget(matrix)
|
2015-03-14 04:28:19 -07:00
|
|
|
vbox.addLayout(Buttons(CancelButton(d), OkButton(d)))
|
2014-07-10 12:44:28 -07:00
|
|
|
d.setLayout(vbox)
|
2015-04-04 06:13:56 -07:00
|
|
|
if not d.exec_():
|
|
|
|
self.response = None
|
|
|
|
self.response = str(matrix.get_value())
|
|
|
|
self.done.set()
|
2014-07-10 12:44:28 -07:00
|
|
|
|
2015-04-04 06:13:56 -07:00
|
|
|
def passphrase_dialog(self):
|
|
|
|
from electrum_gui.qt.password_dialog import make_password_dialog, run_password_dialog
|
|
|
|
d = QDialog()
|
|
|
|
d.setModal(1)
|
|
|
|
d.setLayout(make_password_dialog(d, None, self.message, False))
|
|
|
|
confirmed, p, passphrase = run_password_dialog(d, None, None)
|
|
|
|
if not confirmed:
|
|
|
|
QMessageBox.critical(None, _('Error'), _("Password request canceled"), _('OK'))
|
|
|
|
self.passphrase = None
|
|
|
|
else:
|
|
|
|
if passphrase is None:
|
|
|
|
passphrase = '' # Even blank string is valid Trezor passphrase
|
|
|
|
self.passphrase = unicodedata.normalize('NFKD', unicode(passphrase))
|
|
|
|
self.done.set()
|
2014-08-02 20:18:02 -07:00
|
|
|
|
2015-04-04 06:13:56 -07:00
|
|
|
def message_dialog(self):
|
2014-08-02 20:18:02 -07:00
|
|
|
self.d = QDialog()
|
2014-08-03 14:37:26 -07:00
|
|
|
self.d.setModal(1)
|
2014-08-02 20:18:02 -07:00
|
|
|
self.d.setWindowTitle('Please Check Trezor Device')
|
2014-08-03 14:37:26 -07:00
|
|
|
self.d.setWindowFlags(self.d.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
|
2015-04-04 06:13:56 -07:00
|
|
|
l = QLabel(self.message)
|
2014-08-02 20:18:02 -07:00
|
|
|
vbox = QVBoxLayout(self.d)
|
|
|
|
vbox.addWidget(l)
|
|
|
|
self.d.show()
|
|
|
|
|
2015-04-04 06:13:56 -07:00
|
|
|
def dialog_stop(self):
|
2014-08-02 20:18:02 -07:00
|
|
|
self.d.hide()
|
|
|
|
|
|
|
|
|
2014-07-10 12:44:28 -07:00
|
|
|
if TREZOR:
|
2015-04-04 06:13:56 -07:00
|
|
|
class QtGuiTrezorClient(ProtocolMixin, TrezorGuiMixin, BaseClient):
|
2014-07-10 12:44:28 -07:00
|
|
|
def call_raw(self, msg):
|
|
|
|
try:
|
|
|
|
resp = BaseClient.call_raw(self, msg)
|
|
|
|
except ConnectionError:
|
|
|
|
self.bad = True
|
|
|
|
raise
|
|
|
|
|
|
|
|
return resp
|