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:
parent
919c6a5749
commit
b9926a9fff
@ -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))
|
||||
|
@ -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)))
|
||||
|
@ -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:
|
||||
|
@ -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)
|
||||
|
@ -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()
|
||||
|
@ -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)
|
||||
|
@ -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:
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
@ -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):
|
||||
|
@ -1,9 +1,9 @@
|
||||
from common import *
|
||||
|
||||
from apps.wallet.sign_tx.signing import *
|
||||
from apps.common import coins
|
||||
from trezor.crypto import bip32, bip39
|
||||
|
||||
from apps.common import coins
|
||||
from apps.wallet.sign_tx.signing import *
|
||||
|
||||
|
||||
class TestAddress(unittest.TestCase):
|
||||
# pylint: disable=C0301
|
||||
@ -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(
|
||||
|
Loading…
Reference in New Issue
Block a user