|
@ -7,8 +7,6 @@ import threading |
|
|
import re |
|
|
import re |
|
|
from functools import partial |
|
|
from functools import partial |
|
|
|
|
|
|
|
|
from PyQt4.Qt import QMessageBox, QDialog, QVBoxLayout, QLabel, QThread, SIGNAL, QGridLayout, QInputDialog, QPushButton |
|
|
|
|
|
import PyQt4.QtCore as QtCore |
|
|
|
|
|
|
|
|
|
|
|
import electrum |
|
|
import electrum |
|
|
from electrum import bitcoin |
|
|
from electrum import bitcoin |
|
@ -22,14 +20,9 @@ from electrum.wallet import BIP32_HD_Wallet |
|
|
from electrum.util import print_error, print_msg |
|
|
from electrum.util import print_error, print_msg |
|
|
from electrum.wallet import pw_decode, bip32_private_derivation, bip32_root |
|
|
from electrum.wallet import pw_decode, bip32_private_derivation, bip32_root |
|
|
|
|
|
|
|
|
from electrum_gui.qt.util import * |
|
|
|
|
|
from electrum_gui.qt.main_window import StatusBarButton, ElectrumWindow |
|
|
|
|
|
from electrum_gui.qt.installwizard import InstallWizard |
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
try: |
|
|
from trezorlib.client import types |
|
|
from trezorlib.client import types |
|
|
from trezorlib.client import proto, BaseClient, ProtocolMixin |
|
|
from trezorlib.client import proto, BaseClient, ProtocolMixin |
|
|
from trezorlib.qt.pinmatrix import PinMatrixWidget |
|
|
|
|
|
from trezorlib.transport import ConnectionError |
|
|
from trezorlib.transport import ConnectionError |
|
|
from trezorlib.transport_hid import HidTransport |
|
|
from trezorlib.transport_hid import HidTransport |
|
|
TREZOR = True |
|
|
TREZOR = True |
|
@ -47,6 +40,166 @@ def give_error(message): |
|
|
raise Exception(message) |
|
|
raise Exception(message) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TrezorWallet(BIP32_HD_Wallet): |
|
|
|
|
|
wallet_type = 'trezor' |
|
|
|
|
|
root_derivation = "m/44'/0'" |
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, storage): |
|
|
|
|
|
BIP32_HD_Wallet.__init__(self, storage) |
|
|
|
|
|
self.mpk = None |
|
|
|
|
|
self.device_checked = False |
|
|
|
|
|
self.proper_device = False |
|
|
|
|
|
self.force_watching_only = False |
|
|
|
|
|
|
|
|
|
|
|
def get_action(self): |
|
|
|
|
|
if not self.accounts: |
|
|
|
|
|
return 'create_accounts' |
|
|
|
|
|
|
|
|
|
|
|
def can_import(self): |
|
|
|
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
def can_sign_xpubkey(self, x_pubkey): |
|
|
|
|
|
xpub, sequence = BIP32_Account.parse_xpubkey(x_pubkey) |
|
|
|
|
|
return xpub in self.master_public_keys.values() |
|
|
|
|
|
|
|
|
|
|
|
def can_export(self): |
|
|
|
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
def can_create_accounts(self): |
|
|
|
|
|
return True |
|
|
|
|
|
|
|
|
|
|
|
def can_change_password(self): |
|
|
|
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
def is_watching_only(self): |
|
|
|
|
|
return self.force_watching_only |
|
|
|
|
|
|
|
|
|
|
|
def get_client(self): |
|
|
|
|
|
return self.plugin.get_client() |
|
|
|
|
|
|
|
|
|
|
|
def address_id(self, address): |
|
|
|
|
|
account_id, (change, address_index) = self.get_address_index(address) |
|
|
|
|
|
return "44'/0'/%s'/%d/%d" % (account_id, change, address_index) |
|
|
|
|
|
|
|
|
|
|
|
def create_main_account(self, password): |
|
|
|
|
|
self.create_account('Main account', None) #name, empty password |
|
|
|
|
|
|
|
|
|
|
|
def mnemonic_to_seed(self, mnemonic, passphrase): |
|
|
|
|
|
# trezor uses bip39 |
|
|
|
|
|
import pbkdf2, hashlib, hmac |
|
|
|
|
|
PBKDF2_ROUNDS = 2048 |
|
|
|
|
|
mnemonic = unicodedata.normalize('NFKD', ' '.join(mnemonic.split())) |
|
|
|
|
|
passphrase = unicodedata.normalize('NFKD', passphrase) |
|
|
|
|
|
return pbkdf2.PBKDF2(mnemonic, 'mnemonic' + passphrase, iterations = PBKDF2_ROUNDS, macmodule = hmac, digestmodule = hashlib.sha512).read(64) |
|
|
|
|
|
|
|
|
|
|
|
def derive_xkeys(self, root, derivation, password): |
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
def get_public_key(self, bip32_path): |
|
|
|
|
|
address_n = self.plugin.get_client().expand_path(bip32_path) |
|
|
|
|
|
node = self.plugin.get_client().get_public_node(address_n).node |
|
|
|
|
|
xpub = "0488B21E".decode('hex') + chr(node.depth) + self.i4b(node.fingerprint) + self.i4b(node.child_num) + node.chain_code + node.public_key |
|
|
|
|
|
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): |
|
|
|
|
|
return pack('>I', x) |
|
|
|
|
|
|
|
|
|
|
|
def add_keypairs(self, tx, keypairs, password): |
|
|
|
|
|
#do nothing - no priv keys available |
|
|
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
def decrypt_message(self, pubkey, message, password): |
|
|
|
|
|
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: |
|
|
|
|
|
# twd.stop() |
|
|
|
|
|
#return str(decrypted_msg) |
|
|
|
|
|
|
|
|
|
|
|
def sign_message(self, address, message, password): |
|
|
|
|
|
if self.has_seed(): |
|
|
|
|
|
return BIP32_HD_Wallet.sign_message(self, address, message, password) |
|
|
|
|
|
if not self.check_proper_device(): |
|
|
|
|
|
give_error('Wrong device or password') |
|
|
|
|
|
try: |
|
|
|
|
|
address_path = self.address_id(address) |
|
|
|
|
|
address_n = self.plugin.get_client().expand_path(address_path) |
|
|
|
|
|
except Exception, e: |
|
|
|
|
|
give_error(e) |
|
|
|
|
|
try: |
|
|
|
|
|
msg_sig = self.plugin.get_client().sign_message('Bitcoin', address_n, message) |
|
|
|
|
|
except Exception, e: |
|
|
|
|
|
give_error(e) |
|
|
|
|
|
finally: |
|
|
|
|
|
self.plugin.handler.stop() |
|
|
|
|
|
return msg_sig.signature |
|
|
|
|
|
|
|
|
|
|
|
def sign_transaction(self, tx, password): |
|
|
|
|
|
if self.has_seed(): |
|
|
|
|
|
return BIP32_HD_Wallet.sign_transaction(self, tx, password) |
|
|
|
|
|
if tx.is_complete(): |
|
|
|
|
|
return |
|
|
|
|
|
if not self.check_proper_device(): |
|
|
|
|
|
give_error('Wrong device or password') |
|
|
|
|
|
# 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'] |
|
|
|
|
|
|
|
|
|
|
|
ptx = self.transactions.get(tx_hash) |
|
|
|
|
|
if ptx is None: |
|
|
|
|
|
ptx = self.network.synchronous_get(('blockchain.transaction.get', [tx_hash])) |
|
|
|
|
|
ptx = Transaction(ptx) |
|
|
|
|
|
prev_tx[tx_hash] = ptx |
|
|
|
|
|
|
|
|
|
|
|
for x_pubkey in txin['x_pubkeys']: |
|
|
|
|
|
account_derivation = None |
|
|
|
|
|
if not is_extended_pubkey(x_pubkey): |
|
|
|
|
|
continue |
|
|
|
|
|
xpub = x_to_xpub(x_pubkey) |
|
|
|
|
|
for k, v in self.master_public_keys.items(): |
|
|
|
|
|
if v == xpub: |
|
|
|
|
|
account_id = re.match("x/(\d+)'", k).group(1) |
|
|
|
|
|
account_derivation = "44'/0'/%s'"%account_id |
|
|
|
|
|
if account_derivation is not None: |
|
|
|
|
|
xpub_path[xpub] = account_derivation |
|
|
|
|
|
|
|
|
|
|
|
self.plugin.sign_transaction(tx, prev_tx, xpub_path) |
|
|
|
|
|
|
|
|
|
|
|
def check_proper_device(self): |
|
|
|
|
|
self.get_client().ping('t') |
|
|
|
|
|
if not self.device_checked: |
|
|
|
|
|
address = self.addresses(False)[0] |
|
|
|
|
|
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 |
|
|
|
|
|
self.proper_device = (device_address == address) |
|
|
|
|
|
|
|
|
|
|
|
return self.proper_device |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Plugin(BasePlugin): |
|
|
class Plugin(BasePlugin): |
|
|
|
|
|
|
|
|
def __init__(self, parent, config, name): |
|
|
def __init__(self, parent, config, name): |
|
@ -125,107 +278,6 @@ class Plugin(BasePlugin): |
|
|
if self.handler is None: |
|
|
if self.handler is None: |
|
|
self.handler = TrezorCmdLineHandler() |
|
|
self.handler = TrezorCmdLineHandler() |
|
|
|
|
|
|
|
|
@hook |
|
|
|
|
|
def load_wallet(self, wallet, window): |
|
|
|
|
|
self.print_error("load_wallet") |
|
|
|
|
|
self.wallet = wallet |
|
|
|
|
|
self.wallet.plugin = self |
|
|
|
|
|
self.trezor_button = StatusBarButton(QIcon(":icons/trezor.png"), _("Trezor"), partial(self.settings_dialog, window)) |
|
|
|
|
|
if type(window) is ElectrumWindow: |
|
|
|
|
|
window.statusBar().addPermanentWidget(self.trezor_button) |
|
|
|
|
|
if self.handler is None: |
|
|
|
|
|
self.handler = TrezorQtHandler(window) |
|
|
|
|
|
try: |
|
|
|
|
|
self.get_client().ping('t') |
|
|
|
|
|
except BaseException as e: |
|
|
|
|
|
QMessageBox.information(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(window, _('Error'), _("This wallet does not match your Trezor device"), _('OK')) |
|
|
|
|
|
self.wallet.force_watching_only = True |
|
|
|
|
|
|
|
|
|
|
|
@hook |
|
|
|
|
|
def installwizard_load_wallet(self, wallet, window): |
|
|
|
|
|
if type(wallet) != TrezorWallet: |
|
|
|
|
|
return |
|
|
|
|
|
self.load_wallet(wallet, window) |
|
|
|
|
|
|
|
|
|
|
|
@hook |
|
|
|
|
|
def installwizard_restore(self, wizard, storage): |
|
|
|
|
|
if storage.get('wallet_type') != 'trezor': |
|
|
|
|
|
return |
|
|
|
|
|
seed = wizard.enter_seed_dialog("Enter your Trezor seed", None, func=lambda x:True) |
|
|
|
|
|
if not seed: |
|
|
|
|
|
return |
|
|
|
|
|
wallet = TrezorWallet(storage) |
|
|
|
|
|
self.wallet = wallet |
|
|
|
|
|
handler = TrezorQtHandler(wizard) |
|
|
|
|
|
passphrase = handler.get_passphrase(_("Please enter your Trezor passphrase.") + '\n' + _("Press OK if you do not use one.")) |
|
|
|
|
|
if passphrase is None: |
|
|
|
|
|
return |
|
|
|
|
|
password = wizard.password_dialog() |
|
|
|
|
|
wallet.add_seed(seed, password) |
|
|
|
|
|
wallet.add_cosigner_seed(seed, 'x/', password, passphrase) |
|
|
|
|
|
wallet.create_main_account(password) |
|
|
|
|
|
# disable trezor plugin |
|
|
|
|
|
self.set_enabled(False) |
|
|
|
|
|
return wallet |
|
|
|
|
|
|
|
|
|
|
|
@hook |
|
|
|
|
|
def receive_menu(self, menu, addrs): |
|
|
|
|
|
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() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def settings_dialog(self, window): |
|
|
|
|
|
try: |
|
|
|
|
|
device_id = self.get_client().get_device_id() |
|
|
|
|
|
except BaseException as e: |
|
|
|
|
|
window.show_message(str(e)) |
|
|
|
|
|
return |
|
|
|
|
|
get_label = lambda: self.get_client().features.label |
|
|
|
|
|
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) |
|
|
|
|
|
layout.addWidget(QLabel(" %s" % device_id),1,1) |
|
|
|
|
|
|
|
|
|
|
|
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]) |
|
|
|
|
|
self.handler.show_message("Please confirm label change on Trezor") |
|
|
|
|
|
status = self.get_client().apply_settings(label=new_label) |
|
|
|
|
|
self.handler.stop() |
|
|
|
|
|
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) |
|
|
|
|
|
d.exec_() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def sign_transaction(self, tx, prev_tx, xpub_path): |
|
|
def sign_transaction(self, tx, prev_tx, xpub_path): |
|
|
self.prev_tx = prev_tx |
|
|
self.prev_tx = prev_tx |
|
|
self.xpub_path = xpub_path |
|
|
self.xpub_path = xpub_path |
|
@ -347,169 +399,119 @@ class Plugin(BasePlugin): |
|
|
return self.electrum_tx_to_txtype(tx) |
|
|
return self.electrum_tx_to_txtype(tx) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from PyQt4.Qt import QMessageBox, QDialog, QVBoxLayout, QLabel, QThread, SIGNAL, QGridLayout, QInputDialog, QPushButton |
|
|
|
|
|
import PyQt4.QtCore as QtCore |
|
|
|
|
|
from electrum_gui.qt.util import * |
|
|
|
|
|
from electrum_gui.qt.main_window import StatusBarButton, ElectrumWindow |
|
|
|
|
|
from electrum_gui.qt.installwizard import InstallWizard |
|
|
|
|
|
from trezorlib.qt.pinmatrix import PinMatrixWidget |
|
|
|
|
|
|
|
|
|
|
|
class QtPlugin(Plugin): |
|
|
|
|
|
|
|
|
class TrezorWallet(BIP32_HD_Wallet): |
|
|
@hook |
|
|
wallet_type = 'trezor' |
|
|
def load_wallet(self, wallet, window): |
|
|
root_derivation = "m/44'/0'" |
|
|
self.print_error("load_wallet") |
|
|
|
|
|
self.wallet = wallet |
|
|
def __init__(self, storage): |
|
|
self.wallet.plugin = self |
|
|
BIP32_HD_Wallet.__init__(self, storage) |
|
|
self.trezor_button = StatusBarButton(QIcon(":icons/trezor.png"), _("Trezor"), partial(self.settings_dialog, window)) |
|
|
self.mpk = None |
|
|
if type(window) is ElectrumWindow: |
|
|
self.device_checked = False |
|
|
window.statusBar().addPermanentWidget(self.trezor_button) |
|
|
self.proper_device = False |
|
|
if self.handler is None: |
|
|
self.force_watching_only = False |
|
|
self.handler = TrezorQtHandler(window) |
|
|
|
|
|
try: |
|
|
def get_action(self): |
|
|
self.get_client().ping('t') |
|
|
if not self.accounts: |
|
|
except BaseException as e: |
|
|
return 'create_accounts' |
|
|
QMessageBox.information(window, _('Error'), _("Trezor device not detected.\nContinuing in watching-only mode." + '\n\nReason:\n' + str(e)), _('OK')) |
|
|
|
|
|
self.wallet.force_watching_only = True |
|
|
def can_import(self): |
|
|
return |
|
|
return False |
|
|
if self.wallet.addresses() and not self.wallet.check_proper_device(): |
|
|
|
|
|
QMessageBox.information(window, _('Error'), _("This wallet does not match your Trezor device"), _('OK')) |
|
|
def can_sign_xpubkey(self, x_pubkey): |
|
|
self.wallet.force_watching_only = True |
|
|
xpub, sequence = BIP32_Account.parse_xpubkey(x_pubkey) |
|
|
|
|
|
return xpub in self.master_public_keys.values() |
|
|
|
|
|
|
|
|
|
|
|
def can_export(self): |
|
|
|
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
def can_create_accounts(self): |
|
|
|
|
|
return True |
|
|
|
|
|
|
|
|
|
|
|
def can_change_password(self): |
|
|
|
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
def is_watching_only(self): |
|
|
|
|
|
return self.force_watching_only |
|
|
|
|
|
|
|
|
|
|
|
def get_client(self): |
|
|
|
|
|
return self.plugin.get_client() |
|
|
|
|
|
|
|
|
|
|
|
def address_id(self, address): |
|
|
|
|
|
account_id, (change, address_index) = self.get_address_index(address) |
|
|
|
|
|
return "44'/0'/%s'/%d/%d" % (account_id, change, address_index) |
|
|
|
|
|
|
|
|
|
|
|
def create_main_account(self, password): |
|
|
|
|
|
self.create_account('Main account', None) #name, empty password |
|
|
|
|
|
|
|
|
|
|
|
def mnemonic_to_seed(self, mnemonic, passphrase): |
|
|
|
|
|
# trezor uses bip39 |
|
|
|
|
|
import pbkdf2, hashlib, hmac |
|
|
|
|
|
PBKDF2_ROUNDS = 2048 |
|
|
|
|
|
mnemonic = unicodedata.normalize('NFKD', ' '.join(mnemonic.split())) |
|
|
|
|
|
passphrase = unicodedata.normalize('NFKD', passphrase) |
|
|
|
|
|
return pbkdf2.PBKDF2(mnemonic, 'mnemonic' + passphrase, iterations = PBKDF2_ROUNDS, macmodule = hmac, digestmodule = hashlib.sha512).read(64) |
|
|
|
|
|
|
|
|
|
|
|
def derive_xkeys(self, root, derivation, password): |
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
def get_public_key(self, bip32_path): |
|
|
|
|
|
address_n = self.plugin.get_client().expand_path(bip32_path) |
|
|
|
|
|
node = self.plugin.get_client().get_public_node(address_n).node |
|
|
|
|
|
xpub = "0488B21E".decode('hex') + chr(node.depth) + self.i4b(node.fingerprint) + self.i4b(node.child_num) + node.chain_code + node.public_key |
|
|
|
|
|
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): |
|
|
@hook |
|
|
return pack('>I', x) |
|
|
def installwizard_load_wallet(self, wallet, window): |
|
|
|
|
|
if type(wallet) != TrezorWallet: |
|
|
|
|
|
return |
|
|
|
|
|
self.load_wallet(wallet, window) |
|
|
|
|
|
|
|
|
def add_keypairs(self, tx, keypairs, password): |
|
|
@hook |
|
|
#do nothing - no priv keys available |
|
|
def installwizard_restore(self, wizard, storage): |
|
|
pass |
|
|
if storage.get('wallet_type') != 'trezor': |
|
|
|
|
|
return |
|
|
|
|
|
seed = wizard.enter_seed_dialog("Enter your Trezor seed", None, func=lambda x:True) |
|
|
|
|
|
if not seed: |
|
|
|
|
|
return |
|
|
|
|
|
wallet = TrezorWallet(storage) |
|
|
|
|
|
self.wallet = wallet |
|
|
|
|
|
handler = TrezorQtHandler(wizard) |
|
|
|
|
|
passphrase = handler.get_passphrase(_("Please enter your Trezor passphrase.") + '\n' + _("Press OK if you do not use one.")) |
|
|
|
|
|
if passphrase is None: |
|
|
|
|
|
return |
|
|
|
|
|
password = wizard.password_dialog() |
|
|
|
|
|
wallet.add_seed(seed, password) |
|
|
|
|
|
wallet.add_cosigner_seed(seed, 'x/', password, passphrase) |
|
|
|
|
|
wallet.create_main_account(password) |
|
|
|
|
|
# disable trezor plugin |
|
|
|
|
|
self.set_enabled(False) |
|
|
|
|
|
return wallet |
|
|
|
|
|
|
|
|
def decrypt_message(self, pubkey, message, password): |
|
|
@hook |
|
|
raise BaseException( _('Decrypt method is not implemented in Trezor') ) |
|
|
def receive_menu(self, menu, addrs): |
|
|
#address = public_key_to_bc_address(pubkey.decode('hex')) |
|
|
if not self.wallet.is_watching_only() and self.atleast_version(1, 3) and len(addrs) == 1: |
|
|
#address_path = self.address_id(address) |
|
|
menu.addAction(_("Show on TREZOR"), lambda: self.show_address(addrs[0])) |
|
|
#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: |
|
|
|
|
|
# twd.stop() |
|
|
|
|
|
#return str(decrypted_msg) |
|
|
|
|
|
|
|
|
|
|
|
def sign_message(self, address, message, password): |
|
|
def show_address(self, address): |
|
|
if self.has_seed(): |
|
|
if not self.wallet.check_proper_device(): |
|
|
return BIP32_HD_Wallet.sign_message(self, address, message, password) |
|
|
|
|
|
if not self.check_proper_device(): |
|
|
|
|
|
give_error('Wrong device or password') |
|
|
give_error('Wrong device or password') |
|
|
try: |
|
|
try: |
|
|
address_path = self.address_id(address) |
|
|
address_path = self.wallet.address_id(address) |
|
|
address_n = self.plugin.get_client().expand_path(address_path) |
|
|
address_n = self.get_client().expand_path(address_path) |
|
|
except Exception, e: |
|
|
except Exception, e: |
|
|
give_error(e) |
|
|
give_error(e) |
|
|
try: |
|
|
try: |
|
|
msg_sig = self.plugin.get_client().sign_message('Bitcoin', address_n, message) |
|
|
self.get_client().get_address('Bitcoin', address_n, True) |
|
|
except Exception, e: |
|
|
except Exception, e: |
|
|
give_error(e) |
|
|
give_error(e) |
|
|
finally: |
|
|
finally: |
|
|
self.plugin.handler.stop() |
|
|
self.handler.stop() |
|
|
return msg_sig.signature |
|
|
|
|
|
|
|
|
|
|
|
def sign_transaction(self, tx, password): |
|
|
|
|
|
if self.has_seed(): |
|
|
def settings_dialog(self, window): |
|
|
return BIP32_HD_Wallet.sign_transaction(self, tx, password) |
|
|
try: |
|
|
if tx.is_complete(): |
|
|
device_id = self.get_client().get_device_id() |
|
|
|
|
|
except BaseException as e: |
|
|
|
|
|
window.show_message(str(e)) |
|
|
return |
|
|
return |
|
|
if not self.check_proper_device(): |
|
|
get_label = lambda: self.get_client().features.label |
|
|
give_error('Wrong device or password') |
|
|
update_label = lambda: current_label_label.setText("Label: %s" % get_label()) |
|
|
# previous transactions used as inputs |
|
|
d = QDialog() |
|
|
prev_tx = {} |
|
|
layout = QGridLayout(d) |
|
|
# path of the xpubs that are involved |
|
|
layout.addWidget(QLabel("Trezor Options"),0,0) |
|
|
xpub_path = {} |
|
|
layout.addWidget(QLabel("ID:"),1,0) |
|
|
for txin in tx.inputs: |
|
|
layout.addWidget(QLabel(" %s" % device_id),1,1) |
|
|
tx_hash = txin['prevout_hash'] |
|
|
|
|
|
|
|
|
|
|
|
ptx = self.transactions.get(tx_hash) |
|
|
def modify_label(): |
|
|
if ptx is None: |
|
|
response = QInputDialog().getText(None, "Set New Trezor Label", "New Trezor Label: (upon submission confirm on Trezor)") |
|
|
ptx = self.network.synchronous_get(('blockchain.transaction.get', [tx_hash])) |
|
|
if not response[1]: |
|
|
ptx = Transaction(ptx) |
|
|
return |
|
|
prev_tx[tx_hash] = ptx |
|
|
new_label = str(response[0]) |
|
|
|
|
|
self.handler.show_message("Please confirm label change on Trezor") |
|
|
|
|
|
status = self.get_client().apply_settings(label=new_label) |
|
|
|
|
|
self.handler.stop() |
|
|
|
|
|
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) |
|
|
|
|
|
d.exec_() |
|
|
|
|
|
|
|
|
for x_pubkey in txin['x_pubkeys']: |
|
|
|
|
|
account_derivation = None |
|
|
|
|
|
if not is_extended_pubkey(x_pubkey): |
|
|
|
|
|
continue |
|
|
|
|
|
xpub = x_to_xpub(x_pubkey) |
|
|
|
|
|
for k, v in self.master_public_keys.items(): |
|
|
|
|
|
if v == xpub: |
|
|
|
|
|
account_id = re.match("x/(\d+)'", k).group(1) |
|
|
|
|
|
account_derivation = "44'/0'/%s'"%account_id |
|
|
|
|
|
if account_derivation is not None: |
|
|
|
|
|
xpub_path[xpub] = account_derivation |
|
|
|
|
|
|
|
|
|
|
|
self.plugin.sign_transaction(tx, prev_tx, xpub_path) |
|
|
|
|
|
|
|
|
|
|
|
def check_proper_device(self): |
|
|
|
|
|
self.get_client().ping('t') |
|
|
|
|
|
if not self.device_checked: |
|
|
|
|
|
address = self.addresses(False)[0] |
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TrezorGuiMixin(object): |
|
|
class TrezorGuiMixin(object): |
|
|