Skip to content

Commit

Permalink
Change fill order encoding to match the new changes in Mintlayer core
Browse files Browse the repository at this point in the history
  • Loading branch information
OBorce committed Oct 14, 2024
1 parent a3bda3f commit 1e4b295
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 152 deletions.
3 changes: 1 addition & 2 deletions common/protob/messages-mintlayer.proto
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,7 @@ message MintlayerConcludeOrder {
message MintlayerFillOrder {
required bytes order_id = 1; // order id
required bytes amount = 2; // value
optional bytes token_id = 3; // value
required string destination = 4; // the destination
required string destination = 3; // the destination
}

/** Data type for account command
Expand Down
14 changes: 6 additions & 8 deletions core/embed/extmod/modtrezormintlayer/modtrezormintlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,11 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(
mod_trezormintlayer_utils_mintlayer_encode_conclude_order_account_command_input);

/// def encode_fill_order_account_command_input(nonce: int, order_id: bytes,
/// amount: bytes, token_id: bytes, destination: bytes)
/// amount: bytes, destination: bytes)
/// -> bytes:
/// """
/// encodes an fill order account command from the nonce, order id, output
/// value and destination
/// amount and destination
/// """
STATIC mp_obj_t
mod_trezormintlayer_utils_mintlayer_encode_fill_order_account_command_input(
Expand All @@ -211,14 +211,12 @@ mod_trezormintlayer_utils_mintlayer_encode_fill_order_account_command_input(
mp_get_buffer_raise(args[1], &order_id, MP_BUFFER_READ);
mp_buffer_info_t amount = {0};
mp_get_buffer_raise(args[2], &amount, MP_BUFFER_READ);
mp_buffer_info_t token_id = {0};
mp_get_buffer_raise(args[3], &token_id, MP_BUFFER_READ);
mp_buffer_info_t destination = {0};
mp_get_buffer_raise(args[4], &destination, MP_BUFFER_READ);
mp_get_buffer_raise(args[3], &destination, MP_BUFFER_READ);

ByteArray arr = mintlayer_encode_fill_order_account_command_input(
nonce, order_id.buf, order_id.len, amount.buf, amount.len, token_id.buf,
token_id.len, destination.buf, destination.len);
nonce, order_id.buf, order_id.len, amount.buf, amount.len,
destination.buf, destination.len);
handle_err(&arr);

vstr_t pkh = {0};
Expand All @@ -233,7 +231,7 @@ mod_trezormintlayer_utils_mintlayer_encode_fill_order_account_command_input(

STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
mod_trezormintlayer_utils_mintlayer_encode_fill_order_account_command_input_obj,
5, 5,
4, 4,
mod_trezormintlayer_utils_mintlayer_encode_fill_order_account_command_input);

/// def encode_transfer_output(amount: bytes, token_id: bytes, address: bytes)
Expand Down
3 changes: 1 addition & 2 deletions core/embed/rust/mintlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ ByteArray mintlayer_encode_conclude_order_account_command_input(
ByteArray mintlayer_encode_fill_order_account_command_input(
uint64_t nonce, const unsigned char* order_id_data,
uint32_t order_id_data_len, const unsigned char* coin_amount_data,
uint32_t coin_amount_data_len, const unsigned char* token_id_data,
uint32_t token_id_data_len, const unsigned char* destination_data,
uint32_t coin_amount_data_len, const unsigned char* destination_data,
uint32_t destination_data_len);

ByteArray mintlayer_encode_transfer_output(
Expand Down
18 changes: 6 additions & 12 deletions core/embed/rust/src/mintlayer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,6 @@ extern "C" fn mintlayer_encode_fill_order_account_command_input(
order_id_data_len: u32,
amount_data: *const u8,
amount_data_len: u32,
token_id_data: *const u8,
token_id_data_len: u32,
destination_data: *const u8,
destination_data_len: u32,
) -> ByteArray {
Expand All @@ -232,14 +230,10 @@ extern "C" fn mintlayer_encode_fill_order_account_command_input(
Ok(hash) => hash,
Err(_) => return MintlayerErrorCode::WrongHashSize.into(),
});
let value = match parse_output_value(
amount_data,
amount_data_len,
token_id_data_len,
token_id_data,
) {
Ok(value) => value,
Err(value) => return value,
let coin_amount = unsafe { core::slice::from_raw_parts(amount_data, amount_data_len as usize) };
let amount = match Amount::from_bytes_be(coin_amount.as_ref()) {
Some(amount) => amount,
None => return MintlayerErrorCode::InvalidAmount.into(),
};

let destination_bytes =
Expand All @@ -248,7 +242,7 @@ extern "C" fn mintlayer_encode_fill_order_account_command_input(
Ok(destination) => destination,
Err(_) => return MintlayerErrorCode::InvalidDestination.into(),
};
let account_command = AccountCommand::FillOrder(order_id, value, destination);
let account_command = AccountCommand::FillOrder(order_id, amount, destination);

let tx_input = TxInput::AccountCommand(nonce, account_command);
let vec_data = tx_input.encode();
Expand Down Expand Up @@ -1508,7 +1502,7 @@ enum AccountCommand {
#[codec(index = 6)]
ConcludeOrder(OrderId),
#[codec(index = 7)]
FillOrder(OrderId, OutputValue, Destination),
FillOrder(OrderId, Amount, Destination),
// Change token metadata uri
#[codec(index = 8)]
ChangeTokenMetadataUri(TokenId, parity_scale_codec::alloc::vec::Vec<u8>),
Expand Down
4 changes: 2 additions & 2 deletions core/mocks/generated/trezormintlayer.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ def encode_conclude_order_account_command_input(nonce: int, order_id: bytes)

# extmod/modtrezormintlayer/modtrezormintlayer.h
def encode_fill_order_account_command_input(nonce: int, order_id: bytes,
amount: bytes, token_id: bytes, destination: bytes)
amount: bytes, destination: bytes)
-> bytes:
"""
encodes an fill order account command from the nonce, order id, output
value and destination
amount and destination
"""


Expand Down
18 changes: 12 additions & 6 deletions core/src/apps/mintlayer/sign_tx/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,19 @@ def confirm_dialog(self) -> Awaitable[Any]:
return layout.confirm_multiple_accounts()


def confirm_output(output: MintlayerTxOutput, coin: CoinInfo, output_index: int, chunkify: bool) -> Awaitable[None]: # type: ignore [awaitable-return-type]
def confirm_output(
output: MintlayerTxOutput, coin: CoinInfo, output_index: int, chunkify: bool
) -> Awaitable[None]: # type: ignore [awaitable-return-type]
return (
yield UiConfirmOutput( # type: ignore [awaitable-return-type]
output, coin, output_index, chunkify
)
)


def confirm_total(spending: int, fee: int, fee_rate: float, coin: CoinInfo) -> Awaitable[None]: # type: ignore [awaitable-return-type]
def confirm_total(
spending: int, fee: int, fee_rate: float, coin: CoinInfo
) -> Awaitable[None]: # type: ignore [awaitable-return-type]
return (yield UiConfirmTotal(spending, fee, fee_rate, coin)) # type: ignore [awaitable-return-type]


Expand Down Expand Up @@ -145,7 +149,9 @@ def request_tx_input(tx_req: MintlayerTxRequest, i: int) -> Awaitable[MintlayerT
return _sanitize_tx_input(ack.tx.input)


def request_tx_output(tx_req: MintlayerTxRequest, i: int, tx_hash: bytes | None = None) -> Awaitable[MintlayerTxOutput]: # type: ignore [awaitable-return-type]
def request_tx_output(
tx_req: MintlayerTxRequest, i: int, tx_hash: bytes | None = None
) -> Awaitable[MintlayerTxOutput]: # type: ignore [awaitable-return-type]
from trezor.messages import MintlayerTxAckOutput

assert tx_req.details is not None
Expand Down Expand Up @@ -242,7 +248,7 @@ def _sanitize_tx_input(txi: MintlayerTxInput) -> MintlayerTxInput:
if txi.utxo.prev_index < 0:
raise DataError("Invalid UTXO previous index.")

if not txi.utxo.address_n:
if txi.utxo.address_n is None:
raise DataError("Input's address_n must be present for signing.")
elif txi.account_command:
cmd = txi.account_command
Expand All @@ -260,10 +266,10 @@ def _sanitize_tx_input(txi: MintlayerTxInput) -> MintlayerTxInput:
if no_cmd:
raise DataError("No account command present")

if not txi.account_command.address_n:
if txi.account_command.address_n is None:
raise DataError("Input's address_n must be present for signing.")
elif txi.account:
if not txi.account.address_n:
if txi.account.address_n is None:
raise DataError("Input's address_n must be present for signing.")
else:
raise DataError(
Expand Down
23 changes: 12 additions & 11 deletions core/src/apps/mintlayer/sign_tx/signer.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,18 +193,20 @@ async def step1_process_inputs(self) -> Dict[str, int]:
)
node = []
for address in txi.utxo.address_n:
node.append(
(self.keychain.derive(address.address_n), address.multisig_idx)
)
node.append((
self.keychain.derive(address.address_n),
address.multisig_idx,
))

update_totals(totals, txo)
self.tx_info.add_input(txi, txo, node)
elif txi.account:
node = []
for address in txi.account.address_n:
node.append(
(self.keychain.derive(address.address_n), address.multisig_idx)
)
node.append((
self.keychain.derive(address.address_n),
address.multisig_idx,
))
value = txi.account.value
amount = int.from_bytes(value.amount, "big")
token_or_coin = (
Expand All @@ -220,9 +222,10 @@ async def step1_process_inputs(self) -> Dict[str, int]:
elif txi.account_command:
node = []
for address in txi.account_command.address_n:
node.append(
(self.keychain.derive(address.address_n), address.multisig_idx)
)
node.append((
self.keychain.derive(address.address_n),
address.multisig_idx,
))
self.tx_info.add_input(txi, None, node)
else:
raise Exception("Unhandled tx input type")
Expand Down Expand Up @@ -311,14 +314,12 @@ async def step4_serialize_inputs(self) -> Tuple[List[bytes], List[bytes]]:
continue
elif x.fill_order:
ord = x.fill_order
token_id = b"" if not ord.token_id else ord.token_id
destination = mintlayer_decode_address_to_bytes(ord.destination)
encoded_inp = (
mintlayer_utils.encode_fill_order_account_command_input(
x.nonce,
ord.order_id,
ord.amount,
token_id,
destination,
)
)
Expand Down
2 changes: 0 additions & 2 deletions core/src/trezor/messages.py

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

5 changes: 1 addition & 4 deletions python/src/trezorlib/messages.py

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

Loading

0 comments on commit 1e4b295

Please sign in to comment.