2018-10-12 14:50:18 +00:00
|
|
|
from trezor.utils import ensure
|
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
if False:
|
|
|
|
from trezor.utils import Writer
|
|
|
|
|
2018-10-12 14:50:18 +00:00
|
|
|
|
2018-08-23 12:39:30 +00:00
|
|
|
def empty_bytearray(preallocate: int) -> bytearray:
|
|
|
|
"""
|
|
|
|
Returns bytearray that won't allocate for at least `preallocate` bytes.
|
|
|
|
Useful in case you want to avoid allocating too often.
|
|
|
|
"""
|
|
|
|
b = bytearray(preallocate)
|
|
|
|
b[:] = bytes()
|
|
|
|
return b
|
|
|
|
|
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
def write_uint8(w: Writer, n: int) -> int:
|
2018-10-12 14:50:18 +00:00
|
|
|
ensure(0 <= n <= 0xFF)
|
2018-08-23 12:39:30 +00:00
|
|
|
w.append(n)
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
def write_uint16_le(w: Writer, n: int) -> int:
|
2018-10-12 14:50:18 +00:00
|
|
|
ensure(0 <= n <= 0xFFFF)
|
2018-10-08 12:34:51 +00:00
|
|
|
w.append(n & 0xFF)
|
|
|
|
w.append((n >> 8) & 0xFF)
|
|
|
|
return 2
|
|
|
|
|
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
def write_uint16_be(w: Writer, n: int) -> int:
|
2019-04-16 17:20:40 +00:00
|
|
|
ensure(0 <= n <= 0xFFFF)
|
|
|
|
w.append((n >> 8) & 0xFF)
|
|
|
|
w.append(n & 0xFF)
|
|
|
|
return 2
|
|
|
|
|
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
def write_uint32_le(w: Writer, n: int) -> int:
|
2020-11-23 13:24:13 +00:00
|
|
|
ensure(0 <= n <= 0xFFFF_FFFF)
|
2018-08-23 12:39:30 +00:00
|
|
|
w.append(n & 0xFF)
|
|
|
|
w.append((n >> 8) & 0xFF)
|
|
|
|
w.append((n >> 16) & 0xFF)
|
|
|
|
w.append((n >> 24) & 0xFF)
|
|
|
|
return 4
|
|
|
|
|
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
def write_uint32_be(w: Writer, n: int) -> int:
|
2020-11-23 13:24:13 +00:00
|
|
|
ensure(0 <= n <= 0xFFFF_FFFF)
|
2018-08-23 12:39:30 +00:00
|
|
|
w.append((n >> 24) & 0xFF)
|
|
|
|
w.append((n >> 16) & 0xFF)
|
|
|
|
w.append((n >> 8) & 0xFF)
|
|
|
|
w.append(n & 0xFF)
|
|
|
|
return 4
|
|
|
|
|
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
def write_uint64_le(w: Writer, n: int) -> int:
|
2020-11-23 13:24:13 +00:00
|
|
|
ensure(0 <= n <= 0xFFFF_FFFF_FFFF_FFFF)
|
2018-08-23 12:39:30 +00:00
|
|
|
w.append(n & 0xFF)
|
|
|
|
w.append((n >> 8) & 0xFF)
|
|
|
|
w.append((n >> 16) & 0xFF)
|
|
|
|
w.append((n >> 24) & 0xFF)
|
|
|
|
w.append((n >> 32) & 0xFF)
|
|
|
|
w.append((n >> 40) & 0xFF)
|
|
|
|
w.append((n >> 48) & 0xFF)
|
|
|
|
w.append((n >> 56) & 0xFF)
|
|
|
|
return 8
|
|
|
|
|
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
def write_uint64_be(w: Writer, n: int) -> int:
|
2020-11-23 13:24:13 +00:00
|
|
|
ensure(0 <= n <= 0xFFFF_FFFF_FFFF_FFFF)
|
2018-08-23 12:39:30 +00:00
|
|
|
w.append((n >> 56) & 0xFF)
|
|
|
|
w.append((n >> 48) & 0xFF)
|
|
|
|
w.append((n >> 40) & 0xFF)
|
|
|
|
w.append((n >> 32) & 0xFF)
|
|
|
|
w.append((n >> 24) & 0xFF)
|
|
|
|
w.append((n >> 16) & 0xFF)
|
|
|
|
w.append((n >> 8) & 0xFF)
|
|
|
|
w.append(n & 0xFF)
|
|
|
|
return 8
|
|
|
|
|
|
|
|
|
2020-03-16 10:31:11 +00:00
|
|
|
def write_bytes_unchecked(w: Writer, b: bytes) -> int:
|
2018-08-23 12:39:30 +00:00
|
|
|
w.extend(b)
|
|
|
|
return len(b)
|
|
|
|
|
|
|
|
|
2020-03-16 10:31:11 +00:00
|
|
|
def write_bytes_fixed(w: Writer, b: bytes, length: int) -> int:
|
|
|
|
ensure(len(b) == length)
|
|
|
|
w.extend(b)
|
|
|
|
return length
|
|
|
|
|
|
|
|
|
|
|
|
def write_bytes_reversed(w: Writer, b: bytes, length: int) -> int:
|
|
|
|
ensure(len(b) == length)
|
2018-08-23 12:39:30 +00:00
|
|
|
w.extend(bytes(reversed(b)))
|
2020-03-16 10:31:11 +00:00
|
|
|
return length
|
2020-05-15 18:59:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
def write_bitcoin_varint(w: Writer, n: int) -> None:
|
2020-11-23 13:24:13 +00:00
|
|
|
ensure(n >= 0 and n <= 0xFFFF_FFFF)
|
2020-05-15 18:59:06 +00:00
|
|
|
if n < 253:
|
|
|
|
w.append(n & 0xFF)
|
2020-11-23 13:24:13 +00:00
|
|
|
elif n < 0x1_0000:
|
2020-05-15 18:59:06 +00:00
|
|
|
w.append(253)
|
|
|
|
w.append(n & 0xFF)
|
|
|
|
w.append((n >> 8) & 0xFF)
|
|
|
|
else:
|
|
|
|
w.append(254)
|
|
|
|
w.append(n & 0xFF)
|
|
|
|
w.append((n >> 8) & 0xFF)
|
|
|
|
w.append((n >> 16) & 0xFF)
|
|
|
|
w.append((n >> 24) & 0xFF)
|