1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-26 09:28:13 +00:00

Merge pull request #196 from saleemrashid/nem

NEM Support
This commit is contained in:
Pavol Rusnak 2017-10-03 12:12:06 +02:00 committed by GitHub
commit a856542b75
15 changed files with 1597 additions and 2 deletions

View File

@ -24,6 +24,8 @@ OBJS += signing.o
OBJS += crypto.o
OBJS += ethereum.o
OBJS += ethereum_tokens.o
OBJS += nem2.o
OBJS += nem_mosaics.o
OBJS += debug.o
@ -61,6 +63,8 @@ OBJS += ../vendor/trezor-crypto/aes/aeskey.o
OBJS += ../vendor/trezor-crypto/aes/aestab.o
OBJS += ../vendor/trezor-crypto/aes/aes_modes.o
OBJS += ../vendor/trezor-crypto/nem.o
OBJS += ../vendor/trezor-qrenc/qr_encode.o
# OBJS += protob/pb_common.o
@ -88,6 +92,7 @@ CFLAGS += -DDEBUG_LINK=$(DEBUG_LINK)
CFLAGS += -DDEBUG_LOG=$(DEBUG_LOG)
CFLAGS += -DSCM_REVISION='"$(shell git rev-parse HEAD | sed 's:\(..\):\\x\1:g')"'
CFLAGS += -DUSE_ETHEREUM=1
CFLAGS += -DUSE_NEM=1
bootloader.o: ../fastflash/bootloader.bin
$(OBJCOPY) -I binary -O elf32-littlearm -B arm \

View File

@ -50,6 +50,8 @@
#include "secp256k1.h"
#include <libopencm3/stm32/flash.h>
#include "ethereum.h"
#include "nem.h"
#include "nem2.h"
#include "gettext.h"
// message methods
@ -1104,6 +1106,215 @@ void fsm_msgSetU2FCounter(SetU2FCounter *msg)
layoutHome();
}
void fsm_msgNEMGetAddress(NEMGetAddress *msg)
{
if (!msg->has_network) {
msg->network = NEM_NETWORK_MAINNET;
}
const char *network;
CHECK_PARAM((network = nem_network_name(msg->network)), _("Invalid NEM network"));
CHECK_INITIALIZED
CHECK_PIN
RESP_INIT(NEMAddress);
HDNode *node = fsm_getDerivedNode(ED25519_KECCAK_NAME, msg->address_n, msg->address_n_count);
if (!node) return;
if (!hdnode_get_nem_address(node, msg->network, resp->address))
return;
if (msg->has_show_display && msg->show_display) {
char desc[16];
strlcpy(desc, network, sizeof(desc));
strlcat(desc, ":", sizeof(desc));
bool qrcode = false;
for (;;) {
layoutAddress(resp->address, desc, qrcode);
if (protectButton(ButtonRequestType_ButtonRequest_Address, false)) {
break;
}
qrcode = !qrcode;
}
}
msg_write(MessageType_MessageType_NEMAddress, resp);
layoutHome();
}
void fsm_msgNEMSignTx(NEMSignTx *msg) {
const char *reason;
#define NEM_CHECK_PARAM(s) CHECK_PARAM( (reason = (s)) == NULL, reason)
#define NEM_CHECK_PARAM_WHEN(b, s) CHECK_PARAM(!(b) || (reason = (s)) == NULL, reason)
CHECK_PARAM(msg->has_transaction, _("No common provided"));
// Ensure exactly one transaction is provided
unsigned int provided = msg->has_transfer +
msg->has_provision_namespace +
msg->has_mosaic_creation +
msg->has_supply_change +
msg->has_aggregate_modification;
CHECK_PARAM(provided != 0, _("No transaction provided"));
CHECK_PARAM(provided == 1, _("More than one transaction provided"));
NEM_CHECK_PARAM(nem_validate_common(&msg->transaction, false));
NEM_CHECK_PARAM_WHEN(msg->has_transfer, nem_validate_transfer(&msg->transfer, msg->transaction.network));
NEM_CHECK_PARAM_WHEN(msg->has_provision_namespace, nem_validate_provision_namespace(&msg->provision_namespace, msg->transaction.network));
NEM_CHECK_PARAM_WHEN(msg->has_mosaic_creation, nem_validate_mosaic_creation(&msg->mosaic_creation, msg->transaction.network));
NEM_CHECK_PARAM_WHEN(msg->has_supply_change, nem_validate_supply_change(&msg->supply_change));
NEM_CHECK_PARAM_WHEN(msg->has_aggregate_modification, nem_validate_aggregate_modification(&msg->aggregate_modification, !msg->has_multisig));
bool cosigning = msg->has_cosigning && msg->cosigning;
if (msg->has_multisig) {
NEM_CHECK_PARAM(nem_validate_common(&msg->multisig, true));
CHECK_PARAM(msg->transaction.network == msg->multisig.network, _("Inner transaction network is different"));
} else {
CHECK_PARAM(!cosigning, _("No multisig transaction to cosign"));
}
CHECK_INITIALIZED
CHECK_PIN
const char *network = nem_network_name(msg->transaction.network);
if (msg->has_multisig) {
char address[NEM_ADDRESS_SIZE + 1];
nem_get_address(msg->multisig.signer.bytes, msg->multisig.network, address);
if (!nem_askMultisig(address, network, cosigning, msg->transaction.fee)) {
fsm_sendFailure(FailureType_Failure_ActionCancelled, _("Signing cancelled by user"));
layoutHome();
return;
}
}
RESP_INIT(NEMSignedTx);
HDNode *node = fsm_getDerivedNode(ED25519_KECCAK_NAME, msg->transaction.address_n, msg->transaction.address_n_count);
if (!node) return;
hdnode_fill_public_key(node);
const NEMTransactionCommon *common = msg->has_multisig ? &msg->multisig : &msg->transaction;
char address[NEM_ADDRESS_SIZE + 1];
hdnode_get_nem_address(node, common->network, address);
if (msg->has_transfer) {
msg->transfer.mosaics_count = nem_canonicalizeMosaics(msg->transfer.mosaics, msg->transfer.mosaics_count);
}
if (msg->has_transfer && !nem_askTransfer(common, &msg->transfer, network)) {
fsm_sendFailure(FailureType_Failure_ActionCancelled, _("Signing cancelled by user"));
layoutHome();
return;
}
if (msg->has_provision_namespace && !nem_askProvisionNamespace(common, &msg->provision_namespace, network)) {
fsm_sendFailure(FailureType_Failure_ActionCancelled, _("Signing cancelled by user"));
layoutHome();
return;
}
if (msg->has_mosaic_creation && !nem_askMosaicCreation(common, &msg->mosaic_creation, network, address)) {
fsm_sendFailure(FailureType_Failure_ActionCancelled, _("Signing cancelled by user"));
layoutHome();
return;
}
if (msg->has_supply_change && !nem_askSupplyChange(common, &msg->supply_change, network)) {
fsm_sendFailure(FailureType_Failure_ActionCancelled, _("Signing cancelled by user"));
layoutHome();
return;
}
if (msg->has_aggregate_modification && !nem_askAggregateModification(common, &msg->aggregate_modification, network, !msg->has_multisig)) {
fsm_sendFailure(FailureType_Failure_ActionCancelled, _("Signing cancelled by user"));
layoutHome();
return;
}
nem_transaction_ctx context;
nem_transaction_start(&context, &node->public_key[1], resp->data.bytes, sizeof(resp->data.bytes));
if (msg->has_multisig) {
uint8_t buffer[sizeof(resp->data.bytes)];
nem_transaction_ctx inner;
nem_transaction_start(&inner, msg->multisig.signer.bytes, buffer, sizeof(buffer));
if (msg->has_transfer && !nem_fsmTransfer(&inner, NULL, &msg->multisig, &msg->transfer)) {
layoutHome();
return;
}
if (msg->has_provision_namespace && !nem_fsmProvisionNamespace(&inner, &msg->multisig, &msg->provision_namespace)) {
layoutHome();
return;
}
if (msg->has_mosaic_creation && !nem_fsmMosaicCreation(&inner, &msg->multisig, &msg->mosaic_creation)) {
layoutHome();
return;
}
if (msg->has_supply_change && !nem_fsmSupplyChange(&inner, &msg->multisig, &msg->supply_change)) {
layoutHome();
return;
}
if (msg->has_aggregate_modification && !nem_fsmAggregateModification(&inner, &msg->multisig, &msg->aggregate_modification)) {
layoutHome();
return;
}
if (!nem_fsmMultisig(&context, &msg->transaction, &inner, cosigning)) {
layoutHome();
return;
}
} else {
if (msg->has_transfer && !nem_fsmTransfer(&context, node, &msg->transaction, &msg->transfer)) {
layoutHome();
return;
}
if (msg->has_provision_namespace && !nem_fsmProvisionNamespace(&context, &msg->transaction, &msg->provision_namespace)) {
layoutHome();
return;
}
if (msg->has_mosaic_creation && !nem_fsmMosaicCreation(&context, &msg->transaction, &msg->mosaic_creation)) {
layoutHome();
return;
}
if (msg->has_supply_change && !nem_fsmSupplyChange(&context, &msg->transaction, &msg->supply_change)) {
layoutHome();
return;
}
if (msg->has_aggregate_modification && !nem_fsmAggregateModification(&context, &msg->transaction, &msg->aggregate_modification)) {
layoutHome();
return;
}
}
resp->has_data = true;
resp->data.size = nem_transaction_end(&context, node->private_key, resp->signature.bytes);
resp->has_signature = true;
resp->signature.size = sizeof(ed25519_signature);
msg_write(MessageType_MessageType_NEMSignedTx, resp);
layoutHome();
}
#if DEBUG_LINK
void fsm_msgDebugLinkGetState(DebugLinkGetState *msg)

