Skip to content

Commit

Permalink
update mintlayer messages
Browse files Browse the repository at this point in the history
  • Loading branch information
OBorce committed Jul 29, 2024
1 parent 3d78f7f commit 5abd648
Show file tree
Hide file tree
Showing 20 changed files with 901 additions and 537 deletions.
1 change: 0 additions & 1 deletion common/protob/messages-bitcoin.proto
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ enum AmountUnit {
MILLIBITCOIN = 1; // mBTC
MICROBITCOIN = 2; // uBTC
SATOSHI = 3; // sat
ML = 4; // Mintlayer
}

/**
Expand Down
45 changes: 29 additions & 16 deletions common/protob/messages-mintlayer.proto
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ message MintlayerPublicKey {
}

/**
* Request: Ask device to verify message
* Request: Ask device to sign a message
* @start
* @next Success
* @next Failure
*/
message MintlayerVerifySig {
message MintlayerSignMessage {
repeated uint32 address_n = 1; // BIP-32 path to derive the key from master node
required bytes signature = 2; // signature to verify
required bytes message = 3; // message to verify
required string address = 2; // destination address in Base58 encoding; script_type must be PAYTOADDRESS
required bytes message = 3; // message to verify
}

/**
Expand Down Expand Up @@ -112,12 +112,19 @@ message MintlayerTxRequest {
optional bytes tx_hash = 2; // tx_hash of requested transaction
}
/**
* Structure representing request details
*/
message MintlayerSignature {
required bytes signature = 1; // a single signature
optional uint32 multisig_idx = 2; // in case of multisig the index of the key
}
/**
* Structure representing serialized data
*/
message MintlayerTxRequestSerializedType {
optional uint32 signature_index = 1; // 'signature' field contains signed input of this index
optional bytes signature = 2; // signature of the signature_index input
optional bytes serialized_tx = 3; // part of serialized and signed transaction
optional uint32 signature_index = 1; // 'signature' field contains signed input of this index
repeated MintlayerSignature signatures = 2; // signature of the signature_index input
optional bytes serialized_tx = 3; // part of serialized and signed transaction
}
}

Expand All @@ -139,16 +146,24 @@ enum MintlayerUtxoType {
BLOCK = 1;
}

/** Data type for output value coins or token
* @embed
*/
message MintlayerAddressPath {
repeated uint32 address_n = 1; // BIP-32 path to derive the key from master node
optional uint32 multisig_idx = 2; // in case of a multisig the key index
}

