2016-05-20 13:27:20 -07:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2013-09-12 20:31:24 -07:00
|
|
|
import os
|
2014-06-12 04:26:24 -07:00
|
|
|
import sys
|
2014-04-09 12:42:30 -07:00
|
|
|
import time
|
2014-01-05 16:54:53 -08:00
|
|
|
import binascii
|
2014-01-08 06:59:18 -08:00
|
|
|
import hashlib
|
2014-02-20 17:30:10 -08:00
|
|
|
import unicodedata
|
2014-06-27 08:02:44 -07:00
|
|
|
import json
|
2014-11-06 16:09:53 -08:00
|
|
|
import getpass
|
2013-09-12 20:31:24 -07:00
|
|
|
|
2014-02-20 16:48:11 -08:00
|
|
|
from mnemonic import Mnemonic
|
2013-09-12 20:31:24 -07:00
|
|
|
|
2016-05-04 19:57:14 -07:00
|
|
|
from . import tools
|
|
|
|
from . import mapping
|
|
|
|
from . import messages_pb2 as proto
|
|
|
|
from . import types_pb2 as types
|
|
|
|
from .debuglink import DebugLink
|
|
|
|
|
2014-11-06 16:58:20 -08:00
|
|
|
# try:
|
|
|
|
# from PIL import Image
|
|
|
|
# SCREENSHOT = True
|
|
|
|
# except:
|
|
|
|
# SCREENSHOT = False
|
|
|
|
|
|
|
|
SCREENSHOT = False
|
|
|
|
|
2015-06-23 07:26:31 -07:00
|
|
|
DEFAULT_CURVE = 'secp256k1'
|
|
|
|
|
2014-02-01 02:29:44 -08:00
|
|
|
# monkeypatching: text formatting of protobuf messages
|
|
|
|
tools.monkeypatch_google_protobuf_text_format()
|
2014-01-31 12:21:19 -08:00
|
|
|
|
2014-02-13 07:46:21 -08:00
|
|
|
def get_buttonrequest_value(code):
|
|
|
|
# Converts integer code to its string representation of ButtonRequestType
|
|
|
|
return [ k for k, v in types.ButtonRequestType.items() if v == code][0]
|
2014-02-02 09:27:44 -08:00
|
|
|
|
2014-02-13 08:20:40 -08:00
|
|
|
def pprint(msg):
|
2014-06-27 08:02:44 -07:00
|
|
|
msg_class = msg.__class__.__name__
|
|
|
|
msg_size = msg.ByteSize()
|
|
|
|
"""
|
|
|
|
msg_ser = msg.SerializeToString()
|
|
|
|
msg_id = mapping.get_type(msg)
|
|
|
|
msg_json = json.dumps(protobuf_json.pb2json(msg))
|
|
|
|
"""
|
2014-02-21 10:13:14 -08:00
|
|
|
if isinstance(msg, proto.FirmwareUpload):
|
2014-06-27 08:02:44 -07:00
|
|
|
return "<%s> (%d bytes):\n" % (msg_class, msg_size)
|
2014-02-21 10:13:14 -08:00
|
|
|
else:
|
2016-05-20 04:55:43 -07:00
|
|
|
return "<%s> (%d bytes):\n%s" % (msg_class, msg_size, msg)
|
2014-02-13 08:20:40 -08:00
|
|
|
|
2014-06-12 04:26:24 -07:00
|
|
|
def log(msg):
|
|
|
|
sys.stderr.write("%s\n" % msg)
|
|
|
|
sys.stderr.flush()
|
|
|
|
|
2013-09-12 20:31:24 -07:00
|
|
|
class CallException(Exception):
|
2014-02-08 15:12:22 -08:00
|
|
|
def __init__(self, code, message):
|
|
|
|
super(CallException, self).__init__()
|
|
|
|
self.args = [code, message]
|
2013-09-12 20:31:24 -07:00
|
|
|
|
|
|
|
class PinException(CallException):
|
|
|
|
pass
|
|
|
|
|
2014-02-13 07:46:21 -08:00
|
|
|
class field(object):
|
|
|
|
# Decorator extracts single value from
|
|
|
|
# protobuf object. If the field is not
|
|
|
|
# present, raises an exception.
|
|
|
|
def __init__(self, field):
|
|
|
|
self.field = field
|
|
|
|
|
|
|
|
def __call__(self, f):
|
|
|
|
def wrapped_f(*args, **kwargs):
|
|
|
|
ret = f(*args, **kwargs)
|
|
|
|
ret.HasField(self.field)
|
|
|
|
return getattr(ret, self.field)
|
|
|
|
return wrapped_f
|
|
|
|
|
|
|
|
class expect(object):
|
|
|
|
# Decorator checks if the method
|
|
|
|
# returned one of expected protobuf messages
|
|
|
|
# or raises an exception
|
|
|
|
def __init__(self, *expected):
|
|
|
|
self.expected = expected
|
2016-01-12 15:17:38 -08:00
|
|
|
|
2014-02-13 07:46:21 -08:00
|
|
|
def __call__(self, f):
|
|
|
|
def wrapped_f(*args, **kwargs):
|
|
|
|
ret = f(*args, **kwargs)
|
|
|
|
if not isinstance(ret, self.expected):
|
|
|
|
raise Exception("Got %s, expected %s" % (ret.__class__, self.expected))
|
|
|
|
return ret
|
|
|
|
return wrapped_f
|
|
|
|
|
2014-02-20 17:30:10 -08:00
|
|
|
def normalize_nfc(txt):
|
2016-05-20 10:18:33 -07:00
|
|
|
if sys.version_info[0] < 3:
|
|
|
|
if isinstance(txt, unicode):
|
|
|
|
return unicodedata.normalize('NFC', txt).encode('utf-8')
|
|
|
|
if isinstance(txt, str):
|
|
|
|
return unicodedata.normalize('NFC', txt.decode('utf-8')).encode('utf-8')
|
2014-02-20 17:30:10 -08:00
|
|
|
else:
|
2016-05-20 10:18:33 -07:00
|
|
|
if isinstance(txt, bytes):
|
|
|
|
return unicodedata.normalize('NFC', txt.decode('utf-8')).encode('utf-8')
|
|
|
|
if isinstance(txt, str):
|
|
|
|
return unicodedata.normalize('NFC', txt).encode('utf-8')
|
2014-02-20 17:30:10 -08:00
|
|
|
|
2016-05-20 10:18:33 -07:00
|
|
|
raise Exception('unicode/str or bytes/str expected')
|
2014-02-20 17:30:10 -08:00
|
|
|
|
2014-02-13 07:46:21 -08:00
|
|
|
class BaseClient(object):
|
|
|
|
# Implements very basic layer of sending raw protobuf
|
|
|
|
# messages to device and getting its response back.
|
2014-02-13 08:20:40 -08:00
|
|
|
def __init__(self, transport, **kwargs):
|
2014-02-13 07:46:21 -08:00
|
|
|
self.transport = transport
|
2014-02-13 08:20:40 -08:00
|
|
|
super(BaseClient, self).__init__() # *args, **kwargs)
|
2013-12-30 14:35:20 -08:00
|
|
|
|
2016-02-10 07:46:58 -08:00
|
|
|
def cancel(self):
|
|
|
|
self.transport.write(proto.Cancel())
|
|
|
|
|
2014-02-15 11:30:39 -08:00
|
|
|
def call_raw(self, msg):
|
2014-02-13 07:46:21 -08:00
|
|
|
try:
|
|
|
|
self.transport.session_begin()
|
|
|
|
self.transport.write(msg)
|
|
|
|
resp = self.transport.read_blocking()
|
2014-02-15 11:30:39 -08:00
|
|
|
finally:
|
|
|
|
self.transport.session_end()
|
|
|
|
|
|
|
|
return resp
|
|
|
|
|
|
|
|
def call(self, msg):
|
|
|
|
try:
|
|
|
|
self.transport.session_begin()
|
|
|
|
|
|
|
|
resp = self.call_raw(msg)
|
2014-02-13 07:46:21 -08:00
|
|
|
handler_name = "callback_%s" % resp.__class__.__name__
|
|
|
|
handler = getattr(self, handler_name, None)
|
2014-01-12 19:44:57 -08:00
|
|
|
|
2014-02-13 07:46:21 -08:00
|
|
|
if handler != None:
|
|
|
|
msg = handler(resp)
|
|
|
|
if msg == None:
|
2014-02-13 07:54:58 -08:00
|
|
|
raise Exception("Callback %s must return protobuf message, not None" % handler)
|
2014-02-13 07:46:21 -08:00
|
|
|
|
|
|
|
resp = self.call(msg)
|
|
|
|
|
|
|
|
finally:
|
|
|
|
self.transport.session_end()
|
|
|
|
|
|
|
|
return resp
|
|
|
|
|
|
|
|
def callback_Failure(self, msg):
|
|
|
|
if msg.code in (types.Failure_PinInvalid,
|
|
|
|
types.Failure_PinCancelled, types.Failure_PinExpected):
|
|
|
|
raise PinException(msg.code, msg.message)
|
|
|
|
|
|
|
|
raise CallException(msg.code, msg.message)
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self.transport.close()
|
2014-01-27 02:25:27 -08:00
|
|
|
|
2014-02-20 07:09:58 -08:00
|
|
|
class DebugWireMixin(object):
|
|
|
|
def call_raw(self, msg):
|
2014-06-12 04:26:24 -07:00
|
|
|
log("SENDING " + pprint(msg))
|
2014-02-20 07:09:58 -08:00
|
|
|
resp = super(DebugWireMixin, self).call_raw(msg)
|
2014-06-16 08:16:15 -07:00
|
|
|
log("RECEIVED " + pprint(resp))
|
2014-02-20 07:09:58 -08:00
|
|
|
return resp
|
|
|
|
|
2014-02-13 07:46:21 -08:00
|
|
|
class TextUIMixin(object):
|
|
|
|
# This class demonstrates easy test-based UI
|
|
|
|
# integration between the device and wallet.
|
|
|
|
# You can implement similar functionality
|
|
|
|
# by implementing your own GuiMixin with
|
|
|
|
# graphical widgets for every type of these callbacks.
|
|
|
|
|
2014-02-13 08:20:40 -08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(TextUIMixin, self).__init__(*args, **kwargs)
|
|
|
|
|
2014-02-13 07:46:21 -08:00
|
|
|
def callback_ButtonRequest(self, msg):
|
2014-06-12 08:22:22 -07:00
|
|
|
# log("Sending ButtonAck for %s " % get_buttonrequest_value(msg.code))
|
2014-02-13 07:46:21 -08:00
|
|
|
return proto.ButtonAck()
|
|
|
|
|
|
|
|
def callback_PinMatrixRequest(self, msg):
|
2014-03-28 08:26:48 -07:00
|
|
|
if msg.type == 1:
|
2015-02-28 05:06:23 -08:00
|
|
|
desc = 'current PIN'
|
2014-03-28 08:26:48 -07:00
|
|
|
elif msg.type == 2:
|
2014-04-02 11:06:54 -07:00
|
|
|
desc = 'new PIN'
|
2014-03-28 08:26:48 -07:00
|
|
|
elif msg.type == 3:
|
2014-04-02 11:06:54 -07:00
|
|
|
desc = 'new PIN again'
|
2014-03-28 08:26:48 -07:00
|
|
|
else:
|
2014-04-02 11:06:54 -07:00
|
|
|
desc = 'PIN'
|
2014-06-12 08:22:22 -07:00
|
|
|
|
2015-08-21 06:16:27 -07:00
|
|
|
log("Use the numeric keypad to describe number positions. The layout is:")
|
|
|
|
log(" 7 8 9")
|
|
|
|
log(" 4 5 6")
|
|
|
|
log(" 1 2 3")
|
2014-06-12 08:22:22 -07:00
|
|
|
log("Please enter %s: " % desc)
|
2014-11-06 16:09:53 -08:00
|
|
|
pin = getpass.getpass('')
|
2014-02-13 07:46:21 -08:00
|
|
|
return proto.PinMatrixAck(pin=pin)
|
|
|
|
|
|
|
|
def callback_PassphraseRequest(self, msg):
|
2014-06-12 08:22:22 -07:00
|
|
|
log("Passphrase required: ")
|
2014-11-06 16:09:53 -08:00
|
|
|
passphrase = getpass.getpass('')
|
|
|
|
log("Confirm your Passphrase: ")
|
2014-11-23 04:28:09 -08:00
|
|
|
if passphrase == getpass.getpass(''):
|
2016-05-20 10:18:33 -07:00
|
|
|
passphrase = normalize_nfc(passphrase)
|
2014-11-06 16:09:53 -08:00
|
|
|
return proto.PassphraseAck(passphrase=passphrase)
|
|
|
|
else:
|
|
|
|
log("Passphrase did not match! ")
|
|
|
|
exit()
|
2014-02-13 07:46:21 -08:00
|
|
|
|
|
|
|
def callback_WordRequest(self, msg):
|
2014-06-12 08:22:22 -07:00
|
|
|
log("Enter one word of mnemonic: ")
|
2016-05-20 04:36:17 -07:00
|
|
|
try:
|
|
|
|
word = raw_input()
|
|
|
|
except NameError:
|
|
|
|
word = input() # Python 3
|
2014-02-13 07:46:21 -08:00
|
|
|
return proto.WordAck(word=word)
|
2014-02-20 22:28:10 -08:00
|
|
|
|
2014-02-13 07:46:21 -08:00
|
|
|
class DebugLinkMixin(object):
|
|
|
|
# This class implements automatic responses
|
|
|
|
# and other functionality for unit tests
|
|
|
|
# for various callbacks, created in order
|
|
|
|
# to automatically pass unit tests.
|
|
|
|
#
|
|
|
|
# This mixing should be used only for purposes
|
|
|
|
# of unit testing, because it will fail to work
|
|
|
|
# without special DebugLink interface provided
|
|
|
|
# by the device.
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(DebugLinkMixin, self).__init__(*args, **kwargs)
|
|
|
|
self.debug = None
|
2014-02-20 22:28:10 -08:00
|
|
|
self.in_with_statement = 0
|
2014-12-10 06:26:18 -08:00
|
|
|
self.button_wait = 0
|
2014-11-06 16:58:20 -08:00
|
|
|
self.screenshot_id = 0
|
2014-02-13 07:46:21 -08:00
|
|
|
|
|
|
|
# Always press Yes and provide correct pin
|
|
|
|
self.setup_debuglink(True, True)
|
2016-01-12 15:17:38 -08:00
|
|
|
|
2014-02-15 11:30:39 -08:00
|
|
|
# Do not expect any specific response from device
|
2014-02-20 22:28:10 -08:00
|
|
|
self.expected_responses = None
|
2014-02-15 11:30:39 -08:00
|
|
|
|
|
|
|
# Use blank passphrase
|
|
|
|
self.set_passphrase('')
|
2014-02-13 07:46:21 -08:00
|
|
|
|
|
|
|
def close(self):
|
|
|
|
super(DebugLinkMixin, self).close()
|
|
|
|
if self.debug:
|
|
|
|
self.debug.close()
|
|
|
|
|
|
|
|
def set_debuglink(self, debug_transport):
|
|
|
|
self.debug = DebugLink(debug_transport)
|
|
|
|
|
2014-12-10 06:26:18 -08:00
|
|
|
def set_buttonwait(self, secs):
|
|
|
|
self.button_wait = secs
|
|
|
|
|
2014-02-20 22:28:10 -08:00
|
|
|
def __enter__(self):
|
|
|
|
# For usage in with/expected_responses
|
|
|
|
self.in_with_statement += 1
|
|
|
|
return self
|
|
|
|
|
2014-02-21 12:00:56 -08:00
|
|
|
def __exit__(self, _type, value, traceback):
|
2014-02-20 22:28:10 -08:00
|
|
|
self.in_with_statement -= 1
|
|
|
|
|
2014-02-21 12:00:56 -08:00
|
|
|
if _type != None:
|
|
|
|
# Another exception raised
|
|
|
|
return False
|
|
|
|
|
|
|
|
# return isinstance(value, TypeError)
|
2014-02-20 22:28:10 -08:00
|
|
|
# Evaluate missed responses in 'with' statement
|
|
|
|
if self.expected_responses != None and len(self.expected_responses):
|
|
|
|
raise Exception("Some of expected responses didn't come from device: %s" % \
|
|
|
|
[ pprint(x) for x in self.expected_responses ])
|
|
|
|
|
|
|
|
# Cleanup
|
|
|
|
self.expected_responses = None
|
|
|
|
return False
|
|
|
|
|
2014-02-15 11:30:39 -08:00
|
|
|
def set_expected_responses(self, expected):
|
2014-02-20 22:28:10 -08:00
|
|
|
if not self.in_with_statement:
|
|
|
|
raise Exception("Must be called inside 'with' statement")
|
2014-02-15 11:30:39 -08:00
|
|
|
self.expected_responses = expected
|
2014-02-13 07:46:21 -08:00
|
|
|
|
|
|
|
def setup_debuglink(self, button, pin_correct):
|
|
|
|
self.button = button # True -> YES button, False -> NO button
|
|
|
|
self.pin_correct = pin_correct
|
2014-02-13 08:20:40 -08:00
|
|
|
|
2014-02-15 11:30:39 -08:00
|
|
|
def set_passphrase(self, passphrase):
|
2016-05-20 10:18:33 -07:00
|
|
|
self.passphrase = normalize_nfc(passphrase)
|
2014-02-21 12:00:56 -08:00
|
|
|
|
|
|
|
def set_mnemonic(self, mnemonic):
|
2016-05-20 10:18:33 -07:00
|
|
|
self.mnemonic = normalize_nfc(passphrase)
|
2014-02-15 11:30:39 -08:00
|
|
|
|
|
|
|
def call_raw(self, msg):
|
2014-11-06 16:58:20 -08:00
|
|
|
|
|
|
|
if SCREENSHOT and self.debug:
|
|
|
|
layout = self.debug.read_layout()
|
|
|
|
im = Image.new("RGB", (128, 64))
|
|
|
|
pix = im.load()
|
|
|
|
for x in range(128):
|
|
|
|
for y in range(64):
|
|
|
|
rx, ry = 127 - x, 63 - y
|
|
|
|
if (ord(layout[rx + (ry / 8) * 128]) & (1 << (ry % 8))) > 0:
|
|
|
|
pix[x, y] = (255, 255, 255)
|
|
|
|
im.save('scr%05d.png' % self.screenshot_id)
|
|
|
|
self.screenshot_id += 1
|
|
|
|
|
2014-02-15 11:30:39 -08:00
|
|
|
resp = super(DebugLinkMixin, self).call_raw(msg)
|
|
|
|
self._check_request(resp)
|
|
|
|
return resp
|
2016-01-12 15:17:38 -08:00
|
|
|
|
2014-02-15 11:30:39 -08:00
|
|
|
def _check_request(self, msg):
|
|
|
|
if self.expected_responses != None:
|
2014-02-13 07:46:21 -08:00
|
|
|
try:
|
2014-02-15 11:30:39 -08:00
|
|
|
expected = self.expected_responses.pop(0)
|
2014-02-13 07:46:21 -08:00
|
|
|
except IndexError:
|
|
|
|
raise CallException(types.Failure_Other,
|
2014-02-15 11:30:39 -08:00
|
|
|
"Got %s, but no message has been expected" % pprint(msg))
|
2014-02-13 07:46:21 -08:00
|
|
|
|
2014-02-15 11:30:39 -08:00
|
|
|
if msg.__class__ != expected.__class__:
|
|
|
|
raise CallException(types.Failure_Other,
|
|
|
|
"Expected %s, got %s" % (pprint(expected), pprint(msg)))
|
|
|
|
|
|
|
|
fields = expected.ListFields() # only filled (including extensions)
|
|
|
|
for field, value in fields:
|
|
|
|
if not msg.HasField(field.name) or getattr(msg, field.name) != value:
|
|
|
|
raise CallException(types.Failure_Other,
|
|
|
|
"Expected %s, got %s" % (pprint(expected), pprint(msg)))
|
2016-01-12 15:17:38 -08:00
|
|
|
|
2014-02-15 11:30:39 -08:00
|
|
|
def callback_ButtonRequest(self, msg):
|
2014-06-12 04:26:24 -07:00
|
|
|
log("ButtonRequest code: " + get_buttonrequest_value(msg.code))
|
2014-02-13 07:46:21 -08:00
|
|
|
|
2014-06-13 10:24:53 -07:00
|
|
|
log("Pressing button " + str(self.button))
|
2014-12-10 06:26:18 -08:00
|
|
|
if self.button_wait:
|
|
|
|
log("Waiting %d seconds " % self.button_wait)
|
|
|
|
time.sleep(self.button_wait)
|
2014-02-13 07:46:21 -08:00
|
|
|
self.debug.press_button(self.button)
|
|
|
|
return proto.ButtonAck()
|
|
|
|
|
|
|
|
def callback_PinMatrixRequest(self, msg):
|
|
|
|
if self.pin_correct:
|
|
|
|
pin = self.debug.read_pin_encoded()
|
2014-01-12 19:44:57 -08:00
|
|
|
else:
|
2014-02-13 07:46:21 -08:00
|
|
|
pin = '444222'
|
|
|
|
return proto.PinMatrixAck(pin=pin)
|
|
|
|
|
|
|
|
def callback_PassphraseRequest(self, msg):
|
2014-06-12 04:26:24 -07:00
|
|
|
log("Provided passphrase: '%s'" % self.passphrase)
|
2014-02-15 11:30:39 -08:00
|
|
|
return proto.PassphraseAck(passphrase=self.passphrase)
|
2014-01-12 19:44:57 -08:00
|
|
|
|
2014-02-13 07:46:21 -08:00
|
|
|
def callback_WordRequest(self, msg):
|
2014-03-07 08:25:55 -08:00
|
|
|
(word, pos) = self.debug.read_recovery_word()
|
2014-02-21 12:00:56 -08:00
|
|
|
if word != '':
|
|
|
|
return proto.WordAck(word=word)
|
|
|
|
if pos != 0:
|
|
|
|
return proto.WordAck(word=self.mnemonic[pos - 1])
|
|
|
|
|
|
|
|
raise Exception("Unexpected call")
|
2014-02-13 07:46:21 -08:00
|
|
|
|
|
|
|
class ProtocolMixin(object):
|
|
|
|
PRIME_DERIVATION_FLAG = 0x80000000
|
2016-02-12 14:11:57 -08:00
|
|
|
VENDORS = ('bitcointrezor.com',)
|
2014-02-13 07:46:21 -08:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2014-02-13 08:20:40 -08:00
|
|
|
super(ProtocolMixin, self).__init__(*args, **kwargs)
|
2013-09-12 20:31:24 -07:00
|
|
|
self.init_device()
|
2014-03-28 11:47:53 -07:00
|
|
|
self.tx_api = None
|
2014-02-13 07:46:21 -08:00
|
|
|
|
2014-03-28 11:47:53 -07:00
|
|
|
def set_tx_api(self, tx_api):
|
|
|
|
self.tx_api = tx_api
|
2014-02-13 07:46:21 -08:00
|
|
|
|
|
|
|
def init_device(self):
|
|
|
|
self.features = expect(proto.Features)(self.call)(proto.Initialize())
|
2015-01-27 20:31:30 -08:00
|
|
|
if str(self.features.vendor) not in self.VENDORS:
|
|
|
|
raise Exception("Unsupported device")
|
2014-01-27 02:25:27 -08:00
|
|
|
|
2013-09-12 20:31:24 -07:00
|
|
|
def _get_local_entropy(self):
|
|
|
|
return os.urandom(32)
|
2013-12-30 14:35:20 -08:00
|
|
|
|
|
|
|
def _convert_prime(self, n):
|
|
|
|
# Convert minus signs to uint32 with flag
|
2014-02-13 07:46:21 -08:00
|
|
|
return [ int(abs(x) | self.PRIME_DERIVATION_FLAG) if x < 0 else x for x in n ]
|
2014-01-27 02:25:27 -08:00
|
|
|
|
2016-02-10 06:53:14 -08:00
|
|
|
@staticmethod
|
|
|
|
def expand_path(n):
|
2014-01-09 08:34:29 -08:00
|
|
|
# Convert string of bip32 path to list of uint32 integers with prime flags
|
|
|
|
# 0/-1/1' -> [0, 0x80000001, 0x80000001]
|
2014-01-09 15:11:03 -08:00
|
|
|
if not n:
|
|
|
|
return []
|
|
|
|
|
2014-01-09 08:34:29 -08:00
|
|
|
n = n.split('/')
|
|
|
|
path = []
|
|
|
|
for x in n:
|
|
|
|
prime = False
|
2014-01-09 15:11:03 -08:00
|
|
|
if x.endswith("'"):
|
2014-01-09 08:34:29 -08:00
|
|
|
x = x.replace('\'', '')
|
|
|
|
prime = True
|
2014-01-09 15:11:03 -08:00
|
|
|
if x.startswith('-'):
|
2014-01-09 08:34:29 -08:00
|
|
|
prime = True
|
2014-01-09 15:11:03 -08:00
|
|
|
|
|
|
|
x = abs(int(x))
|
2014-01-27 02:25:27 -08:00
|
|
|
|
2014-01-09 08:34:29 -08:00
|
|
|
if prime:
|
2016-02-10 06:53:14 -08:00
|
|
|
x |= ProtocolMixin.PRIME_DERIVATION_FLAG
|
2014-01-09 15:11:03 -08:00
|
|
|
|
|
|
|
path.append(x)
|
2014-01-09 08:34:29 -08:00
|
|
|
|
|
|
|
return path
|
|
|
|
|
2014-02-13 07:46:21 -08:00
|
|
|
@expect(proto.PublicKey)
|
2015-11-19 02:47:19 -08:00
|
|
|
def get_public_node(self, n, ecdsa_curve_name=DEFAULT_CURVE, show_display=False):
|
2014-02-21 09:56:51 -08:00
|
|
|
n = self._convert_prime(n)
|
2015-11-19 02:47:19 -08:00
|
|
|
if not ecdsa_curve_name:
|
|
|
|
ecdsa_curve_name=DEFAULT_CURVE
|
|
|
|
return self.call(proto.GetPublicKey(address_n=n, ecdsa_curve_name=ecdsa_curve_name, show_display=show_display))
|
2014-01-27 02:25:27 -08:00
|
|
|
|
2014-02-13 07:46:21 -08:00
|
|
|
@field('address')
|
|
|
|
@expect(proto.Address)
|
2014-12-10 09:03:54 -08:00
|
|
|
def get_address(self, coin_name, n, show_display=False, multisig=None):
|
2014-01-12 19:44:57 -08:00
|
|
|
n = self._convert_prime(n)
|
2014-12-10 09:03:54 -08:00
|
|
|
if multisig:
|
|
|
|
return self.call(proto.GetAddress(address_n=n, coin_name=coin_name, show_display=show_display, multisig=multisig))
|
|
|
|
else:
|
|
|
|
return self.call(proto.GetAddress(address_n=n, coin_name=coin_name, show_display=show_display))
|
2014-01-27 02:25:27 -08:00
|
|
|
|
2016-05-24 12:32:58 -07:00
|
|
|
@field('address')
|
|
|
|
@expect(proto.EthereumAddress)
|
2016-05-25 23:47:04 -07:00
|
|
|
def ethereum_get_address(self, n, show_display=False, multisig=None):
|
2016-05-24 12:32:58 -07:00
|
|
|
n = self._convert_prime(n)
|
|
|
|
return self.call(proto.EthereumGetAddress(address_n=n, show_display=show_display))
|
|
|
|
|
2014-02-13 07:46:21 -08:00
|
|
|
@field('entropy')
|
|
|
|
@expect(proto.Entropy)
|
2013-09-12 20:31:24 -07:00
|
|
|
def get_entropy(self, size):
|
2014-02-13 07:46:21 -08:00
|
|
|
return self.call(proto.GetEntropy(size=size))
|
2013-09-12 20:31:24 -07:00
|
|
|
|
2014-02-13 07:46:21 -08:00
|
|
|
@field('message')
|
|
|
|
@expect(proto.Success)
|
2014-02-07 02:31:12 -08:00
|
|
|
def ping(self, msg, button_protection=False, pin_protection=False, passphrase_protection=False):
|
2014-02-03 15:31:44 -08:00
|
|
|
msg = proto.Ping(message=msg,
|
2014-02-03 16:02:16 -08:00
|
|
|
button_protection=button_protection,
|
|
|
|
pin_protection=pin_protection,
|
|
|
|
passphrase_protection=passphrase_protection)
|
2014-02-13 07:46:21 -08:00
|
|
|
return self.call(msg)
|
2013-09-12 20:31:24 -07:00
|
|
|
|
2013-10-08 11:33:39 -07:00
|
|
|
def get_device_id(self):
|
|
|
|
return self.features.device_id
|
2013-09-12 20:31:24 -07:00
|
|
|
|
2014-02-13 07:46:21 -08:00
|
|
|
@field('message')
|
|
|
|
@expect(proto.Success)
|
2015-02-04 11:53:22 -08:00
|
|
|
def apply_settings(self, label=None, language=None, use_passphrase=None, homescreen=None):
|
2013-09-12 20:31:24 -07:00
|
|
|
settings = proto.ApplySettings()
|
2014-01-09 15:11:03 -08:00
|
|
|
if label != None:
|
2013-09-12 20:31:24 -07:00
|
|
|
settings.label = label
|
|
|
|
if language:
|
|
|
|
settings.language = language
|
2014-12-13 10:07:30 -08:00
|
|
|
if use_passphrase != None:
|
|
|
|
settings.use_passphrase = use_passphrase
|
2015-02-04 11:53:22 -08:00
|
|
|
if homescreen != None:
|
|
|
|
settings.homescreen = homescreen
|
2013-09-12 20:31:24 -07:00
|
|
|
|
2014-02-13 07:46:21 -08:00
|
|
|
out = self.call(settings)
|
|
|
|
self.init_device() # Reload Features
|
2013-09-12 20:31:24 -07:00
|
|
|
return out
|
|
|
|
|
2014-06-17 06:31:10 -07:00
|
|
|
@field('message')
|
|
|
|
@expect(proto.Success)
|
|
|
|
def clear_session(self):
|
|
|
|
return self.call(proto.ClearSession())
|
|
|
|
|
2014-02-13 07:46:21 -08:00
|
|
|
@field('message')
|
|
|
|
@expect(proto.Success)
|
2014-01-31 10:48:19 -08:00
|
|
|
def change_pin(self, remove=False):
|
|
|
|
ret = self.call(proto.ChangePin(remove=remove))
|
|
|
|
self.init_device() # Re-read features
|
|
|
|
return ret
|
|
|
|
|
2014-02-13 07:46:21 -08:00
|
|
|
@expect(proto.MessageSignature)
|
2014-02-20 17:30:10 -08:00
|
|
|
def sign_message(self, coin_name, n, message):
|
2014-02-13 07:46:21 -08:00
|
|
|
n = self._convert_prime(n)
|
2016-05-20 10:18:33 -07:00
|
|
|
# Convert message to UTF8 NFC (seems to be a bitcoin-qt standard)
|
|
|
|
message = normalize_nfc(message)
|
2014-02-20 17:30:10 -08:00
|
|
|
return self.call(proto.SignMessage(coin_name=coin_name, address_n=n, message=message))
|
2014-02-13 07:46:21 -08:00
|
|
|
|
2015-02-20 09:50:53 -08:00
|
|
|
@expect(proto.SignedIdentity)
|
2015-06-23 07:26:31 -07:00
|
|
|
def sign_identity(self, identity, challenge_hidden, challenge_visual, ecdsa_curve_name=DEFAULT_CURVE):
|
|
|
|
return self.call(proto.SignIdentity(identity=identity, challenge_hidden=challenge_hidden, challenge_visual=challenge_visual, ecdsa_curve_name=ecdsa_curve_name))
|
2015-02-20 09:50:53 -08:00
|
|
|
|
2014-02-13 07:46:21 -08:00
|
|
|
def verify_message(self, address, signature, message):
|
2016-05-20 10:18:33 -07:00
|
|
|
# Convert message to UTF8 NFC (seems to be a bitcoin-qt standard)
|
|
|
|
message = normalize_nfc(message)
|
2014-02-13 07:46:21 -08:00
|
|
|
try:
|
2014-11-06 16:59:28 -08:00
|
|
|
if address:
|
|
|
|
resp = self.call(proto.VerifyMessage(address=address, signature=signature, message=message))
|
|
|
|
else:
|
|
|
|
resp = self.call(proto.VerifyMessage(signature=signature, message=message))
|
2014-02-13 07:46:21 -08:00
|
|
|
except CallException as e:
|
|
|
|
resp = e
|
|
|
|
if isinstance(resp, proto.Success):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2014-11-03 10:42:22 -08:00
|
|
|
@expect(proto.EncryptedMessage)
|
2014-10-29 16:48:06 -07:00
|
|
|
def encrypt_message(self, pubkey, message, display_only, coin_name, n):
|
2014-11-26 09:49:22 -08:00
|
|
|
if coin_name and n:
|
|
|
|
n = self._convert_prime(n)
|
|
|
|
return self.call(proto.EncryptMessage(pubkey=pubkey, message=message, display_only=display_only, coin_name=coin_name, address_n=n))
|
|
|
|
else:
|
|
|
|
return self.call(proto.EncryptMessage(pubkey=pubkey, message=message, display_only=display_only))
|
2014-06-12 08:02:46 -07:00
|
|
|
|
2014-11-03 10:42:22 -08:00
|
|
|
@expect(proto.DecryptedMessage)
|
|
|
|
def decrypt_message(self, n, nonce, message, msg_hmac):
|
2014-06-12 08:02:46 -07:00
|
|
|
n = self._convert_prime(n)
|
2014-11-03 10:42:22 -08:00
|
|
|
return self.call(proto.DecryptMessage(address_n=n, nonce=nonce, message=message, hmac=msg_hmac))
|
2014-06-12 08:02:46 -07:00
|
|
|
|
2014-11-03 10:42:22 -08:00
|
|
|
@field('value')
|
|
|
|
@expect(proto.CipheredKeyValue)
|
2015-12-24 08:31:09 -08:00
|
|
|
def encrypt_keyvalue(self, n, key, value, ask_on_encrypt=True, ask_on_decrypt=True, iv=None):
|
2014-06-06 05:40:07 -07:00
|
|
|
n = self._convert_prime(n)
|
|
|
|
return self.call(proto.CipherKeyValue(address_n=n,
|
|
|
|
key=key,
|
|
|
|
value=value,
|
|
|
|
encrypt=True,
|
|
|
|
ask_on_encrypt=ask_on_encrypt,
|
2015-12-24 08:31:09 -08:00
|
|
|
ask_on_decrypt=ask_on_decrypt,
|
|
|
|
iv=iv if iv is not None else ''))
|
2014-06-06 05:40:07 -07:00
|
|
|
|
2014-11-03 10:42:22 -08:00
|
|
|
@field('value')
|
|
|
|
@expect(proto.CipheredKeyValue)
|
2015-12-24 08:31:09 -08:00
|
|
|
def decrypt_keyvalue(self, n, key, value, ask_on_encrypt=True, ask_on_decrypt=True, iv=None):
|
2014-06-06 05:40:07 -07:00
|
|
|
n = self._convert_prime(n)
|
|
|
|
return self.call(proto.CipherKeyValue(address_n=n,
|
|
|
|
key=key,
|
|
|
|
value=value,
|
|
|
|
encrypt=False,
|
|
|
|
ask_on_encrypt=ask_on_encrypt,
|
2015-12-24 08:31:09 -08:00
|
|
|
ask_on_decrypt=ask_on_decrypt,
|
|
|
|
iv=iv if iv is not None else ''))
|
2014-06-06 05:40:07 -07:00
|
|
|
|
2014-02-13 07:46:21 -08:00
|
|
|
@field('tx_size')
|
|
|
|
@expect(proto.TxSize)
|
|
|
|
def estimate_tx_size(self, coin_name, inputs, outputs):
|
|
|
|
msg = proto.EstimateTxSize()
|
|
|
|
msg.coin_name = coin_name
|
|
|
|
msg.inputs_count = len(inputs)
|
|
|
|
msg.outputs_count = len(outputs)
|
|
|
|
return self.call(msg)
|
|
|
|
|
|
|
|
def _prepare_simple_sign_tx(self, coin_name, inputs, outputs):
|
|
|
|
msg = proto.SimpleSignTx()
|
|
|
|
msg.coin_name = coin_name
|
|
|
|
msg.inputs.extend(inputs)
|
|
|
|
msg.outputs.extend(outputs)
|
|
|
|
|
|
|
|
known_hashes = []
|
|
|
|
for inp in inputs:
|
|
|
|
if inp.prev_hash in known_hashes:
|
|
|
|
continue
|
|
|
|
|
|
|
|
tx = msg.transactions.add()
|
2015-06-03 05:53:53 -07:00
|
|
|
if self.tx_api:
|
|
|
|
tx.CopyFrom(self.tx_api.get_tx(binascii.hexlify(inp.prev_hash)))
|
|
|
|
else:
|
|
|
|
raise Exception('TX_API not defined')
|
2014-02-13 07:46:21 -08:00
|
|
|
known_hashes.append(inp.prev_hash)
|
|
|
|
|
|
|
|
return msg
|
|
|
|
|
|
|
|
def simple_sign_tx(self, coin_name, inputs, outputs):
|
|
|
|
msg = self._prepare_simple_sign_tx(coin_name, inputs, outputs)
|
2014-04-17 07:27:00 -07:00
|
|
|
return self.call(msg).serialized.serialized_tx
|
2014-04-09 12:42:30 -07:00
|
|
|
|
|
|
|
def _prepare_sign_tx(self, coin_name, inputs, outputs):
|
|
|
|
tx = types.TransactionType()
|
|
|
|
tx.inputs.extend(inputs)
|
2014-04-16 23:30:34 -07:00
|
|
|
tx.outputs.extend(outputs)
|
2014-04-09 12:42:30 -07:00
|
|
|
|
|
|
|
txes = {}
|
|
|
|
txes[''] = tx
|
|
|
|
|
|
|
|
known_hashes = []
|
|
|
|
for inp in inputs:
|
|
|
|
if inp.prev_hash in known_hashes:
|
|
|
|
continue
|
|
|
|
|
2015-06-03 05:53:53 -07:00
|
|
|
if self.tx_api:
|
|
|
|
txes[inp.prev_hash] = self.tx_api.get_tx(binascii.hexlify(inp.prev_hash))
|
|
|
|
else:
|
|
|
|
raise Exception('TX_API not defined')
|
2014-04-09 12:42:30 -07:00
|
|
|
known_hashes.append(inp.prev_hash)
|
|
|
|
|
|
|
|
return txes
|
2014-02-13 07:46:21 -08:00
|
|
|
|
2014-04-18 09:56:12 -07:00
|
|
|
def sign_tx(self, coin_name, inputs, outputs, debug_processor=None):
|
2014-04-09 12:42:30 -07:00
|
|
|
|
|
|
|
start = time.time()
|
|
|
|
txes = self._prepare_sign_tx(coin_name, inputs, outputs)
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.transport.session_begin()
|
|
|
|
|
|
|
|
# Prepare and send initial message
|
|
|
|
tx = proto.SignTx()
|
|
|
|
tx.inputs_count = len(inputs)
|
|
|
|
tx.outputs_count = len(outputs)
|
|
|
|
tx.coin_name = coin_name
|
|
|
|
res = self.call(tx)
|
|
|
|
|
|
|
|
# Prepare structure for signatures
|
|
|
|
signatures = [None] * len(inputs)
|
|
|
|
serialized_tx = ''
|
|
|
|
|
|
|
|
counter = 0
|
|
|
|
while True:
|
|
|
|
counter += 1
|
|
|
|
|
|
|
|
if isinstance(res, proto.Failure):
|
|
|
|
raise CallException("Signing failed")
|
|
|
|
|
|
|
|
if not isinstance(res, proto.TxRequest):
|
|
|
|
raise CallException("Unexpected message")
|
|
|
|
|
|
|
|
# If there's some part of signed transaction, let's add it
|
2014-04-16 23:30:34 -07:00
|
|
|
if res.HasField('serialized') and res.serialized.HasField('serialized_tx'):
|
2014-06-12 04:26:24 -07:00
|
|
|
log("RECEIVED PART OF SERIALIZED TX (%d BYTES)" % len(res.serialized.serialized_tx))
|
2014-04-16 23:30:34 -07:00
|
|
|
serialized_tx += res.serialized.serialized_tx
|
2014-04-09 12:42:30 -07:00
|
|
|
|
2014-04-16 23:30:34 -07:00
|
|
|
if res.HasField('serialized') and res.serialized.HasField('signature_index'):
|
2014-04-18 09:09:39 -07:00
|
|
|
if signatures[res.serialized.signature_index] != None:
|
|
|
|
raise Exception("Signature for index %d already filled" % res.serialized.signature_index)
|
2014-04-16 23:30:34 -07:00
|
|
|
signatures[res.serialized.signature_index] = res.serialized.signature
|
2014-04-09 12:42:30 -07:00
|
|
|
|
|
|
|
if res.request_type == types.TXFINISHED:
|
|
|
|
# Device didn't ask for more information, finish workflow
|
|
|
|
break
|
|
|
|
|
|
|
|
# Device asked for one more information, let's process it.
|
2014-04-16 23:30:34 -07:00
|
|
|
current_tx = txes[res.details.tx_hash]
|
2014-04-09 12:42:30 -07:00
|
|
|
|
|
|
|
if res.request_type == types.TXMETA:
|
|
|
|
msg = types.TransactionType()
|
|
|
|
msg.version = current_tx.version
|
|
|
|
msg.lock_time = current_tx.lock_time
|
2014-04-17 05:05:45 -07:00
|
|
|
msg.inputs_cnt = len(current_tx.inputs)
|
2014-04-16 23:30:34 -07:00
|
|
|
if res.details.tx_hash:
|
2014-04-17 05:05:45 -07:00
|
|
|
msg.outputs_cnt = len(current_tx.bin_outputs)
|
2014-04-16 23:30:34 -07:00
|
|
|
else:
|
2014-04-17 05:05:45 -07:00
|
|
|
msg.outputs_cnt = len(current_tx.outputs)
|
2014-04-09 12:42:30 -07:00
|
|
|
res = self.call(proto.TxAck(tx=msg))
|
|
|
|
continue
|
|
|
|
|
|
|
|
elif res.request_type == types.TXINPUT:
|
|
|
|
msg = types.TransactionType()
|
2014-04-16 23:30:34 -07:00
|
|
|
msg.inputs.extend([current_tx.inputs[res.details.request_index], ])
|
2014-04-09 12:42:30 -07:00
|
|
|
res = self.call(proto.TxAck(tx=msg))
|
|
|
|
continue
|
|
|
|
|
|
|
|
elif res.request_type == types.TXOUTPUT:
|
|
|
|
msg = types.TransactionType()
|
2014-04-16 23:30:34 -07:00
|
|
|
if res.details.tx_hash:
|
|
|
|
msg.bin_outputs.extend([current_tx.bin_outputs[res.details.request_index], ])
|
|
|
|
else:
|
|
|
|
msg.outputs.extend([current_tx.outputs[res.details.request_index], ])
|
2014-04-18 09:56:12 -07:00
|
|
|
|
|
|
|
if debug_processor != None:
|
|
|
|
# If debug_processor function is provided,
|
|
|
|
# pass thru it the request and prepared response.
|
|
|
|
# This is useful for unit tests, see test_msg_signtx
|
|
|
|
msg = debug_processor(res, msg)
|
|
|
|
|
2014-04-09 12:42:30 -07:00
|
|
|
res = self.call(proto.TxAck(tx=msg))
|
|
|
|
continue
|
|
|
|
|
|
|
|
finally:
|
|
|
|
self.transport.session_end()
|
|
|
|
|
2014-04-18 09:09:39 -07:00
|
|
|
if None in signatures:
|
|
|
|
raise Exception("Some signatures are missing!")
|
|
|
|
|
2014-06-12 04:26:24 -07:00
|
|
|
log("SIGNED IN %.03f SECONDS, CALLED %d MESSAGES, %d BYTES" % \
|
|
|
|
(time.time() - start, counter, len(serialized_tx)))
|
2014-04-09 12:42:30 -07:00
|
|
|
|
|
|
|
return (signatures, serialized_tx)
|
2014-02-13 07:46:21 -08:00
|
|
|
|
|
|
|
@field('message')
|
|
|
|
@expect(proto.Success)
|
|
|
|
def wipe_device(self):
|
|
|
|
ret = self.call(proto.WipeDevice())
|
|
|
|
self.init_device()
|
|
|
|
return ret
|
|
|
|
|
|
|
|
@field('message')
|
|
|
|
@expect(proto.Success)
|
|
|
|
def recovery_device(self, word_count, passphrase_protection, pin_protection, label, language):
|
|
|
|
if self.features.initialized:
|
|
|
|
raise Exception("Device is initialized already. Call wipe_device() and try again.")
|
|
|
|
|
|
|
|
if word_count not in (12, 18, 24):
|
|
|
|
raise Exception("Invalid word count. Use 12/18/24")
|
|
|
|
|
|
|
|
res = self.call(proto.RecoveryDevice(word_count=int(word_count),
|
|
|
|
passphrase_protection=bool(passphrase_protection),
|
|
|
|
pin_protection=bool(pin_protection),
|
|
|
|
label=label,
|
|
|
|
language=language,
|
|
|
|
enforce_wordlist=True))
|
|
|
|
|
|
|
|
self.init_device()
|
|
|
|
return res
|
|
|
|
|
|
|
|
@field('message')
|
|
|
|
@expect(proto.Success)
|
|
|
|
def reset_device(self, display_random, strength, passphrase_protection, pin_protection, label, language):
|
|
|
|
if self.features.initialized:
|
|
|
|
raise Exception("Device is initialized already. Call wipe_device() and try again.")
|
|
|
|
|
|
|
|
# Begin with device reset workflow
|
|
|
|
msg = proto.ResetDevice(display_random=display_random,
|
|
|
|
strength=strength,
|
|
|
|
language=language,
|
|
|
|
passphrase_protection=bool(passphrase_protection),
|
|
|
|
pin_protection=bool(pin_protection),
|
|
|
|
label=label)
|
|
|
|
|
|
|
|
resp = self.call(msg)
|
|
|
|
if not isinstance(resp, proto.EntropyRequest):
|
|
|
|
raise Exception("Invalid response, expected EntropyRequest")
|
|
|
|
|
|
|
|
external_entropy = self._get_local_entropy()
|
2014-06-12 04:26:24 -07:00
|
|
|
log("Computer generated entropy: " + binascii.hexlify(external_entropy))
|
2014-02-21 12:00:56 -08:00
|
|
|
ret = self.call(proto.EntropyAck(entropy=external_entropy))
|
|
|
|
self.init_device()
|
|
|
|
return ret
|
2014-02-13 07:46:21 -08:00
|
|
|
|
|
|
|
@field('message')
|
|
|
|
@expect(proto.Success)
|
2014-02-20 16:48:11 -08:00
|
|
|
def load_device_by_mnemonic(self, mnemonic, pin, passphrase_protection, label, language, skip_checksum=False):
|
|
|
|
m = Mnemonic('english')
|
|
|
|
if not skip_checksum and not m.check(mnemonic):
|
|
|
|
raise Exception("Invalid mnemonic checksum")
|
|
|
|
|
|
|
|
# Convert mnemonic to UTF8 NKFD
|
|
|
|
mnemonic = Mnemonic.normalize_string(mnemonic)
|
|
|
|
|
|
|
|
# Convert mnemonic to ASCII stream
|
2016-05-20 10:18:33 -07:00
|
|
|
mnemonic = normalize_nfc(mnemonic)
|
2014-02-20 16:48:11 -08:00
|
|
|
|
2014-02-13 07:46:21 -08:00
|
|
|
if self.features.initialized:
|
|
|
|
raise Exception("Device is initialized already. Call wipe_device() and try again.")
|
|
|
|
|
|
|
|
resp = self.call(proto.LoadDevice(mnemonic=mnemonic, pin=pin,
|
|
|
|
passphrase_protection=passphrase_protection,
|
|
|
|
language=language,
|
2014-02-20 16:48:11 -08:00
|
|
|
label=label,
|
|
|
|
skip_checksum=skip_checksum))
|
2014-02-13 07:46:21 -08:00
|
|
|
self.init_device()
|
|
|
|
return resp
|
|
|
|
|
|
|
|
@field('message')
|
|
|
|
@expect(proto.Success)
|
2014-03-07 12:44:06 -08:00
|
|
|
def load_device_by_xprv(self, xprv, pin, passphrase_protection, label, language):
|
2014-02-13 07:46:21 -08:00
|
|
|
if self.features.initialized:
|
|
|
|
raise Exception("Device is initialized already. Call wipe_device() and try again.")
|
|
|
|
|
|
|
|
if xprv[0:4] not in ('xprv', 'tprv'):
|
|
|
|
raise Exception("Unknown type of xprv")
|
|
|
|
|
|
|
|
if len(xprv) < 100 and len(xprv) > 112:
|
|
|
|
raise Exception("Invalid length of xprv")
|
|
|
|
|
|
|
|
node = types.HDNodeType()
|
2016-05-20 10:18:33 -07:00
|
|
|
data = binascii.hexlify(tools.b58decode(xprv, None))
|
2014-02-13 07:46:21 -08:00
|
|
|
|
|
|
|
if data[90:92] != '00':
|
|
|
|
raise Exception("Contain invalid private key")
|
|
|
|
|
|
|
|
checksum = hashlib.sha256(hashlib.sha256(binascii.unhexlify(data[:156])).digest()).hexdigest()[:8]
|
|
|
|
if checksum != data[156:]:
|
|
|
|
raise Exception("Checksum doesn't match")
|
|
|
|
|
|
|
|
# version 0488ade4
|
|
|
|
# depth 00
|
|
|
|
# fingerprint 00000000
|
|
|
|
# child_num 00000000
|
|
|
|
# chaincode 873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d508
|
|
|
|
# privkey 00e8f32e723decf4051aefac8e2c93c9c5b214313817cdb01a1494b917c8436b35
|
|
|
|
# checksum e77e9d71
|
|
|
|
|
|
|
|
node.depth = int(data[8:10], 16)
|
|
|
|
node.fingerprint = int(data[10:18], 16)
|
|
|
|
node.child_num = int(data[18:26], 16)
|
|
|
|
node.chain_code = data[26:90].decode('hex')
|
|
|
|
node.private_key = data[92:156].decode('hex') # skip 0x00 indicating privkey
|
|
|
|
|
|
|
|
resp = self.call(proto.LoadDevice(node=node,
|
|
|
|
pin=pin,
|
|
|
|
passphrase_protection=passphrase_protection,
|
2014-03-07 12:44:06 -08:00
|
|
|
language=language,
|
2014-02-13 07:46:21 -08:00
|
|
|
label=label))
|
|
|
|
self.init_device()
|
|
|
|
return resp
|
|
|
|
|
|
|
|
def firmware_update(self, fp):
|
|
|
|
if self.features.bootloader_mode == False:
|
|
|
|
raise Exception("Device must be in bootloader mode")
|
|
|
|
|
|
|
|
resp = self.call(proto.FirmwareErase())
|
|
|
|
if isinstance(resp, proto.Failure) and resp.code == types.Failure_FirmwareError:
|
|
|
|
return False
|
|
|
|
|
2014-07-02 13:57:55 -07:00
|
|
|
data = fp.read()
|
|
|
|
fingerprint = hashlib.sha256(data[256:]).hexdigest()
|
|
|
|
log("Firmware fingerprint: " + fingerprint)
|
|
|
|
resp = self.call(proto.FirmwareUpload(payload=data))
|
|
|
|
|
2014-02-13 07:46:21 -08:00
|
|
|
if isinstance(resp, proto.Success):
|
|
|
|
return True
|
|
|
|
|
|
|
|
elif isinstance(resp, proto.Failure) and resp.code == types.Failure_FirmwareError:
|
|
|
|
return False
|
|
|
|
|
2014-08-11 16:25:36 -07:00
|
|
|
raise Exception("Unexpected result %s" % resp)
|
2014-02-13 07:46:21 -08:00
|
|
|
|
2014-02-13 08:20:40 -08:00
|
|
|
class TrezorClient(ProtocolMixin, TextUIMixin, BaseClient):
|
2014-02-13 07:46:21 -08:00
|
|
|
pass
|
|
|
|
|
2014-02-20 07:09:58 -08:00
|
|
|
class TrezorClientDebug(ProtocolMixin, TextUIMixin, DebugWireMixin, BaseClient):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class TrezorDebugClient(ProtocolMixin, DebugLinkMixin, DebugWireMixin, BaseClient):
|
2014-02-13 07:46:21 -08:00
|
|
|
pass
|