View File

@ -66,6 +66,9 @@ void fsm_msgEthereumTxAck(EthereumTxAck *msg);
void fsm_msgEthereumSignMessage(EthereumSignMessage *msg);
void fsm_msgEthereumVerifyMessage(EthereumVerifyMessage *msg);
void fsm_msgNEMGetAddress(NEMGetAddress *msg);
void fsm_msgNEMSignTx(NEMSignTx *msg);
// debug message functions
#if DEBUG_LINK
//void fsm_msgDebugLinkDecision(DebugLinkDecision *msg);

View File

@ -30,6 +30,8 @@
#include "qr_encode.h"
#include "timer.h"
#include "bignum.h"
#include "secp256k1.h"
#include "nem2.h"
#include "gettext.h"
#define BITCOIN_DIVISIBILITY (8)
@ -465,3 +467,180 @@ void layoutU2FDialog(const char *verb, const char *appname, const BITMAP *appico
}
layoutDialog(appicon, NULL, verb, NULL, verb, _("U2F security key?"), NULL, appname, NULL, NULL);
}
void layoutNEMDialog(const BITMAP *icon, const char *btnNo, const char *btnYes, const char *desc, const char *line1, const char *address) {
static char first_third[NEM_ADDRESS_SIZE / 3 + 1];
strlcpy(first_third, address, sizeof(first_third));
static char second_third[NEM_ADDRESS_SIZE / 3 + 1];
strlcpy(second_third, &address[NEM_ADDRESS_SIZE / 3], sizeof(second_third));
const char *third_third = &address[NEM_ADDRESS_SIZE * 2 / 3];
layoutDialogSwipe(icon,
btnNo,
btnYes,
desc,
line1,
first_third,
second_third,
third_third,
NULL,
NULL);
}
void layoutNEMTransferXEM(const char *desc, uint64_t quantity, const bignum256 *multiplier, uint64_t fee) {
char str_out[32], str_fee[32];
nem_mosaicFormatAmount(NEM_MOSAIC_DEFINITION_XEM, quantity, multiplier, str_out, sizeof(str_out));
nem_mosaicFormatAmount(NEM_MOSAIC_DEFINITION_XEM, fee, NULL, str_fee, sizeof(str_fee));
layoutDialogSwipe(&bmp_icon_question,
_("Cancel"),
_("Next"),
desc,
_("Confirm transfer of"),
str_out,
_("and network fee of"),
str_fee,
NULL,
NULL);
}
void layoutNEMNetworkFee(const char *desc, bool confirm, const char *fee1_desc, uint64_t fee1, const char *fee2_desc, uint64_t fee2) {
char str_fee1[32], str_fee2[32];
nem_mosaicFormatAmount(NEM_MOSAIC_DEFINITION_XEM, fee1, NULL, str_fee1, sizeof(str_fee1));
if (fee2_desc) {
nem_mosaicFormatAmount(NEM_MOSAIC_DEFINITION_XEM, fee2, NULL, str_fee2, sizeof(str_fee2));
}
layoutDialogSwipe(&bmp_icon_question,
_("Cancel"),
confirm ? _("Confirm") : _("Next"),
desc,
fee1_desc,
str_fee1,
fee2_desc,
fee2_desc ? str_fee2 : NULL,
NULL,
NULL);
}
void layoutNEMTransferMosaic(const NEMMosaicDefinition *definition, uint64_t quantity, const bignum256 *multiplier, uint8_t network) {
char str_out[32], str_levy[32];
nem_mosaicFormatAmount(definition, quantity, multiplier, str_out, sizeof(str_out));
if (definition->has_levy) {
nem_mosaicFormatLevy(definition, quantity, multiplier, network, str_levy, sizeof(str_levy));
}
layoutDialogSwipe(&bmp_icon_question,
_("Cancel"),
_("Next"),
definition->has_name ? definition->name : _("Mosaic"),
_("Confirm transfer of"),
str_out,
definition->has_levy ? _("and levy of") : NULL,
definition->has_levy ? str_levy : NULL,
NULL,
NULL);
}
void layoutNEMTransferUnknownMosaic(const char *namespace, const char *mosaic, uint64_t quantity, const bignum256 *multiplier) {
char mosaic_name[32];
nem_mosaicFormatName(namespace, mosaic, mosaic_name, sizeof(mosaic_name));
char str_out[32];
nem_mosaicFormatAmount(NULL, quantity, multiplier, str_out, sizeof(str_out));
char *decimal = strchr(str_out, '.');
if (decimal != NULL) {
*decimal = '\0';
}
layoutDialogSwipe(&bmp_icon_question,
_("Cancel"),
_("I take the risk"),
_("Unknown Mosaic"),
_("Confirm transfer of"),
str_out,
_("raw units of"),
mosaic_name,
NULL,
NULL);
}
void layoutNEMTransferPayload(const uint8_t *payload, size_t length, bool encrypted) {
if (payload[0] == 0xFE) {
char encoded[(length - 1) * 2 + 1];
data2hex(&payload[1], length - 1, encoded);
const char **str = split_message((uint8_t *) encoded, sizeof(encoded) - 1, 16);
layoutDialogSwipe(&bmp_icon_question, _("Cancel"), _("Next"),
encrypted ? _("Encrypted hex data") : _("Unencrypted hex data"),
str[0], str[1], str[2], str[3], NULL, NULL);
} else {
const char **str = split_message(payload, length, 16);
layoutDialogSwipe(&bmp_icon_question, _("Cancel"), _("Next"),
encrypted ? _("Encrypted message") : _("Unencrypted message"),
str[0], str[1], str[2], str[3], NULL, NULL);
}
}
void layoutNEMMosaicDescription(const char *description) {
const char **str = split_message((uint8_t *) description, strlen(description), 16);
layoutDialogSwipe(&bmp_icon_question, _("Cancel"), _("Next"),
_("Mosaic Description"),
str[0], str[1], str[2], str[3], NULL, NULL);
}
void layoutNEMLevy(const NEMMosaicDefinition *definition, uint8_t network) {
const NEMMosaicDefinition *mosaic;
if (nem_mosaicMatches(definition, definition->levy_namespace, definition->levy_mosaic, network)) {
mosaic = definition;
} else {
mosaic = nem_mosaicByName(definition->levy_namespace, definition->levy_mosaic, network);
}
char mosaic_name[32];
if (mosaic == NULL) {
nem_mosaicFormatName(definition->levy_namespace, definition->levy_mosaic, mosaic_name, sizeof(mosaic_name));
}
char str_out[32];
switch (definition->levy) {
case NEMMosaicLevy_MosaicLevy_Percentile:
bn_format_uint64(definition->fee, NULL, NULL, 0, 0, false, str_out, sizeof(str_out));
layoutDialogSwipe(&bmp_icon_question,
_("Cancel"),
_("Next"),
_("Percentile Levy"),
_("Raw levy value is"),
str_out,
_("in"),
mosaic ? (mosaic == definition ? _("the same mosaic") : mosaic->name) : mosaic_name,
NULL,
NULL);
break;
case NEMMosaicLevy_MosaicLevy_Absolute:
default:
nem_mosaicFormatAmount(mosaic, definition->fee, NULL, str_out, sizeof(str_out));
layoutDialogSwipe(&bmp_icon_question,
_("Cancel"),
_("Next"),
_("Absolute Levy"),
_("Levy is"),
str_out,
mosaic ? (mosaic == definition ? _("in the same mosaic") : NULL) : _("in raw units of"),
mosaic ? NULL : mosaic_name,
NULL,
NULL);
break;
}
}

