Skip to content

Commit

Permalink
Use enums from protos
Browse files Browse the repository at this point in the history
  • Loading branch information
OBorce committed Dec 13, 2024
1 parent c929c9a commit 6e03e86
Show file tree
Hide file tree
Showing 19 changed files with 516 additions and 147 deletions.
2 changes: 1 addition & 1 deletion common/protob/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
check: messages.pb messages-binance.pb messages-bitcoin.pb messages-bootloader.pb messages-cardano.pb messages-common.pb messages-crypto.pb messages-debug.pb messages-ethereum.pb messages-management.pb messages-monero.pb messages-nem.pb messages-ripple.pb messages-stellar.pb messages-tezos.pb messages-eos.pb messages-mintlayer.pb
check: messages.pb messages-binance.pb messages-bitcoin.pb messages-bootloader.pb messages-cardano.pb messages-common.pb messages-crypto.pb messages-debug.pb messages-ethereum.pb messages-management.pb messages-monero.pb messages-nem.pb messages-ripple.pb messages-stellar.pb messages-tezos.pb messages-eos.pb messages-mintlayer.pb messages-mintlayer-enums.proto

%.pb: %.proto
protoc -I/usr/include -I. $< -o $@
Expand Down
31 changes: 31 additions & 0 deletions common/protob/messages-mintlayer-enums.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
syntax = "proto2";
package hw.trezor.messages.mintlayer;

// Sugar for easier handling in Java
option java_package = "com.satoshilabs.trezor.lib.protobuf";
option java_outer_classname = "TrezorMessageMintlayer";

/**
* Enum for Account Command type passed between the Python and Rust code
*/
enum MintlayerAccountCommandType {
MINT_TOKENS = 0;
UNMINT_TOKENS = 1;
LOCK_TOKEN_SUPPLY = 2;
FREEZE_TOKEN = 3;
UNFREEZE_TOKEN = 4;
CHANGE_TOKEN_AUTHORITY = 5;
CONCLUDE_ORDER = 6;
FILL_ORDER = 7;
CHANGE_TOKEN_METADATA_URI = 8;
}

/**
* Enum for Output time lock type passed between the Python and Rust code
*/
enum MintlayerOutputTimeLockType {
UNTIL_HEIGHT = 0;
UNTIL_TIME = 1;
FOR_BLOCK_COUNT = 2;
FOR_SECONDS = 3;
}
18 changes: 9 additions & 9 deletions common/protob/messages.proto
Original file line number Diff line number Diff line change
Expand Up @@ -377,13 +377,13 @@ enum MessageType {
MessageType_SolanaTxSignature = 905 [(wire_out) = true];

// Mintlayer
MessageType_MintlayerGetAddress = 1000 [(wire_in) = true];
MessageType_MintlayerAddress = 1001 [(wire_out) = true];
MessageType_MintlayerGetPublicKey = 1002 [(wire_in) = true];
MessageType_MintlayerPublicKey = 1003 [(wire_out) = true];
MessageType_MintlayerSignMessage = 1004 [(wire_in) = true];
MessageType_MintlayerSignTx = 1005 [(wire_in) = true];
MessageType_MintlayerTxRequest = 1006 [(wire_out) = true];
MessageType_MintlayerTxAckUtxoInput = 1007 [(wire_in) = true];
MessageType_MintlayerTxAckOutput = 1008 [(wire_in) = true];
MessageType_MintlayerGetAddress = 10000 [(wire_in) = true];
MessageType_MintlayerAddress = 10001 [(wire_out) = true];
MessageType_MintlayerGetPublicKey = 10002 [(wire_in) = true];
MessageType_MintlayerPublicKey = 10003 [(wire_out) = true];
MessageType_MintlayerSignMessage = 10004 [(wire_in) = true];
MessageType_MintlayerSignTx = 10005 [(wire_in) = true];
MessageType_MintlayerTxRequest = 10006 [(wire_out) = true];
MessageType_MintlayerTxAckUtxoInput = 10007 [(wire_in) = true];
MessageType_MintlayerTxAckOutput = 10008 [(wire_in) = true];
}
41 changes: 38 additions & 3 deletions core/embed/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 29 additions & 30 deletions core/embed/rust/src/mintlayer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use core::{
};

