mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-02-17 01:52:02 +00:00
trezorlib: move mostly-debug methods from device to debuglink
This commit is contained in:
parent
65d8adb0be
commit
d5dee0c897
@ -31,7 +31,7 @@ from . import messages as proto
|
||||
from . import btc, cosi, device, ethereum, firmware, lisk, misc, nem, stellar
|
||||
from . import mapping
|
||||
from . import tools
|
||||
from .debuglink import DebugLink
|
||||
from . import debuglink
|
||||
|
||||
if sys.version_info.major < 3:
|
||||
raise Exception("Trezorlib does not support Python 2 anymore.")
|
||||
@ -273,7 +273,7 @@ class DebugLinkMixin(object):
|
||||
self.debug.close()
|
||||
|
||||
def set_debuglink(self, debug_transport):
|
||||
self.debug = DebugLink(debug_transport)
|
||||
self.debug = debuglink.DebugLink(debug_transport)
|
||||
|
||||
def set_buttonwait(self, secs):
|
||||
self.button_wait = secs
|
||||
@ -454,15 +454,15 @@ class ProtocolMixin(object):
|
||||
return self.call(proto.ClearSession())
|
||||
|
||||
# Device functionality
|
||||
self_test = MovedTo(device.self_test)
|
||||
|
||||
wipe_device = MovedTo(device.wipe)
|
||||
recovery_device = MovedTo(device.recover)
|
||||
reset_device = MovedTo(device.reset)
|
||||
backup_device = MovedTo(device.backup)
|
||||
|
||||
load_device_by_mnemonic = MovedTo(device.load_device_by_mnemonic)
|
||||
load_device_by_xprv = MovedTo(device.load_device_by_xprv)
|
||||
# debugging
|
||||
load_device_by_mnemonic = MovedTo(debuglink.load_device_by_mnemonic)
|
||||
load_device_by_xprv = MovedTo(debuglink.load_device_by_xprv)
|
||||
self_test = MovedTo(debuglink.self_test)
|
||||
|
||||
set_u2f_counter = MovedTo(device.set_u2f_counter)
|
||||
|
||||
|
@ -14,9 +14,13 @@
|
||||
# You should have received a copy of the License along with this library.
|
||||
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
|
||||
|
||||
from __future__ import print_function
|
||||
import binascii
|
||||
|
||||
from mnemonic import Mnemonic
|
||||
|
||||
from . import messages as proto
|
||||
from . import tools
|
||||
from .tools import expect
|
||||
|
||||
|
||||
def pin_info(pin):
|
||||
@ -138,3 +142,83 @@ class DebugLink(object):
|
||||
|
||||
def flash_erase(self, sector):
|
||||
self._call(proto.DebugLinkFlashErase(sector=sector), nowait=True)
|
||||
|
||||
|
||||
@expect(proto.Success, field="message")
|
||||
def load_device_by_mnemonic(client, mnemonic, pin, passphrase_protection, label, language='english', skip_checksum=False, expand=False):
|
||||
# Convert mnemonic to UTF8 NKFD
|
||||
mnemonic = Mnemonic.normalize_string(mnemonic)
|
||||
|
||||
# Convert mnemonic to ASCII stream
|
||||
mnemonic = mnemonic.encode('utf-8')
|
||||
|
||||
m = Mnemonic('english')
|
||||
|
||||
if expand:
|
||||
mnemonic = m.expand(mnemonic)
|
||||
|
||||
if not skip_checksum and not m.check(mnemonic):
|
||||
raise ValueError("Invalid mnemonic checksum")
|
||||
|
||||
if client.features.initialized:
|
||||
raise RuntimeError("Device is initialized already. Call wipe_device() and try again.")
|
||||
|
||||
resp = client.call(proto.LoadDevice(mnemonic=mnemonic, pin=pin,
|
||||
passphrase_protection=passphrase_protection,
|
||||
language=language,
|
||||
label=label,
|
||||
skip_checksum=skip_checksum))
|
||||
client.init_device()
|
||||
return resp
|
||||
|
||||
|
||||
@expect(proto.Success, field="message")
|
||||
def load_device_by_xprv(client, xprv, pin, passphrase_protection, label, language):
|
||||
if client.features.initialized:
|
||||
raise RuntimeError("Device is initialized already. Call wipe_device() and try again.")
|
||||
|
||||
if xprv[0:4] not in ('xprv', 'tprv'):
|
||||
raise ValueError("Unknown type of xprv")
|
||||
|
||||
if not 100 < len(xprv) < 112: # yes this is correct in Python
|
||||
raise ValueError("Invalid length of xprv")
|
||||
|
||||
node = proto.HDNodeType()
|
||||
data = binascii.hexlify(tools.b58decode(xprv, None))
|
||||
|
||||
if data[90:92] != b'00':
|
||||
raise ValueError("Contain invalid private key")
|
||||
|
||||
checksum = binascii.hexlify(tools.btc_hash(binascii.unhexlify(data[:156]))[:4])
|
||||
if checksum != data[156:]:
|
||||
raise ValueError("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 = binascii.unhexlify(data[26:90])
|
||||
node.private_key = binascii.unhexlify(data[92:156]) # skip 0x00 indicating privkey
|
||||
|
||||
resp = client.call(proto.LoadDevice(node=node,
|
||||
pin=pin,
|
||||
passphrase_protection=passphrase_protection,
|
||||
language=language,
|
||||
label=label))
|
||||
client.init_device()
|
||||
return resp
|
||||
|
||||
|
||||
@expect(proto.Success, field="message")
|
||||
def self_test(client):
|
||||
if client.features.bootloader_mode is False:
|
||||
raise RuntimeError("Device must be in bootloader mode")
|
||||
|
||||
return client.call(proto.SelfTest(payload=b'\x00\xFF\x55\xAA\x66\x99\x33\xCCABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\x00\xFF\x55\xAA\x66\x99\x33\xCC'))
|
||||
|
@ -152,83 +152,3 @@ def reset(client, display_random, strength, passphrase_protection, pin_protectio
|
||||
def backup(client):
|
||||
ret = client.call(proto.BackupDevice())
|
||||
return ret
|
||||
|
||||
|
||||
@expect(proto.Success, field="message")
|
||||
def load_device_by_mnemonic(client, mnemonic, pin, passphrase_protection, label, language='english', skip_checksum=False, expand=False):
|
||||
# Convert mnemonic to UTF8 NKFD
|
||||
mnemonic = Mnemonic.normalize_string(mnemonic)
|
||||
|
||||
# Convert mnemonic to ASCII stream
|
||||
mnemonic = mnemonic.encode('utf-8')
|
||||
|
||||
m = Mnemonic('english')
|
||||
|
||||
if expand:
|
||||
mnemonic = m.expand(mnemonic)
|
||||
|
||||
if not skip_checksum and not m.check(mnemonic):
|
||||
raise ValueError("Invalid mnemonic checksum")
|
||||
|
||||
if client.features.initialized:
|
||||
raise RuntimeError("Device is initialized already. Call wipe_device() and try again.")
|
||||
|
||||
resp = client.call(proto.LoadDevice(mnemonic=mnemonic, pin=pin,
|
||||
passphrase_protection=passphrase_protection,
|
||||
language=language,
|
||||
label=label,
|
||||
skip_checksum=skip_checksum))
|
||||
client.init_device()
|
||||
return resp
|
||||
|
||||
|
||||
@expect(proto.Success, field="message")
|
||||
def load_device_by_xprv(client, xprv, pin, passphrase_protection, label, language):
|
||||
if client.features.initialized:
|
||||
raise RuntimeError("Device is initialized already. Call wipe_device() and try again.")
|
||||
|
||||
if xprv[0:4] not in ('xprv', 'tprv'):
|
||||
raise ValueError("Unknown type of xprv")
|
||||
|
||||
if not 100 < len(xprv) < 112: # yes this is correct in Python
|
||||
raise ValueError("Invalid length of xprv")
|
||||
|
||||
node = proto.HDNodeType()
|
||||
data = binascii.hexlify(tools.b58decode(xprv, None))
|
||||
|
||||
if data[90:92] != b'00':
|
||||
raise ValueError("Contain invalid private key")
|
||||
|
||||
checksum = binascii.hexlify(tools.btc_hash(binascii.unhexlify(data[:156]))[:4])
|
||||
if checksum != data[156:]:
|
||||
raise ValueError("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 = binascii.unhexlify(data[26:90])
|
||||
node.private_key = binascii.unhexlify(data[92:156]) # skip 0x00 indicating privkey
|
||||
|
||||
resp = client.call(proto.LoadDevice(node=node,
|
||||
pin=pin,
|
||||
passphrase_protection=passphrase_protection,
|
||||
language=language,
|
||||
label=label))
|
||||
client.init_device()
|
||||
return resp
|
||||
|
||||
|
||||
@expect(proto.Success, field="message")
|
||||
def self_test(client):
|
||||
if client.features.bootloader_mode is False:
|
||||
raise RuntimeError("Device must be in bootloader mode")
|
||||
|
||||
return client.call(proto.SelfTest(payload=b'\x00\xFF\x55\xAA\x66\x99\x33\xCCABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\x00\xFF\x55\xAA\x66\x99\x33\xCC'))
|
||||
|
Loading…
Reference in New Issue
Block a user