Skip to content

Commit 08dce8d

Browse files
committed
Add support for Mintlayer orders
1 parent 4e26426 commit 08dce8d

File tree

10 files changed

+1393
-193
lines changed

10 files changed

+1393
-193
lines changed

common/protob/messages-mintlayer.proto

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,55 +196,73 @@ message MintlayerAccountCommandTxInput {
196196
optional MintlayerFreezeToken freeze_token = 8; // freeze supply
197197
optional MintlayerUnfreezeToken unfreeze_token = 9; // unfreeze supply
198198
optional MintlayerChangeTokenAuhtority change_token_authority = 10; // change token authority
199-
optional MintlayerChangeTokenMetadataUri change_token_metadata_uri = 11; // change token metadata uri
199+
optional MintlayerConcludeOrder conclude_order = 11; // conclude an order
200+
optional MintlayerFillOrder fill_order = 12; // fill an order
201+
optional MintlayerChangeTokenMetadataUri change_token_metadata_uri = 13; // change token metadata uri
200202
}
201203

202-
/** Data type for output value coins or token
204+
/** Data type for account command
203205
* @embed
204206
*/
205207
message MintlayerMintTokens {
206208
required bytes token_id = 1; // token id
207209
required bytes amount = 2; // amoun of tokens to mint
208210
}
209211

210-
/** Data type for output value coins or token
212+
/** Data type for account command
211213
* @embed
212214
*/
213215
message MintlayerUnmintTokens {
214216
required bytes token_id = 1; // token id
215217
}
216218

217-
/** Data type for output value coins or token
219+
/** Data type for account command
218220
* @embed
219221
*/
220222
message MintlayerLockTokenSupply {
221223
required bytes token_id = 1; // token id
222224
}
223225

224-
/** Data type for output value coins or token
226+
/** Data type for account command
225227
* @embed
226228
*/
227229
message MintlayerFreezeToken {
228230
required bytes token_id = 1; // token id
229231
required bool is_token_unfreezabe = 2; // is unfreezeable after freezing it
230232
}
231233

232-
/** Data type for output value coins or token
234+
/** Data type for account command
233235
* @embed
234236
*/
235237
message MintlayerUnfreezeToken {
236238
required bytes token_id = 1; // token id
237239
}
238240

239-
/** Data type for output value coins or token
241+
/** Data type for account command
240242
* @embed
241243
*/
242244
message MintlayerChangeTokenAuhtority {
243245
required bytes token_id = 1; // token id
244246
required string destination = 2; // destination for the new authority
245247
}
246248