use ml_common::{
AccountCommand, AccountCommandIndex, AccountOutPoint, AccountSpending, Amount, Destination,
AccountCommand, AccountCommandTag, AccountOutPoint, AccountSpending, Amount, Destination,
HashedTimelockContract, HtlcSecretHash, IsTokenFreezable, IsTokenUnfreezable, Metadata,
NftIssuance, NftIssuanceV0, OrderData, OutPointSourceId, OutPointSourceIdIndex, OutputTimeLock,
OutputTimeLockIndex, OutputValue, PublicKey, PublicKeyHolder, StakePoolData, TokenIssuance,
TokenIssuanceV1, TokenTotalSupply, TokenTotalSupplyIndex, TxInput, TxOutput, UtxoOutPoint,
NftIssuance, NftIssuanceV0, OrderData, OutPointSourceId, OutPointSourceIdTag, OutputTimeLock,
OutputTimeLockTag, OutputValue, PublicKey, PublicKeyHolder, StakePoolData, TokenIssuance,
TokenIssuanceV1, TokenTotalSupply, TokenTotalSupplyTag, TxInput, TxOutput, UtxoOutPoint,
VRFPublicKeyHolder, H256,
};
use num_traits::FromPrimitive;
Expand Down Expand Up @@ -73,11 +73,11 @@ fn mintlayer_encode_utxo_input_impl(
hash.try_into()
.map_err(|_| MintlayerErrorCode::WrongHashSize)?,
);
let outpoint = match OutPointSourceIdIndex::from_u32(utxo_type)
let outpoint = match OutPointSourceIdTag::from_u32(utxo_type)
.ok_or(MintlayerErrorCode::InvalidUtxoType)?
{
OutPointSourceIdIndex::Transaction => OutPointSourceId::Transaction(hash),
OutPointSourceIdIndex::BlockReward => OutPointSourceId::BlockReward(hash),
OutPointSourceIdTag::Transaction => OutPointSourceId::Transaction(hash),
OutPointSourceIdTag::BlockReward => OutPointSourceId::BlockReward(hash),
};
let utxo_outpoint = UtxoOutPoint::new(outpoint, index);
let tx_input = TxInput::Utxo(utxo_outpoint);
Expand Down Expand Up @@ -146,28 +146,28 @@ fn mintlayer_encode_token_account_command_input_impl(
Ok(hash) => hash,
Err(_) => return Err(MintlayerErrorCode::WrongHashSize),
});
let account_command = match AccountCommandIndex::from_u32(command)
let account_command = match AccountCommandTag::from_u32(command)
.ok_or(MintlayerErrorCode::InvalidAccountCommand)?
{
AccountCommandIndex::MintTokens => {
AccountCommandTag::MintTokens => {
let amount =
Amount::from_bytes_be(data.as_ref()).ok_or(MintlayerErrorCode::InvalidAmount)?;
AccountCommand::MintTokens(token_id, amount)
}
AccountCommandIndex::UnmintTokens => AccountCommand::UnmintTokens(token_id),
AccountCommandIndex::LockTokenSupply => AccountCommand::LockTokenSupply(token_id),
AccountCommandIndex::FreezeToken => {
AccountCommandTag::UnmintTokens => AccountCommand::UnmintTokens(token_id),
AccountCommandTag::LockTokenSupply => AccountCommand::LockTokenSupply(token_id),
AccountCommandTag::FreezeToken => {
let is_token_unfreezabe = IsTokenUnfreezable::decode_all(&mut data.as_ref())
.map_err(|_| MintlayerErrorCode::InvalidIsTokenUnfreezable)?;
AccountCommand::FreezeToken(token_id, is_token_unfreezabe)
}
AccountCommandIndex::UnfreezeToken => AccountCommand::UnfreezeToken(token_id),
AccountCommandIndex::ChangeTokenAuthority => {
AccountCommandTag::UnfreezeToken => AccountCommand::UnfreezeToken(token_id),
AccountCommandTag::ChangeTokenAuthority => {
let destination = Destination::decode_all(&mut data.as_ref())
.map_err(|_| MintlayerErrorCode::InvalidDestination)?;
AccountCommand::ChangeTokenAuthority(token_id, destination)
}
AccountCommandIndex::ChangeTokenMetadataUri => {
AccountCommandTag::ChangeTokenMetadataUri => {
AccountCommand::ChangeTokenMetadataUri(token_id, data.to_vec())
}
_ => return Err(MintlayerErrorCode::InvalidAccountCommand),
Expand Down Expand Up @@ -325,12 +325,11 @@ extern "C" fn mintlayer_encode_lock_then_transfer_output(
Ok(destination) => destination,
Err(_) => return MintlayerErrorCode::InvalidDestination.into(),
};

let lock = match OutputTimeLockIndex::from_u8(lock_type) {
Some(OutputTimeLockIndex::UntilHeight) => OutputTimeLock::UntilHeight(lock_amount),
Some(OutputTimeLockIndex::UntilTime) => OutputTimeLock::UntilTime(lock_amount),
Some(OutputTimeLockIndex::ForBlockCount) => OutputTimeLock::ForBlockCount(lock_amount),
Some(OutputTimeLockIndex::ForSeconds) => OutputTimeLock::ForSeconds(lock_amount),
let lock = match OutputTimeLockTag::from_u8(lock_type) {
Some(OutputTimeLockTag::UntilHeight) => OutputTimeLock::UntilHeight(lock_amount),
Some(OutputTimeLockTag::UntilTime) => OutputTimeLock::UntilTime(lock_amount),
Some(OutputTimeLockTag::ForBlockCount) => OutputTimeLock::ForBlockCount(lock_amount),
Some(OutputTimeLockTag::ForSeconds) => OutputTimeLock::ForSeconds(lock_amount),
None => return MintlayerErrorCode::InvalidOutputTimeLock.into(),
};

Expand Down Expand Up @@ -579,16 +578,16 @@ fn mintlayer_encode_issue_fungible_token_output_impl(
.map_err(|_| MintlayerErrorCode::InvalidDestination)?;
let is_freezable = IsTokenFreezable::from_u8(is_freezable)
.ok_or(MintlayerErrorCode::InvalidIsTokenFreezable)?;
let total_supply = match TokenTotalSupplyIndex::from_u32(total_supply_type)
let total_supply = match TokenTotalSupplyTag::from_u32(total_supply_type)
.ok_or(MintlayerErrorCode::InvalidTokenTotalSupply)?
{
TokenTotalSupplyIndex::Fixed => {
TokenTotalSupplyTag::Fixed => {
let amount = Amount::from_bytes_be(coin_amount.as_ref())
.ok_or(MintlayerErrorCode::InvalidAmount)?;
TokenTotalSupply::Fixed(amount)
}
TokenTotalSupplyIndex::Lockable => TokenTotalSupply::Lockable,
TokenTotalSupplyIndex::Unlimited => TokenTotalSupply::Unlimited,
TokenTotalSupplyTag::Lockable => TokenTotalSupply::Lockable,
TokenTotalSupplyTag::Unlimited => TokenTotalSupply::Unlimited,
};
let issuance = TokenIssuance::V1(TokenIssuanceV1 {
token_ticker,
Expand Down Expand Up @@ -794,13 +793,13 @@ fn mintlayer_encode_htlc_output_impl(
hash.try_into()
.map_err(|_| MintlayerErrorCode::WrongHashSize)?,
);
let refund_timelock = match OutputTimeLockIndex::from_u8(lock_type)
let refund_timelock = match OutputTimeLockTag::from_u8(lock_type)
.ok_or(MintlayerErrorCode::InvalidOutputTimeLock)?
{
OutputTimeLockIndex::UntilHeight => OutputTimeLock::UntilHeight(lock_amount),
OutputTimeLockIndex::UntilTime => OutputTimeLock::UntilTime(lock_amount),
OutputTimeLockIndex::ForBlockCount => OutputTimeLock::ForBlockCount(lock_amount),
OutputTimeLockIndex::ForSeconds => OutputTimeLock::ForSeconds(lock_amount),
OutputTimeLockTag::UntilHeight => OutputTimeLock::UntilHeight(lock_amount),
OutputTimeLockTag::UntilTime => OutputTimeLock::UntilTime(lock_amount),
OutputTimeLockTag::ForBlockCount => OutputTimeLock::ForBlockCount(lock_amount),
OutputTimeLockTag::ForSeconds => OutputTimeLock::ForSeconds(lock_amount),
};
let txo = TxOutput::Htlc(
value,
Expand Down
4 changes: 4 additions & 0 deletions core/src/all_modules.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions core/src/apps/mintlayer/sign_tx/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import TYPE_CHECKING, Tuple

from trezor import utils
from trezor.enums import MintlayerRequestType
from trezor.enums import MintlayerOutputTimeLockType, MintlayerRequestType
from trezor.wire import DataError

from . import layout
Expand Down Expand Up @@ -289,18 +289,18 @@ def _sanitize_tx_output(txo: MintlayerTxOutput) -> MintlayerTxOutput:
return txo


def get_lock(x: MintlayerOutputTimeLock) -> Tuple[int, int]:
def get_lock(x: MintlayerOutputTimeLock) -> Tuple[MintlayerOutputTimeLockType, int]:
if x.until_height:
lock_type = 0
lock_type = MintlayerOutputTimeLockType.UNTIL_HEIGHT
lock_amount = x.until_height
elif x.until_time:
lock_type = 1
lock_type = MintlayerOutputTimeLockType.UNTIL_TIME
lock_amount = x.until_time
elif x.for_block_count:
lock_type = 2
lock_type = MintlayerOutputTimeLockType.FOR_BLOCK_COUNT
lock_amount = x.for_block_count
elif x.for_seconds:
lock_type = 3
lock_type = MintlayerOutputTimeLockType.FOR_SECONDS
lock_amount = x.for_seconds
else:
raise DataError("unhandled mintlayer lock type")
Expand Down
21 changes: 11 additions & 10 deletions core/src/apps/mintlayer/sign_tx/signer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from trezor.crypto.bech32 import mintlayer_decode
from trezor.crypto.curve import bip340
from trezor.crypto.hashlib import blake2b
from trezor.enums import MintlayerAccountCommandType
from trezor.messages import (
MintlayerSignature,
MintlayerSignatures,
Expand Down Expand Up @@ -269,31 +270,31 @@ async def step3_serialize_inputs(self) -> Tuple[List[bytes], List[bytes]]:
elif inp.input.account_command:
x = inp.input.account_command
if x.mint:
command = 0
command = MintlayerAccountCommandType.MINT_TOKENS
token_id = x.mint.token_id
data = x.mint.amount
elif x.unmint:
command = 1
command = MintlayerAccountCommandType.UNMINT_TOKENS
token_id = x.unmint.token_id
data = b""
elif x.lock_token_supply:
command = 2
command = MintlayerAccountCommandType.LOCK_TOKEN_SUPPLY
token_id = x.lock_token_supply.token_id
data = b""
elif x.freeze_token:
command = 3
command = MintlayerAccountCommandType.FREEZE_TOKEN
token_id = x.freeze_token.token_id
data = int(x.freeze_token.is_token_unfreezabe).to_bytes(1, "big")
elif x.unfreeze_token:
command = 4
command = MintlayerAccountCommandType.UNFREEZE_TOKEN
token_id = x.unfreeze_token.token_id
data = b""
elif x.change_token_authority:
command = 5
command = MintlayerAccountCommandType.CHANGE_TOKEN_AUTHORITY
token_id = x.change_token_authority.token_id
data = decode_nullable_address(x.change_token_authority.destination)
elif x.change_token_metadata_uri:
command = 8
command = MintlayerAccountCommandType.CHANGE_TOKEN_METADATA_URI
token_id = x.change_token_metadata_uri.token_id
data = x.change_token_metadata_uri.metadata_uri
elif x.conclude_order:
Expand Down Expand Up @@ -324,7 +325,7 @@ async def step3_serialize_inputs(self) -> Tuple[List[bytes], List[bytes]]:
raise Exception("unknown account command")

encoded_inp = mintlayer_utils.encode_token_account_command_input(
x.nonce, command, mintlayer_decode(token_id), data
x.nonce, int(command), mintlayer_decode(token_id), data
)
encoded_inputs.append(encoded_inp)
encoded_input_utxos.append(b"\x00")
Expand All @@ -349,7 +350,7 @@ def serialize_output(self, out: MintlayerTxOutput) -> bytes:
b"" if not x.value.token else mintlayer_decode(x.value.token.token_id)
)
encoded_out = mintlayer_utils.encode_lock_then_transfer_output(
x.value.amount, token_id, lock_type, lock_amount, data
x.value.amount, token_id, int(lock_type), lock_amount, data
)
elif out.burn:
x = out.burn
Expand Down Expand Up @@ -430,7 +431,7 @@ def serialize_output(self, out: MintlayerTxOutput) -> bytes:
encoded_out = mintlayer_utils.encode_htlc_output(
x.value.amount,
token_id,
lock_type,
int(lock_type),
lock_amount,
refund_key,
spend_key,
Expand Down
Loading

0 comments on commit 6e03e86

Please sign in to comment.