1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-15 19:08:07 +00:00
trezor-firmware/tests/test_apps.tezos.encode.py
Adrian Matejov 8b252bff80 tezos: fixed nitpicks
Signed-off-by: Adrian Matejov <adrian.matejov@simplestaking.com>
2018-09-13 15:06:03 +02:00

101 lines
3.3 KiB
Python

from ubinascii import unhexlify
from common import *
from trezor.messages import TezosContractType
from trezor.messages.TezosContractID import TezosContractID
from apps.tezos.helpers import base58_decode_check, base58_encode_check
from apps.tezos.sign_tx import (
_encode_bool,
_encode_contract_id,
_encode_data_with_bool_prefix,
_encode_zarith,
)
class TestTezosEncoding(unittest.TestCase):
def test_tezos_encode_zarith(self):
inputs = [2000000, 159066, 200, 60000, 157000000, 0]
outputs = ["80897a", "dada09", "c801", "e0d403", "c0c2ee4a", "00"]
for i, o in zip(inputs, outputs):
w = bytearray()
_encode_zarith(w, i)
self.assertEqual(bytes(w), unhexlify(o))
def test_tezos_encode_data_with_bool_prefix(self):
w = bytearray()
_encode_data_with_bool_prefix(w, None)
self.assertEqual(bytes(w), bytes([0]))
data = "afffeb1dc3c0"
w = bytearray()
_encode_data_with_bool_prefix(w, unhexlify(data))
self.assertEqual(bytes(w), unhexlify("ff" + data))
def test_tezos_encode_bool(self):
w = bytearray()
_encode_bool(w, True)
self.assertEqual(bytes(w), bytes([255]))
w = bytearray()
_encode_bool(w, False)
self.assertEqual(bytes(w), bytes([0]))
def test_tezos_encode_contract_id(self):
implicit = TezosContractID(
tag=TezosContractType.Implicit,
hash=unhexlify("00101368afffeb1dc3c089facbbe23f5c30b787ce9"),
)
w = bytearray()
_encode_contract_id(w, implicit)
self.assertEqual(
bytes(w), unhexlify("0000101368afffeb1dc3c089facbbe23f5c30b787ce9")
)
originated = TezosContractID(
tag=TezosContractType.Originated,
hash=unhexlify("65671dedc69669f066f45d586a2ecdeddacc95af00"),
)
w = bytearray()
_encode_contract_id(w, originated)
self.assertEqual(
bytes(w), unhexlify("0165671dedc69669f066f45d586a2ecdeddacc95af00")
)
def test_tezos_base58_encode_check(self):
pkh = unhexlify("101368afffeb1dc3c089facbbe23f5c30b787ce9")
self.assertEqual(
base58_encode_check(pkh, prefix="tz1"),
"tz1M72kkAJrntPtayM4yU4CCwQPLSdpEgRrn",
)
self.assertEqual(
base58_encode_check(pkh, prefix="tz2"),
"tz29nEixktH9p9XTFX7p8hATUyeLxXEz96KR",
)
self.assertEqual(
base58_encode_check(pkh, prefix="tz3"),
"tz3Mo3gHekQhCmykfnC58ecqJLXrjMKzkF2Q",
)
self.assertEqual(base58_encode_check(pkh), "2U14dJ6ED97bBHDZTQWA6umVL8SAVefXj")
def test_tezos_base58_decode_check(self):
pkh = unhexlify("101368afffeb1dc3c089facbbe23f5c30b787ce9")
address = "tz1M72kkAJrntPtayM4yU4CCwQPLSdpEgRrn"
self.assertEqual(base58_decode_check(address, prefix="tz1"), pkh)
address = "tz29nEixktH9p9XTFX7p8hATUyeLxXEz96KR"
self.assertEqual(base58_decode_check(address, prefix="tz2"), pkh)
address = "tz3Mo3gHekQhCmykfnC58ecqJLXrjMKzkF2Q"
self.assertEqual(base58_decode_check(address, prefix="tz3"), pkh)
address = "2U14dJ6ED97bBHDZTQWA6umVL8SAVefXj"
self.assertEqual(base58_decode_check(address), pkh)
if __name__ == "__main__":
unittest.main()