You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
246 lines
7.7 KiB
246 lines
7.7 KiB
5 years ago
|
from micropython import const
|
||
|
|
||
6 years ago
|
from trezor import wire
|
||
|
from trezor.crypto import hashlib
|
||
6 years ago
|
from trezor.crypto.curve import ed25519
|
||
6 years ago
|
from trezor.messages import TezosContractType
|
||
|
from trezor.messages.TezosSignedTx import TezosSignedTx
|
||
|
|
||
6 years ago
|
from apps.common import paths
|
||
5 years ago
|
from apps.tezos import CURVE, helpers, layout
|
||
5 years ago
|
from apps.tezos.writers import (
|
||
|
write_bool,
|
||
|
write_bytes,
|
||
|
write_uint8,
|
||
|
write_uint16,
|
||
|
write_uint32,
|
||
|
write_uint64,
|
||
|
)
|
||
|
|
||
|
PROPOSAL_LENGTH = const(32)
|
||
6 years ago
|
|
||
|
|
||
6 years ago
|
async def sign_tx(ctx, msg, keychain):
|
||
5 years ago
|
await paths.validate_path(
|
||
|
ctx, helpers.validate_full_path, keychain, msg.address_n, CURVE
|
||
|
)
|
||
6 years ago
|
|
||
5 years ago
|
node = keychain.derive(msg.address_n, CURVE)
|
||
6 years ago
|
|
||
|
if msg.transaction is not None:
|
||
|
to = _get_address_from_contract(msg.transaction.destination)
|
||
6 years ago
|
await layout.require_confirm_tx(ctx, to, msg.transaction.amount)
|
||
|
await layout.require_confirm_fee(
|
||
|
ctx, msg.transaction.amount, msg.transaction.fee
|
||
|
)
|
||
6 years ago
|
|
||
|
elif msg.origination is not None:
|
||
|
source = _get_address_from_contract(msg.origination.source)
|
||
6 years ago
|
await layout.require_confirm_origination(ctx, source)
|
||
6 years ago
|
|
||
|
# if we are immediately delegating contract
|
||
|
if msg.origination.delegate is not None:
|
||
|
delegate = _get_address_by_tag(msg.origination.delegate)
|
||
6 years ago
|
await layout.require_confirm_delegation_baker(ctx, delegate)
|
||
6 years ago
|
|
||
6 years ago
|
await layout.require_confirm_origination_fee(
|
||
6 years ago
|
ctx, msg.origination.balance, msg.origination.fee
|
||
|
)
|
||
|
|
||
|
elif msg.delegation is not None:
|
||
|
source = _get_address_from_contract(msg.delegation.source)
|
||
|
|
||
6 years ago
|
delegate = None
|
||
|
if msg.delegation.delegate is not None:
|
||
|
delegate = _get_address_by_tag(msg.delegation.delegate)
|
||
|
|
||
|
if delegate is not None and source != delegate:
|
||
6 years ago
|
await layout.require_confirm_delegation_baker(ctx, delegate)
|
||
|
await layout.require_confirm_set_delegate(ctx, msg.delegation.fee)
|
||
6 years ago
|
# if account registers itself as a delegate
|
||
|
else:
|
||
6 years ago
|
await layout.require_confirm_register_delegate(
|
||
|
ctx, source, msg.delegation.fee
|
||
|
)
|
||
6 years ago
|
|
||
5 years ago
|
elif msg.proposal is not None:
|
||
|
proposed_protocols = _get_protocol_hash_from_msg(msg.proposal.proposals)
|
||
|
|
||
|
# byte count larger than PROPOSAL_LENGTH indicates more than 1 proposal, use paginated screen
|
||
|
if msg.proposal.bytes_in_next_field > PROPOSAL_LENGTH:
|
||
|
await layout.show_proposals(ctx, proposed_protocols)
|
||
|
else:
|
||
|
await layout.require_confirm_proposal(ctx, proposed_protocols)
|
||
|
|
||
|
elif msg.ballot is not None:
|
||
|
proposed_protocol = _get_protocol_hash_from_msg(msg.ballot.proposal)
|
||
|
submitted_ballot = _get_ballot(msg.ballot.ballot)
|
||
|
await layout.require_confirm_ballot(ctx, proposed_protocol, submitted_ballot)
|
||
|
|
||
6 years ago
|
else:
|
||
|
raise wire.DataError("Invalid operation")
|
||
|
|
||
6 years ago
|
w = bytearray()
|
||
|
_get_operation_bytes(w, msg)
|
||
|
|
||
|
opbytes = bytes(w)
|
||
6 years ago
|
|
||
6 years ago
|
# watermark 0x03 is prefix for transactions, delegations, originations, reveals...
|
||
6 years ago
|
watermark = bytes([3])
|
||
|
wm_opbytes = watermark + opbytes
|
||
|
wm_opbytes_hash = hashlib.blake2b(wm_opbytes, outlen=32).digest()
|
||
|
|
||
6 years ago
|
signature = ed25519.sign(node.private_key(), wm_opbytes_hash)
|
||
6 years ago
|
|
||
|
sig_op_contents = opbytes + signature
|
||
|
sig_op_contents_hash = hashlib.blake2b(sig_op_contents, outlen=32).digest()
|
||
6 years ago
|
ophash = helpers.base58_encode_check(sig_op_contents_hash, prefix="o")
|
||
6 years ago
|
|
||
6 years ago
|
sig_prefixed = helpers.base58_encode_check(
|
||
|
signature, prefix=helpers.TEZOS_SIGNATURE_PREFIX
|
||
|
)
|
||
6 years ago
|
|
||
|
return TezosSignedTx(
|
||
|
signature=sig_prefixed, sig_op_contents=sig_op_contents, operation_hash=ophash
|
||
|
)
|
||
|
|
||
|
|
||
|
def _get_address_by_tag(address_hash):
|
||
6 years ago
|
prefixes = ["tz1", "tz2", "tz3"]
|
||
6 years ago
|
tag = int(address_hash[0])
|
||
6 years ago
|
|
||
|
if 0 <= tag < len(prefixes):
|
||
6 years ago
|
return helpers.base58_encode_check(address_hash[1:], prefix=prefixes[tag])
|
||
6 years ago
|
raise wire.DataError("Invalid tag in address hash")
|
||
6 years ago
|
|
||
|
|
||
|
def _get_address_from_contract(address):
|
||
|
if address.tag == TezosContractType.Implicit:
|
||
|
return _get_address_by_tag(address.hash)
|
||
|
|
||
|
elif address.tag == TezosContractType.Originated:
|
||
6 years ago
|
return helpers.base58_encode_check(
|
||
|
address.hash[:-1], prefix=helpers.TEZOS_ORIGINATED_ADDRESS_PREFIX
|
||
6 years ago
|
)
|
||
6 years ago
|
|
||
|
raise wire.DataError("Invalid contract type")
|
||
|
|
||
|
|
||
5 years ago
|
def _get_protocol_hash_from_msg(proposals):
|
||
|
# split the proposals
|
||
|
proposal_list = list(
|
||
|
[
|
||
|
proposals[i : i + PROPOSAL_LENGTH]
|
||
|
for i in range(0, len(proposals), PROPOSAL_LENGTH)
|
||
|
]
|
||
|
)
|
||
|
return [
|
||
|
helpers.base58_encode_check(proposal, prefix="P") for proposal in proposal_list
|
||
|
]
|
||
|
|
||
|
|
||
|
def _get_ballot(encoded_ballot):
|
||
|
encoded_ballot = int(encoded_ballot[0])
|
||
|
if encoded_ballot == 0:
|
||
|
return "yay"
|
||
|
elif encoded_ballot == 1:
|
||
|
return "nay"
|
||
|
elif encoded_ballot == 2:
|
||
|
return "pass"
|
||
|
|
||
|
|
||
6 years ago
|
def _get_operation_bytes(w: bytearray, msg):
|
||
|
write_bytes(w, msg.branch)
|
||
6 years ago
|
|
||
|
# when the account sends first operation in lifetime,
|
||
5 years ago
|
# we need to reveal its public key
|
||
6 years ago
|
if msg.reveal is not None:
|
||
6 years ago
|
_encode_common(w, msg.reveal, "reveal")
|
||
|
write_bytes(w, msg.reveal.public_key)
|
||
6 years ago
|
|
||
|
# transaction operation
|
||
|
if msg.transaction is not None:
|
||
6 years ago
|
_encode_common(w, msg.transaction, "transaction")
|
||
|
_encode_zarith(w, msg.transaction.amount)
|
||
|
_encode_contract_id(w, msg.transaction.destination)
|
||
|
_encode_data_with_bool_prefix(w, msg.transaction.parameters)
|
||
6 years ago
|
# origination operation
|
||
|
elif msg.origination is not None:
|
||
6 years ago
|
_encode_common(w, msg.origination, "origination")
|
||
|
write_bytes(w, msg.origination.manager_pubkey)
|
||
|
_encode_zarith(w, msg.origination.balance)
|
||
|
_encode_bool(w, msg.origination.spendable)
|
||
|
_encode_bool(w, msg.origination.delegatable)
|
||
|
_encode_data_with_bool_prefix(w, msg.origination.delegate)
|
||
|
_encode_data_with_bool_prefix(w, msg.origination.script)
|
||
6 years ago
|
# delegation operation
|
||
|
elif msg.delegation is not None:
|
||
6 years ago
|
_encode_common(w, msg.delegation, "delegation")
|
||
|
_encode_data_with_bool_prefix(w, msg.delegation.delegate)
|
||
5 years ago
|
elif msg.proposal is not None:
|
||
|
_encode_proposal(w, msg.proposal)
|
||
|
elif msg.ballot is not None:
|
||
|
_encode_ballot(w, msg.ballot)
|
||
6 years ago
|
|
||
|
|
||
6 years ago
|
def _encode_common(w: bytearray, operation, str_operation):
|
||
6 years ago
|
operation_tags = {"reveal": 7, "transaction": 8, "origination": 9, "delegation": 10}
|
||
6 years ago
|
write_uint8(w, operation_tags[str_operation])
|
||
|
_encode_contract_id(w, operation.source)
|
||
|
_encode_zarith(w, operation.fee)
|
||
|
_encode_zarith(w, operation.counter)
|
||
|
_encode_zarith(w, operation.gas_limit)
|
||
|
_encode_zarith(w, operation.storage_limit)
|
||
6 years ago
|
|
||
|
|
||
6 years ago
|
def _encode_contract_id(w: bytearray, contract_id):
|
||
|
write_uint8(w, contract_id.tag)
|
||
|
write_bytes(w, contract_id.hash)
|
||
6 years ago
|
|
||
|
|
||
6 years ago
|
def _encode_bool(w: bytearray, boolean):
|
||
|
if boolean:
|
||
|
write_uint8(w, 255)
|
||
|
else:
|
||
|
write_uint8(w, 0)
|
||
6 years ago
|
|
||
|
|
||
6 years ago
|
def _encode_data_with_bool_prefix(w: bytearray, data):
|
||
|
if data:
|
||
|
_encode_bool(w, True)
|
||
|
write_bytes(w, data)
|
||
|
else:
|
||
|
_encode_bool(w, False)
|
||
6 years ago
|
|
||
|
|
||
6 years ago
|
def _encode_zarith(w: bytearray, num):
|
||
6 years ago
|
while True:
|
||
|
byte = num & 127
|
||
|
num = num >> 7
|
||
|
|
||
|
if num == 0:
|
||
6 years ago
|
write_uint8(w, byte)
|
||
6 years ago
|
break
|
||
|
|
||
6 years ago
|
write_uint8(w, 128 | byte)
|
||
5 years ago
|
|
||
|
|
||
|
def _encode_proposal(w: bytearray, proposal):
|
||
|
proposal_tag = 5
|
||
|
|
||
|
write_uint8(w, proposal_tag)
|
||
|
write_bytes(w, proposal.source)
|
||
|
write_uint32(w, proposal.period)
|
||
|
write_uint32(w, proposal.bytes_in_next_field)
|
||
|
write_bytes(w, proposal.proposals)
|
||
|
|
||
|
|
||
|
def _encode_ballot(w: bytearray, ballot):
|
||
|
ballot_tag = 6
|
||
|
|
||
|
write_uint8(w, ballot_tag)
|
||
|
write_bytes(w, ballot.source)
|
||
|
write_uint32(w, ballot.period)
|
||
|
write_bytes(w, ballot.proposal)
|
||
|
write_bytes(w, ballot.ballot)
|