1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-26 16:18:22 +00:00

refactor(core): create and use protobuf uvarint writer

This commit is contained in:
matejcik 2021-05-27 13:48:16 +02:00 committed by matejcik
parent a8623c4b59
commit da2ef8ed41
4 changed files with 24 additions and 19 deletions

View File

@ -99,3 +99,13 @@ def write_bitcoin_varint(w: Writer, n: int) -> None:
w.append((n >> 8) & 0xFF)
w.append((n >> 16) & 0xFF)
w.append((n >> 24) & 0xFF)
def write_uvarint(w: Writer, n: int) -> None:
ensure(n >= 0 and n <= 0xFFFF_FFFF_FFFF_FFFF)
shifted = 1
while shifted:
shifted = n >> 7
byte = (n & 0x7F) | (0x80 if shifted else 0x00)
w.append(byte)
n = shifted

View File

@ -73,7 +73,7 @@ async def process_unknown_action(
ctx: wire.Context, w: Writer, action: EosTxActionAck
) -> None:
checksum = HashWriter(sha256())
writers.write_variant32(checksum, action.unknown.data_size)
writers.write_uvarint(checksum, action.unknown.data_size)
checksum.extend(action.unknown.data_chunk)
writers.write_bytes_unchecked(w, action.unknown.data_chunk)

View File

@ -28,7 +28,7 @@ async def sign_tx(ctx: wire.Context, msg: EosSignTx, keychain: Keychain) -> EosS
sha = HashWriter(sha256())
await _init(ctx, sha, msg)
await _actions(ctx, sha, msg.num_actions)
writers.write_variant32(sha, 0)
writers.write_uvarint(sha, 0)
writers.write_bytes_fixed(sha, bytearray(32), 32)
digest = sha.get_digest()
@ -42,8 +42,8 @@ async def sign_tx(ctx: wire.Context, msg: EosSignTx, keychain: Keychain) -> EosS
async def _init(ctx: wire.Context, sha: HashWriter, msg: EosSignTx) -> None:
writers.write_bytes_fixed(sha, msg.chain_id, 32)
writers.write_header(sha, msg.header)
writers.write_variant32(sha, 0)
writers.write_variant32(sha, msg.num_actions)
writers.write_uvarint(sha, 0)
writers.write_uvarint(sha, msg.num_actions)
await require_sign_tx(ctx, msg.num_actions)

View File

@ -1,5 +1,3 @@
from protobuf import dump_uvarint
from apps.common.writers import (
write_bytes_fixed,
write_bytes_unchecked,
@ -7,6 +5,7 @@ from apps.common.writers import (
write_uint16_le,
write_uint32_le,
write_uint64_le,
write_uvarint,
)
if False:
@ -33,19 +32,19 @@ if False:
def write_auth(w: Writer, auth: EosAuthorization) -> None:
write_uint32_le(w, auth.threshold)
write_variant32(w, len(auth.keys))
write_uvarint(w, len(auth.keys))
for key in auth.keys:
write_variant32(w, key.type)
write_uvarint(w, key.type)
write_bytes_fixed(w, key.key, 33)
write_uint16_le(w, key.weight)
write_variant32(w, len(auth.accounts))
write_uvarint(w, len(auth.accounts))
for account in auth.accounts:
write_uint64_le(w, account.account.actor)
write_uint64_le(w, account.account.permission)
write_uint16_le(w, account.weight)
write_variant32(w, len(auth.waits))
write_uvarint(w, len(auth.waits))
for wait in auth.waits:
write_uint32_le(w, wait.wait_sec)
write_uint16_le(w, wait.weight)
@ -55,9 +54,9 @@ def write_header(hasher: Writer, header: EosTxHeader) -> None:
write_uint32_le(hasher, header.expiration)
write_uint16_le(hasher, header.ref_block_num)
write_uint32_le(hasher, header.ref_block_prefix)
write_variant32(hasher, header.max_net_usage_words)
write_uvarint(hasher, header.max_net_usage_words)
write_uint8(hasher, header.max_cpu_usage_ms)
write_variant32(hasher, header.delay_sec)
write_uvarint(hasher, header.delay_sec)
def write_action_transfer(w: Writer, msg: EosActionTransfer) -> None:
@ -106,7 +105,7 @@ def write_action_refund(w: Writer, msg: EosActionRefund) -> None:
def write_action_voteproducer(w: Writer, msg: EosActionVoteProducer) -> None:
write_uint64_le(w, msg.voter)
write_uint64_le(w, msg.proxy)
write_variant32(w, len(msg.producers))
write_uvarint(w, len(msg.producers))
for producer in msg.producers:
write_uint64_le(w, producer)
@ -146,7 +145,7 @@ def write_action_newaccount(w: Writer, msg: EosActionNewAccount) -> None:
def write_action_common(w: Writer, msg: EosActionCommon) -> None:
write_uint64_le(w, msg.account)
write_uint64_le(w, msg.name)
write_variant32(w, len(msg.authorization))
write_uvarint(w, len(msg.authorization))
for authorization in msg.authorization:
write_uint64_le(w, authorization.actor)
write_uint64_le(w, authorization.permission)
@ -157,10 +156,6 @@ def write_asset(w: Writer, asset: EosAsset) -> None:
write_uint64_le(w, asset.symbol)
def write_variant32(w: Writer, value: int) -> None:
dump_uvarint(w.extend, value)
def write_bytes_prefixed(w: Writer, data: bytes) -> None:
write_variant32(w, len(data))
write_uvarint(w, len(data))
write_bytes_unchecked(w, data)