mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-05-25 18:28:47 +00:00
more python3 fixes
This commit is contained in:
parent
021389efe8
commit
1fe94e7fa5
@ -15,10 +15,6 @@ from . import messages_pb2 as proto
|
|||||||
from . import types_pb2 as types
|
from . import types_pb2 as types
|
||||||
from .debuglink import DebugLink
|
from .debuglink import DebugLink
|
||||||
|
|
||||||
if sys.version_info[0] == 3:
|
|
||||||
from io import BytesIO
|
|
||||||
unicode = lambda s, enc: BytesIO(bytes(s, enc))
|
|
||||||
|
|
||||||
# try:
|
# try:
|
||||||
# from PIL import Image
|
# from PIL import Image
|
||||||
# SCREENSHOT = True
|
# SCREENSHOT = True
|
||||||
@ -91,15 +87,18 @@ class expect(object):
|
|||||||
return wrapped_f
|
return wrapped_f
|
||||||
|
|
||||||
def normalize_nfc(txt):
|
def normalize_nfc(txt):
|
||||||
# Normalize string to UTF8 NFC for sign_message
|
if sys.version_info[0] < 3:
|
||||||
if isinstance(txt, str):
|
if isinstance(txt, unicode):
|
||||||
utxt = txt.decode('utf8')
|
return unicodedata.normalize('NFC', txt).encode('utf-8')
|
||||||
elif isinstance(txt, unicode):
|
if isinstance(txt, str):
|
||||||
utxt = txt
|
return unicodedata.normalize('NFC', txt.decode('utf-8')).encode('utf-8')
|
||||||
else:
|
else:
|
||||||
raise Exception("String value expected")
|
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')
|
||||||
|
|
||||||
return unicodedata.normalize('NFC', utxt)
|
raise Exception('unicode/str or bytes/str expected')
|
||||||
|
|
||||||
class BaseClient(object):
|
class BaseClient(object):
|
||||||
# Implements very basic layer of sending raw protobuf
|
# Implements very basic layer of sending raw protobuf
|
||||||
@ -195,7 +194,7 @@ class TextUIMixin(object):
|
|||||||
passphrase = getpass.getpass('')
|
passphrase = getpass.getpass('')
|
||||||
log("Confirm your Passphrase: ")
|
log("Confirm your Passphrase: ")
|
||||||
if passphrase == getpass.getpass(''):
|
if passphrase == getpass.getpass(''):
|
||||||
passphrase = unicode(str(bytearray(passphrase, 'utf-8')), 'utf-8')
|
passphrase = normalize_nfc(passphrase)
|
||||||
return proto.PassphraseAck(passphrase=passphrase)
|
return proto.PassphraseAck(passphrase=passphrase)
|
||||||
else:
|
else:
|
||||||
log("Passphrase did not match! ")
|
log("Passphrase did not match! ")
|
||||||
@ -279,10 +278,10 @@ class DebugLinkMixin(object):
|
|||||||
self.pin_correct = pin_correct
|
self.pin_correct = pin_correct
|
||||||
|
|
||||||
def set_passphrase(self, passphrase):
|
def set_passphrase(self, passphrase):
|
||||||
self.passphrase = unicode(str(bytearray(Mnemonic.normalize_string(passphrase), 'utf-8')), 'utf-8')
|
self.passphrase = normalize_nfc(passphrase)
|
||||||
|
|
||||||
def set_mnemonic(self, mnemonic):
|
def set_mnemonic(self, mnemonic):
|
||||||
self.mnemonic = unicode(str(bytearray(Mnemonic.normalize_string(mnemonic), 'utf-8')), 'utf-8').split(' ')
|
self.mnemonic = normalize_nfc(passphrase)
|
||||||
|
|
||||||
def call_raw(self, msg):
|
def call_raw(self, msg):
|
||||||
|
|
||||||
@ -465,15 +464,8 @@ class ProtocolMixin(object):
|
|||||||
@expect(proto.MessageSignature)
|
@expect(proto.MessageSignature)
|
||||||
def sign_message(self, coin_name, n, message):
|
def sign_message(self, coin_name, n, message):
|
||||||
n = self._convert_prime(n)
|
n = self._convert_prime(n)
|
||||||
|
# Convert message to UTF8 NFC (seems to be a bitcoin-qt standard)
|
||||||
try:
|
message = normalize_nfc(message)
|
||||||
# Convert message to UTF8 NFC (seems to be a bitcoin-qt standard)
|
|
||||||
message = normalize_nfc(message)
|
|
||||||
# Convert message to ASCII stream
|
|
||||||
message = str(bytearray(message, 'utf-8'))
|
|
||||||
except:
|
|
||||||
pass # it was not UTF8 string
|
|
||||||
|
|
||||||
return self.call(proto.SignMessage(coin_name=coin_name, address_n=n, message=message))
|
return self.call(proto.SignMessage(coin_name=coin_name, address_n=n, message=message))
|
||||||
|
|
||||||
@expect(proto.SignedIdentity)
|
@expect(proto.SignedIdentity)
|
||||||
@ -481,14 +473,8 @@ class ProtocolMixin(object):
|
|||||||
return self.call(proto.SignIdentity(identity=identity, challenge_hidden=challenge_hidden, challenge_visual=challenge_visual, ecdsa_curve_name=ecdsa_curve_name))
|
return self.call(proto.SignIdentity(identity=identity, challenge_hidden=challenge_hidden, challenge_visual=challenge_visual, ecdsa_curve_name=ecdsa_curve_name))
|
||||||
|
|
||||||
def verify_message(self, address, signature, message):
|
def verify_message(self, address, signature, message):
|
||||||
try:
|
# Convert message to UTF8 NFC (seems to be a bitcoin-qt standard)
|
||||||
# Convert message to UTF8 NFC (seems to be a bitcoin-qt standard)
|
message = normalize_nfc(message)
|
||||||
message = normalize_nfc(message)
|
|
||||||
# Convert message to ASCII stream
|
|
||||||
message = str(bytearray(message, 'utf-8'))
|
|
||||||
except:
|
|
||||||
pass # it was not UTF8 string
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if address:
|
if address:
|
||||||
resp = self.call(proto.VerifyMessage(address=address, signature=signature, message=message))
|
resp = self.call(proto.VerifyMessage(address=address, signature=signature, message=message))
|
||||||
@ -743,7 +729,7 @@ class ProtocolMixin(object):
|
|||||||
mnemonic = Mnemonic.normalize_string(mnemonic)
|
mnemonic = Mnemonic.normalize_string(mnemonic)
|
||||||
|
|
||||||
# Convert mnemonic to ASCII stream
|
# Convert mnemonic to ASCII stream
|
||||||
mnemonic = unicode(str(bytearray(mnemonic, 'utf-8')), 'utf-8')
|
mnemonic = normalize_nfc(mnemonic)
|
||||||
|
|
||||||
if self.features.initialized:
|
if self.features.initialized:
|
||||||
raise Exception("Device is initialized already. Call wipe_device() and try again.")
|
raise Exception("Device is initialized already. Call wipe_device() and try again.")
|
||||||
@ -769,7 +755,7 @@ class ProtocolMixin(object):
|
|||||||
raise Exception("Invalid length of xprv")
|
raise Exception("Invalid length of xprv")
|
||||||
|
|
||||||
node = types.HDNodeType()
|
node = types.HDNodeType()
|
||||||
data = tools.b58decode(xprv, None).encode('hex')
|
data = binascii.hexlify(tools.b58decode(xprv, None))
|
||||||
|
|
||||||
if data[90:92] != '00':
|
if data[90:92] != '00':
|
||||||
raise Exception("Contain invalid private key")
|
raise Exception("Contain invalid private key")
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import hashlib
|
import hashlib
|
||||||
import binascii
|
import binascii
|
||||||
|
import sys
|
||||||
|
|
||||||
Hash = lambda x: hashlib.sha256(hashlib.sha256(x).digest()).digest()
|
Hash = lambda x: hashlib.sha256(hashlib.sha256(x).digest()).digest()
|
||||||
|
|
||||||
@ -79,7 +80,10 @@ def b58decode(v, length):
|
|||||||
if length is not None and len(result) != length:
|
if length is not None and len(result) != length:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
return result
|
if sys.version_info[0] < 3:
|
||||||
|
return result
|
||||||
|
else:
|
||||||
|
return str.encode(result)
|
||||||
|
|
||||||
def monkeypatch_google_protobuf_text_format():
|
def monkeypatch_google_protobuf_text_format():
|
||||||
# monkeypatching: text formatting of protobuf messages
|
# monkeypatching: text formatting of protobuf messages
|
||||||
|
Loading…
Reference in New Issue
Block a user