Skip to content

Commit

Permalink
Add support for Mintlayer orders
Browse files Browse the repository at this point in the history
  • Loading branch information
OBorce committed Sep 16, 2024
1 parent 4e26426 commit 77bb6af
Show file tree
Hide file tree
Showing 10 changed files with 1,398 additions and 197 deletions.
44 changes: 36 additions & 8 deletions common/protob/messages-mintlayer.proto
Original file line number Diff line number Diff line change
Expand Up @@ -196,55 +196,73 @@ message MintlayerAccountCommandTxInput {
optional MintlayerFreezeToken freeze_token = 8; // freeze supply
optional MintlayerUnfreezeToken unfreeze_token = 9; // unfreeze supply
optional MintlayerChangeTokenAuhtority change_token_authority = 10; // change token authority
optional MintlayerChangeTokenMetadataUri change_token_metadata_uri = 11; // change token metadata uri
optional MintlayerConcludeOrder conclude_order = 11; // conclude an order
optional MintlayerFillOrder fill_order = 12; // fill an order
optional MintlayerChangeTokenMetadataUri change_token_metadata_uri = 13; // change token metadata uri
}

/** Data type for output value coins or token
/** Data type for account command
* @embed
*/
message MintlayerMintTokens {
required bytes token_id = 1; // token id
required bytes amount = 2; // amoun of tokens to mint
}

/** Data type for output value coins or token
/** Data type for account command
* @embed
*/
message MintlayerUnmintTokens {
required bytes token_id = 1; // token id
}

/** Data type for output value coins or token
/** Data type for account command
* @embed
*/
message MintlayerLockTokenSupply {
required bytes token_id = 1; // token id
}

/** Data type for output value coins or token
/** Data type for account command
* @embed
*/
message MintlayerFreezeToken {
required bytes token_id = 1; // token id
required bool is_token_unfreezabe = 2; // is unfreezeable after freezing it
}

/** Data type for output value coins or token
/** Data type for account command
* @embed
*/
message MintlayerUnfreezeToken {
required bytes token_id = 1; // token id
}

/** Data type for output value coins or token
/** Data type for account command
* @embed
*/
message MintlayerChangeTokenAuhtority {
required bytes token_id = 1; // token id
required string destination = 2; // destination for the new authority
}

/** Data type for output value coins or token
/** Data type for account command
* @embed
*/
message MintlayerConcludeOrder {
required bytes order_id = 1; // order id
}

/** Data type for account command
* @embed
*/
message MintlayerFillOrder {
required bytes order_id = 1; // order id
required MintlayerOutputValue value = 2; // value
required string destination = 3; // the destination
}

/** Data type for account command
* @embed
*/
message MintlayerChangeTokenMetadataUri {
Expand All @@ -267,6 +285,7 @@ message MintlayerTxOutput {
optional MintlayerIssueNftTxOutput issue_nft = 9; // issue NFT output
optional MintlayerDataDepositTxOutput data_deposit = 10; // data deposit output
optional MintlayerHtlcTxOutput htlc = 11; // HTLC output
optional MintlayerAnyoneCanTakeTxOutput anyone_can_take = 12; // Anyone can take output
}

/** Data type for output value information for the token
Expand Down Expand Up @@ -420,6 +439,15 @@ message MintlayerHtlcTxOutput {
required string refund_key = 5; // the key for a refund after the time lock has expired
}

/** Data type for transaction output to be signed.
* @embed
*/
message MintlayerAnyoneCanTakeTxOutput {
required string conclude_key = 1; // The key that can authorize conclusion of an order
required MintlayerOutputValue ask = 2; // ask value of the order
required MintlayerOutputValue give = 3; // give value of the order
}

/** Data type for metadata about previous transaction which contains the UTXO being spent.
* @embed
*/
Expand Down
15 changes: 11 additions & 4 deletions core/embed/extmod/modtrezormintlayer/modtrezormintlayer.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
*/

#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include "common.h"

Expand All @@ -38,9 +36,15 @@ STATIC const mp_rom_map_elem_t mod_trezormintlayer_globals_table[] = {
{MP_ROM_QSTR(MP_QSTR_encode_account_spending_input),
MP_ROM_PTR(
&mod_trezormintlayer_utils_mintlayer_encode_account_spending_input_obj)},
{MP_ROM_QSTR(MP_QSTR_encode_account_command_input),
{MP_ROM_QSTR(MP_QSTR_encode_token_account_command_input),
MP_ROM_PTR(
&mod_trezormintlayer_utils_mintlayer_encode_account_command_input_obj)},
&mod_trezormintlayer_utils_mintlayer_encode_token_account_command_input_obj)},
{MP_ROM_QSTR(MP_QSTR_encode_conclude_order_account_command_input),
MP_ROM_PTR(
&mod_trezormintlayer_utils_mintlayer_encode_conclude_order_account_command_input_obj)},
{MP_ROM_QSTR(MP_QSTR_encode_fill_order_account_command_input),
MP_ROM_PTR(
&mod_trezormintlayer_utils_mintlayer_encode_fill_order_account_command_input_obj)},
{MP_ROM_QSTR(MP_QSTR_encode_transfer_output),
MP_ROM_PTR(
&mod_trezormintlayer_utils_mintlayer_encode_transfer_output_obj)},
Expand Down Expand Up @@ -72,6 +76,9 @@ STATIC const mp_rom_map_elem_t mod_trezormintlayer_globals_table[] = {
&mod_trezormintlayer_utils_mintlayer_encode_data_deposit_output_obj)},
{MP_ROM_QSTR(MP_QSTR_encode_htlc_output),
MP_ROM_PTR(&mod_trezormintlayer_utils_mintlayer_encode_htlc_output_obj)},
{MP_ROM_QSTR(MP_QSTR_encode_anyone_can_take_output),
MP_ROM_PTR(
&mod_trezormintlayer_utils_mintlayer_encode_anyone_can_take_output_obj)},
{MP_ROM_QSTR(MP_QSTR_encode_compact_length),
MP_ROM_PTR(&mod_trezormintlayer_utils_mintlayer_encode_comact_length_obj)},
};
Expand Down
123 changes: 117 additions & 6 deletions core/embed/extmod/modtrezormintlayer/modtrezormintlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_3(
mod_trezormintlayer_utils_mintlayer_encode_account_spending_input_obj,
mod_trezormintlayer_utils_mintlayer_encode_account_spending_input);