247-
/** Data type for output value coins or token
249+
/** Data type for account command
250+
* @embed
251+
*/
252+
message MintlayerConcludeOrder {
253+
required bytes order_id = 1; // order id
254+
}
255+
256+
/** Data type for account command
257+
* @embed
258+
*/
259+
message MintlayerFillOrder {
260+
required bytes order_id = 1; // order id
261+
required MintlayerOutputValue value = 2; // value
262+
required string destination = 3; // the destination
263+
}
264+
265+
/** Data type for account command
248266
* @embed
249267
*/
250268
message MintlayerChangeTokenMetadataUri {
@@ -267,6 +285,7 @@ message MintlayerTxOutput {
267285
optional MintlayerIssueNftTxOutput issue_nft = 9; // issue NFT output
268286
optional MintlayerDataDepositTxOutput data_deposit = 10; // data deposit output
269287
optional MintlayerHtlcTxOutput htlc = 11; // HTLC output
288+
optional MintlayerAnyoneCanTakeTxOutput anyone_can_take = 12; // Anyone can take output
270289
}
271290

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

442+
/** Data type for transaction output to be signed.
443+
* @embed
444+
*/
445+
message MintlayerAnyoneCanTakeTxOutput {
446+
required string conclude_key = 1; // The key that can authorize conclusion of an order
447+
required MintlayerOutputValue ask = 2; // ask value of the order
448+
required MintlayerOutputValue give = 3; // give value of the order
449+
}
450+
423451
/** Data type for metadata about previous transaction which contains the UTXO being spent.
424452
* @embed
425453
*/

core/embed/extmod/modtrezormintlayer/modtrezormintlayer.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
*/
1919

2020
#include <stdint.h>
21-
#include <stdio.h>
22-
#include <string.h>
2321

2422
#include "common.h"
2523

@@ -38,9 +36,15 @@ STATIC const mp_rom_map_elem_t mod_trezormintlayer_globals_table[] = {
3836
{MP_ROM_QSTR(MP_QSTR_encode_account_spending_input),
3937
MP_ROM_PTR(
4038
&mod_trezormintlayer_utils_mintlayer_encode_account_spending_input_obj)},
41-
{MP_ROM_QSTR(MP_QSTR_encode_account_command_input),
39+
{MP_ROM_QSTR(MP_QSTR_encode_token_account_command_input),
4240
MP_ROM_PTR(
43-
&mod_trezormintlayer_utils_mintlayer_encode_account_command_input_obj)},
41+
&mod_trezormintlayer_utils_mintlayer_encode_token_account_command_input_obj)},
42+
{MP_ROM_QSTR(MP_QSTR_encode_conclude_order_account_command_input),
43+
MP_ROM_PTR(
44+
&mod_trezormintlayer_utils_mintlayer_encode_conclude_order_account_command_input_obj)},
45+
{MP_ROM_QSTR(MP_QSTR_encode_fill_order_account_command_input),
46+
MP_ROM_PTR(
47+
&mod_trezormintlayer_utils_mintlayer_encode_fill_order_account_command_input_obj)},
4448
{MP_ROM_QSTR(MP_QSTR_encode_transfer_output),
4549
MP_ROM_PTR(
4650
&mod_trezormintlayer_utils_mintlayer_encode_transfer_output_obj)},
@@ -72,6 +76,9 @@ STATIC const mp_rom_map_elem_t mod_trezormintlayer_globals_table[] = {
7276
&mod_trezormintlayer_utils_mintlayer_encode_data_deposit_output_obj)},
7377
{MP_ROM_QSTR(MP_QSTR_encode_htlc_output),
7478
MP_ROM_PTR(&mod_trezormintlayer_utils_mintlayer_encode_htlc_output_obj)},
79+
{MP_ROM_QSTR(MP_QSTR_encode_anyone_can_take_output),
80+
MP_ROM_PTR(
81+
&mod_trezormintlayer_utils_mintlayer_encode_anyone_can_take_output_obj)},
7582
{MP_ROM_QSTR(MP_QSTR_encode_compact_length),
7683
MP_ROM_PTR(&mod_trezormintlayer_utils_mintlayer_encode_comact_length_obj)},
7784
};

core/embed/extmod/modtrezormintlayer/modtrezormintlayer.h

Lines changed: 117 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,14 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_3(
125125
mod_trezormintlayer_utils_mintlayer_encode_account_spending_input_obj,
126126
mod_trezormintlayer_utils_mintlayer_encode_account_spending_input);
127127

