mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-12-22 14:28:07 +00:00
core/tezos: refactor bytes constants
This commit is contained in:
parent
384275ac73
commit
322417d9ed
@ -12,67 +12,40 @@ TEZOS_PUBLICKEY_PREFIX = "edpk"
|
||||
TEZOS_SIGNATURE_PREFIX = "edsig"
|
||||
TEZOS_PREFIX_BYTES = {
|
||||
# addresses
|
||||
"tz1": [6, 161, 159],
|
||||
"tz2": [6, 161, 161],
|
||||
"tz3": [6, 161, 164],
|
||||
"KT1": [2, 90, 121],
|
||||
"tz1": b"\x06\xa1\x9f", # 06a19f
|
||||
"tz2": b"\x06\xa1\xa1", # 06a1a1
|
||||
"tz3": b"\x06\xa1\xa4", # 06a1a4
|
||||
"KT1": b"\x02Zy", # 025a79
|
||||
# public keys
|
||||
"edpk": [13, 15, 37, 217],
|
||||
"edpk": b"\r\x0f%\xd9", # 0d0f25d9
|
||||
# signatures
|
||||
"edsig": [9, 245, 205, 134, 18],
|
||||
"edsig": b"\t\xf5\xcd\x86\x12", # 09f5cd8612
|
||||
# operation hash
|
||||
"o": [5, 116],
|
||||
"o": b"\x05t", # 0574
|
||||
# protocol hash
|
||||
"P": [2, 170],
|
||||
"P": b"\x02\xaa", # 02aa
|
||||
}
|
||||
|
||||
# MICHELSON instruction bytes
|
||||
MICHELSON_INSTRUCTION_BYTES = {
|
||||
"DROP": [3, 32], # '0320'
|
||||
"NIL": [5, 61], # '053d'
|
||||
"operation": [3, 109], # '036d'
|
||||
"NONE": [5, 62], # '053e'
|
||||
"key_hash": [3, 93], # '035d'
|
||||
"SET_DELEGATE": [3, 78], # '034e'
|
||||
"CONS": [3, 27], # '031b'
|
||||
"IMPLICIT_ACCOUNT": [3, 30], # '031e'
|
||||
"PUSH": [7, 67], # '0743'
|
||||
"mutez": [3, 106], # '036a'
|
||||
"UNIT": [3, 79], # '034f'
|
||||
"TRANSFER_TOKENS": [3, 77], # '034d'
|
||||
"SOME": [3, 70], # '0346'
|
||||
"address": [3, 110], # '036e'
|
||||
"CONTRACT": [5, 85], # '0555'
|
||||
"unit": [3, 108], # '036c'
|
||||
"DROP": b"\x03 ", # 0320
|
||||
"NIL": b"\x05=", # 053d
|
||||
"operation": b"\x03m", # 036d
|
||||
"NONE": b"\x05>", # 053e
|
||||
"key_hash": b"\x03]", # 035d
|
||||
"SET_DELEGATE": b"\x03N", # 034e
|
||||
"CONS": b"\x03\x1b", # 031b
|
||||
"IMPLICIT_ACCOUNT": b"\x03\x1e", # 031e
|
||||
"PUSH": b"\x07C", # 0743
|
||||
"mutez": b"\x03j", # 036a
|
||||
"UNIT": b"\x03O", # 034f
|
||||
"TRANSFER_TOKENS": b"\x03M", # 034d
|
||||
"SOME": b"\x03F", # 0346
|
||||
"address": b"\x03n", # 036e
|
||||
"CONTRACT": b"\x05U", # 0555
|
||||
"unit": b"\x03l", # 036c
|
||||
# ASSERT_SOME unfolded as { IF_NONE { { UNIT ; FAILWITH } } {} }
|
||||
"ASSERT_SOME": [
|
||||
2,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
21,
|
||||
7,
|
||||
47,
|
||||
2,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
9,
|
||||
2,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
4,
|
||||
3,
|
||||
79,
|
||||
3,
|
||||
39,
|
||||
2,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
],
|
||||
# 0200000015072f02000000090200000004034f03270200000000
|
||||
"ASSERT_SOME": b"\x02\x00\x00\x00\x15\x07/\x02\x00\x00\x00\t\x02\x00\x00\x00\x04\x03O\x03'\x02\x00\x00\x00\x00",
|
||||
}
|
||||
|
||||
DO_ENTRYPOINT_TAG = const(2)
|
||||
@ -82,7 +55,7 @@ MICHELSON_SEQUENCE_TAG = const(2)
|
||||
def base58_encode_check(payload, prefix=None):
|
||||
result = payload
|
||||
if prefix is not None:
|
||||
result = bytes(TEZOS_PREFIX_BYTES[prefix]) + payload
|
||||
result = TEZOS_PREFIX_BYTES[prefix] + payload
|
||||
return base58.encode_check(result)
|
||||
|
||||
|
||||
|
@ -295,14 +295,14 @@ def _encode_manager_common(w: bytearray, sequence_length, operation, to_contract
|
||||
write_uint32_be(w, argument_length)
|
||||
write_uint8(w, helpers.MICHELSON_SEQUENCE_TAG)
|
||||
write_uint32_be(w, sequence_length)
|
||||
write_bytes(w, bytes(helpers.MICHELSON_INSTRUCTION_BYTES["DROP"]))
|
||||
write_bytes(w, bytes(helpers.MICHELSON_INSTRUCTION_BYTES["NIL"]))
|
||||
write_bytes(w, bytes(helpers.MICHELSON_INSTRUCTION_BYTES["operation"]))
|
||||
write_bytes(w, bytes(helpers.MICHELSON_INSTRUCTION_BYTES[operation]))
|
||||
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["DROP"])
|
||||
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["NIL"])
|
||||
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["operation"])
|
||||
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES[operation])
|
||||
if to_contract is True:
|
||||
write_bytes(w, bytes(helpers.MICHELSON_INSTRUCTION_BYTES["address"]))
|
||||
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["address"])
|
||||
else:
|
||||
write_bytes(w, bytes(helpers.MICHELSON_INSTRUCTION_BYTES["key_hash"]))
|
||||
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["key_hash"])
|
||||
if operation == "PUSH":
|
||||
write_bytes(w, bytes([10])) # byte sequence
|
||||
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")
|
||||
write_bytes(w, manager_transfer.destination.hash)
|
||||
write_bytes(w, bytes(helpers.MICHELSON_INSTRUCTION_BYTES["IMPLICIT_ACCOUNT"]))
|
||||
write_bytes(w, bytes(helpers.MICHELSON_INSTRUCTION_BYTES["PUSH"]))
|
||||
write_bytes(w, bytes(helpers.MICHELSON_INSTRUCTION_BYTES["mutez"]))
|
||||
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["IMPLICIT_ACCOUNT"])
|
||||
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["PUSH"])
|
||||
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["mutez"])
|
||||
_encode_natural(w, manager_transfer.amount)
|
||||
write_bytes(w, bytes(helpers.MICHELSON_INSTRUCTION_BYTES["UNIT"]))
|
||||
write_bytes(w, bytes(helpers.MICHELSON_INSTRUCTION_BYTES["TRANSFER_TOKENS"]))
|
||||
write_bytes(w, bytes(helpers.MICHELSON_INSTRUCTION_BYTES["CONS"]))
|
||||
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["UNIT"])
|
||||
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["TRANSFER_TOKENS"])
|
||||
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["CONS"])
|
||||
|
||||
|
||||
# smart_contract_delegation
|
||||
@ -335,16 +335,16 @@ def _encode_manager_delegation(w: bytearray, delegate):
|
||||
|
||||
_encode_manager_common(w, MICHELSON_LENGTH, "PUSH")
|
||||
write_bytes(w, delegate)
|
||||
write_bytes(w, bytes(helpers.MICHELSON_INSTRUCTION_BYTES["SOME"]))
|
||||
write_bytes(w, bytes(helpers.MICHELSON_INSTRUCTION_BYTES["SET_DELEGATE"]))
|
||||
write_bytes(w, bytes(helpers.MICHELSON_INSTRUCTION_BYTES["CONS"]))
|
||||
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["SOME"])
|
||||
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["SET_DELEGATE"])
|
||||
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["CONS"])
|
||||
|
||||
|
||||
def _encode_manager_delegation_remove(w: bytearray):
|
||||
MICHELSON_LENGTH = 14 # length is fixed this time(no variable length fields)
|
||||
_encode_manager_common(w, MICHELSON_LENGTH, "NONE")
|
||||
write_bytes(w, bytes(helpers.MICHELSON_INSTRUCTION_BYTES["SET_DELEGATE"]))
|
||||
write_bytes(w, bytes(helpers.MICHELSON_INSTRUCTION_BYTES["CONS"]))
|
||||
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["SET_DELEGATE"])
|
||||
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["CONS"])
|
||||
|
||||
|
||||
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_contract_id(w, manager_transfer.destination)
|
||||
write_bytes(w, bytes(helpers.MICHELSON_INSTRUCTION_BYTES["CONTRACT"]))
|
||||
write_bytes(w, bytes(helpers.MICHELSON_INSTRUCTION_BYTES["unit"]))
|
||||
write_bytes(w, bytes(helpers.MICHELSON_INSTRUCTION_BYTES["ASSERT_SOME"]))
|
||||
write_bytes(w, bytes(helpers.MICHELSON_INSTRUCTION_BYTES["PUSH"]))
|
||||
write_bytes(w, bytes(helpers.MICHELSON_INSTRUCTION_BYTES["mutez"]))
|
||||
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["CONTRACT"])
|
||||
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["unit"])
|
||||
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["ASSERT_SOME"])
|
||||
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["PUSH"])
|
||||
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["mutez"])
|
||||
_encode_natural(w, manager_transfer.amount)
|
||||
write_bytes(w, bytes(helpers.MICHELSON_INSTRUCTION_BYTES["UNIT"]))
|
||||
write_bytes(w, bytes(helpers.MICHELSON_INSTRUCTION_BYTES["TRANSFER_TOKENS"]))
|
||||
write_bytes(w, bytes(helpers.MICHELSON_INSTRUCTION_BYTES["CONS"]))
|
||||
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["UNIT"])
|
||||
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["TRANSFER_TOKENS"])
|
||||
write_bytes(w, helpers.MICHELSON_INSTRUCTION_BYTES["CONS"])
|
||||
|
Loading…
Reference in New Issue
Block a user