View File

@ -23,6 +23,7 @@
#include "layout.h"
#include "types.pb.h"
#include "bitmaps.h"
#include "bignum.h"
extern void *layoutLast;
@ -47,4 +48,13 @@ void layoutSignIdentity(const IdentityType *identity, const char *challenge);
void layoutDecryptIdentity(const IdentityType *identity);
void layoutU2FDialog(const char *verb, const char *appname, const BITMAP *appicon);
void layoutNEMDialog(const BITMAP *icon, const char *btnNo, const char *btnYes, const char *desc, const char *line1, const char *address);
void layoutNEMTransferXEM(const char *desc, uint64_t quantity, const bignum256 *multiplier, uint64_t fee);
void layoutNEMNetworkFee(const char *desc, bool confirm, const char *fee1_desc, uint64_t fee1, const char *fee2_desc, uint64_t fee2);
void layoutNEMTransferMosaic(const NEMMosaicDefinition *definition, uint64_t quantity, const bignum256 *multiplier, uint8_t network);
void layoutNEMTransferUnknownMosaic(const char *namespace, const char *mosaic, uint64_t quantity, const bignum256 *multiplier);
void layoutNEMTransferPayload(const uint8_t *payload, size_t length, bool encrypted);
void layoutNEMMosaicDescription(const char *description);
void layoutNEMLevy(const NEMMosaicDefinition *definition, uint8_t network);
#endif

770
firmware/nem2.c Normal file
View File

@ -0,0 +1,770 @@
/*
* This file is part of the TREZOR project.
*
* Copyright (C) 2017 Saleem Rashid <dev@saleemrashid.com>
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "nem2.h"
#include "aes.h"
#include "fsm.h"
#include "gettext.h"
#include "layout2.h"
#include "protect.h"
#include "rng.h"
#include "secp256k1.h"
const char *nem_validate_common(NEMTransactionCommon *common, bool inner) {
if (!common->has_network) {
common->has_network = true;
common->network = NEM_NETWORK_MAINNET;
}
if (nem_network_name(common->network) == NULL) {
return inner ? _("Invalid NEM network in inner transaction") : _("Invalid NEM network");
}
if (!common->has_timestamp) {
return inner ? _("No timestamp provided in inner transaction") : _("No timestamp provided");
}
if (!common->has_fee) {
return inner ? _("No fee provided in inner transaction") : _("No fee provided");
}
if (!common->has_deadline) {
return inner ? _("No deadline provided in inner transaction") : _("No deadline provided");
}
if (inner != common->has_signer) {
return inner ? _("No signer provided in inner transaction") : _("Signer not allowed in outer transaction");
}
if (common->has_signer && common->signer.size != sizeof(ed25519_public_key)) {
return _("Invalid signer public key in inner transaction");
}
return NULL;
}
const char *nem_validate_transfer(const NEMTransfer *transfer, uint8_t network) {
if (!transfer->has_recipient) return _("No recipient provided");
if (!transfer->has_amount) return _("No amount provided");
if (transfer->has_public_key && transfer->public_key.size != sizeof(ed25519_public_key)) {
return _("Invalid recipient public key");
}
if (!nem_validate_address(transfer->recipient, network)) return _("Invalid recipient address");
for (size_t i = 0; i < transfer->mosaics_count; i++) {
const NEMMosaic *mosaic = &transfer->mosaics[i];
if (!mosaic->has_namespace) return _("No mosaic namespace provided");
if (!mosaic->has_mosaic) return _("No mosaic name provided");
if (!mosaic->has_quantity) return _("No mosaic quantity provided");
}
return NULL;
}
const char *nem_validate_provision_namespace(const NEMProvisionNamespace *provision_namespace, uint8_t network) {
if (!provision_namespace->has_namespace) return _("No namespace provided");
if (!provision_namespace->has_sink) return _("No rental sink provided");
if (!provision_namespace->has_fee) return _("No rental sink fee provided");
if (!nem_validate_address(provision_namespace->sink, network)) return _("Invalid rental sink address");
return NULL;
}
const char *nem_validate_mosaic_creation(const NEMMosaicCreation *mosaic_creation, uint8_t network) {
if (!mosaic_creation->has_definition) return _("No mosaic definition provided");
if (!mosaic_creation->has_sink) return _("No creation sink provided");
if (!mosaic_creation->has_fee) return _("No creation sink fee provided");
if (!nem_validate_address(mosaic_creation->sink, network)) return _("Invalid creation sink address");
if (mosaic_creation->definition.has_name) return _("Name not allowed in mosaic creation transactions");
if (mosaic_creation->definition.has_ticker) return _("Ticker not allowed in mosaic creation transactions");
if (mosaic_creation->definition.networks_count) return _("Networks not allowed in mosaic creation transactions");
if (!mosaic_creation->definition.has_namespace) return _("No mosaic namespace provided");
if (!mosaic_creation->definition.has_mosaic) return _("No mosaic name provided");
if (mosaic_creation->definition.has_levy) {
if (!mosaic_creation->definition.has_fee) return _("No levy address provided");
if (!mosaic_creation->definition.has_levy_address) return _("No levy address provided");
if (!mosaic_creation->definition.has_levy_namespace) return _("No levy namespace provided");
if (!mosaic_creation->definition.has_levy_mosaic) return _("No levy mosaic name provided");
if (!mosaic_creation->definition.has_divisibility) return _("No divisibility provided");
if (!mosaic_creation->definition.has_supply) return _("No supply provided");
if (!mosaic_creation->definition.has_mutable_supply) return _("No supply mutability provided");
if (!mosaic_creation->definition.has_transferable) return _("No mosaic transferability provided");
if (!mosaic_creation->definition.has_description) return _("No description provided");
if (mosaic_creation->definition.divisibility > NEM_MAX_DIVISIBILITY) return _("Invalid divisibility provided");
if (mosaic_creation->definition.supply > NEM_MAX_SUPPLY) return _("Invalid supply provided");
if (!nem_validate_address(mosaic_creation->definition.levy_address, network)) return _("Invalid levy address");
}
return NULL;
}
const char *nem_validate_supply_change(const NEMMosaicSupplyChange *supply_change) {
if (!supply_change->has_namespace) return _("No namespace provided");
if (!supply_change->has_mosaic) return _("No mosaic provided");
if (!supply_change->has_type) return _("No type provided");
if (!supply_change->has_delta) return _("No delta provided");
return NULL;
}
const char *nem_validate_aggregate_modification(const NEMAggregateModification *aggregate_modification, bool creation) {
if (creation && aggregate_modification->modifications_count == 0) {
return _("No modifications provided");
}
for (size_t i = 0; i < aggregate_modification->modifications_count; i++) {
const NEMCosignatoryModification *modification = &aggregate_modification->modifications[i];
if (!modification->has_type) return _("No modification type provided");
if (!modification->has_public_key) return _("No cosignatory public key provided");
if (modification->public_key.size != 32) return _("Invalid cosignatory public key provided");
if (creation && modification->type == NEMModificationType_CosignatoryModification_Delete) {
return _("Cannot remove cosignatory when converting account");
}
}
return NULL;
}
bool nem_askTransfer(const NEMTransactionCommon *common, const NEMTransfer *transfer, const char *desc) {
if (transfer->mosaics_count) {
const NEMMosaic *xem = NULL;
bool unknownMosaic = false;
const NEMMosaicDefinition *definitions[transfer->mosaics_count];
for (size_t i = 0; i < transfer->mosaics_count; i++) {
const NEMMosaic *mosaic = &transfer->mosaics[i];
definitions[i] = nem_mosaicByName(mosaic->namespace, mosaic->mosaic, common->network);
if (definitions[i] == NEM_MOSAIC_DEFINITION_XEM) {
xem = mosaic;
} else if (definitions[i] == NULL) {
unknownMosaic = true;
}
}
bignum256 multiplier;
bn_read_uint64(transfer->amount, &multiplier);
if (unknownMosaic) {
layoutDialogSwipe(&bmp_icon_question,
_("Cancel"),
_("I take the risk"),
_("Unknown Mosaics"),
_("Divisibility and levy"),
_("cannot be shown for"),
_("unknown mosaics!"),
NULL,
NULL,
NULL);
if (!protectButton(ButtonRequestType_ButtonRequest_ConfirmOutput, false)) {
return false;
}
}
layoutNEMTransferXEM(desc, xem ? xem->quantity : 0, &multiplier, common->fee);
if (!protectButton(ButtonRequestType_ButtonRequest_ConfirmOutput, false)) {
return false;
}
for (size_t i = 0; i < transfer->mosaics_count; i++) {
const NEMMosaic *mosaic = &transfer->mosaics[i];
if (mosaic == xem) {
continue;
}
if (definitions[i]) {
layoutNEMTransferMosaic(definitions[i], mosaic->quantity, &multiplier, common->network);
} else {
layoutNEMTransferUnknownMosaic(mosaic->namespace, mosaic->mosaic, mosaic->quantity, &multiplier);
}
if (!protectButton(ButtonRequestType_ButtonRequest_ConfirmOutput, false)) {
return false;
}
}
} else {
layoutNEMTransferXEM(desc, transfer->amount, NULL, common->fee);
if (!protectButton(ButtonRequestType_ButtonRequest_ConfirmOutput, false)) {
return false;
}
}
if (transfer->has_payload) {
layoutNEMTransferPayload(transfer->payload.bytes, transfer->payload.size, transfer->has_public_key);
if (!protectButton(ButtonRequestType_ButtonRequest_ConfirmOutput, false)) {
return false;
}
}
layoutNEMDialog(&bmp_icon_question,
_("Cancel"),
_("Confirm"),
desc,
_("Confirm transfer to"),
transfer->recipient);
if (!protectButton(ButtonRequestType_ButtonRequest_SignTx, false)) {
return false;
}
return true;
}
bool nem_fsmTransfer(nem_transaction_ctx *context, const HDNode *node, const NEMTransactionCommon *common, const NEMTransfer *transfer) {
static uint8_t encrypted[NEM_ENCRYPTED_PAYLOAD_SIZE(sizeof(transfer->payload.bytes))];
const uint8_t *payload = transfer->payload.bytes;
size_t size = transfer->payload.size;
if (transfer->has_public_key) {
if (node == NULL) {
fsm_sendFailure(FailureType_Failure_ProcessError, _("Private key unavailable for encrypted message"));
return false;
}
random_buffer(encrypted, NEM_SALT_SIZE + AES_BLOCK_SIZE);
// hdnode_nem_encrypt mutates the IV
uint8_t iv[AES_BLOCK_SIZE];
memcpy(iv, &encrypted[NEM_SALT_SIZE], AES_BLOCK_SIZE);
const uint8_t *salt = encrypted;
uint8_t *buffer = &encrypted[NEM_SALT_SIZE + AES_BLOCK_SIZE];
bool ret = hdnode_nem_encrypt(node,
transfer->public_key.bytes,
iv,
salt,
payload,
size,
buffer);
if (!ret) {
fsm_sendFailure(FailureType_Failure_ProcessError, _("Failed to encrypt payload"));
return false;
}
payload = encrypted;
size = NEM_ENCRYPTED_PAYLOAD_SIZE(size);
}
bool ret = nem_transaction_create_transfer(context,
common->network,
common->timestamp,
NULL,
common->fee,
common->deadline,
transfer->recipient,
transfer->amount,
payload,
size,
transfer->has_public_key,
transfer->mosaics_count);
if (!ret) {
fsm_sendFailure(FailureType_Failure_ProcessError, _("Failed to create transfer transaction"));
return false;
}
for (size_t i = 0; i < transfer->mosaics_count; i++) {
const NEMMosaic *mosaic = &transfer->mosaics[i];
ret = nem_transaction_write_mosaic(context,
mosaic->namespace,
mosaic->mosaic,
mosaic->quantity);
if (!ret) {
fsm_sendFailure(FailureType_Failure_ProcessError, _("Failed to attach mosaics"));
return false;
}
}
return true;
}
bool nem_askProvisionNamespace(const NEMTransactionCommon *common, const NEMProvisionNamespace *provision_namespace, const char *desc) {
layoutDialogSwipe(&bmp_icon_question,
_("Cancel"),
_("Next"),
desc,
_("Create namespace"),
provision_namespace->namespace,
provision_namespace->has_parent ? _("under namespace") : NULL,
provision_namespace->has_parent ? provision_namespace->parent : NULL,
NULL,
NULL);
if (!protectButton(ButtonRequestType_ButtonRequest_ConfirmOutput, false)) {
return false;
}
layoutNEMNetworkFee(desc, true, _("Confirm rental fee of"), provision_namespace->fee, _("and network fee of"), common->fee);
if (!protectButton(ButtonRequestType_ButtonRequest_SignTx, false)) {
return false;
}
return true;
}
bool nem_fsmProvisionNamespace(nem_transaction_ctx *context, const NEMTransactionCommon *common, const NEMProvisionNamespace *provision_namespace) {
return nem_transaction_create_provision_namespace(context,
common->network,
common->timestamp,
NULL,
common->fee,
common->deadline,
provision_namespace->namespace,
provision_namespace->has_parent ? provision_namespace->parent : NULL,
provision_namespace->sink,
provision_namespace->fee);
}
bool nem_askMosaicCreation(const NEMTransactionCommon *common, const NEMMosaicCreation *mosaic_creation, const char *desc, const char *address) {
layoutDialogSwipe(&bmp_icon_question,
_("Cancel"),
_("Next"),
desc,
_("Create mosaic"),
mosaic_creation->definition.mosaic,
_("under namespace"),
mosaic_creation->definition.namespace,
NULL,
NULL);
if (!protectButton(ButtonRequestType_ButtonRequest_ConfirmOutput, false)) {
return false;
}
layoutNEMMosaicDescription(mosaic_creation->definition.description);
if (!protectButton(ButtonRequestType_ButtonRequest_ConfirmOutput, false)) {
return false;
}
char str_out[32];
bn_format_uint64(mosaic_creation->definition.supply,
NULL,
NULL,
mosaic_creation->definition.divisibility,
mosaic_creation->definition.divisibility,
true,
str_out,
sizeof(str_out));
layoutDialogSwipe(&bmp_icon_question,
_("Cancel"),
_("Next"),
_("Properties"),
mosaic_creation->definition.mutable_supply ? _("Mutable supply:") : _("Immutable supply:"),
str_out,
_("Mosaic will be"),
mosaic_creation->definition.transferable ? _("transferable") : _("non-transferable"),
NULL,
NULL);
if (!protectButton(ButtonRequestType_ButtonRequest_ConfirmOutput, false)) {
return false;
}
if (mosaic_creation->definition.has_levy) {
layoutNEMLevy(&mosaic_creation->definition, common->network);
if (!protectButton(ButtonRequestType_ButtonRequest_ConfirmOutput, false)) {
return false;
}
if (strcmp(address, mosaic_creation->definition.levy_address) == 0) {
layoutDialogSwipe(&bmp_icon_question,
_("Cancel"),
_("Next"),
_("Levy Recipient"),
_("Levy will be paid to"),
_("yourself"),
NULL,
NULL,
NULL,
NULL);
} else {
layoutNEMDialog(&bmp_icon_question,
_("Cancel"),
_("Next"),
_("Levy Recipient"),
_("Levy will be paid to"),
mosaic_creation->definition.levy_address);
}
if (!protectButton(ButtonRequestType_ButtonRequest_ConfirmOutput, false)) {
return false;
}
}
layoutNEMNetworkFee(desc, true, _("Confirm creation fee"), mosaic_creation->fee, _("and network fee of"), common->fee);
if (!protectButton(ButtonRequestType_ButtonRequest_ConfirmOutput, false)) {
return false;
}
return true;
}
bool nem_fsmMosaicCreation(nem_transaction_ctx *context, const NEMTransactionCommon *common, const NEMMosaicCreation *mosaic_creation) {
return nem_transaction_create_mosaic_creation(context,
common->network,
common->timestamp,
NULL,
common->fee,
common->deadline,
mosaic_creation->definition.namespace,
mosaic_creation->definition.mosaic,
mosaic_creation->definition.description,
mosaic_creation->definition.divisibility,
mosaic_creation->definition.supply,
mosaic_creation->definition.mutable_supply,
mosaic_creation->definition.transferable,
mosaic_creation->definition.levy,
mosaic_creation->definition.fee,
mosaic_creation->definition.levy_address,
mosaic_creation->definition.levy_namespace,
mosaic_creation->definition.levy_mosaic,
mosaic_creation->sink,
mosaic_creation->fee);
}
bool nem_askSupplyChange(const NEMTransactionCommon *common, const NEMMosaicSupplyChange *supply_change, const char *desc) {
layoutDialogSwipe(&bmp_icon_question,
_("Cancel"),
_("Next"),
desc,
_("Modify supply for"),
supply_change->mosaic,
_("under namespace"),
supply_change->namespace,
NULL,
NULL);
if (!protectButton(ButtonRequestType_ButtonRequest_ConfirmOutput, false)) {
return false;
}
char str_out[32];
bn_format_uint64(supply_change->delta, NULL, NULL, 0, 0, false, str_out, sizeof(str_out));
layoutDialogSwipe(&bmp_icon_question,
_("Cancel"),
_("Next"),
desc,
supply_change->type == NEMSupplyChangeType_SupplyChange_Increase ? _("Increase supply by") : _("Decrease supply by"),
str_out,
_("whole units"),
NULL,
NULL,
NULL);
if (!protectButton(ButtonRequestType_ButtonRequest_ConfirmOutput, false)) {
return false;
}
layoutNEMNetworkFee(desc, true, _("Confirm network fee"), common->fee, NULL, 0);
if (!protectButton(ButtonRequestType_ButtonRequest_SignTx, false)) {
return false;
}
return true;
}
bool nem_fsmSupplyChange(nem_transaction_ctx *context, const NEMTransactionCommon *common, const NEMMosaicSupplyChange *supply_change) {
return nem_transaction_create_mosaic_supply_change(context,
common->network,
common->timestamp,
NULL,
common->fee,
common->deadline,
supply_change->namespace,
supply_change->mosaic,
supply_change->type,
supply_change->delta);
}
bool nem_askAggregateModification(const NEMTransactionCommon *common, const NEMAggregateModification *aggregate_modification, const char *desc, bool creation) {
if (creation) {
layoutDialogSwipe(&bmp_icon_question,
_("Cancel"),
_("Next"),
desc,
_("Convert account to"),
_("multisig account?"),
NULL,
NULL,
NULL,
NULL);
if (!protectButton(ButtonRequestType_ButtonRequest_ConfirmOutput, false)) {
return false;
}
}
char address[NEM_ADDRESS_SIZE + 1];
for (size_t i = 0; i < aggregate_modification->modifications_count; i++) {
const NEMCosignatoryModification *modification = &aggregate_modification->modifications[i];
nem_get_address(modification->public_key.bytes, common->network, address);
layoutNEMDialog(&bmp_icon_question,
_("Cancel"),
_("Next"),
desc,
modification->type == NEMModificationType_CosignatoryModification_Add ? _("Add cosignatory") : _("Remove cosignatory"),
address);
if (!protectButton(ButtonRequestType_ButtonRequest_ConfirmOutput, false)) {
return false;
}
}
int32_t relative_change = aggregate_modification->relative_change;
if (relative_change) {
char str_out[32];
bn_format_uint64(relative_change < 0 ? -relative_change : relative_change,
NULL,
NULL,
0,
0,
false,
str_out,
sizeof(str_out));
layoutDialogSwipe(&bmp_icon_question,
_("Cancel"),
_("Next"),
desc,
creation ? _("Set minimum") : (relative_change < 0 ? _("Decrease minimum") : _("Increase minimum")),
creation ? _("cosignatories to") : _("cosignatories by"),
str_out,
NULL,
NULL,
NULL);
if (!protectButton(ButtonRequestType_ButtonRequest_ConfirmOutput, false)) {
return false;
}
}
layoutNEMNetworkFee(desc, true, _("Confirm network fee"), common->fee, NULL, 0);
if (!protectButton(ButtonRequestType_ButtonRequest_SignTx, false)) {
return false;
}
return true;
}
bool nem_fsmAggregateModification(nem_transaction_ctx *context, const NEMTransactionCommon *common, const NEMAggregateModification *aggregate_modification) {
bool ret = nem_transaction_create_aggregate_modification(context,
common->network,
common->timestamp,
NULL,
common->fee,
common->deadline,
aggregate_modification->modifications_count,
aggregate_modification->relative_change != 0);
if (!ret) return false;
for (size_t i = 0; i < aggregate_modification->modifications_count; i++) {
const NEMCosignatoryModification *modification = &aggregate_modification->modifications[i];
ret = nem_transaction_write_cosignatory_modification(context,
modification->type,
modification->public_key.bytes);
if (!ret) return false;
}
if (aggregate_modification->relative_change) {
ret = nem_transaction_write_minimum_cosignatories(context, aggregate_modification->relative_change);
if (!ret) return false;
}
return true;
}
bool nem_askMultisig(const char *address, const char *desc, bool cosigning, uint64_t fee) {
layoutNEMDialog(&bmp_icon_question,
_("Cancel"),
_("Next"),
desc,
cosigning ? _("Cosign transaction for") : _("Initiate transaction for"),
address);
if (!protectButton(ButtonRequestType_ButtonRequest_ConfirmOutput, false)) {
return false;
}
layoutNEMNetworkFee(desc, false, _("Confirm multisig fee"), fee, NULL, 0);
if (!protectButton(ButtonRequestType_ButtonRequest_ConfirmOutput, false)) {
return false;
}
return true;
}
bool nem_fsmMultisig(nem_transaction_ctx *context, const NEMTransactionCommon *common, const nem_transaction_ctx *inner, bool cosigning) {
bool ret;
if (cosigning) {
ret = nem_transaction_create_multisig_signature(context,
common->network,
common->timestamp,
NULL,
common->fee,
common->deadline,
inner);
} else {
ret = nem_transaction_create_multisig(context,
common->network,
common->timestamp,
NULL,
common->fee,
common->deadline,
inner);
}
if (!ret) {
fsm_sendFailure(FailureType_Failure_ProcessError, _("Failed to create multisig transaction"));
return false;
}
return true;
}
const NEMMosaicDefinition *nem_mosaicByName(const char *namespace, const char *mosaic, uint8_t network) {
for (size_t i = 0; i < NEM_MOSAIC_DEFINITIONS_COUNT; i++) {
const NEMMosaicDefinition *definition = &NEM_MOSAIC_DEFINITIONS[i];
if (nem_mosaicMatches(definition, namespace, mosaic, network)) {
return definition;
}
}
return NULL;
}
static inline size_t format_amount(const NEMMosaicDefinition *definition, const bignum256 *amnt, const bignum256 *multiplier, int divisor, char *str_out, size_t size) {
bignum256 val;
memcpy(&val, amnt, sizeof(bignum256));
if (multiplier) {
bn_multiply(multiplier, &val, &secp256k1.prime);
divisor += NEM_MOSAIC_DEFINITION_XEM->divisibility;
}
return bn_format(&val,
NULL,
definition && definition->has_ticker ? definition->ticker : NULL,
definition && definition->has_divisibility ? definition->divisibility : 0,
-divisor,
false,
str_out,
size);
}
size_t nem_canonicalizeMosaics(NEMMosaic *mosaics, size_t mosaics_count) {
if (mosaics_count <= 1) {
return mosaics_count;
}
size_t actual_count = 0;
bool skip[mosaics_count];
memset(skip, 0, sizeof(skip));
// Merge duplicates
for (size_t i = 0; i < mosaics_count; i++) {
if (skip[i]) continue;
NEMMosaic *mosaic = &mosaics[actual_count];
if (actual_count++ != i) {
memcpy(mosaic, &mosaics[i], sizeof(NEMMosaic));
}
for (size_t j = i + 1; j < mosaics_count; j++) {
if (skip[j]) continue;
const NEMMosaic *new_mosaic = &mosaics[j];
if (nem_mosaicCompare(mosaic, new_mosaic) == 0) {
skip[j] = true;
mosaic->quantity += new_mosaic->quantity;
}
}
}
NEMMosaic temp;
// Sort mosaics
for (size_t i = 0; i < actual_count - 1; i++) {
NEMMosaic *a = &mosaics[i];
for (size_t j = i + 1; j < actual_count; j++) {
NEMMosaic *b = &mosaics[j];
if (nem_mosaicCompare(a, b) > 0) {
memcpy(&temp, a, sizeof(NEMMosaic));
memcpy(a, b, sizeof(NEMMosaic));
memcpy(b, &temp, sizeof(NEMMosaic));
}
}
}
return actual_count;
}
void nem_mosaicFormatAmount(const NEMMosaicDefinition *definition, uint64_t quantity, const bignum256 *multiplier, char *str_out, size_t size) {
bignum256 amnt;
bn_read_uint64(quantity, &amnt);
format_amount(definition, &amnt, multiplier, 0, str_out, size);
}
bool nem_mosaicFormatLevy(const NEMMosaicDefinition *definition, uint64_t quantity, const bignum256 *multiplier, uint8_t network, char *str_out, size_t size) {
if (!definition->has_levy || !definition->has_fee) {
return false;
}
bignum256 amnt, fee;
bn_read_uint64(quantity, &amnt);
bn_read_uint64(definition->fee, &fee);
const NEMMosaicDefinition *mosaic = nem_mosaicByName(definition->levy_namespace, definition->levy_mosaic, network);
switch (definition->levy) {
case NEMMosaicLevy_MosaicLevy_Absolute:
return format_amount(mosaic, &fee, NULL, 0, str_out, size);
case NEMMosaicLevy_MosaicLevy_Percentile:
bn_multiply(&fee, &amnt, &secp256k1.prime);
return format_amount(mosaic, &amnt, multiplier, NEM_LEVY_PERCENTILE_DIVISOR, str_out, size);
default:
return false;
}
}

107
firmware/nem2.h Normal file
View File

@ -0,0 +1,107 @@
/*
* This file is part of the TREZOR project.
*
* Copyright (C) 2017 Saleem Rashid <dev@saleemrashid.com>
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __NEM2_H__
#define __NEM2_H__
#include "nem.h"
#include "nem_mosaics.h"
#include "messages.pb.h"
#include "types.pb.h"
#include <stdbool.h>
const char *nem_validate_common(NEMTransactionCommon *common, bool inner);
const char *nem_validate_transfer(const NEMTransfer *transfer, uint8_t network);
const char *nem_validate_provision_namespace(const NEMProvisionNamespace *provision_namespace, uint8_t network);
const char *nem_validate_mosaic_creation(const NEMMosaicCreation *mosaic_creation, uint8_t network);
const char *nem_validate_supply_change(const NEMMosaicSupplyChange *supply_change);
const char *nem_validate_aggregate_modification(const NEMAggregateModification *aggregate_modification, bool creation);
bool nem_askTransfer(const NEMTransactionCommon *common, const NEMTransfer *transfer, const char *desc);
bool nem_fsmTransfer(nem_transaction_ctx *context, const HDNode *node, const NEMTransactionCommon *common, const NEMTransfer *transfer);
bool nem_askProvisionNamespace(const NEMTransactionCommon *common, const NEMProvisionNamespace *provision_namespace, const char *desc);
bool nem_fsmProvisionNamespace(nem_transaction_ctx *context, const NEMTransactionCommon *common, const NEMProvisionNamespace *provision_namespace);
bool nem_askMosaicCreation(const NEMTransactionCommon *common, const NEMMosaicCreation *mosaic_creation, const char *desc, const char *address);
bool nem_fsmMosaicCreation(nem_transaction_ctx *context, const NEMTransactionCommon *common, const NEMMosaicCreation *mosaic_creation);
bool nem_askSupplyChange(const NEMTransactionCommon *common, const NEMMosaicSupplyChange *supply_change, const char *desc);
bool nem_fsmSupplyChange(nem_transaction_ctx *context, const NEMTransactionCommon *common, const NEMMosaicSupplyChange *supply_change);
bool nem_askAggregateModification(const NEMTransactionCommon *common, const NEMAggregateModification *aggregate_modification, const char *desc, bool creation);
bool nem_fsmAggregateModification(nem_transaction_ctx *context, const NEMTransactionCommon *common, const NEMAggregateModification *aggregate_modification);
bool nem_askMultisig(const char *address, const char *desc, bool cosigning, uint64_t fee);
bool nem_fsmMultisig(nem_transaction_ctx *context, const NEMTransactionCommon *common, const nem_transaction_ctx *inner, bool cosigning);
const NEMMosaicDefinition *nem_mosaicByName(const char *namespace, const char *mosaic, uint8_t network);
size_t nem_canonicalizeMosaics(NEMMosaic *mosaics, size_t mosaics_count);
void nem_mosaicFormatAmount(const NEMMosaicDefinition *definition, uint64_t quantity, const bignum256 *multiplier, char *str_out, size_t size);
bool nem_mosaicFormatLevy(const NEMMosaicDefinition *definition, uint64_t quantity, const bignum256 *multiplier, uint8_t network, char *str_out, size_t size);
static inline void nem_mosaicFormatName(const char *namespace, const char *mosaic, char *str_out, size_t size) {
strlcpy(str_out, namespace, size);
strlcat(str_out, ".", size);
strlcat(str_out, mosaic, size);
}
static inline bool nem_mosaicMatches(const NEMMosaicDefinition *definition, const char *namespace, const char *mosaic, uint8_t network) {
if (strcmp(namespace, definition->namespace) == 0 && strcmp(mosaic, definition->mosaic) == 0) {
if (definition->networks_count == 0) {
return true;
}
for (size_t i = 0; i < definition->networks_count; i++) {
if (definition->networks[i] == network) {
return true;
}
}
}
return false;
}
static inline int nem_mosaicCompare(const NEMMosaic *a, const NEMMosaic *b) {
size_t namespace_length = strlen(a->namespace);
// Ensure that strlen(a->namespace) <= strlen(b->namespace)
if (namespace_length > strlen(b->namespace)) {
return -nem_mosaicCompare(b, a);
}
int r = strncmp(a->namespace, b->namespace, namespace_length);
if (r == 0 && b->namespace[namespace_length] != '\0') {
// The next character would be the separator
r = (':' - b->namespace[namespace_length]);
}
if (r == 0) {
// Finally compare the mosaic
r = strcmp(a->mosaic, b->mosaic);
}
return r;
}
#endif

104
firmware/nem_mosaics.c Normal file
View File

@ -0,0 +1,104 @@
// This file is automatically generated by nem_mosaics.py -- DO NOT EDIT!
#include "nem_mosaics.h"
const NEMMosaicDefinition NEM_MOSAIC_DEFINITIONS[NEM_MOSAIC_DEFINITIONS_COUNT] = {{
.has_name = true,
.name = "XEM",
.has_ticker = true,
.ticker = " XEM",
.has_namespace = true,
.namespace = "nem",
.has_mosaic = true,
.mosaic = "xem",
.has_divisibility = true,
.divisibility = 6,
},
{
.has_name = true,
.name = "DIMCOIN",
.has_ticker = true,
.ticker = " DIM",
.has_namespace = true,
.namespace = "dim",
.has_mosaic = true,
.mosaic = "coin",
.has_divisibility = true,
.divisibility = 6,
.has_levy = true,
.levy = NEMMosaicLevy_MosaicLevy_Percentile,
.has_fee = true,
.fee = 10,
.has_levy_namespace = true,
.levy_namespace = "dim",
.has_levy_mosaic = true,
.levy_mosaic = "coin",
.networks_count = 1,
.networks = { 104 },
},
{
.has_name = true,
.name = "DIM TOKEN",
.has_ticker = true,
.ticker = " DIMTOK",
.has_namespace = true,
.namespace = "dim",
.has_mosaic = true,
.mosaic = "token",
.has_divisibility = true,
.divisibility = 6,
.networks_count = 1,
.networks = { 104 },
},
{
.has_name = true,
.name = "Breeze Token",
.has_ticker = true,
.ticker = " BREEZE",
.has_namespace = true,
.namespace = "breeze",
.has_mosaic = true,
.mosaic = "breeze-token",
.has_divisibility = true,
.divisibility = 0,
.networks_count = 1,
.networks = { 104 },
},
{
.has_name = true,
.name = "PacNEM Game Credits",
.has_ticker = true,
.ticker = " PAC:HRT",
.has_namespace = true,
.namespace = "pacnem",
.has_mosaic = true,
.mosaic = "heart",
.has_divisibility = true,
.divisibility = 0,
.networks_count = 1,
.networks = { 104 },
},
{
.has_name = true,
.name = "PacNEM Score Tokens",
.has_ticker = true,
.ticker = " PAC:CHS",
.has_namespace = true,
.namespace = "pacnem",
.has_mosaic = true,
.mosaic = "cheese",
.has_divisibility = true,
.divisibility = 6,
.has_levy = true,
.levy = NEMMosaicLevy_MosaicLevy_Percentile,
.has_fee = true,
.fee = 100,
.has_levy_namespace = true,
.levy_namespace = "nem",
.has_levy_mosaic = true,
.levy_mosaic = "xem",
.networks_count = 1,
.networks = { 104 },
}};
const NEMMosaicDefinition *NEM_MOSAIC_DEFINITION_XEM = NEM_MOSAIC_DEFINITIONS;

13
firmware/nem_mosaics.h Normal file
View File

@ -0,0 +1,13 @@
// This file is automatically generated by nem_mosaics.py -- DO NOT EDIT!
#ifndef __NEM_MOSAICS_H__
#define __NEM_MOSAICS_H__
#include "types.pb.h"
#define NEM_MOSAIC_DEFINITIONS_COUNT (6)
extern const NEMMosaicDefinition NEM_MOSAIC_DEFINITIONS[NEM_MOSAIC_DEFINITIONS_COUNT];
extern const NEMMosaicDefinition *NEM_MOSAIC_DEFINITION_XEM;
#endif

57
firmware/nem_mosaics.json Normal file
View File

@ -0,0 +1,57 @@
[
{
"name": "XEM",
"ticker": " XEM",
"namespace": "nem",
"mosaic": "xem",
"divisibility": 6
},
{
"name": "DIMCOIN",
"ticker": " DIM",
"namespace": "dim",
"mosaic": "coin",
"divisibility": 6,
"levy": "MosaicLevy_Percentile",
"fee": 10,
"levy_namespace": "dim",
"levy_mosaic": "coin",
"networks": [ 104 ]
},
{
"name": "DIM TOKEN",
"ticker": " DIMTOK",
"namespace": "dim",
"mosaic": "token",
"divisibility": 6,
"networks": [ 104 ]
},
{
"name": "Breeze Token",
"ticker": " BREEZE",
"namespace": "breeze",
"mosaic": "breeze-token",
"divisibility": 0,
"networks": [ 104 ]
},
{
"name": "PacNEM Game Credits",
"ticker": " PAC:HRT",
"namespace": "pacnem",
"mosaic": "heart",
"divisibility": 0,
"networks": [ 104 ]
},
{
"name": "PacNEM Score Tokens",
"ticker": " PAC:CHS",
"namespace": "pacnem",
"mosaic": "cheese",
"divisibility": 6,
"levy": "MosaicLevy_Percentile",
"fee": 100,
"levy_namespace": "nem",
"levy_mosaic": "xem",
"networks": [ 104 ]
}
]

95
firmware/nem_mosaics.py Executable file
View File

@ -0,0 +1,95 @@
#!/usr/bin/env python3
import json, os
from google.protobuf import json_format
from itertools import chain
import protob.types_pb2 as types
HEADER_TEMPLATE = """
// This file is automatically generated by nem_mosaics.py -- DO NOT EDIT!
#ifndef __NEM_MOSAICS_H__
#define __NEM_MOSAICS_H__
#include "types.pb.h"
#define NEM_MOSAIC_DEFINITIONS_COUNT ({count})
extern const NEMMosaicDefinition NEM_MOSAIC_DEFINITIONS[NEM_MOSAIC_DEFINITIONS_COUNT];
extern const NEMMosaicDefinition *NEM_MOSAIC_DEFINITION_XEM;
#endif
""".lstrip()
CODE_TEMPLATE = """
// This file is automatically generated by nem_mosaics.py -- DO NOT EDIT!
#include "nem_mosaics.h"
const NEMMosaicDefinition NEM_MOSAIC_DEFINITIONS[NEM_MOSAIC_DEFINITIONS_COUNT] = {code};
const NEMMosaicDefinition *NEM_MOSAIC_DEFINITION_XEM = NEM_MOSAIC_DEFINITIONS;
""".lstrip()
def format_primitive(value):
if type(value) is bool:
return ("false", "true")[value]
elif type(value) in (int, float):
return str(value)
elif type(value) is str:
return json.dumps(value)
elif type(value) is list:
return "{ " + ", ".join(
format_primitive(item) for item in value
) + " }"
else:
raise TypeError
def format_struct(struct):
return "{\n" + "\n".join(
"\t.{0} = {1},".format(member, value) for member, value in struct.items()
) + "\n}"
def format_field(field, value):
if field.message_type is not None:
raise TypeError
elif field.enum_type:
return "{0}_{1}".format(field.enum_type.name, field.enum_type.values_by_number[value].name)
elif hasattr(value, "_values"):
return format_primitive(value._values)
else:
return format_primitive(value)
def field_to_meta(field, value):
if hasattr(value, "_values"):
return ("{}_count".format(field.name), format_primitive(len(value._values)))
else:
return ("has_{}".format(field.name), format_primitive(True))
def message_to_struct(_message, proto):
message = json_format.ParseDict(_message, proto())
return dict(chain.from_iterable(
(
field_to_meta(field, value),
(field.name, format_field(field, value)),
) for field, value in message.ListFields()
))
def format_message(message, proto):
return format_struct(message_to_struct(message, proto))
def format_messages(messages, proto):
return "{" + ",\n".join(
format_message(message, proto) for message in messages
) + "}"
if __name__ == "__main__":
os.chdir(os.path.abspath(os.path.dirname(__file__)))
messages = json.load(open("nem_mosaics.json"))
with open("nem_mosaics.h", "w+") as f:
f.write(HEADER_TEMPLATE.format(count=format_primitive(len(messages))))
with open("nem_mosaics.c", "w+") as f:
f.write(CODE_TEMPLATE.format(code=format_messages(messages, types.NEMMosaicDefinition)))

View File

@ -142,6 +142,13 @@ GetECDHSessionKey.ecdsa_curve_name max_size:32
ECDHSessionKey.session_key max_size:65
NEMGetAddress.address_n max_count:8
NEMAddress.address max_size:41
NEMSignedTx.data max_size:2048
NEMSignedTx.signature max_size:64
# deprecated
SimpleSignTx skip_message:true

View File

@ -36,3 +36,37 @@ IdentityType.user max_size:64
IdentityType.host max_size:64
IdentityType.port max_size:6
IdentityType.path max_size:256
NEMTransactionCommon.address_n max_count:8
NEMTransactionCommon.signer max_size:32
NEMTransfer.recipient max_size:41
NEMTransfer.public_key max_size:32
NEMTransfer.payload max_size:256
NEMTransfer.mosaics max_count:16
NEMMosaic.namespace max_size:145
NEMMosaic.mosaic max_size:33
NEMProvisionNamespace.namespace max_size:65
NEMProvisionNamespace.parent max_size:81
NEMProvisionNamespace.sink max_size:41
NEMMosaicCreation.sink max_size:41
NEMMosaicDefinition.name max_size:32
NEMMosaicDefinition.ticker max_size:16
NEMMosaicDefinition.namespace max_size:145
NEMMosaicDefinition.mosaic max_size:33
NEMMosaicDefinition.levy_address max_size:41
NEMMosaicDefinition.levy_namespace max_size:145
NEMMosaicDefinition.levy_mosaic max_size:33
NEMMosaicDefinition.description max_size:513
NEMMosaicDefinition.networks max_count:8
NEMMosaicSupplyChange.namespace max_size:145
NEMMosaicSupplyChange.mosaic max_size:33
NEMAggregateModification.modifications max_count:16
NEMCosignatoryModification.public_key max_size:32

@ -1 +1 @@
Subproject commit 5f7a1a7a5a4311fd71ed113624dd1d0fe9e72ff5
Subproject commit c41f84e221f0fc370fcad399541f6fff00f717b8

@ -1 +1 @@
Subproject commit 9dfc6a4477a7ac344ab630441e0b76d5d623a313
Subproject commit 9c919856747a6a519e5a553cc9b67f1c408337af