1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-02-02 02:41:28 +00:00

no need to use byteindex/iterbytes anymore

This commit is contained in:
Pavol Rusnak 2017-12-18 22:26:26 +01:00
parent f2a52400c3
commit a9291e89c5
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
3 changed files with 9 additions and 34 deletions

View File

@ -25,13 +25,6 @@ import common
from trezorlib import messages as proto from trezorlib import messages as proto
if sys.version_info < (3,):
def byteindex(data, index):
return ord(data[index])
else:
def byteindex(data, index):
return data[index]
TXHASH_d5f65e = binascii.unhexlify('d5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882') TXHASH_d5f65e = binascii.unhexlify('d5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882')
@ -63,7 +56,7 @@ class TestZeroSig(common.TrezorTest):
tx = self.client.call(msg) tx = self.client.call(msg)
siglen = byteindex(tx.serialized_tx, 44) siglen = tx.serialized_tx[44]
print(siglen) print(siglen)
if siglen < 67: if siglen < 67:
print("!!!!", n) print("!!!!", n)
@ -89,7 +82,7 @@ class TestZeroSig(common.TrezorTest):
) )
(signatures, serialized_tx) = self.client.sign_tx('Bitcoin', [inp1, ], [out1, ]) (signatures, serialized_tx) = self.client.sign_tx('Bitcoin', [inp1, ], [out1, ])
siglen = byteindex(serialized_tx, 44) siglen = serialized_tx[44]
# TREZOR must strip leading zero from signature # TREZOR must strip leading zero from signature
self.assertEqual(siglen, 67) self.assertEqual(siglen, 67)
@ -112,7 +105,7 @@ class TestZeroSig(common.TrezorTest):
) )
(signatures, serialized_tx) = self.client.sign_tx('Bitcoin', [inp1, ], [out1, ]) (signatures, serialized_tx) = self.client.sign_tx('Bitcoin', [inp1, ], [out1, ])
siglen = byteindex(serialized_tx, 44) siglen = serialized_tx[44]
# TREZOR must strip leading zero from signature # TREZOR must strip leading zero from signature
self.assertEqual(siglen, 66) self.assertEqual(siglen, 66)

View File

@ -32,20 +32,13 @@ from . import messages as proto
PRIME_DERIVATION_FLAG = 0x80000000 PRIME_DERIVATION_FLAG = 0x80000000
if sys.version_info < (3,):
def byteindex(data, index):
return ord(data[index])
else:
def byteindex(data, index):
return data[index]
def point_to_pubkey(point): def point_to_pubkey(point):
order = SECP256k1.order order = SECP256k1.order
x_str = number_to_string(point.x(), order) x_str = number_to_string(point.x(), order)
y_str = number_to_string(point.y(), order) y_str = number_to_string(point.y(), order)
vk = x_str + y_str vk = x_str + y_str
return struct.pack('B', (byteindex(vk, 63) & 1) + 2) + vk[0:32] # To compressed key return struct.pack('B', vk[63] & 1 + 2) + vk[0:32] # To compressed key
def sec_to_public_pair(pubkey): def sec_to_public_pair(pubkey):
@ -152,7 +145,7 @@ def deserialize(xpub):
node.chain_code = data[13:45] node.chain_code = data[13:45]
key = data[45:-4] key = data[45:-4]
if byteindex(key, 0) == 0: if key[0] == 0:
node.private_key = key[1:] node.private_key = key[1:]
else: else:
node.public_key = key node.public_key = key

View File

@ -22,17 +22,6 @@ import binascii
import struct import struct
import sys import sys
if sys.version_info < (3,):
def byteindex(data, index):
return ord(data[index])
def iterbytes(data):
return (ord(char) for char in data)
else:
def byteindex(data, index):
return data[index]
iterbytes = iter
def Hash(data): def Hash(data):
return hashlib.sha256(hashlib.sha256(data).digest()).digest() return hashlib.sha256(hashlib.sha256(data).digest()).digest()
@ -52,8 +41,8 @@ def hash_160_to_bc_address(h160, address_type):
def compress_pubkey(public_key): def compress_pubkey(public_key):
if byteindex(public_key, 0) == 4: if public_key[0] == 4:
return bytes((byteindex(public_key, 64) & 1) + 2) + public_key[1:33] return bytes(public_key[64] & 1 + 2) + public_key[1:33]
raise ValueError("Pubkey is already compressed") raise ValueError("Pubkey is already compressed")
@ -73,7 +62,7 @@ def b58encode(v):
""" encode v, which is a string of bytes, to base58.""" """ encode v, which is a string of bytes, to base58."""
long_value = 0 long_value = 0
for c in iterbytes(v): for c in v:
long_value = long_value * 256 + c long_value = long_value * 256 + c
result = '' result = ''
@ -86,7 +75,7 @@ def b58encode(v):
# Bitcoin does a little leading-zero-compression: # Bitcoin does a little leading-zero-compression:
# leading 0-bytes in the input become leading-1s # leading 0-bytes in the input become leading-1s
nPad = 0 nPad = 0
for c in iterbytes(v): for c in v:
if c == 0: if c == 0:
nPad += 1 nPad += 1
else: else: