From b9926a9fffe8a593beb3adb79e5fc429233e41a3 Mon Sep 17 00:00:00 2001 From: Jan Pochyla Date: Tue, 30 Oct 2018 15:48:26 +0100 Subject: [PATCH] utils: simplify HashWriter interface --- src/apps/common/signverify.py | 4 ++-- src/apps/ethereum/sign_message.py | 2 +- src/apps/ethereum/sign_tx.py | 2 +- src/apps/lisk/sign_message.py | 2 +- src/apps/lisk/sign_tx.py | 2 +- src/apps/wallet/sign_tx/decred_prefix_hasher.py | 2 +- src/apps/wallet/sign_tx/multisig.py | 2 +- src/apps/wallet/sign_tx/segwit_bip143.py | 8 ++++---- src/apps/wallet/sign_tx/signing.py | 14 +++++++------- src/apps/wallet/sign_tx/zcash.py | 10 +++++----- src/trezor/utils.py | 7 ++----- tests/test_apps.wallet.address.py | 6 +++--- 12 files changed, 29 insertions(+), 32 deletions(-) diff --git a/src/apps/common/signverify.py b/src/apps/common/signverify.py index d5a48f9e0..99476f740 100644 --- a/src/apps/common/signverify.py +++ b/src/apps/common/signverify.py @@ -8,9 +8,9 @@ from apps.wallet.sign_tx.signing import write_varint def message_digest(coin, message): if coin.decred: - h = HashWriter(blake256) + h = HashWriter(blake256()) else: - h = HashWriter(sha256) + h = HashWriter(sha256()) write_varint(h, len(coin.signed_message_header)) h.extend(coin.signed_message_header) write_varint(h, len(message)) diff --git a/src/apps/ethereum/sign_message.py b/src/apps/ethereum/sign_message.py index 792da3bf7..618eed85d 100644 --- a/src/apps/ethereum/sign_message.py +++ b/src/apps/ethereum/sign_message.py @@ -10,7 +10,7 @@ from apps.common.signverify import split_message def message_digest(message): - h = HashWriter(sha3_256, keccak=True) + h = HashWriter(sha3_256(keccak=True)) signed_message_header = "\x19Ethereum Signed Message:\n" h.extend(signed_message_header) h.extend(str(len(message))) diff --git a/src/apps/ethereum/sign_tx.py b/src/apps/ethereum/sign_tx.py index 1f4972358..576ebb782 100644 --- a/src/apps/ethereum/sign_tx.py +++ b/src/apps/ethereum/sign_tx.py @@ -61,7 +61,7 @@ async def sign_tx(ctx, msg): total_length = get_total_length(msg, data_total) - sha = HashWriter(sha3_256, keccak=True) + sha = HashWriter(sha3_256(keccak=True)) sha.extend(rlp.encode_length(total_length, True)) # total length if msg.tx_type is not None: diff --git a/src/apps/lisk/sign_message.py b/src/apps/lisk/sign_message.py index 11280a166..109319023 100644 --- a/src/apps/lisk/sign_message.py +++ b/src/apps/lisk/sign_message.py @@ -13,7 +13,7 @@ from apps.wallet.sign_tx.signing import write_varint def message_digest(message): - h = HashWriter(sha256) + h = HashWriter(sha256()) signed_message_header = "Lisk Signed Message:\n" write_varint(h, len(signed_message_header)) h.extend(signed_message_header) diff --git a/src/apps/lisk/sign_tx.py b/src/apps/lisk/sign_tx.py index 9e05b7de9..bcbfbe567 100644 --- a/src/apps/lisk/sign_tx.py +++ b/src/apps/lisk/sign_tx.py @@ -25,7 +25,7 @@ async def sign_tx(ctx, msg): await layout.require_confirm_fee(ctx, transaction.amount, transaction.fee) txbytes = _get_transaction_bytes(transaction) - txhash = HashWriter(sha256) + txhash = HashWriter(sha256()) for field in txbytes: txhash.extend(field) digest = txhash.get_digest() diff --git a/src/apps/wallet/sign_tx/decred_prefix_hasher.py b/src/apps/wallet/sign_tx/decred_prefix_hasher.py index c818a5421..e9ff7beb6 100644 --- a/src/apps/wallet/sign_tx/decred_prefix_hasher.py +++ b/src/apps/wallet/sign_tx/decred_prefix_hasher.py @@ -30,7 +30,7 @@ class DecredPrefixHasher: """ def __init__(self, tx: SignTx): - self.h_prefix = HashWriter(blake256) + self.h_prefix = HashWriter(blake256()) self.last_output_bytes = None write_uint32(self.h_prefix, tx.version | DECRED_SERIALIZE_NO_WITNESS) write_varint(self.h_prefix, tx.inputs_count) diff --git a/src/apps/wallet/sign_tx/multisig.py b/src/apps/wallet/sign_tx/multisig.py index 4a335e661..213f2c9dc 100644 --- a/src/apps/wallet/sign_tx/multisig.py +++ b/src/apps/wallet/sign_tx/multisig.py @@ -50,7 +50,7 @@ def multisig_fingerprint(multisig: MultisigRedeemScriptType) -> bytes: # casting to bytes(), sorting on bytearray() is not supported in MicroPython pubkeys = sorted(pubkeys, key=lambda hd: bytes(hd.node.public_key)) - h = HashWriter(sha256) + h = HashWriter(sha256()) write_uint32(h, m) write_uint32(h, n) for hd in pubkeys: diff --git a/src/apps/wallet/sign_tx/segwit_bip143.py b/src/apps/wallet/sign_tx/segwit_bip143.py index 33f6998d8..b4eba5a8a 100644 --- a/src/apps/wallet/sign_tx/segwit_bip143.py +++ b/src/apps/wallet/sign_tx/segwit_bip143.py @@ -25,9 +25,9 @@ class Bip143Error(ValueError): class Bip143: def __init__(self): - self.h_prevouts = HashWriter(sha256) - self.h_sequence = HashWriter(sha256) - self.h_outputs = HashWriter(sha256) + self.h_prevouts = HashWriter(sha256()) + self.h_sequence = HashWriter(sha256()) + self.h_outputs = HashWriter(sha256()) def add_prevouts(self, txi: TxInputType): write_bytes_reversed(self.h_prevouts, txi.prev_hash) @@ -56,7 +56,7 @@ class Bip143: pubkeyhash: bytes, sighash: int, ) -> bytes: - h_preimage = HashWriter(sha256) + h_preimage = HashWriter(sha256()) ensure(not tx.overwintered) diff --git a/src/apps/wallet/sign_tx/signing.py b/src/apps/wallet/sign_tx/signing.py index d1c446070..b2a8d43f8 100644 --- a/src/apps/wallet/sign_tx/signing.py +++ b/src/apps/wallet/sign_tx/signing.py @@ -64,7 +64,7 @@ async def check_tx_fee(tx: SignTx, root: bip32.HDNode): # h_first is used to make sure the inputs and outputs streamed in Phase 1 # are the same as in Phase 2. it is thus not required to fully hash the # tx, as the SignTx info is streamed only once - h_first = HashWriter(sha256) # not a real tx hash + h_first = HashWriter(sha256()) # not a real tx hash if coin.decred: hash143 = DecredPrefixHasher(tx) # pseudo bip143 prefix hashing @@ -333,7 +333,7 @@ async def sign_tx(tx: SignTx, root: bip32.HDNode): else: raise ValueError("Unknown input script type") - h_witness = HashWriter(blake256) + h_witness = HashWriter(blake256()) write_uint32(h_witness, tx.version | DECRED_SERIALIZE_WITNESS_SIGNING) write_varint(h_witness, tx.inputs_count) @@ -348,7 +348,7 @@ async def sign_tx(tx: SignTx, root: bip32.HDNode): h_witness, double=coin.sign_hash_double, reverse=False ) - h_sign = HashWriter(blake256) + h_sign = HashWriter(blake256()) write_uint32(h_sign, DECRED_SIGHASHALL) write_bytes(h_sign, prefix_hash) write_bytes(h_sign, witness_hash) @@ -380,9 +380,9 @@ async def sign_tx(tx: SignTx, root: bip32.HDNode): else: # hash of what we are signing with this input - h_sign = HashWriter(sha256) + h_sign = HashWriter(sha256()) # same as h_first, checked before signing the digest - h_second = HashWriter(sha256) + h_second = HashWriter(sha256()) if tx.overwintered: write_uint32( @@ -575,9 +575,9 @@ async def get_prevtx_output_value( tx = await request_tx_meta(tx_req, prev_hash) if coin.decred: - txh = HashWriter(blake256) + txh = HashWriter(blake256()) else: - txh = HashWriter(sha256) + txh = HashWriter(sha256()) if tx.overwintered: write_uint32(txh, tx.version | OVERWINTERED) # nVersion | fOverwintered diff --git a/src/apps/wallet/sign_tx/zcash.py b/src/apps/wallet/sign_tx/zcash.py index 3a1ab1b0e..366b35ac8 100644 --- a/src/apps/wallet/sign_tx/zcash.py +++ b/src/apps/wallet/sign_tx/zcash.py @@ -46,9 +46,9 @@ def derive_script_code(txi: TxInputType, pubkeyhash: bytes) -> bytearray: class Zip143: def __init__(self): - self.h_prevouts = HashWriter(blake2b, outlen=32, personal=b"ZcashPrevoutHash") - self.h_sequence = HashWriter(blake2b, outlen=32, personal=b"ZcashSequencHash") - self.h_outputs = HashWriter(blake2b, outlen=32, personal=b"ZcashOutputsHash") + self.h_prevouts = HashWriter(blake2b(outlen=32, personal=b"ZcashPrevoutHash")) + self.h_sequence = HashWriter(blake2b(outlen=32, personal=b"ZcashSequencHash")) + self.h_outputs = HashWriter(blake2b(outlen=32, personal=b"ZcashOutputsHash")) def add_prevouts(self, txi: TxInputType): write_bytes_reversed(self.h_prevouts, txi.prev_hash) @@ -78,7 +78,7 @@ class Zip143: sighash: int, ) -> bytes: h_preimage = HashWriter( - blake2b, outlen=32, personal=b"ZcashSigHash\x19\x1b\xa8\x5b" + blake2b(outlen=32, personal=b"ZcashSigHash\x19\x1b\xa8\x5b") ) # BRANCH_ID = 0x5ba81b19 / Overwinter ensure(tx.overwintered) @@ -123,7 +123,7 @@ class Zip243(Zip143): sighash: int, ) -> bytes: h_preimage = HashWriter( - blake2b, outlen=32, personal=b"ZcashSigHash\xbb\x09\xb8\x76" + blake2b(outlen=32, personal=b"ZcashSigHash\xbb\x09\xb8\x76") ) # BRANCH_ID = 0x76b809bb / Sapling ensure(tx.overwintered) diff --git a/src/trezor/utils.py b/src/trezor/utils.py index 3af4e3c3d..bdc16cbf5 100644 --- a/src/trezor/utils.py +++ b/src/trezor/utils.py @@ -62,11 +62,8 @@ def format_ordinal(number): class HashWriter: - def __init__(self, hashfunc, *hashargs, **hashkwargs): - if callable(hashfunc): - self.ctx = hashfunc(*hashargs, **hashkwargs) - else: - self.ctx = hashfunc + def __init__(self, ctx): + self.ctx = ctx self.buf = bytearray(1) # used in append() def extend(self, buf: bytearray): diff --git a/tests/test_apps.wallet.address.py b/tests/test_apps.wallet.address.py index aace29ba4..131225857 100644 --- a/tests/test_apps.wallet.address.py +++ b/tests/test_apps.wallet.address.py @@ -1,8 +1,8 @@ from common import * +from trezor.crypto import bip32, bip39 -from apps.wallet.sign_tx.signing import * from apps.common import coins -from trezor.crypto import bip32, bip39 +from apps.wallet.sign_tx.signing import * class TestAddress(unittest.TestCase): @@ -59,7 +59,7 @@ class TestAddress(unittest.TestCase): # pubkey OP_CHECKSIG script = unhexlify('210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ac') - h = HashWriter(sha256) + h = HashWriter(sha256()) write_bytes(h, script) address = address_p2wsh(