2020-07-13 11:57:36 +00:00
|
|
|
from trezor.utils import BufferReader
|
2020-06-24 12:25:48 +00:00
|
|
|
|
|
|
|
|
2020-07-13 11:57:36 +00:00
|
|
|
def read_bitcoin_varint(r: BufferReader) -> int:
|
2020-06-24 12:25:48 +00:00
|
|
|
prefix = r.get()
|
|
|
|
if prefix < 253:
|
|
|
|
n = prefix
|
|
|
|
elif prefix == 253:
|
|
|
|
n = r.get()
|
|
|
|
n += r.get() << 8
|
|
|
|
elif prefix == 254:
|
|
|
|
n = r.get()
|
|
|
|
n += r.get() << 8
|
|
|
|
n += r.get() << 16
|
|
|
|
n += r.get() << 24
|
|
|
|
else:
|
|
|
|
raise ValueError
|
|
|
|
return n
|
2021-01-14 18:58:18 +00:00
|
|
|
|
|
|
|
|
2021-02-05 18:51:01 +00:00
|
|
|
def read_uint16_be(r: BufferReader) -> int:
|
|
|
|
n = r.get()
|
|
|
|
return (n << 8) + r.get()
|
|
|
|
|
|
|
|
|
2021-01-14 18:58:18 +00:00
|
|
|
def read_uint32_be(r: BufferReader) -> int:
|
2021-02-05 18:51:01 +00:00
|
|
|
n = r.get()
|
|
|
|
for _ in range(3):
|
|
|
|
n = (n << 8) + r.get()
|
|
|
|
return n
|
|
|
|
|
|
|
|
|
|
|
|
def read_uint64_be(r: BufferReader) -> int:
|
|
|
|
n = r.get()
|
|
|
|
for _ in range(7):
|
|
|
|
n = (n << 8) + r.get()
|
2021-01-14 18:58:18 +00:00
|
|
|
return n
|