1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-24 15:28:10 +00:00

utils: simplify HashWriter interface

This commit is contained in:
Jan Pochyla 2018-10-30 15:48:26 +01:00
parent 919c6a5749
commit b9926a9fff
12 changed files with 30 additions and 33 deletions

View File

@ -8,9 +8,9 @@ from apps.wallet.sign_tx.signing import write_varint
def message_digest(coin, message): def message_digest(coin, message):
if coin.decred: if coin.decred:
h = HashWriter(blake256) h = HashWriter(blake256())
else: else:
h = HashWriter(sha256) h = HashWriter(sha256())
write_varint(h, len(coin.signed_message_header)) write_varint(h, len(coin.signed_message_header))
h.extend(coin.signed_message_header) h.extend(coin.signed_message_header)
write_varint(h, len(message)) write_varint(h, len(message))

View File

@ -10,7 +10,7 @@ from apps.common.signverify import split_message
def message_digest(message): def message_digest(message):
h = HashWriter(sha3_256, keccak=True) h = HashWriter(sha3_256(keccak=True))
signed_message_header = "\x19Ethereum Signed Message:\n" signed_message_header = "\x19Ethereum Signed Message:\n"
h.extend(signed_message_header) h.extend(signed_message_header)
h.extend(str(len(message))) h.extend(str(len(message)))

View File

@ -61,7 +61,7 @@ async def sign_tx(ctx, msg):
total_length = get_total_length(msg, data_total) 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 sha.extend(rlp.encode_length(total_length, True)) # total length
if msg.tx_type is not None: if msg.tx_type is not None:

View File

@ -13,7 +13,7 @@ from apps.wallet.sign_tx.signing import write_varint
def message_digest(message): def message_digest(message):
h = HashWriter(sha256) h = HashWriter(sha256())
signed_message_header = "Lisk Signed Message:\n" signed_message_header = "Lisk Signed Message:\n"
write_varint(h, len(signed_message_header)) write_varint(h, len(signed_message_header))
h.extend(signed_message_header) h.extend(signed_message_header)

View File

@ -25,7 +25,7 @@ async def sign_tx(ctx, msg):
await layout.require_confirm_fee(ctx, transaction.amount, transaction.fee) await layout.require_confirm_fee(ctx, transaction.amount, transaction.fee)
txbytes = _get_transaction_bytes(transaction) txbytes = _get_transaction_bytes(transaction)
txhash = HashWriter(sha256) txhash = HashWriter(sha256())
for field in txbytes: for field in txbytes:
txhash.extend(field) txhash.extend(field)
digest = txhash.get_digest() digest = txhash.get_digest()

View File

@ -30,7 +30,7 @@ class DecredPrefixHasher:
""" """
def __init__(self, tx: SignTx): def __init__(self, tx: SignTx):
self.h_prefix = HashWriter(blake256) self.h_prefix = HashWriter(blake256())
self.last_output_bytes = None self.last_output_bytes = None
write_uint32(self.h_prefix, tx.version | DECRED_SERIALIZE_NO_WITNESS) write_uint32(self.h_prefix, tx.version | DECRED_SERIALIZE_NO_WITNESS)
write_varint(self.h_prefix, tx.inputs_count) write_varint(self.h_prefix, tx.inputs_count)

View File

@ -50,7 +50,7 @@ def multisig_fingerprint(multisig: MultisigRedeemScriptType) -> bytes:
# casting to bytes(), sorting on bytearray() is not supported in MicroPython # casting to bytes(), sorting on bytearray() is not supported in MicroPython
pubkeys = sorted(pubkeys, key=lambda hd: bytes(hd.node.public_key)) pubkeys = sorted(pubkeys, key=lambda hd: bytes(hd.node.public_key))
h = HashWriter(sha256) h = HashWriter(sha256())
write_uint32(h, m) write_uint32(h, m)
write_uint32(h, n) write_uint32(h, n)
for hd in pubkeys: for hd in pubkeys:

View File

@ -25,9 +25,9 @@ class Bip143Error(ValueError):
class Bip143: class Bip143:
def __init__(self): def __init__(self):
self.h_prevouts = HashWriter(sha256) self.h_prevouts = HashWriter(sha256())
self.h_sequence = HashWriter(sha256) self.h_sequence = HashWriter(sha256())
self.h_outputs = HashWriter(sha256) self.h_outputs = HashWriter(sha256())
def add_prevouts(self, txi: TxInputType): def add_prevouts(self, txi: TxInputType):
write_bytes_reversed(self.h_prevouts, txi.prev_hash) write_bytes_reversed(self.h_prevouts, txi.prev_hash)
@ -56,7 +56,7 @@ class Bip143:
pubkeyhash: bytes, pubkeyhash: bytes,
sighash: int, sighash: int,
) -> bytes: ) -> bytes:
h_preimage = HashWriter(sha256) h_preimage = HashWriter(sha256())
ensure(not tx.overwintered) ensure(not tx.overwintered)

View File