128-
/// def encode_account_command_input(nonce: int, command: int, token_id: bytes,
129-
/// data: bytes) -> bytes:
128+
/// def encode_token_account_command_input(nonce: int, command: int, token_id:
129+
/// bytes, data: bytes) -> bytes:
130130
/// """
131131
/// encodes an account command from the nonce, command, token id and
132132
/// additional command data
133133
/// """
134134
STATIC mp_obj_t
135-
mod_trezormintlayer_utils_mintlayer_encode_account_command_input(
135+
mod_trezormintlayer_utils_mintlayer_encode_token_account_command_input(
136136
size_t n_args, const mp_obj_t *args) {
137137
uint64_t nonce = trezor_obj_get_uint64(args[0]);
138138
uint32_t command = trezor_obj_get_uint(args[1]);
@@ -147,7 +147,7 @@ mod_trezormintlayer_utils_mintlayer_encode_account_command_input(
147147
mp_buffer_info_t data = {0};
148148
mp_get_buffer_raise(args[3], &data, MP_BUFFER_READ);
149149

150-
ByteArray arr = mintlayer_encode_account_command_input(
150+
ByteArray arr = mintlayer_encode_token_account_command_input(
151151
nonce, command, hash.buf, hash.len, data.buf, data.len);
152152
handle_err(&arr);
153153

@@ -162,8 +162,79 @@ mod_trezormintlayer_utils_mintlayer_encode_account_command_input(
162162
}
163163

164164
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
165-
mod_trezormintlayer_utils_mintlayer_encode_account_command_input_obj, 4, 4,
166-
mod_trezormintlayer_utils_mintlayer_encode_account_command_input);
165+
mod_trezormintlayer_utils_mintlayer_encode_token_account_command_input_obj,
166+
4, 4,
167+
mod_trezormintlayer_utils_mintlayer_encode_token_account_command_input);
168+
169+
/// def encode_conclude_order_account_command_input(nonce: int, order_id: bytes)
170+
/// -> bytes:
171+
/// """
172+
/// encodes an conclude order account command from the nonce and order id
173+
/// """
174+
STATIC mp_obj_t
175+
mod_trezormintlayer_utils_mintlayer_encode_conclude_order_account_command_input(
176+
mp_obj_t nonce_obj, mp_obj_t order_id_obj) {
177+
uint64_t nonce = trezor_obj_get_uint64(nonce_obj);
178+
mp_buffer_info_t order_id = {0};
179+
mp_get_buffer_raise(order_id_obj, &order_id, MP_BUFFER_READ);
180+
181+
ByteArray arr = mintlayer_encode_conclude_order_account_command_input(
182+
nonce, order_id.buf, order_id.len);
183+
handle_err(&arr);
184+
185+
vstr_t pkh = {0};
186+
vstr_init_len(&pkh, arr.len_or_err.len);
187+
int i = 0;
188+
for (; i < arr.len_or_err.len; i++) {
189+
((uint8_t *)pkh.buf)[i] = (uint8_t)arr.data[i];
190+
}
191+
192+
return mp_obj_new_str_from_vstr(&mp_type_bytes, &pkh);
193+
}
194+
195+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(
196+
mod_trezormintlayer_utils_mintlayer_encode_conclude_order_account_command_input_obj,
197+
mod_trezormintlayer_utils_mintlayer_encode_conclude_order_account_command_input);
198+
199+
/// def encode_fill_order_account_command_input(nonce: int, order_id: bytes,
200+
/// amount: bytes, token_id: bytes, destination: bytes)
201+
/// -> bytes:
202+
/// """
203+
/// encodes an fill order account command from the nonce, order id, output
204+
/// value and destination
205+
/// """
206+
STATIC mp_obj_t
207+
mod_trezormintlayer_utils_mintlayer_encode_fill_order_account_command_input(
208+
size_t n_args, const mp_obj_t *args) {
209+
uint64_t nonce = trezor_obj_get_uint64(args[0]);
210+
mp_buffer_info_t order_id = {0};
211+
mp_get_buffer_raise(args[1], &order_id, MP_BUFFER_READ);
212+
mp_buffer_info_t amount = {0};
213+
mp_get_buffer_raise(args[2], &amount, MP_BUFFER_READ);
214+
mp_buffer_info_t token_id = {0};
215+
mp_get_buffer_raise(args[3], &token_id, MP_BUFFER_READ);
216+
mp_buffer_info_t destination = {0};
217+
mp_get_buffer_raise(args[4], &destination, MP_BUFFER_READ);
218+
219+
ByteArray arr = mintlayer_encode_fill_order_account_command_input(
220+
nonce, order_id.buf, order_id.len, amount.buf, amount.len, token_id.buf,
221+
token_id.len, destination.buf, destination.len);
222+
handle_err(&arr);
223+
224+
vstr_t pkh = {0};
225+
vstr_init_len(&pkh, arr.len_or_err.len);
226+
int i = 0;
227+
for (; i < arr.len_or_err.len; i++) {
228+
((uint8_t *)pkh.buf)[i] = (uint8_t)arr.data[i];
229+
}
230+
231+
return mp_obj_new_str_from_vstr(&mp_type_bytes, &pkh);
232+
}
233+
234+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
235+
mod_trezormintlayer_utils_mintlayer_encode_fill_order_account_command_input_obj,
236+
5, 5,
237+
mod_trezormintlayer_utils_mintlayer_encode_fill_order_account_command_input);
167238

168239
/// def encode_transfer_output(amount: bytes, token_id: bytes, address: bytes)
169240
/// -> bytes:
@@ -557,6 +628,46 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
557628
mod_trezormintlayer_utils_mintlayer_encode_htlc_output_obj, 5, 5,
558629
mod_trezormintlayer_utils_mintlayer_encode_htlc_output);
559630

