2015-12-27 01:23:46 -08:00
|
|
|
import re
|
2016-01-01 16:43:56 -08:00
|
|
|
import time
|
|
|
|
|
2015-12-26 02:42:57 -08:00
|
|
|
from binascii import unhexlify
|
2015-12-28 04:58:09 -08:00
|
|
|
from struct import pack
|
2015-12-26 02:42:57 -08:00
|
|
|
|
|
|
|
from electrum.account import BIP32_Account
|
2015-12-28 04:58:09 -08:00
|
|
|
from electrum.bitcoin import (bc_address_to_hash_160, xpub_from_pubkey,
|
2015-12-28 05:11:11 -08:00
|
|
|
EncodeBase58Check)
|
2015-12-26 02:42:57 -08:00
|
|
|
from electrum.i18n import _
|
|
|
|
from electrum.plugins import BasePlugin, hook
|
2015-12-27 01:23:46 -08:00
|
|
|
from electrum.transaction import (deserialize, is_extended_pubkey,
|
|
|
|
Transaction, x_to_xpub)
|
2015-12-28 04:58:09 -08:00
|
|
|
from electrum.wallet import BIP32_HD_Wallet, BIP44_Wallet
|
2016-01-03 03:46:47 -08:00
|
|
|
from electrum.util import ThreadJob
|
2016-01-04 13:47:14 -08:00
|
|
|
from electrum.plugins import DeviceMgr
|
2016-01-03 06:44:33 -08:00
|
|
|
from electrum.wizard import WizardBase
|
2015-12-26 02:42:57 -08:00
|
|
|
|
2016-01-01 16:43:56 -08:00
|
|
|
class DeviceDisconnectedError(Exception):
|
|
|
|
pass
|
|
|
|
|
2016-01-04 13:47:14 -08:00
|
|
|
class OutdatedFirmwareError(Exception):
|
|
|
|
pass
|
|
|
|
|
2015-12-28 03:24:15 -08:00
|
|
|
class TrezorCompatibleWallet(BIP44_Wallet):
|
|
|
|
# Extend BIP44 Wallet as required by hardware implementation.
|
2015-12-27 01:23:46 -08:00
|
|
|
# Derived classes must set:
|
|
|
|
# - device
|
2016-01-04 13:47:14 -08:00
|
|
|
# - DEVICE_IDS
|
2015-12-27 01:23:46 -08:00
|
|
|
# - wallet_type
|
2015-12-28 04:58:09 -08:00
|
|
|
|
2015-12-28 03:24:15 -08:00
|
|
|
restore_wallet_class = BIP44_Wallet
|
2015-12-27 01:23:46 -08:00
|
|
|
|
|
|
|
def __init__(self, storage):
|
2015-12-28 03:24:15 -08:00
|
|
|
BIP44_Wallet.__init__(self, storage)
|
2016-01-01 16:43:56 -08:00
|
|
|
# This is set when paired with a device, and used to re-pair
|
|
|
|
# a device that is disconnected and re-connected
|
|
|
|
self.device_id = None
|
2016-01-03 00:25:47 -08:00
|
|
|
# After timeout seconds we clear the device session
|
|
|
|
self.session_timeout = storage.get('session_timeout', 180)
|
2016-01-01 16:43:56 -08:00
|
|
|
# Errors and other user interaction is done through the wallet's
|
|
|
|
# handler. The handler is per-window and preserved across
|
|
|
|
# device reconnects
|
|
|
|
self.handler = None
|
|
|
|
|
2016-01-03 00:25:47 -08:00
|
|
|
def set_session_timeout(self, seconds):
|
|
|
|
self.print_error("setting session timeout to %d seconds" % seconds)
|
|
|
|
self.session_timeout = seconds
|
|
|
|
self.storage.put('session_timeout', seconds)
|
|
|
|
|
2016-01-01 16:43:56 -08:00
|
|
|
def disconnected(self):
|
2016-01-03 04:03:11 -08:00
|
|
|
'''A device paired with the wallet was diconnected. Note this is
|
|
|
|
called in the context of the Plugins thread.'''
|
2016-01-01 16:43:56 -08:00
|
|
|
self.print_error("disconnected")
|
|
|
|
self.handler.watching_only_changed()
|
|
|
|
|
|
|
|
def connected(self):
|
2016-01-03 04:03:11 -08:00
|
|
|
'''A device paired with the wallet was (re-)connected. Note this
|
|
|
|
is called in the context of the Plugins thread.'''
|
2016-01-01 16:43:56 -08:00
|
|
|
self.print_error("connected")
|
|
|
|
self.handler.watching_only_changed()
|
2015-12-27 01:23:46 -08:00
|
|
|
|
2015-12-27 07:12:26 -08:00
|
|
|
def wiped(self):
|
|
|
|
self.print_error("wiped")
|
|
|
|
self.handler.watching_only_changed()
|
|
|
|
|
2016-01-03 00:25:47 -08:00
|
|
|
def timeout(self):
|
2016-01-03 04:03:11 -08:00
|
|
|
'''Informs the wallet it timed out. Note this is called from
|
|
|
|
the Plugins thread.'''
|
2016-01-03 00:25:47 -08:00
|
|
|
self.print_error("timed out")
|
2015-12-27 07:12:26 -08:00
|
|
|
|
2015-12-28 04:58:09 -08:00
|
|
|
def get_action(self):
|
2015-12-30 18:36:33 -08:00
|
|
|
pass
|
2015-12-28 04:58:09 -08:00
|
|
|
|
2016-01-02 18:10:31 -08:00
|
|
|
def can_create_accounts(self):
|
|
|
|
return True
|
|
|
|
|
2015-12-27 01:23:46 -08:00
|
|
|
def can_export(self):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def is_watching_only(self):
|
2016-01-02 16:18:29 -08:00
|
|
|
'''The wallet is watching-only if its trezor device is not connected,
|
|
|
|
or if it is connected but uninitialized.'''
|
2015-12-30 16:56:05 -08:00
|
|
|
assert not self.has_seed()
|
2016-01-04 13:47:14 -08:00
|
|
|
client = self.get_client(DeviceMgr.CACHED)
|
2016-01-02 16:18:29 -08:00
|
|
|
return not (client and client.is_initialized())
|
2015-12-27 01:23:46 -08:00
|
|
|
|
|
|
|
def can_change_password(self):
|
|
|
|
return False
|
|
|
|
|
2016-01-04 13:47:14 -08:00
|
|
|
def get_client(self, lookup=DeviceMgr.PAIRED):
|
|
|
|
return self.plugin.get_client(self, lookup)
|
|
|
|
|
|
|
|
def first_address(self):
|
|
|
|
'''Used to check a hardware wallet matches a software wallet'''
|
|
|
|
account = self.accounts.get('0')
|
|
|
|
derivation = self.address_derivation('0', 0, 0)
|
|
|
|
return (account.first_address()[0] if account else None, derivation)
|
2015-12-27 01:23:46 -08:00
|
|
|
|
2015-12-28 04:58:09 -08:00
|
|
|
def derive_xkeys(self, root, derivation, password):
|
2015-12-28 05:11:11 -08:00
|
|
|
if self.master_public_keys.get(root):
|
|
|
|
return BIP44_wallet.derive_xkeys(self, root, derivation, password)
|
|
|
|
|
2016-01-01 16:43:56 -08:00
|
|
|
# When creating a wallet we need to ask the device for the
|
|
|
|
# master public key
|
2015-12-28 05:11:11 -08:00
|
|
|
derivation = derivation.replace(self.root_name, self.prefix() + "/")
|
|
|
|
xpub = self.get_public_key(derivation)
|
|
|
|
return xpub, None
|
2015-12-28 04:58:09 -08:00
|
|
|
|
|
|
|
def get_public_key(self, bip32_path):
|
2016-01-04 13:47:14 -08:00
|
|
|
client = self.get_client()
|
2016-01-01 04:11:46 -08:00
|
|
|
address_n = client.expand_path(bip32_path)
|
|
|
|
node = client.get_public_node(address_n).node
|
2015-12-28 04:58:09 -08:00
|
|
|
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 i4b(self, x):
|
|
|
|
return pack('>I', x)
|
|
|
|
|
2015-12-27 01:23:46 -08:00
|
|
|
def decrypt_message(self, pubkey, message, password):
|
|
|
|
raise RuntimeError(_('Decrypt method is not implemented'))
|
|
|
|
|
|
|
|
def sign_message(self, address, message, password):
|
2016-01-04 13:47:14 -08:00
|
|
|
client = self.get_client()
|
2016-01-01 16:43:56 -08:00
|
|
|
address_path = self.address_id(address)
|
|
|
|
address_n = client.expand_path(address_path)
|
|
|
|
msg_sig = client.sign_message('Bitcoin', address_n, message)
|
2015-12-27 01:23:46 -08:00
|
|
|
return msg_sig.signature
|
|
|
|
|
|
|
|
def sign_transaction(self, tx, password):
|
|
|
|
if tx.is_complete() or self.is_watching_only():
|
|
|
|
return
|
|
|
|
# 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']:
|
|
|
|
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:
|
|
|
|
acc_id = re.match("x/(\d+)'", k).group(1)
|
|
|
|
xpub_path[xpub] = self.account_derivation(acc_id)
|
|
|
|
|
2015-12-30 00:03:26 -08:00
|
|
|
self.plugin.sign_transaction(self, tx, prev_tx, xpub_path)
|
2015-12-27 01:23:46 -08:00
|
|
|
|
2015-12-26 22:10:53 -08:00
|
|
|
|
2016-01-03 03:46:47 -08:00
|
|
|
class TrezorCompatiblePlugin(BasePlugin, ThreadJob):
|
2015-12-26 02:42:57 -08:00
|
|
|
# Derived classes provide:
|
2015-12-26 20:56:50 -08:00
|
|
|
#
|
2015-12-26 22:00:58 -08:00
|
|
|
# class-static variables: client_class, firmware_URL, handler_class,
|
2015-12-26 20:56:50 -08:00
|
|
|
# libraries_available, libraries_URL, minimum_firmware,
|
|
|
|
# wallet_class, ckd_public, types, HidTransport
|
2015-12-26 02:42:57 -08:00
|
|
|
|
|
|
|
def __init__(self, parent, config, name):
|
|
|
|
BasePlugin.__init__(self, parent, config, name)
|
2015-12-26 20:56:50 -08:00
|
|
|
self.device = self.wallet_class.device
|
2015-12-30 18:36:33 -08:00
|
|
|
self.wallet_class.plugin = self
|
2016-01-03 00:25:47 -08:00
|
|
|
self.prevent_timeout = time.time() + 3600 * 24 * 365
|
2016-01-04 13:47:14 -08:00
|
|
|
self.device_manager().register_devices(self, self.DEVICE_IDS)
|
|
|
|
|
|
|
|
def is_enabled(self):
|
|
|
|
return self.libraries_available
|
|
|
|
|
|
|
|
def device_manager(self):
|
|
|
|
return self.parent.device_manager
|
2015-12-26 02:42:57 -08:00
|
|
|
|
2016-01-03 03:46:47 -08:00
|
|
|
def thread_jobs(self):
|
2016-01-04 13:47:14 -08:00
|
|
|
# Thread job to handle device timeouts
|
2016-01-03 03:46:47 -08:00
|
|
|
return [self] if self.libraries_available else []
|
|
|
|
|
|
|
|
def run(self):
|
2016-01-04 13:47:14 -08:00
|
|
|
'''Handle device timeouts. Runs in the context of the Plugins
|
|
|
|
thread.'''
|
2016-01-03 03:46:47 -08:00
|
|
|
now = time.time()
|
2016-01-04 13:47:14 -08:00
|
|
|
for wallet in self.device_manager().paired_wallets():
|
|
|
|
if (isinstance(wallet, self.wallet_class)
|
|
|
|
and hasattr(wallet, 'last_operation')
|
|
|
|
and now > wallet.last_operation + wallet.session_timeout):
|
|
|
|
client = self.get_client(wallet, DeviceMgr.CACHED)
|
|
|
|
if client:
|
|
|
|
wallet.last_operation = self.prevent_timeout
|
|
|
|
client.clear_session()
|
|
|
|
wallet.timeout()
|
|
|
|
|
|
|
|
def create_client(self, path, product_key):
|
|
|
|
pair = ((None, path) if self.HidTransport._detect_debuglink(path)
|
|
|
|
else (path, None))
|
|
|
|
try:
|
|
|
|
transport = self.HidTransport(pair)
|
|
|
|
except BaseException as e:
|
|
|
|
# We were probably just disconnected; never mind
|
|
|
|
self.print_error("cannot connect at", path, str(e))
|
|
|
|
return None
|
|
|
|
self.print_error("connected to device at", path)
|
|
|
|
return self.client_class(transport, path, self)
|
2016-01-01 16:43:56 -08:00
|
|
|
|
2016-01-04 13:47:14 -08:00
|
|
|
def get_client(self, wallet, lookup=DeviceMgr.PAIRED, check_firmware=True):
|
|
|
|
'''check_firmware is ignored unless doing a PAIRED lookup.'''
|
|
|
|
client = self.device_manager().get_client(wallet, lookup)
|
2016-01-01 16:43:56 -08:00
|
|
|
|
2016-01-04 13:47:14 -08:00
|
|
|
# Try a ping if doing at least a PRESENT lookup
|
|
|
|
if client and lookup != DeviceMgr.CACHED:
|
|
|
|
self.print_error("set last_operation")
|
|
|
|
wallet.last_operation = time.time()
|
2016-01-01 16:43:56 -08:00
|
|
|
try:
|
2016-01-04 13:47:14 -08:00
|
|
|
client.ping('t')
|
2016-01-01 16:43:56 -08:00
|
|
|
except BaseException as e:
|
2016-01-04 13:47:14 -08:00
|
|
|
self.print_error("ping failed", str(e))
|
|
|
|
# Remove it from the manager's cache
|
|
|
|
self.device_manager().close_client(client)
|
|
|
|
client = None
|
|
|
|
|
|
|
|
if lookup == DeviceMgr.PAIRED:
|
|
|
|
assert wallet.handler
|
|
|
|
if not client:
|
|
|
|
msg = (_('Could not connect to your %s. Verify the '
|
|
|
|
'cable is connected and that no other app is '
|
|
|
|
'using it.\nContinuing in watching-only mode '
|
|
|
|
'until the device is re-connected.') % self.device)
|
|
|
|
wallet.handler.show_error(msg)
|
|
|
|
raise DeviceDisconnectedError(msg)
|
|
|
|
|
|
|
|
if (check_firmware and not
|
|
|
|
client.atleast_version(*self.minimum_firmware)):
|
|
|
|
msg = (_('Outdated %s firmware for device labelled %s. Please '
|
|
|
|
'download the updated firmware from %s') %
|
|
|
|
(self.device, client.label(), self.firmware_URL))
|
|
|
|
wallet.handler.show_error(msg)
|
|
|
|
raise OutdatedFirmwareError(msg)
|
2016-01-01 16:43:56 -08:00
|
|
|
|
2016-01-04 13:47:14 -08:00
|
|
|
return client
|
2016-01-01 16:43:56 -08:00
|
|
|
|
2016-01-04 13:47:14 -08:00
|
|
|
@hook
|
|
|
|
def close_wallet(self, wallet):
|
|
|
|
if isinstance(wallet, self.wallet_class):
|
|
|
|
self.device_manager().close_wallet(wallet)
|
2016-01-01 16:43:56 -08:00
|
|
|
|
2016-01-02 20:32:33 -08:00
|
|
|
def initialize_device(self, wallet, wizard):
|
2016-01-03 00:25:47 -08:00
|
|
|
# Prevent timeouts during initialization
|
|
|
|
wallet.last_operation = self.prevent_timeout
|
|
|
|
|
2016-01-03 06:44:33 -08:00
|
|
|
# Initialization method
|
|
|
|
msg = _("Please select how you want to initialize your %s.\n"
|
|
|
|
"The first two are secure as no secret information is entered "
|
|
|
|
"onto your computer.\nFor the last two methods you enter "
|
|
|
|
"secrets into your computer and upload them to the device, "
|
|
|
|
"and so should only be done on a computer you know to be "
|
|
|
|
"trustworthy and free of malware."
|
|
|
|
) % self.device
|
|
|
|
|
|
|
|
methods = [
|
|
|
|
_("Let the device generate a completely new seed randomly"),
|
|
|
|
_("Recover from an existing %s seed you have previously written "
|
|
|
|
"down" % self.device),
|
|
|
|
_("Upload a BIP39 mnemonic to generate the seed"),
|
|
|
|
_("Upload a master private key")
|
|
|
|
]
|
|
|
|
|
|
|
|
method = wizard.query_choice(msg, methods)
|
|
|
|
(item, label, pin_protection, passphrase_protection) \
|
|
|
|
= wizard.request_trezor_init_settings(method, self.device)
|
2016-01-02 20:32:33 -08:00
|
|
|
|
2016-01-04 13:47:14 -08:00
|
|
|
client = self.get_client(wallet)
|
2016-01-03 06:44:33 -08:00
|
|
|
language = 'english'
|
|
|
|
|
|
|
|
if method == WizardBase.TIM_NEW:
|
|
|
|
strength = 64 * (item + 2) # 128, 192 or 256
|
|
|
|
client.reset_device(True, strength, passphrase_protection,
|
|
|
|
pin_protection, label, language)
|
|
|
|
elif method == WizardBase.TIM_RECOVER:
|
|
|
|
word_count = 6 * (item + 2) # 12, 18 or 24
|
|
|
|
client.recovery_device(word_count, passphrase_protection,
|
|
|
|
pin_protection, label, language)
|
|
|
|
elif method == WizardBase.TIM_MNEMONIC:
|
|
|
|
pin = pin_protection # It's the pin, not a boolean
|
|
|
|
client.load_device_by_mnemonic(str(item), pin,
|
|
|
|
passphrase_protection,
|
|
|
|
label, language)
|
|
|
|
else:
|
|
|
|
pin = pin_protection # It's the pin, not a boolean
|
|
|
|
client.load_device_by_xprv(item, pin, passphrase_protection,
|
|
|
|
label, language)
|
2016-01-02 20:32:33 -08:00
|
|
|
|
2016-01-01 16:43:56 -08:00
|
|
|
def select_device(self, wallet, wizard):
|
2016-01-02 20:32:33 -08:00
|
|
|
'''Called when creating a new wallet. Select the device to use. If
|
|
|
|
the device is uninitialized, go through the intialization
|
|
|
|
process.'''
|
2016-01-04 13:47:14 -08:00
|
|
|
self.device_manager().scan_devices()
|
|
|
|
clients = self.device_manager().clients_of_type(self.client_class)
|
2016-01-03 06:44:33 -08:00
|
|
|
suffixes = [_(" (wiped)"), _(" (initialized)")]
|
|
|
|
def client_desc(client):
|
|
|
|
label = client.label() or _("An unnamed device")
|
|
|
|
return label + suffixes[client.is_initialized()]
|
|
|
|
labels = list(map(client_desc, clients))
|
|
|
|
|
2016-01-02 20:32:33 -08:00
|
|
|
msg = _("Please select which %s device to use:") % self.device
|
|
|
|
client = clients[wizard.query_choice(msg, labels)]
|
2016-01-04 13:47:14 -08:00
|
|
|
self.device_manager().pair_wallet(wallet, client)
|
2016-01-02 20:32:33 -08:00
|
|
|
if not client.is_initialized():
|
|
|
|
self.initialize_device(wallet, wizard)
|
2016-01-01 16:43:56 -08:00
|
|
|
|
2015-12-30 18:36:33 -08:00
|
|
|
def on_restore_wallet(self, wallet, wizard):
|
|
|
|
assert isinstance(wallet, self.wallet_class)
|
|
|
|
|
|
|
|
msg = _("Enter the seed for your %s wallet:" % self.device)
|
|
|
|
seed = wizard.request_seed(msg, is_valid = self.is_valid_seed)
|
|
|
|
|
|
|
|
# Restored wallets are not hardware wallets
|
|
|
|
wallet_class = self.wallet_class.restore_wallet_class
|
|
|
|
wallet.storage.put('wallet_type', wallet_class.wallet_type)
|
|
|
|
wallet = wallet_class(wallet.storage)
|
|
|
|
|
|
|
|
passphrase = wizard.request_passphrase(self.device, restore=True)
|
|
|
|
password = wizard.request_password()
|
|
|
|
wallet.add_seed(seed, password)
|
|
|
|
wallet.add_cosigner_seed(seed, 'x/', password, passphrase)
|
|
|
|
wallet.create_main_account(password)
|
|
|
|
return wallet
|
|
|
|
|
2015-12-30 00:03:26 -08:00
|
|
|
def sign_transaction(self, wallet, tx, prev_tx, xpub_path):
|
2015-12-26 02:42:57 -08:00
|
|
|
self.prev_tx = prev_tx
|
|
|
|
self.xpub_path = xpub_path
|
2016-01-04 13:47:14 -08:00
|
|
|
client = self.get_client(wallet)
|
2015-12-26 02:42:57 -08:00
|
|
|
inputs = self.tx_inputs(tx, True)
|
2015-12-30 00:03:26 -08:00
|
|
|
outputs = self.tx_outputs(wallet, tx)
|
2016-01-01 16:43:56 -08:00
|
|
|
signed_tx = client.sign_tx('Bitcoin', inputs, outputs)[1]
|
2015-12-26 02:42:57 -08:00
|
|
|
raw = signed_tx.encode('hex')
|
|
|
|
tx.update_signatures(raw)
|
|
|
|
|
2015-12-30 00:03:26 -08:00
|
|
|
def show_address(self, wallet, address):
|
2016-01-04 13:47:14 -08:00
|
|
|
client = self.get_client(wallet)
|
2016-01-01 16:43:56 -08:00
|
|
|
if not client.atleast_version(1, 3):
|
|
|
|
wallet.handler.show_error(_("Your device firmware is too old"))
|
|
|
|
return
|
|
|
|
address_path = wallet.address_id(address)
|
|
|
|
address_n = client.expand_path(address_path)
|
|
|
|
client.get_address('Bitcoin', address_n, True)
|
2015-12-26 02:42:57 -08:00
|
|
|
|
|
|
|
def tx_inputs(self, tx, for_sig=False):
|
|
|
|
inputs = []
|
|
|
|
for txin in tx.inputs:
|
|
|
|
txinputtype = self.types.TxInputType()
|
|
|
|
if txin.get('is_coinbase'):
|
|
|
|
prev_hash = "\0"*32
|
2015-12-26 22:10:53 -08:00
|
|
|
prev_index = 0xffffffff # signed int -1
|
2015-12-26 02:42:57 -08:00
|
|
|
else:
|
|
|
|
if for_sig:
|
|
|
|
x_pubkeys = txin['x_pubkeys']
|
|
|
|
if len(x_pubkeys) == 1:
|
|
|
|
x_pubkey = x_pubkeys[0]
|
|
|
|
xpub, s = BIP32_Account.parse_xpubkey(x_pubkey)
|
2016-01-01 04:11:46 -08:00
|
|
|
xpub_n = self.client_class.expand_path(self.xpub_path[xpub])
|
2015-12-26 02:42:57 -08:00
|
|
|
txinputtype.address_n.extend(xpub_n + s)
|
|
|
|
else:
|
|
|
|
def f(x_pubkey):
|
|
|
|
if is_extended_pubkey(x_pubkey):
|
|
|
|
xpub, s = BIP32_Account.parse_xpubkey(x_pubkey)
|
|
|
|
else:
|
|
|
|
xpub = xpub_from_pubkey(x_pubkey.decode('hex'))
|
|
|
|
s = []
|
|
|
|
node = ckd_public.deserialize(xpub)
|
|
|
|
return self.types.HDNodePathType(node=node, address_n=s)
|
|
|
|
pubkeys = map(f, x_pubkeys)
|
|
|
|
multisig = self.types.MultisigRedeemScriptType(
|
|
|
|
pubkeys=pubkeys,
|
|
|
|
signatures=map(lambda x: x.decode('hex') if x else '', txin.get('signatures')),
|
|
|
|
m=txin.get('num_sig'),
|
|
|
|
)
|
|
|
|
txinputtype = self.types.TxInputType(
|
|
|
|
script_type=self.types.SPENDMULTISIG,
|
2015-12-26 22:10:53 -08:00
|
|
|
multisig=multisig
|
2015-12-26 02:42:57 -08:00
|
|
|
)
|
|
|
|
# find which key is mine
|
|
|
|
for x_pubkey in x_pubkeys:
|
|
|
|
if is_extended_pubkey(x_pubkey):
|
|
|
|
xpub, s = BIP32_Account.parse_xpubkey(x_pubkey)
|
|
|
|
if xpub in self.xpub_path:
|
2016-01-01 04:11:46 -08:00
|
|
|
xpub_n = self.client_class.expand_path(self.xpub_path[xpub])
|
2015-12-26 02:42:57 -08:00
|
|
|
txinputtype.address_n.extend(xpub_n + s)
|
|
|
|
break
|
|
|
|
|
|
|
|
prev_hash = unhexlify(txin['prevout_hash'])
|
|
|
|
prev_index = txin['prevout_n']
|
|
|
|
|
|
|
|
txinputtype.prev_hash = prev_hash
|
|
|
|
txinputtype.prev_index = prev_index
|
|
|
|
|
|
|
|
if 'scriptSig' in txin:
|
|
|
|
script_sig = txin['scriptSig'].decode('hex')
|
|
|
|
txinputtype.script_sig = script_sig
|
|
|
|
|
|
|
|
if 'sequence' in txin:
|
|
|
|
sequence = txin['sequence']
|
|
|
|
txinputtype.sequence = sequence
|
|
|
|
|
|
|
|
inputs.append(txinputtype)
|
|
|
|
|
|
|
|
return inputs
|
|
|
|
|
2015-12-30 00:03:26 -08:00
|
|
|
def tx_outputs(self, wallet, tx):
|
2015-12-26 02:42:57 -08:00
|
|
|
outputs = []
|
|
|
|
|
|
|
|
for type, address, amount in tx.outputs:
|
|
|
|
assert type == 'address'
|
|
|
|
txoutputtype = self.types.TxOutputType()
|
2015-12-30 00:03:26 -08:00
|
|
|
if wallet.is_change(address):
|
|
|
|
address_path = wallet.address_id(address)
|
2016-01-01 04:11:46 -08:00
|
|
|
address_n = self.client_class.expand_path(address_path)
|
2015-12-26 02:42:57 -08:00
|
|
|
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 = self.types.PAYTOADDRESS
|
|
|
|
elif addrtype == 5:
|
|
|
|
txoutputtype.script_type = self.types.PAYTOSCRIPTHASH
|
|
|
|
else:
|
|
|
|
raise BaseException('addrtype')
|
|
|
|
outputs.append(txoutputtype)
|
|
|
|
|
|
|
|
return outputs
|
|
|
|
|
|
|
|
def electrum_tx_to_txtype(self, tx):
|
|
|
|
t = self.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
|
|
|
|
|
2015-12-27 02:29:57 -08:00
|
|
|
# This function is called from the trezor libraries (via tx_api)
|
2015-12-26 02:42:57 -08:00
|
|
|
def get_tx(self, tx_hash):
|
|
|
|
tx = self.prev_tx[tx_hash]
|
|
|
|
tx.deserialize()
|
|
|
|
return self.electrum_tx_to_txtype(tx)
|
2016-01-01 01:38:43 -08:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def is_valid_seed(seed):
|
|
|
|
return True
|