@ -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 # 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 # 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 # 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: if coin.decred:
hash143 = DecredPrefixHasher(tx) # pseudo bip143 prefix hashing hash143 = DecredPrefixHasher(tx) # pseudo bip143 prefix hashing
@ -333,7 +333,7 @@ async def sign_tx(tx: SignTx, root: bip32.HDNode):
else: else:
raise ValueError("Unknown input script type") 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_uint32(h_witness, tx.version | DECRED_SERIALIZE_WITNESS_SIGNING)
write_varint(h_witness, tx.inputs_count) 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_witness, double=coin.sign_hash_double, reverse=False
) )
h_sign = HashWriter(blake256) h_sign = HashWriter(blake256())
write_uint32(h_sign, DECRED_SIGHASHALL) write_uint32(h_sign, DECRED_SIGHASHALL)
write_bytes(h_sign, prefix_hash) write_bytes(h_sign, prefix_hash)
write_bytes(h_sign, witness_hash) write_bytes(h_sign, witness_hash)
@ -380,9 +380,9 @@ async def sign_tx(tx: SignTx, root: bip32.HDNode):
else: else:
# hash of what we are signing with this input # 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 # same as h_first, checked before signing the digest
h_second = HashWriter(sha256) h_second = HashWriter(sha256())
if tx.overwintered: if tx.overwintered:
write_uint32( write_uint32(
@ -575,9 +575,9 @@ async def get_prevtx_output_value(
tx = await request_tx_meta(tx_req, prev_hash) tx = await request_tx_meta(tx_req, prev_hash)
if coin.decred: if coin.decred:
txh = HashWriter(blake256) txh = HashWriter(blake256())
else: else:
txh = HashWriter(sha256) txh = HashWriter(sha256())
if tx.overwintered: if tx.overwintered:
write_uint32(txh, tx.version | OVERWINTERED) # nVersion | fOverwintered write_uint32(txh, tx.version | OVERWINTERED) # nVersion | fOverwintered

View File

@ -46,9 +46,9 @@ def derive_script_code(txi: TxInputType, pubkeyhash: bytes) -> bytearray:
class Zip143: class Zip143:
def __init__(self): def __init__(self):
self.h_prevouts = HashWriter(blake2b, outlen=32, personal=b"ZcashPrevoutHash") self.h_prevouts = HashWriter(blake2b(outlen=32, personal=b"ZcashPrevoutHash"))
self.h_sequence = HashWriter(blake2b, outlen=32, personal=b"ZcashSequencHash") self.h_sequence = HashWriter(blake2b(outlen=32, personal=b"ZcashSequencHash"))
self.h_outputs = HashWriter(blake2b, outlen=32, personal=b"ZcashOutputsHash") self.h_outputs = HashWriter(blake2b(outlen=32, personal=b"ZcashOutputsHash"))
def add_prevouts(self, txi: TxInputType): def add_prevouts(self, txi: TxInputType):
write_bytes_reversed(self.h_prevouts, txi.prev_hash) write_bytes_reversed(self.h_prevouts, txi.prev_hash)
@ -78,7 +78,7 @@ class Zip143:
sighash: int, sighash: int,
) -> bytes: ) -> bytes:
h_preimage = HashWriter( 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 ) # BRANCH_ID = 0x5ba81b19 / Overwinter
ensure(tx.overwintered) ensure(tx.overwintered)
@ -123,7 +123,7 @@ class Zip243(Zip143):
sighash: int, sighash: int,
) -> bytes: ) -> bytes:
h_preimage = HashWriter( 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 ) # BRANCH_ID = 0x76b809bb / Sapling
ensure(tx.overwintered) ensure(tx.overwintered)

View File

@ -62,11 +62,8 @@ def format_ordinal(number):
class HashWriter: class HashWriter:
def __init__(self, hashfunc, *hashargs, **hashkwargs): def __init__(self, ctx):
if callable(hashfunc): self.ctx = ctx
self.ctx = hashfunc(*hashargs, **hashkwargs)
else:
self.ctx = hashfunc
self.buf = bytearray(1) # used in append() self.buf = bytearray(1) # used in append()
def extend(self, buf: bytearray): def extend(self, buf: bytearray):

View File

@ -1,9 +1,9 @@
from common import * from common import *
from apps.wallet.sign_tx.signing import *
from apps.common import coins
from trezor.crypto import bip32, bip39 from trezor.crypto import bip32, bip39
from apps.common import coins
from apps.wallet.sign_tx.signing import *
class TestAddress(unittest.TestCase): class TestAddress(unittest.TestCase):
# pylint: disable=C0301 # pylint: disable=C0301
@ -59,7 +59,7 @@ class TestAddress(unittest.TestCase):
# pubkey OP_CHECKSIG # pubkey OP_CHECKSIG
script = unhexlify('210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ac') script = unhexlify('210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ac')
h = HashWriter(sha256) h = HashWriter(sha256())
write_bytes(h, script) write_bytes(h, script)
address = address_p2wsh( address = address_p2wsh(