1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-23 06:48:16 +00:00

core/tezos: factor out writing Michelson instructions

This commit is contained in:
matejcik 2020-03-16 11:29:06 +01:00 committed by Tomas Susanka
parent 694f714719
commit 9a5f6b025a
2 changed files with 30 additions and 26 deletions

View File

@ -3,7 +3,7 @@ from micropython import const
from trezor.crypto import base58 from trezor.crypto import base58
from apps.common import HARDENED from apps.common import HARDENED
from apps.common.writers import write_uint8 from apps.common.writers import write_uint8, write_bytes
TEZOS_AMOUNT_DECIMALS = const(6) TEZOS_AMOUNT_DECIMALS = const(6)
TEZOS_ED25519_ADDRESS_PREFIX = "tz1" TEZOS_ED25519_ADDRESS_PREFIX = "tz1"
@ -96,3 +96,7 @@ def write_bool(w: bytearray, boolean: bool):
write_uint8(w, 255) write_uint8(w, 255)
else: else:
write_uint8(w, 0) write_uint8(w, 0)
def write_instruction(w: bytearray, instruction: str) -> int:
write_bytes(w, MICHELSON_INSTRUCTION_BYTES[instruction])

View File

@ -295,14 +295,14 @@ def _encode_manager_common(w: bytearray, sequence_length, operation, to_contract
write_uint32_be(w, argument_length) write_uint32_be(w, argument_length)
write_uint8(w, helpers.MICHELSON_SEQUENCE_TAG) write_uint8(w, helpers.MICHELSON_SEQUENCE_TAG)
write_uint32_be(w, sequence_length) write_uint32_be(w, sequence_length)
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["DROP"]) helpers.write_instruction(w, "DROP")
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["NIL"]) helpers.write_instruction(w, "NIL")
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["operation"]) helpers.write_instruction(w, "operation")
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES[operation]) helpers.write_instruction(w, operation)
if to_contract is True: if to_contract is True:
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["address"]) helpers.write_instruction(w, "address")
else: else:
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["key_hash"]) helpers.write_instruction(w, "key_hash")
if operation == "PUSH": if operation == "PUSH":
write_bytes(w, bytes([10])) # byte sequence write_bytes(w, bytes([10])) # byte sequence
if to_contract is True: if to_contract is True:
@ -320,13 +320,13 @@ def _encode_manager_to_implicit_transfer(w: bytearray, manager_transfer):
_encode_manager_common(w, sequence_length, "PUSH") _encode_manager_common(w, sequence_length, "PUSH")
write_bytes(w, manager_transfer.destination.hash) write_bytes(w, manager_transfer.destination.hash)
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["IMPLICIT_ACCOUNT"]) helpers.write_instruction(w, "IMPLICIT_ACCOUNT")
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["PUSH"]) helpers.write_instruction(w, "PUSH")
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["mutez"]) helpers.write_instruction(w, "mutez")
_encode_natural(w, manager_transfer.amount) _encode_natural(w, manager_transfer.amount)
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["UNIT"]) helpers.write_instruction(w, "UNIT")
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["TRANSFER_TOKENS"]) helpers.write_instruction(w, "TRANSFER_TOKENS")
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["CONS"]) helpers.write_instruction(w, "CONS")
# smart_contract_delegation # smart_contract_delegation
@ -335,16 +335,16 @@ def _encode_manager_delegation(w: bytearray, delegate):
_encode_manager_common(w, MICHELSON_LENGTH, "PUSH") _encode_manager_common(w, MICHELSON_LENGTH, "PUSH")
write_bytes(w, delegate) write_bytes(w, delegate)
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["SOME"]) helpers.write_instruction(w, "SOME")
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["SET_DELEGATE"]) helpers.write_instruction(w, "SET_DELEGATE")
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["CONS"]) helpers.write_instruction(w, "CONS")
def _encode_manager_delegation_remove(w: bytearray): def _encode_manager_delegation_remove(w: bytearray):
MICHELSON_LENGTH = 14 # length is fixed this time(no variable length fields) MICHELSON_LENGTH = 14 # length is fixed this time(no variable length fields)
_encode_manager_common(w, MICHELSON_LENGTH, "NONE") _encode_manager_common(w, MICHELSON_LENGTH, "NONE")
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["SET_DELEGATE"]) helpers.write_instruction(w, "SET_DELEGATE")
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["CONS"]) helpers.write_instruction(w, "CONS")
def _encode_manager_to_manager_transfer(w: bytearray, manager_transfer): def _encode_manager_to_manager_transfer(w: bytearray, manager_transfer):
@ -356,12 +356,12 @@ def _encode_manager_to_manager_transfer(w: bytearray, manager_transfer):
_encode_manager_common(w, sequence_length, "PUSH", to_contract=True) _encode_manager_common(w, sequence_length, "PUSH", to_contract=True)
_encode_contract_id(w, manager_transfer.destination) _encode_contract_id(w, manager_transfer.destination)
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["CONTRACT"]) helpers.write_instruction(w, "CONTRACT")
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["unit"]) helpers.write_instruction(w, "unit")
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["ASSERT_SOME"]) helpers.write_instruction(w, "ASSERT_SOME")
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["PUSH"]) helpers.write_instruction(w, "PUSH")
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["mutez"]) helpers.write_instruction(w, "mutez")
_encode_natural(w, manager_transfer.amount) _encode_natural(w, manager_transfer.amount)
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["UNIT"]) helpers.write_instruction(w, "UNIT")
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["TRANSFER_TOKENS"]) helpers.write_instruction(w, "TRANSFER_TOKENS")
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["CONS"]) helpers.write_instruction(w, "CONS")