/// def encode_account_command_input(nonce: int, command: int, token_id: bytes,
/// data: bytes) -> bytes:
/// def encode_token_account_command_input(nonce: int, command: int, token_id:
/// bytes, data: bytes) -> bytes:
/// """
/// encodes an account command from the nonce, command, token id and
/// additional command data
/// """
STATIC mp_obj_t
mod_trezormintlayer_utils_mintlayer_encode_account_command_input(
mod_trezormintlayer_utils_mintlayer_encode_token_account_command_input(
size_t n_args, const mp_obj_t *args) {
uint64_t nonce = trezor_obj_get_uint64(args[0]);
uint32_t command = trezor_obj_get_uint(args[1]);
Expand All @@ -147,7 +147,7 @@ mod_trezormintlayer_utils_mintlayer_encode_account_command_input(
mp_buffer_info_t data = {0};
mp_get_buffer_raise(args[3], &data, MP_BUFFER_READ);

ByteArray arr = mintlayer_encode_account_command_input(
ByteArray arr = mintlayer_encode_token_account_command_input(
nonce, command, hash.buf, hash.len, data.buf, data.len);
handle_err(&arr);

Expand All @@ -162,8 +162,79 @@ mod_trezormintlayer_utils_mintlayer_encode_account_command_input(
}

STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
mod_trezormintlayer_utils_mintlayer_encode_account_command_input_obj, 4, 4,
mod_trezormintlayer_utils_mintlayer_encode_account_command_input);
mod_trezormintlayer_utils_mintlayer_encode_token_account_command_input_obj,
4, 4,
mod_trezormintlayer_utils_mintlayer_encode_token_account_command_input);

/// def encode_conclude_order_account_command_input(nonce: int, order_id: bytes)
/// -> bytes:
/// """
/// encodes an conclude order account command from the nonce and order id
/// """
STATIC mp_obj_t
mod_trezormintlayer_utils_mintlayer_encode_conclude_order_account_command_input(
mp_obj_t nonce_obj, mp_obj_t order_id_obj) {
uint64_t nonce = trezor_obj_get_uint64(nonce_obj);
mp_buffer_info_t order_id = {0};
mp_get_buffer_raise(order_id_obj, &order_id, MP_BUFFER_READ);

ByteArray arr = mintlayer_encode_conclude_order_account_command_input(
nonce, order_id.buf, order_id.len);
handle_err(&arr);

vstr_t pkh = {0};
vstr_init_len(&pkh, arr.len_or_err.len);
int i = 0;
for (; i < arr.len_or_err.len; i++) {
((uint8_t *)pkh.buf)[i] = (uint8_t)arr.data[i];
}

return mp_obj_new_str_from_vstr(&mp_type_bytes, &pkh);
}

STATIC MP_DEFINE_CONST_FUN_OBJ_2(
mod_trezormintlayer_utils_mintlayer_encode_conclude_order_account_command_input_obj,
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)
/// -> bytes:
/// """
/// encodes an fill order account command from the nonce, order id, output
/// value and destination
/// """
STATIC mp_obj_t
mod_trezormintlayer_utils_mintlayer_encode_fill_order_account_command_input(
size_t n_args, const mp_obj_t *args) {
uint64_t nonce = trezor_obj_get_uint64(args[0]);
mp_buffer_info_t order_id = {0};
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);

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);
handle_err(&arr);

vstr_t pkh = {0};
vstr_init_len(&pkh, arr.len_or_err.len);
int i = 0;
for (; i < arr.len_or_err.len; i++) {
((uint8_t *)pkh.buf)[i] = (uint8_t)arr.data[i];
}

return mp_obj_new_str_from_vstr(&mp_type_bytes, &pkh);
}

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