631+
/// def encode_anyone_can_take_output(destination: bytes, ask_amount: bytes,
632+
/// ask_token_id: bytes, give_amount: bytes, give_token_id: bytes) -> bytes:
633+
/// """
634+
/// encodes an anyone can take output with given the conclude key, give and
635+
/// take amounts
636+
/// """
637+
STATIC mp_obj_t
638+
mod_trezormintlayer_utils_mintlayer_encode_anyone_can_take_output(
639+
size_t n_args, const mp_obj_t *args) {
640+
mp_buffer_info_t conclude_key = {0};
641+
mp_get_buffer_raise(args[0], &conclude_key, MP_BUFFER_READ);
642+
mp_buffer_info_t ask_amount = {0};
643+
mp_get_buffer_raise(args[1], &ask_amount, MP_BUFFER_READ);
644+
mp_buffer_info_t ask_token_id = {0};
645+
mp_get_buffer_raise(args[2], &ask_token_id, MP_BUFFER_READ);
646+
mp_buffer_info_t give_amount = {0};
647+
mp_get_buffer_raise(args[3], &give_amount, MP_BUFFER_READ);
648+
mp_buffer_info_t give_token_id = {0};
649+
mp_get_buffer_raise(args[4], &give_token_id, MP_BUFFER_READ);
650+
651+
ByteArray arr = mintlayer_encode_anyone_can_take_output(
652+
conclude_key.buf, conclude_key.len, ask_amount.buf, ask_amount.len,
653+
ask_token_id.buf, ask_token_id.len, give_amount.buf, give_amount.len,
654+
give_token_id.buf, give_token_id.len);
655+
handle_err(&arr);
656+
657+
vstr_t encoding = {0};
658+
vstr_init_len(&encoding, arr.len_or_err.len);
659+
int i = 0;
660+
for (; i < arr.len_or_err.len; i++) {
661+
((uint8_t *)encoding.buf)[i] = (uint8_t)arr.data[i];
662+
}
663+
664+
return mp_obj_new_str_from_vstr(&mp_type_bytes, &encoding);
665+
}
666+
667+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
668+
mod_trezormintlayer_utils_mintlayer_encode_anyone_can_take_output_obj, 5, 5,
669+
mod_trezormintlayer_utils_mintlayer_encode_anyone_can_take_output);
670+
560671
/// def encode_compact_length(length: int) -> bytes:
561672
/// """
562673
/// encodes a comapct length to bytes

core/embed/rust/mintlayer.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,21 @@ ByteArray mintlayer_encode_account_spending_input(
3636
uint32_t delegation_id_data_len, const unsigned char* amount_data,
3737
uint32_t amount_data_len);
3838

39-
ByteArray mintlayer_encode_account_command_input(
39+
ByteArray mintlayer_encode_token_account_command_input(
4040
uint64_t nonce, uint32_t command, const unsigned char* token_id_data,
4141
uint32_t token_id_data_len, const unsigned char* data, uint32_t data_len);
4242

43+
ByteArray mintlayer_encode_conclude_order_account_command_input(
44+
uint64_t nonce, const unsigned char* order_id_data,
45+
uint32_t order_id_data_len);
46+
47+
ByteArray mintlayer_encode_fill_order_account_command_input(
48+
uint64_t nonce, const unsigned char* order_id_data,
49+
uint32_t order_id_data_len, const unsigned char* coin_amount_data,
50+
uint32_t coin_amount_data_len, const unsigned char* token_id_data,
51+
uint32_t token_id_data_len, const unsigned char* destination_data,
52+
uint32_t destination_data_len);
53+
4354
ByteArray mintlayer_encode_transfer_output(
4455
const unsigned char* coin_amount_data, uint32_t coin_amount_data_len,
4556
const unsigned char* token_id_data, uint32_t token_id_data_len,
@@ -112,4 +123,12 @@ ByteArray mintlayer_encode_htlc_output(
112123
const unsigned char* spend_key_data, uint32_t spend_key_data_len,
113124
const unsigned char* secret_hash_data, uint32_t secret_hash_data_len);
114125

126+
ByteArray mintlayer_encode_anyone_can_take_output(
127+
const unsigned char* conclude_key_data, uint32_t conclude_key_data_len,
128+
const unsigned char* ask_coin_amount_data,
129+
uint32_t ask_coin_amount_data_len, const unsigned char* ask_token_id_data,
130+
uint32_t ask_token_id_data_len, const unsigned char* give_coin_amount_data,
131+
uint32_t give_coin_amount_data_len, const unsigned char* give_token_id_data,
132+
uint32_t give_token_id_data_len);
133+
115134
ByteArray mintlayer_encode_compact_length(uint32_t length);

0 commit comments

Comments
 (0)