1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 23:48:12 +00:00

fix(core/stellar): review usages of write_bytes_unchecked

This commit is contained in:
matejcik 2021-01-14 16:40:38 +01:00 committed by Tomas Susanka
parent 5b5ed8cce1
commit 781e9f4db8
3 changed files with 16 additions and 11 deletions

View File

@ -119,7 +119,7 @@ def write_set_options_op(w, msg: StellarSetOptionsOp):
elif msg.signer_type in consts.SIGN_TYPES:
writers.write_bool(w, True)
writers.write_uint32(w, msg.signer_type)
writers.write_bytes_unchecked(w, msg.signer_key)
writers.write_bytes_fixed(w, msg.signer_key, 32)
writers.write_uint32(w, msg.signer_weight)
else:
raise ProcessError("Stellar: unknown signer type")
@ -146,10 +146,10 @@ def _write_asset_code(w, asset_type: int, asset_code: str):
return # nothing is needed
elif asset_type == consts.ASSET_TYPE_ALPHANUM4:
# pad with zeros to 4 chars
writers.write_bytes_unchecked(w, code + bytearray([0] * (4 - len(code))))
writers.write_bytes_fixed(w, code + bytearray([0] * (4 - len(code))), 4)
elif asset_type == consts.ASSET_TYPE_ALPHANUM12:
# pad with zeros to 12 chars
writers.write_bytes_unchecked(w, code + bytearray([0] * (12 - len(code))))
writers.write_bytes_fixed(w, code + bytearray([0] * (12 - len(code))), 12)
else:
raise ProcessError("Stellar: invalid asset type")

View File

@ -48,8 +48,8 @@ async def _final(ctx, w: bytearray, msg: StellarSignTx):
async def _init(ctx, w: bytearray, pubkey: bytes, msg: StellarSignTx):
network_passphrase_hash = sha256(msg.network_passphrase).digest()
writers.write_bytes_unchecked(w, network_passphrase_hash)
writers.write_bytes_unchecked(w, consts.TX_TYPE)
writers.write_bytes_fixed(w, network_passphrase_hash, 32)
writers.write_bytes_fixed(w, consts.TX_TYPE, 4)
address = helpers.address_from_public_key(pubkey)
accounts_match = msg.source_account == address
@ -104,7 +104,7 @@ async def _memo(ctx, w: bytearray, msg: StellarSignTx):
memo_confirm_text = str(msg.memo_id)
elif msg.memo_type in (consts.MEMO_TYPE_HASH, consts.MEMO_TYPE_RETURN):
# Hash/Return: 32 byte hash
writers.write_bytes_unchecked(w, bytearray(msg.memo_hash))
writers.write_bytes_fixed(w, bytearray(msg.memo_hash), 32)
memo_confirm_text = hexlify(msg.memo_hash).decode()
else:
raise ProcessError("Stellar invalid memo type")

View File

@ -1,4 +1,9 @@
from apps.common.writers import write_bytes_unchecked, write_uint32_be, write_uint64_be
from apps.common.writers import (
write_bytes_fixed,
write_bytes_unchecked,
write_uint32_be,
write_uint64_be,
)
from .helpers import public_key_from_address
@ -18,9 +23,9 @@ def write_string(w, s: AnyStr) -> None:
write_uint32(w, len(buf))
write_bytes_unchecked(w, buf)
# if len isn't a multiple of 4, add padding bytes
reminder = len(buf) % 4
if reminder:
write_bytes_unchecked(w, bytes([0] * (4 - reminder)))
remainder = len(buf) % 4
if remainder:
write_bytes_unchecked(w, bytes([0] * (4 - remainder)))
def write_bool(w, val: bool):
@ -33,4 +38,4 @@ def write_bool(w, val: bool):
def write_pubkey(w, address: str):
# first 4 bytes of an address are the type, there's only one type (0)
write_uint32(w, 0)
write_bytes_unchecked(w, public_key_from_address(address))
write_bytes_fixed(w, public_key_from_address(address), 32)