2020-03-16 10:31:11 +00:00
|
|
|
from apps.common.writers import write_bytes_unchecked, write_uint32_be, write_uint64_be
|
2018-05-17 12:24:37 +00:00
|
|
|
|
2020-05-18 11:17:34 +00:00
|
|
|
from .helpers import public_key_from_address
|
|
|
|
|
2018-08-23 12:39:30 +00:00
|
|
|
write_uint32 = write_uint32_be
|
|
|
|
write_uint64 = write_uint64_be
|
2018-05-17 12:24:37 +00:00
|
|
|
|
2019-10-01 12:44:21 +00:00
|
|
|
if False:
|
|
|
|
from typing import AnyStr
|
2018-05-17 12:24:37 +00:00
|
|
|
|
2019-10-01 12:44:21 +00:00
|
|
|
|
|
|
|
def write_string(w, s: AnyStr) -> None:
|
|
|
|
"""Write XDR string padded to a multiple of 4 bytes."""
|
|
|
|
if isinstance(s, str):
|
|
|
|
buf = s.encode()
|
|
|
|
else:
|
|
|
|
buf = s
|
2018-08-23 12:39:30 +00:00
|
|
|
write_uint32(w, len(buf))
|
2020-03-16 10:31:11 +00:00
|
|
|
write_bytes_unchecked(w, buf)
|
2018-05-24 13:33:03 +00:00
|
|
|
# if len isn't a multiple of 4, add padding bytes
|
2018-08-23 12:39:30 +00:00
|
|
|
reminder = len(buf) % 4
|
2018-05-24 13:33:03 +00:00
|
|
|
if reminder:
|
2020-03-16 10:31:11 +00:00
|
|
|
write_bytes_unchecked(w, bytes([0] * (4 - reminder)))
|
2018-05-17 12:24:37 +00:00
|
|
|
|
|
|
|
|
2018-08-23 12:39:30 +00:00
|
|
|
def write_bool(w, val: bool):
|
2018-05-17 12:24:37 +00:00
|
|
|
if val:
|
|
|
|
write_uint32(w, 1)
|
|
|
|
else:
|
|
|
|
write_uint32(w, 0)
|
|
|
|
|
|
|
|
|
2018-06-14 12:05:41 +00:00
|
|
|
def write_pubkey(w, address: str):
|
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)
|
2020-03-16 10:31:11 +00:00
|
|
|
write_bytes_unchecked(w, public_key_from_address(address))
|