2021-12-08 09:10:58 +00:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
2022-09-15 14:12:01 +00:00
|
|
|
import apps.common.writers as writers
|
2018-05-17 12:24:37 +00:00
|
|
|
|
2022-09-15 14:12:01 +00:00
|
|
|
# Reexporting to other modules
|
|
|
|
write_bytes_fixed = writers.write_bytes_fixed
|
|
|
|
write_uint32 = writers.write_uint32_be
|
|
|
|
write_uint64 = writers.write_uint64_be
|
2018-05-17 12:24:37 +00:00
|
|
|
|
2021-12-08 09:10:58 +00:00
|
|
|
if TYPE_CHECKING:
|
2019-10-01 12:44:21 +00:00
|
|
|
from typing import AnyStr
|
2018-05-17 12:24:37 +00:00
|
|
|
|
2021-07-20 10:59:03 +00:00
|
|
|
from trezor.utils import Writer
|
2019-10-01 12:44:21 +00:00
|
|
|
|
2021-07-20 10:59:03 +00:00
|
|
|
|
|
|
|
def write_string(w: Writer, s: AnyStr) -> None:
|
2019-10-01 12:44:21 +00:00
|
|
|
"""Write XDR string padded to a multiple of 4 bytes."""
|
2022-09-15 14:12:01 +00:00
|
|
|
# NOTE: 2 bytes smaller than if-else
|
|
|
|
buf = s.encode() if isinstance(s, str) else s
|
2018-08-23 12:39:30 +00:00
|
|
|
write_uint32(w, len(buf))
|
2022-09-15 14:12:01 +00:00
|
|
|
writers.write_bytes_unchecked(w, buf)
|
2018-05-24 13:33:03 +00:00
|
|
|
# if len isn't a multiple of 4, add padding bytes
|
2021-01-14 15:40:38 +00:00
|
|
|
remainder = len(buf) % 4
|
|
|
|
if remainder:
|
2022-09-15 14:12:01 +00:00
|
|
|
writers.write_bytes_unchecked(w, bytes([0] * (4 - remainder)))
|
2018-05-17 12:24:37 +00:00
|
|
|
|
|
|
|
|
2021-07-20 10:59:03 +00:00
|
|
|
def write_bool(w: Writer, val: bool) -> None:
|
2022-09-15 14:12:01 +00:00
|
|
|
# NOTE: 10 bytes smaller than if-else
|
|
|
|
write_uint32(w, 1 if val else 0)
|
2018-05-17 12:24:37 +00:00
|
|
|
|
|
|
|
|
2021-07-20 10:59:03 +00:00
|
|
|
def write_pubkey(w: Writer, address: str) -> None:
|
2022-09-15 14:12:01 +00:00
|
|
|
from .helpers import public_key_from_address
|
|
|
|
|
2018-05-17 12:24:37 +00:00
|
|
|
# first 4 bytes of an address are the type, there's only one type (0)
|
|
|
|
write_uint32(w, 0)
|
2022-09-15 14:12:01 +00:00
|
|
|
writers.write_bytes_fixed(w, public_key_from_address(address), 32)
|