Skip to content

Commit

Permalink
Implementing Stax
Browse files Browse the repository at this point in the history
- Implementing missing stax.
- Fixing issues regarding stax, transaction view address view.
-
  • Loading branch information
TeknoPT committed Dec 13, 2023
1 parent ad69df5 commit 2425fa3
Show file tree
Hide file tree
Showing 29 changed files with 177 additions and 102 deletions.
12 changes: 11 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
{
"files.associations": {
"*.h": "c"
"*.h": "c",
"__bit_reference": "c",
"__node_handle": "c",
"bitset": "c",
"__memory": "c",
"limits": "c",
"optional": "c",
"ratio": "c",
"system_error": "c",
"tuple": "c",
"vector": "c"
},
"C_Cpp.clang_format_path": "/usr/bin/clang-format",
"editor.formatOnSave": false,
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ PATH_APP_LOAD_PARAMS = "44'/60'/0'/0/0" # purpose=coin(44) / coin_type=Testnet
# * It must at least contains one value.
# * Values can be the app ticker or anything else but should be unique.
VARIANT_PARAM = COIN
VARIANT_VALUES = phantasma phantasma_testnet
VARIANT_VALUES = phantasma #phantasma_testnet

# Enabling DEBUG flag will enable PRINTF and disable optimizations
#DEBUG = 1
Expand Down
3 changes: 3 additions & 0 deletions src/transaction/deserialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,9 @@ parser_status_e transaction_deserialize(buffer_t *buf, transaction_t *tx) {
tx->token = (uint8_t *) tx->transfer_tokens.args[1].load.buf.ptr;
tx->token_len = tx->transfer_tokens.args[1].load.buf.size;
} else if (tx->type == TRANSACTION_TYPE_CUSTOM) {

tx->from = (uint8_t *) tx->allow_gas.args[3].load.buf.ptr;
tx->from_len = tx->allow_gas.args[3].load.buf.size;
// Handle Stake Tokens
tx->name = (uint8_t *) tx->contract_call.name.buf.ptr;
tx->name_len = tx->contract_call.name.buf.size;
Expand Down
103 changes: 32 additions & 71 deletions src/ui/menu_nbgl.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

/*****************************************************************************
* Ledger App Phantasma.
* (c) 2023 Ledger SAS.
* (c) 2023 Phantasma SAS.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,6 +23,8 @@
#include "nbgl_use_case.h"

#include "../globals.h"
#include "../address.h"
#include "../handler/get_public_key.h"
#include "menu.h"

// -----------------------------------------------------------
Expand Down Expand Up @@ -54,98 +56,57 @@ void ui_menu_main(void) {
// --------------------- SETTINGS MENU -----------------------
// -----------------------------------------------------------

static const char* const INFO_TYPES[] = {"Version", "Developer"};
static const char* const INFO_CONTENTS[] = {APPVERSION, "Ledger"};
static char INFO_ADDRESS[ADDRESS_LEN];
static const char* const INFO_TYPES[] = {"Version", "Developer", "Address"};
static const char* const INFO_CONTENTS[] = {APPVERSION, "Phantasma Team", INFO_ADDRESS};

// settings switches definitions
enum { DUMMY_SWITCH_1_TOKEN = FIRST_USER_TOKEN, DUMMY_SWITCH_2_TOKEN };
enum { DUMMY_SWITCH_1_ID = 0, DUMMY_SWITCH_2_ID, SETTINGS_SWITCHES_NB };
//static nbgl_layoutSwitch_t switches[SETTINGS_SWITCHES_NB] = {0};

static nbgl_layoutSwitch_t switches[SETTINGS_SWITCHES_NB] = {0};
static void build_first_page(nbgl_pageContent_t* content) {
content->type = INFOS_LIST;
content->infosList.nbInfos = 3;
content->infosList.infoTypes = INFO_TYPES;
content->infosList.infoContents = INFO_CONTENTS;

// Set Address
explicit_bzero(&G_context, sizeof(G_context));
G_context.req_type = CONFIRM_ADDRESS;
G_context.state = STATE_NONE;

handler_get_public_key_menu();

memset(INFO_ADDRESS, 0, sizeof(INFO_ADDRESS));
uint8_t address[ADDRESS_LEN] = {0};
if (!address_from_pubkey(G_context.pk_info.raw_public_key, address, sizeof(address))) {
// Handle Errors here.
return;
}

memmove(INFO_ADDRESS, address, ADDRESS_LEN);
}

static bool nav_callback(uint8_t page, nbgl_pageContent_t* content) {
UNUSED(page);

// the first settings page contains only the version and the developer name
// of the app (shall be always on the first setting page)
if (page == 0) {
content->type = INFOS_LIST;
content->infosList.nbInfos = 2;
content->infosList.infoTypes = INFO_TYPES;
content->infosList.infoContents = INFO_CONTENTS;
}
// the second settings page contains 2 toggle setting switches
else if (page == 1) {
switches[DUMMY_SWITCH_1_ID].initState = (nbgl_state_t) N_storage.dummy1_allowed;
switches[DUMMY_SWITCH_1_ID].text = "Dummy 1";
switches[DUMMY_SWITCH_1_ID].subText = "Allow dummy 1\nin transactions";
switches[DUMMY_SWITCH_1_ID].token = DUMMY_SWITCH_1_TOKEN;
switches[DUMMY_SWITCH_1_ID].tuneId = TUNE_TAP_CASUAL;

switches[DUMMY_SWITCH_2_ID].initState = (nbgl_state_t) N_storage.dummy2_allowed;
switches[DUMMY_SWITCH_2_ID].text = "Dummy 2";
switches[DUMMY_SWITCH_2_ID].subText = "Allow dummy 2\nin transactions";
switches[DUMMY_SWITCH_2_ID].token = DUMMY_SWITCH_2_TOKEN;
switches[DUMMY_SWITCH_2_ID].tuneId = TUNE_TAP_CASUAL;

content->type = SWITCHES_LIST;
content->switchesList.nbSwitches = SETTINGS_SWITCHES_NB;
content->switchesList.switches = (nbgl_layoutSwitch_t*) switches;
build_first_page(content);
} else {
return false;
}
// valid page so return true
return true;
}

// callback for setting warning choice
static void review_warning_choice(bool confirm) {
uint8_t switch_value;
if (confirm) {
// toggle the switch value
switch_value = !N_storage.dummy2_allowed;
// store the new setting value in NVM
nvm_write((void*) &N_storage.dummy2_allowed, &switch_value, 1);
}

// return to the settings menu
ui_menu_settings();
}

static void controls_callback(int token, uint8_t index) {
UNUSED(index);
uint8_t switch_value;
if (token == DUMMY_SWITCH_1_TOKEN) {
// Dummy 1 switch touched
// toggle the switch value
switch_value = !N_storage.dummy1_allowed;
// store the new setting value in NVM
nvm_write((void*) &N_storage.dummy1_allowed, &switch_value, 1);
} else if (token == DUMMY_SWITCH_2_TOKEN) {
// Dummy 2 switch touched

// in this example we display a warning when the user wants
// to activate the dummy 2 setting
if (!N_storage.dummy2_allowed) {
// Display the warning message and ask the user to confirm
nbgl_useCaseChoice(&C_warning64px,
"Dummy 2",
"Are you sure to\nallow dummy 2\nin transactions?",
"I understand, confirm",
"Cancel",
review_warning_choice);
} else {
// toggle the switch value
switch_value = !N_storage.dummy2_allowed;
// store the new setting value in NVM
nvm_write((void*) &N_storage.dummy2_allowed, &switch_value, 1);
}
}
UNUSED(token);
}

// settings menu definition
void ui_menu_settings() {
#define TOTAL_SETTINGS_PAGE (2)
#define TOTAL_SETTINGS_PAGE (1)
#define INIT_SETTINGS_PAGE (0)
#define DISABLE_SUB_SETTINGS (false)
nbgl_useCaseSettings(APPNAME,
Expand Down
12 changes: 7 additions & 5 deletions src/ui/nbgl_display_address.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*****************************************************************************
* Ledger App Boilerplate.
* (c) 2020 Ledger SAS.
* (c) 2020 Phantasma SAS.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -36,7 +36,7 @@
#include "../transaction/types.h"
#include "../menu.h"

static char g_address[43];
static char g_address[ADDRESS_LEN];

static void confirm_address_rejection(void) {
// display a status page and go back to main
Expand Down Expand Up @@ -73,9 +73,11 @@ int ui_display_address() {
return io_send_sw(SW_DISPLAY_ADDRESS_FAIL);
}

if (format_hex(address, sizeof(address), g_address, sizeof(g_address)) == -1) {
return io_send_sw(SW_DISPLAY_ADDRESS_FAIL);
}
//if (format_hex(address, sizeof(address), g_address, sizeof(g_address)) == -1) {
// return io_send_sw(SW_DISPLAY_ADDRESS_FAIL);
//}

memmove(g_address, address, ADDRESS_LEN);

nbgl_useCaseReviewStart(&C_app_phantasma_64px,
"Verify Phantasma address",
Expand Down
141 changes: 120 additions & 21 deletions src/ui/nbgl_display_transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,18 @@
// Buffer where the transaction amount string is written
static char g_amount[30];
// Buffer where the transaction address string is written
static char g_address[43];

static nbgl_layoutTagValue_t pairs[2];
//static char g_address[43];
static char g_address[64];
static char g_token[TOKEN_LEN];
static char g_contract[TOKEN_LEN];
static char g_contract_method[CONTRACT_METHOD_LEN];
static char g_contract_method_args[240];
static char g_nexus[NEXUS_LEN];
static char g_chain[CHAIN_LEN];
static char g_txlength[TXLENGTH_LEN];
static char g_scriptlength[SCRIPTLENGTH_LEN];

static nbgl_layoutTagValue_t pairs[7];
static nbgl_layoutTagValueList_t pairList;
static nbgl_pageInfoLongPress_t infoLongPress;

Expand Down Expand Up @@ -74,14 +83,24 @@ static void review_choice(bool confirm) {

static void review_continue(void) {
// Setup data to display
pairs[0].item = "Amount";
pairs[0].value = g_amount;
pairs[1].item = "Address";
pairs[1].value = g_address;

pairs[0].item = "Token";
pairs[0].value = g_token;
pairs[1].item = "Amount";
pairs[1].value = g_amount;
pairs[2].item = "Address";
pairs[2].value = g_address;
pairs[3].item = "Transaction length";
pairs[3].value = g_txlength;
pairs[4].item = "Nexus";
pairs[4].value = g_nexus;
pairs[5].item = "Chain";
pairs[5].value = g_chain;
pairs[6].item = "Script length";
pairs[6].value = g_scriptlength;

// Setup list
pairList.nbMaxLinesForValue = 0;
pairList.nbPairs = 2;
pairList.nbPairs = 7;
pairList.pairs = pairs;

// Info long press
Expand All @@ -102,22 +121,34 @@ int ui_display_transaction() {
return io_send_sw(SW_BAD_STATE);
}

memset(g_txlength, 0, sizeof(g_txlength));
format_u64(g_txlength, sizeof(g_txlength), G_context.tx_info.raw_tx_len);

memset(g_nexus, 0, sizeof(g_nexus));
memmove(g_nexus, G_context.tx_info.transaction.nexus, G_context.tx_info.transaction.nexus_len);

memset(g_chain, 0, sizeof(g_chain));
memmove(g_chain, G_context.tx_info.transaction.chain, G_context.tx_info.transaction.chain_len);

memset(g_scriptlength, 0, sizeof(g_scriptlength));
format_u64(g_scriptlength, sizeof(g_scriptlength), G_context.tx_info.transaction.script_len);

memset(g_token, 0, sizeof(g_token));
memmove(g_token, G_context.tx_info.transaction.token, G_context.tx_info.transaction.token_len);

// Format amount and address to g_amount and g_address buffers
memset(g_amount, 0, sizeof(g_amount));
char amount[30] = {0};
if (!format_fpu64(amount,
sizeof(amount),
G_context.tx_info.transaction.value,
EXPONENT_SMALLEST_UNIT)) {
return io_send_sw(SW_DISPLAY_AMOUNT_FAIL);
}
snprintf(g_amount, sizeof(g_amount), "Phantasma %.*s", sizeof(amount), amount);
memmove(g_amount, G_context.tx_info.transaction.value, G_context.tx_info.transaction.value_len);

//snprintf(g_amount, sizeof(g_amount), "%.*s", sizeof(amount), amount);

memset(g_address, 0, sizeof(g_address));
memmove(g_address, G_context.tx_info.transaction.to, G_context.tx_info.transaction.to_len);

if (format_hex(G_context.tx_info.transaction.to, ADDRESS_LEN, g_address, sizeof(g_address)) ==
-1) {
return io_send_sw(SW_DISPLAY_ADDRESS_FAIL);
}
//if (format_hex(G_context.tx_info.transaction.to, ADDRESS_LEN, g_address, sizeof(g_address)) ==
// -1) {
// return io_send_sw(SW_DISPLAY_ADDRESS_FAIL);
//}

// Start review
nbgl_useCaseReviewStart(&C_app_phantasma_64px,
Expand All @@ -129,4 +160,72 @@ int ui_display_transaction() {
return 0;
}

static void review_continue_custom(void) {
// Setup data to display
pairs[0].item = "Nexus";
pairs[0].value = g_nexus;
pairs[1].item = "Chain";
pairs[1].value = g_chain;
pairs[2].item = "Contract";
pairs[2].value = g_contract;
pairs[3].item = "Contract Method";
pairs[3].value = g_contract_method;
pairs[4].item = "Address";
pairs[4].value = g_address;
pairs[5].item = "Contract Method Args";
pairs[5].value = g_contract_method_args;

// Setup list
pairList.nbMaxLinesForValue = 1;
pairList.nbPairs = 6;
pairList.pairs = pairs;

// Info long press
infoLongPress.icon = &C_app_phantasma_64px;
infoLongPress.text = "Sign transaction\nto send Phantasma";
infoLongPress.longPressText = "Hold to sign";

nbgl_useCaseStaticReview(&pairList, &infoLongPress, "Reject transaction", review_choice);
}

int ui_display_custom_transaction() {
if (G_context.req_type != CONFIRM_TRANSACTION || G_context.state != STATE_PARSED) {
G_context.state = STATE_NONE;
return io_send_sw(SW_BAD_STATE);
}

memset(g_nexus, 0, sizeof(g_nexus));
memmove(g_nexus, G_context.tx_info.transaction.nexus, G_context.tx_info.transaction.nexus_len);

memset(g_chain, 0, sizeof(g_chain));
memmove(g_chain, G_context.tx_info.transaction.chain, G_context.tx_info.transaction.chain_len);

memset(g_contract, 0, sizeof(g_contract));
memmove(g_contract,
(uint8_t *) G_context.tx_info.transaction.name,
G_context.tx_info.transaction.name_len);

memset(g_contract_method, 0, sizeof(g_contract_method));
memmove(g_contract_method,
(uint8_t *) G_context.tx_info.transaction.method,
G_context.tx_info.transaction.method_len);

memset(g_address, 0, sizeof(g_address));
memmove(g_address, G_context.tx_info.transaction.from, G_context.tx_info.transaction.from_len);

memset(g_contract_method_args, 0, sizeof(g_contract_method_args));
memmove(g_contract_method_args,
(uint8_t *) G_context.tx_info.transaction.output_args,
G_context.tx_info.transaction.output_args_len);

// Start review
nbgl_useCaseReviewStart(&C_app_phantasma_64px,
"Review transaction\nto send Phantasma",
NULL,
"Reject transaction",
review_continue_custom,
ask_transaction_rejection_confirmation);
return 0;
}

#endif
Binary file added tests/snapshots/stax/test_app_mainmenu/00000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/snapshots/stax/test_app_mainmenu/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 2425fa3

Please sign in to comment.