/** Data type for transaction input to be signed.
*
* @embed
*/
message MintlayerUtxoTxInput {
repeated uint32 address_n = 1; // BIP-32 path to derive the key from master node
repeated MintlayerAddressPath address_n = 1; // BIP-32 path to derive the key from master node
required string address = 2; // destination address in Base58 encoding; script_type must be PAYTOADDRESS
required bytes prev_hash = 3; // hash of previous transaction output to spend by this input
required uint32 prev_index = 4; // index of previous output to spend
required MintlayerUtxoType type = 5; // index of previous output to spend
required MintlayerUtxoType type = 5; // source type of the utxo (Transaction or BlockReward)
optional uint32 sequence = 6 [default=0xffffffff]; // sequence
required MintlayerOutputValue value = 7; // amount of previous transaction output
}
Expand All @@ -158,7 +173,7 @@ message MintlayerUtxoTxInput {
* @embed
*/
message MintlayerAccountTxInput {
repeated uint32 address_n = 1; // BIP-32 path to derive the key from master node
repeated MintlayerAddressPath address_n = 1; // BIP-32 path to derive the key from master node
required string address = 2; // destination address in Base58 encoding; script_type must be PAYTOADDRESS
optional uint32 sequence = 3 [default=0xffffffff]; // sequence
required MintlayerOutputValue value = 4; // amount of previous transaction output
Expand All @@ -171,7 +186,7 @@ message MintlayerAccountTxInput {
* @embed
*/
message MintlayerAccountCommandTxInput {
repeated uint32 address_n = 1; // BIP-32 path to derive the key from master node
repeated MintlayerAddressPath address_n = 1; // BIP-32 path to derive the key from master node
required string address = 2; // destination address in Base58 encoding; script_type must be PAYTOADDRESS
optional uint32 sequence = 3 [default=0xffffffff]; // sequence
required uint64 nonce = 4; // incrementing account nonce
Expand Down Expand Up @@ -257,8 +272,7 @@ message MintlayerOutputValue {
*/
message MintlayerTransferTxOutput {
optional string address = 1; // destination address in Base58 encoding; script_type must be PAYTOADDRESS
repeated uint32 address_n = 2; // BIP-32 path to derive the destination (used for change addresses)
required MintlayerOutputValue value = 3; // amount to spend in atoms for coin or token
required MintlayerOutputValue value = 2; // amount to spend in atoms for coin or token
}

/** Data type for time lock of an output
Expand All @@ -276,9 +290,8 @@ message MintlayerOutputTimeLock {
*/
message MintlayerLockThenTransferTxOutput {
optional string address = 1; // destination address in Base58 encoding; script_type must be PAYTOADDRESS
repeated uint32 address_n = 2; // BIP-32 path to derive the destination (used for change addresses)
required MintlayerOutputValue value = 3; // amount to spend in atoms
required MintlayerOutputTimeLock lock = 4; // output lock
required MintlayerOutputValue value = 2; // amount to spend in atoms
required MintlayerOutputTimeLock lock = 3; // output lock
}

/** Data type for transaction output to be signed.
Expand Down
2 changes: 1 addition & 1 deletion common/protob/messages.proto
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ enum MessageType {
MessageType_MintlayerAddress = 1001 [(wire_out) = true];
MessageType_MintlayerGetPublicKey = 1002 [(wire_in) = true];
MessageType_MintlayerPublicKey = 1003 [(wire_out) = true];
MessageType_MintlayerVerifySig = 1004 [(wire_in) = 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];
Expand Down
4 changes: 2 additions & 2 deletions core/src/all_modules.py

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

57 changes: 57 additions & 0 deletions core/src/apps/mintlayer/sign_message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from typing import TYPE_CHECKING
from apps.common.keychain import auto_keychain

from trezor.crypto import hashlib

if TYPE_CHECKING:
from trezor.enums import InputScriptType
from trezor.messages import MessageSignature, MintlayerSignMessage

from apps.common.keychain import Keychain


@auto_keychain(__name__)
async def sign_message(msg: MintlayerSignMessage, keychain: Keychain) -> MessageSignature:
from trezor import TR, utils
from trezor.crypto.curve import bip340
from trezor.enums import InputScriptType
from trezor.messages import Success
from trezor.ui.layouts import confirm_signverify, show_success
from trezor.wire import ProcessError
from trezor.messages import MessageSignature

from apps.common import coins
from apps.common.signverify import decode_message, message_digest

message = msg.message
address_n = msg.address_n
MESSAGE_MAGIC_PREFIX = b"===MINTLAYER MESSAGE BEGIN===\n"
MESSAGE_MAGIC_SUFFIX = b"\n===MINTLAYER MESSAGE END==="

await confirm_signverify(
decode_message(message),
msg.address,
verify=False,
account=None,
path=None,
chunkify=False,
)
node = keychain.derive(address_n)
pubkey = bip340.publickey(node.private_key())

msg2 = MESSAGE_MAGIC_PREFIX + message + MESSAGE_MAGIC_SUFFIX
digest = hashlib.blake2b(msg2).digest()[:32]
digest = hashlib.blake2b(digest).digest()[:32]
print(f"digest {digest}")

other_sig = bip340.sign(node.private_key(), digest)
print(f"other sig: {len(other_sig)} {other_sig}")

correct = bip340.verify(pubkey, other_sig, digest)
print(f"verify self: {correct}")

correct = bip340.verify_publickey(pubkey)
print(f"verify pk: {correct}")

return MessageSignature(signature=other_sig, address="")

6 changes: 1 addition & 5 deletions core/src/apps/mintlayer/sign_tx/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,14 +497,10 @@ def _sanitize_tx_output(txo: MintlayerTxOutput) -> MintlayerTxOutput:
from trezor.wire import DataError # local_cache_global

if txo.transfer:
address_n = txo.transfer.address_n # local_cache_attribute

if txo.transfer.value is None:
raise DataError("Missing amount field.")

if address_n and txo.transfer.address:
raise DataError("Both address and address_n provided.")
if not address_n and not txo.transfer.address:
if not txo.transfer.address:
raise DataError("Missing address")
else:
# TODO: senitize other tx outputs
Expand Down
Loading

0 comments on commit 5abd648

Please sign in to comment.