From 6fd355756c35a4cf414d66624691aadce153d961 Mon Sep 17 00:00:00 2001 From: matejcik Date: Thu, 14 Jan 2021 16:40:38 +0100 Subject: [PATCH] fix(core/stellar): review usages of write_bytes_unchecked (cherry picked from commit 781e9f4db8d42f0d5c23e16cbe1efd5af1dae351) --- core/src/apps/stellar/operations/serialize.py | 6 +++--- core/src/apps/stellar/sign_tx.py | 6 +++--- core/src/apps/stellar/writers.py | 15 ++++++++++----- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/core/src/apps/stellar/operations/serialize.py b/core/src/apps/stellar/operations/serialize.py index 05a846d8c..fc88ac750 100644 --- a/core/src/apps/stellar/operations/serialize.py +++ b/core/src/apps/stellar/operations/serialize.py @@ -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") diff --git a/core/src/apps/stellar/sign_tx.py b/core/src/apps/stellar/sign_tx.py index 72ea2c6b7..bf72d3531 100644 --- a/core/src/apps/stellar/sign_tx.py +++ b/core/src/apps/stellar/sign_tx.py @@ -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") diff --git a/core/src/apps/stellar/writers.py b/core/src/apps/stellar/writers.py index af486bb10..0d932fc0d 100644 --- a/core/src/apps/stellar/writers.py +++ b/core/src/apps/stellar/writers.py @@ -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)