/// def encode_transfer_output(amount: bytes, token_id: bytes, address: bytes)
/// -> bytes:
Expand Down Expand Up @@ -557,6 +628,46 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
mod_trezormintlayer_utils_mintlayer_encode_htlc_output_obj, 5, 5,
mod_trezormintlayer_utils_mintlayer_encode_htlc_output);

/// def encode_anyone_can_take_output(destination: bytes, ask_amount: bytes,
/// ask_token_id: bytes, give_amount: bytes, give_token_id: bytes) -> bytes:
/// """
/// encodes an anyone can take output with given the conclude key, give and
/// take amounts
/// """
STATIC mp_obj_t
mod_trezormintlayer_utils_mintlayer_encode_anyone_can_take_output(
size_t n_args, const mp_obj_t *args) {
mp_buffer_info_t conclude_key = {0};
mp_get_buffer_raise(args[0], &conclude_key, MP_BUFFER_READ);
mp_buffer_info_t ask_amount = {0};
mp_get_buffer_raise(args[1], &ask_amount, MP_BUFFER_READ);
mp_buffer_info_t ask_token_id = {0};
mp_get_buffer_raise(args[2], &ask_token_id, MP_BUFFER_READ);
mp_buffer_info_t give_amount = {0};
mp_get_buffer_raise(args[3], &give_amount, MP_BUFFER_READ);
mp_buffer_info_t give_token_id = {0};
mp_get_buffer_raise(args[4], &give_token_id, MP_BUFFER_READ);

ByteArray arr = mintlayer_encode_anyone_can_take_output(
conclude_key.buf, conclude_key.len, ask_amount.buf, ask_amount.len,
ask_token_id.buf, ask_token_id.len, give_amount.buf, give_amount.len,
give_token_id.buf, give_token_id.len);
handle_err(&arr);

vstr_t encoding = {0};
vstr_init_len(&encoding, arr.len_or_err.len);
int i = 0;
for (; i < arr.len_or_err.len; i++) {
((uint8_t *)encoding.buf)[i] = (uint8_t)arr.data[i];
}

return mp_obj_new_str_from_vstr(&mp_type_bytes, &encoding);
}

STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
mod_trezormintlayer_utils_mintlayer_encode_anyone_can_take_output_obj, 5, 5,
mod_trezormintlayer_utils_mintlayer_encode_anyone_can_take_output);

/// def encode_compact_length(length: int) -> bytes:
/// """
/// encodes a comapct length to bytes
Expand Down
21 changes: 20 additions & 1 deletion core/embed/rust/mintlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,21 @@ ByteArray mintlayer_encode_account_spending_input(
uint32_t delegation_id_data_len, const unsigned char* amount_data,
uint32_t amount_data_len);

ByteArray mintlayer_encode_account_command_input(
ByteArray mintlayer_encode_token_account_command_input(
uint64_t nonce, uint32_t command, const unsigned char* token_id_data,
uint32_t token_id_data_len, const unsigned char* data, uint32_t data_len);

ByteArray mintlayer_encode_conclude_order_account_command_input(
uint64_t nonce, const unsigned char* order_id_data,
uint32_t order_id_data_len);

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 destination_data_len);

ByteArray mintlayer_encode_transfer_output(
const unsigned char* coin_amount_data, uint32_t coin_amount_data_len,
const unsigned char* token_id_data, uint32_t token_id_data_len,
Expand Down Expand Up @@ -112,4 +123,12 @@ ByteArray mintlayer_encode_htlc_output(
const unsigned char* spend_key_data, uint32_t spend_key_data_len,
const unsigned char* secret_hash_data, uint32_t secret_hash_data_len);

ByteArray mintlayer_encode_anyone_can_take_output(
const unsigned char* conclude_key_data, uint32_t conclude_key_data_len,
const unsigned char* ask_coin_amount_data,
uint32_t ask_coin_amount_data_len, const unsigned char* ask_token_id_data,
uint32_t ask_token_id_data_len, const unsigned char* give_coin_amount_data,
uint32_t give_coin_amount_data_len, const unsigned char* give_token_id_data,
uint32_t give_token_id_data_len);

ByteArray mintlayer_encode_compact_length(uint32_t length);
Loading

0 comments on commit 77bb6af

Please sign in to comment.