Skip to content

Commit

Permalink
fix errors in Mintlayer layout
Browse files Browse the repository at this point in the history
  • Loading branch information
OBorce committed Sep 19, 2024
1 parent 0bf0d4a commit 3529ba2
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 46 deletions.
2 changes: 1 addition & 1 deletion core/embed/extmod/modtrezormintlayer/modtrezormintlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ STATIC mp_obj_t mod_trezormintlayer_utils_mintlayer_encode_htlc_output(
}

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_obj, 7, 7,
mod_trezormintlayer_utils_mintlayer_encode_htlc_output);

/// def encode_anyone_can_take_output(destination: bytes, ask_amount: bytes,
Expand Down
39 changes: 24 additions & 15 deletions core/embed/rust/src/mintlayer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,11 +463,10 @@ extern "C" fn mintlayer_encode_create_stake_pool_output(
let vrf_public_key = unsafe {
core::slice::from_raw_parts(vrf_public_key_data, vrf_public_key_data_len as usize)
};
let vrf_public_key =
VRFPublicKeyHolder::Schnorrkel(VRFPublicKey(match vrf_public_key.try_into() {
Ok(res) => res,
Err(_) => return MintlayerErrorCode::InvalidVrfPublicKey.into(),
}));
let vrf_public_key = match VRFPublicKeyHolder::decode_all(&mut vrf_public_key.as_ref()) {
Ok(res) => res,
Err(_) => return MintlayerErrorCode::InvalidVrfPublicKey.into(),
};

let destination_bytes = unsafe {
core::slice::from_raw_parts(
Expand Down Expand Up @@ -786,14 +785,16 @@ extern "C" fn mintlayer_encode_issue_nft_output(
};

let issuance = NftIssuance::V0(NftIssuanceV0 {
creator,
name,
description,
ticker,
icon_uri,
additional_metadata_uri,
media_uri,
media_hash,
metadata: Metadata {
creator,
name,
description,
ticker,
icon_uri,
additional_metadata_uri,
media_uri,
media_hash,
},
});

let txo = TxOutput::IssueNft(token_id, issuance, destination);
Expand Down Expand Up @@ -883,7 +884,7 @@ extern "C" fn mintlayer_encode_htlc_output(

let hash =
unsafe { core::slice::from_raw_parts(secret_hash_data, secret_hash_data_len as usize) };
let secret_hash = H256(match hash.try_into() {
let secret_hash = HtlcSecretHash(match hash.try_into() {
Ok(hash) => hash,
Err(_) => return MintlayerErrorCode::WrongHashSize.into(),
});
Expand Down Expand Up @@ -1333,6 +1334,11 @@ enum NftIssuance {

#[derive(Encode)]
struct NftIssuanceV0 {
pub metadata: Metadata,
}

#[derive(Encode)]
struct Metadata {
pub creator: Option<PublicKeyHolder>,
pub name: parity_scale_codec::alloc::vec::Vec<u8>,
pub description: parity_scale_codec::alloc::vec::Vec<u8>,
Expand Down Expand Up @@ -1398,7 +1404,7 @@ enum TxOutput {
#[derive(Encode)]
pub struct HashedTimelockContract {
// can be spent either by a specific address that knows the secret
secret_hash: H256,
secret_hash: HtlcSecretHash,
spend_key: Destination,

// or by a multisig after timelock expires making it possible to refund
Expand All @@ -1409,6 +1415,9 @@ pub struct HashedTimelockContract {
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug, Encode, Decode)]
struct H256(pub [u8; 32]);

#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug, Encode, Decode)]
struct HtlcSecretHash(pub [u8; 20]);

#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, Ord, PartialOrd)]
enum OutPointSourceId {
#[codec(index = 0)]
Expand Down
55 changes: 30 additions & 25 deletions core/src/apps/mintlayer/sign_tx/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,15 @@ async def confirm_output(
elif output.create_stake_pool:
x = output.create_stake_pool
assert x.staker is not None and x.decommission_key is not None
address_short = f"staker: {x.staker}, decommission_key: {x.decommission_key}"
data = convertbits(x.pool_id, 8, 5)
pool_id_address = bech32_encode(POOL_HRP, data, Encoding.BECH32M)
address_short = f"""Pool ID: {pool_id_address}
staker: {x.staker}
decommission_key: {x.decommission_key}"
VFT public key: {x.vrf_public_key}
Margin ratio per thousand: {x.margin_ratio_per_thousand}
Cost per block: {int.from_bytes(x.cost_per_block, "big")}
"""
amount = format_coin_amount(x.pledge, None)
address_label = "Create stake pool"
elif output.produce_block_from_stake:
Expand Down Expand Up @@ -128,20 +136,19 @@ async def confirm_output(
elif x.total_supply.type == MintlayerTokenTotalSupplyType.FIXED:
if not x.total_supply.fixed_amount:
raise ValueError("Token Fixed supply without amount")
amount = int.from_bytes(x.total_supply.fixed_amount, "big")
formated_amount = format_amount(amount, x.number_of_decimals)
fixed_amount = int.from_bytes(x.total_supply.fixed_amount, "big")
formated_amount = format_amount(fixed_amount, x.number_of_decimals)
total_supply = f"FIXED {formated_amount}"
else:
raise ValueError("Unhandled Token total supply type")
is_freezable = "Yes" if x.is_freezable else "No"
address_short = f"""Ticker: {ticker}
Authority: {x.authority}
Metadata URI: {metadata_uri}
Total token supply: {total_supply}
Number of Decimals: {x.number_of_decimals}
Is Freezable: {is_freezable}
"""
amount = "amount"
Authority: {x.authority}
Metadata URI: {metadata_uri}
Total token supply: {total_supply}
Number of Decimals: {x.number_of_decimals}
Is Freezable: {is_freezable}"""
amount = ""
address_label = "Issue fungible token"
elif output.issue_nft:
x = output.issue_nft
Expand All @@ -155,13 +162,12 @@ async def confirm_output(
)
media_uri = x.media_uri.decode("utf-8") if x.media_uri else None
address_short = f"""Name: {name}
Creator: {x.creator}
ticker: {ticker}
Address: {x.destination}
Icon URI: {icon_uri}
Additional medatada URI: {additional_metadata_uri}
Media URI: {media_uri}
"""
Creator: {x.creator}
ticker: {ticker}
Address: {x.destination}
Icon URI: {icon_uri}
Additional medatada URI: {additional_metadata_uri}
Media URI: {media_uri}"""
amount = ""
address_label = "Issue NFT token"
elif output.data_deposit:
Expand All @@ -172,21 +178,20 @@ async def confirm_output(
elif output.htlc:
x = output.htlc
lock = lock_to_string(x.refund_timelock)
address_short = f"""Secret Hash: {x.secret_hash.hex()}
Spend Key: {x.spend_key}
Refund Key: {x.refund_key}
Refund Time Lock: {lock}
"""
hexidied_secret_hash = hexlify(x.secret_hash).decode()
address_short = f"""Secret Hash: {hexidied_secret_hash}
Spend Key: {x.spend_key}
Refund Key: {x.refund_key}
Refund Time Lock: {lock}"""
amount = format_coin_amount(x.value.amount, x.value.token)
address_label = "HTLC"
elif output.anyone_can_take:
x = output.anyone_can_take
ask_amount = format_coin_amount(x.ask.amount, x.ask.token)
give_amount = format_coin_amount(x.give.amount, x.give.token)
address_short = f"""Conclude Key: {x.conclude_key}
Ask: {ask_amount}
Give: {give_amount}"
"""
Ask: {ask_amount}
Give: {give_amount}"""
amount = ""
address_label = "Anyone can take"
else:
Expand Down
18 changes: 13 additions & 5 deletions core/src/apps/mintlayer/sign_tx/signer.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,8 @@ def serialize_output(self, out: MintlayerTxOutput) -> bytes:
x.token_ticker,
x.number_of_decimals,
x.metadata_uri,
x.total_supply.type,
x.total_supply.fixed_amount,
int(x.total_supply.type),
x.total_supply.fixed_amount or b"",
authority,
int(x.is_freezable),
)
Expand All @@ -406,7 +406,7 @@ def serialize_output(self, out: MintlayerTxOutput) -> bytes:
x.token_id,
creator,
x.name,
x.destination,
x.description,
x.ticker,
x.icon_uri,
x.additional_metadata_uri,
Expand Down Expand Up @@ -434,10 +434,11 @@ def serialize_output(self, out: MintlayerTxOutput) -> bytes:
)
elif out.anyone_can_take:
x = out.anyone_can_take
conclude_key = mintlayer_decode_address_to_bytes(x.conclude_key)
ask_token_id = b"" if not x.ask.token else x.ask.token.token_id
give_token_id = b"" if not x.give.token else x.give.token.token_id
encoded_out = mintlayer_utils.encode_anyone_can_take_output(
x.conclude_key, x.ask.amount, ask_token_id, x.give.amount, give_token_id
conclude_key, x.ask.amount, ask_token_id, x.give.amount, give_token_id
)
else:
raise Exception("unhandled tx output type")
Expand Down Expand Up @@ -525,14 +526,21 @@ def update(value: MintlayerOutputValue):
else:
totals[token_or_coin] = amount

if ML_COIN not in totals:
totals[ML_COIN] = 0

if txo.transfer:
update(txo.transfer.value)
elif txo.lock_then_transfer:
update(txo.lock_then_transfer.value)
elif txo.burn:
update(txo.burn.value)
elif txo.issue_nft:
totals[txo.issue_nft.ticker.decode("utf-8")] += 1
token_or_coin = txo.issue_nft.ticker.decode("utf-8")
if token_or_coin in totals:
totals[token_or_coin] += 1
else:
totals[token_or_coin] = 1
elif txo.create_stake_pool:
amount = int.from_bytes(txo.create_stake_pool.pledge, "big")
totals[ML_COIN] += amount
Expand Down

0 comments on commit 3529ba2

Please sign in to comment.