From b7c7190573cd1f163226f156b6075d31f6fb51d1 Mon Sep 17 00:00:00 2001 From: matejcik Date: Fri, 20 Apr 2018 17:23:43 +0200 Subject: [PATCH] trezorlib: remove Hash to btc_hash, prepare for moving it away from things --- tools/signtest.py | 2 +- trezorlib/client.py | 4 ++-- trezorlib/tests/support/ckd_public.py | 4 ++-- trezorlib/tools.py | 7 +++++-- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/tools/signtest.py b/tools/signtest.py index 547c3c6953..1fbdd6e2e3 100755 --- a/tools/signtest.py +++ b/tools/signtest.py @@ -110,7 +110,7 @@ class MyTxApiBitcoin(object): o.script_pubkey = b'\x76\xa9\x14' + pubkey + b'\x88\xac' txser = self.serialize_tx(t) - txhash = tools.Hash(txser)[::-1] + txhash = tools.btc_hash(txser)[::-1] self.inputs.append( proto_types.TxInputType( address_n=self.client.expand_path("44'/0'/0'/0/%d" % idx), diff --git a/trezorlib/client.py b/trezorlib/client.py index f5bdd973d1..8a0bb46a9e 100644 --- a/trezorlib/client.py +++ b/trezorlib/client.py @@ -1029,7 +1029,7 @@ class ProtocolMixin(object): if xprv[0:4] not in ('xprv', 'tprv'): raise ValueError("Unknown type of xprv") - if len(xprv) < 100 and len(xprv) > 112: + if not 100 < len(xprv) < 112: # yes this is correct in Python raise ValueError("Invalid length of xprv") node = proto.HDNodeType() @@ -1038,7 +1038,7 @@ class ProtocolMixin(object): if data[90:92] != b'00': raise ValueError("Contain invalid private key") - checksum = binascii.hexlify(hashlib.sha256(hashlib.sha256(binascii.unhexlify(data[:156])).digest()).digest()[:4]) + checksum = binascii.hexlify(tools.btc_hash(binascii.unhexlify(data[:156]))[:4]) if checksum != data[156:]: raise ValueError("Checksum doesn't match") diff --git a/trezorlib/tests/support/ckd_public.py b/trezorlib/tests/support/ckd_public.py index e377cf05a9..cb4f2fa5ba 100644 --- a/trezorlib/tests/support/ckd_public.py +++ b/trezorlib/tests/support/ckd_public.py @@ -126,14 +126,14 @@ def serialize(node, version=0x0488B21E): s += b'\x00' + node.private_key else: s += node.public_key - s += tools.Hash(s)[:4] + s += tools.btc_hash(s)[:4] return tools.b58encode(s) def deserialize(xpub): data = tools.b58decode(xpub, None) - if tools.Hash(data[:-4])[:4] != data[-4:]: + if tools.btc_hash(data[:-4])[:4] != data[-4:]: raise ValueError("Checksum failed") node = messages.HDNodeType() diff --git a/trezorlib/tools.py b/trezorlib/tools.py index 8029cb1cbf..bb24e9dd63 100644 --- a/trezorlib/tools.py +++ b/trezorlib/tools.py @@ -35,7 +35,10 @@ def H_(x: int) -> int: return x | HARDENED_FLAG -def Hash(data): +def btc_hash(data): + """ + Double-SHA256 hash as used in BTC + """ return hashlib.sha256(hashlib.sha256(data).digest()).digest() @@ -47,7 +50,7 @@ def hash_160(public_key): def hash_160_to_bc_address(h160, address_type): vh160 = struct.pack('