Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NBGL porting: improve EIP191 #584

Merged
merged 10 commits into from
Jul 22, 2024
3 changes: 2 additions & 1 deletion client/src/ledger_app_clients/ethereum/eip712/InputData.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from client import keychain
from client.client import EthAppClient, EIP712FieldType

from ragger.firmware import Firmware

# global variables
app_client: EthAppClient = None
Expand Down Expand Up @@ -391,7 +392,7 @@ def next_timeout(_signum: int, _frame):


def enable_autonext():
if app_client._client.firmware.device in ("stax", "flex"):
if app_client._client.firmware in (Firmware.STAX, Firmware.FLEX):
delay = 1/3
else:
delay = 1/4
Expand Down
40 changes: 19 additions & 21 deletions client/src/ledger_app_clients/ethereum/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,29 @@ class SettingID(Enum):
DEBUG_DATA = auto()


def get_device_settings(device: str) -> list[SettingID]:
if device == "nanos":
def get_device_settings(firmware: Firmware) -> list[SettingID]:
if firmware == Firmware.NANOS:
return [
SettingID.NONCE,
SettingID.DEBUG_DATA,
]
if device in ("nanox", "nanosp", "stax", "flex"):
return [
SettingID.VERBOSE_ENS,
SettingID.VERBOSE_EIP712,
SettingID.NONCE,
SettingID.DEBUG_DATA,
]
return []
return [
SettingID.VERBOSE_ENS,
SettingID.VERBOSE_EIP712,
SettingID.NONCE,
SettingID.DEBUG_DATA,
]


def get_setting_per_page(device: str) -> int:
if device == "stax":
def get_setting_per_page(firmware: Firmware) -> int:
if firmware == Firmware.STAX:
return 3
return 2


def get_setting_position(device: str, setting: Union[NavInsID, SettingID]) -> tuple[int, int]:
settings_per_page = get_setting_per_page(device)
if device == "stax":
def get_setting_position(firmware: Firmware, setting: Union[NavInsID, SettingID]) -> tuple[int, int]:
settings_per_page = get_setting_per_page(firmware)
if firmware == Firmware.STAX:
screen_height = 672 # px
header_height = 88 # px
footer_height = 92 # px
Expand All @@ -47,15 +45,15 @@ def get_setting_position(device: str, setting: Union[NavInsID, SettingID]) -> tu
option_offset = 420 # px
usable_height = screen_height - (header_height + footer_height)
setting_height = usable_height // settings_per_page
index_in_page = get_device_settings(device).index(SettingID(setting)) % settings_per_page
index_in_page = get_device_settings(firmware).index(SettingID(setting)) % settings_per_page
return option_offset, header_height + (setting_height * index_in_page) + (setting_height // 2)


def settings_toggle(fw: Firmware, nav: Navigator, to_toggle: list[SettingID]):
def settings_toggle(firmware: Firmware, nav: Navigator, to_toggle: list[SettingID]):
moves: list[Union[NavIns, NavInsID]] = list()
settings = get_device_settings(fw.device)
settings = get_device_settings(firmware)
# Assume the app is on the home page
if fw.device.startswith("nano"):
if firmware.is_nano:
moves += [NavInsID.RIGHT_CLICK] * 2
moves += [NavInsID.BOTH_CLICK]
for setting in settings:
Expand All @@ -65,12 +63,12 @@ def settings_toggle(fw: Firmware, nav: Navigator, to_toggle: list[SettingID]):
moves += [NavInsID.BOTH_CLICK] # Back
else:
moves += [NavInsID.USE_CASE_HOME_SETTINGS]
settings_per_page = get_setting_per_page(fw.device)
settings_per_page = get_setting_per_page(firmware)
for setting in settings:
setting_idx = settings.index(setting)
if (setting_idx > 0) and (setting_idx % settings_per_page) == 0:
moves += [NavInsID.USE_CASE_SETTINGS_NEXT]
if setting in to_toggle:
moves += [NavIns(NavInsID.TOUCH, get_setting_position(fw.device, setting))]
moves += [NavIns(NavInsID.TOUCH, get_setting_position(firmware, setting))]
moves += [NavInsID.USE_CASE_SETTINGS_MULTI_PAGE_EXIT]
nav.navigate(moves, screen_change_before_first_instruction=False)
7 changes: 4 additions & 3 deletions ledger_app.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ sdk = "C"
devices = ["nanos", "nanox", "nanos+", "stax", "flex"]

[use_cases] # Coherent build options that make sense for your application
debug = "DEBUG=1"
use_test_keys = "DEBUG=1 CAL_TEST_KEY=1 DOMAIN_NAME_TEST_KEY=1 SET_PLUGIN_TEST_KEY=1 NFT_TEST_KEY=1"
cal_bypass = "DEBUG=1 BYPASS_SIGNATURES=1"
test_keys = "CAL_TEST_KEY=1 DOMAIN_NAME_TEST_KEY=1 SET_PLUGIN_TEST_KEY=1 NFT_TEST_KEY=1"
dbg_test_keys = "DEBUG=1 CAL_TEST_KEY=1 DOMAIN_NAME_TEST_KEY=1 SET_PLUGIN_TEST_KEY=1 NFT_TEST_KEY=1"
cal_bypass = "BYPASS_SIGNATURES=1"
dbg_cal_bypass = "DEBUG=1 BYPASS_SIGNATURES=1"

[tests]
unit_directory = "./tests/unit"
Expand Down
2 changes: 1 addition & 1 deletion src_nbgl/ui_approve_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static void reviewChoice(bool confirm) {
}
}

static const nbgl_icon_details_t *get_tx_icon(void) {
const nbgl_icon_details_t *get_tx_icon(void) {
const nbgl_icon_details_t *icon = NULL;

if (tx_approval_context.fromPlugin && (pluginType == EXTERNAL)) {
Expand Down
68 changes: 21 additions & 47 deletions src_nbgl/ui_confirm_parameter_selector.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,25 @@
#include "network.h"

typedef enum { PARAMETER_CONFIRMATION, SELECTOR_CONFIRMATION } e_confirmation_type;
static nbgl_contentTagValue_t pair;
static nbgl_contentTagValueList_t pairsList;

enum {
TOKEN_APPROVE = FIRST_USER_TOKEN,
};

static void reviewReject(void) {
io_seproxyhal_touch_data_cancel(NULL);
}

static void long_press_cb(int token, uint8_t index, int page) {
UNUSED(index);
UNUSED(page);
if (token == TOKEN_APPROVE) {
static void reviewChoice(bool confirm) {
if (confirm) {
io_seproxyhal_touch_data_ok(NULL);
} else {
io_seproxyhal_touch_data_cancel(NULL);
}
}

static void buildScreen(e_confirmation_type confirm_type) {
static nbgl_genericContents_t contents = {0};
static nbgl_content_t contentsList[3] = {0};
static nbgl_contentTagValue_t pair = {0};
uint8_t nbContents = 0;
uint32_t buf_size = SHARED_BUFFER_SIZE / 2;
nbgl_operationType_t op = TYPE_TRANSACTION;

pair.item = (confirm_type == PARAMETER_CONFIRMATION) ? "Parameter" : "Selector";
pair.value = strings.tmp.tmp;
pairsList.nbPairs = 1;
pairsList.pairs = &pair;
snprintf(g_stax_shared_buffer,
buf_size,
"Verify %s",
Expand All @@ -38,37 +33,16 @@ static void buildScreen(e_confirmation_type confirm_type) {
"Confirm %s",
(confirm_type == PARAMETER_CONFIRMATION) ? "parameter" : "selector");

pair.item = (confirm_type == PARAMETER_CONFIRMATION) ? "Parameter" : "Selector";
pair.value = strings.tmp.tmp;

// Title page
contentsList[nbContents].type = CENTERED_INFO;
contentsList[nbContents].content.centeredInfo.text1 = g_stax_shared_buffer;
contentsList[nbContents].content.centeredInfo.icon = get_app_icon(true);
contentsList[nbContents].content.centeredInfo.style = LARGE_CASE_INFO;
nbContents++;

// Values to be reviewed
contentsList[nbContents].type = TAG_VALUE_LIST;
contentsList[nbContents].content.tagValueList.pairs = &pair;
contentsList[nbContents].content.tagValueList.nbPairs = 1;
nbContents++;

// Approval screen
contentsList[nbContents].type = INFO_LONG_PRESS;
contentsList[nbContents].content.infoLongPress.text = g_stax_shared_buffer + buf_size;
contentsList[nbContents].content.infoLongPress.icon = get_app_icon(true);
contentsList[nbContents].content.infoLongPress.longPressText = "Hold to confirm";
contentsList[nbContents].content.infoLongPress.longPressToken = TOKEN_APPROVE;
contentsList[nbContents].content.infoLongPress.tuneId = NB_TUNES;
contentsList[nbContents].contentActionCallback = long_press_cb;
nbContents++;

contents.callbackCallNeeded = false;
contents.contentsList = contentsList;
contents.nbContents = nbContents;

nbgl_useCaseGenericReview(&contents, REJECT_BUTTON, reviewReject);
if (tmpContent.txContent.dataPresent) {
op |= BLIND_OPERATION;
}
nbgl_useCaseReview(op,
&pairsList,
get_tx_icon(),
g_stax_shared_buffer,
NULL,
g_stax_shared_buffer + buf_size,
reviewChoice);
}

void ui_confirm_parameter(void) {
Expand Down
72 changes: 20 additions & 52 deletions src_nbgl/ui_display_privacy.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,32 @@
#include "nbgl_use_case.h"
#include "nbgl_content.h"

enum {
TOKEN_APPROVE = FIRST_USER_TOKEN,
};

static void reviewReject(void) {
io_seproxyhal_touch_privacy_cancel(NULL);
}

static void long_press_cb(int token, uint8_t index, int page) {
UNUSED(index);
UNUSED(page);
if (token == TOKEN_APPROVE) {
static void reviewChoice(bool confirm) {
if (confirm) {
io_seproxyhal_touch_privacy_ok(NULL);
} else {
io_seproxyhal_touch_privacy_cancel(NULL);
}
}

static void buildFirstPage(const char *review_string) {
static nbgl_genericContents_t contents = {0};
static nbgl_content_t contentsList[3] = {0};
static nbgl_contentTagValue_t pairs[2] = {0};
uint8_t nbContents = 0;
uint8_t nbPairs = 0;

pairs[nbPairs].item = "Address";
pairs[nbPairs].value = strings.common.toAddress;
nbPairs++;
pairs[nbPairs].item = "Key";
pairs[nbPairs].value = strings.common.fullAmount;
nbPairs++;

// Title page
contentsList[nbContents].type = CENTERED_INFO;
contentsList[nbContents].content.centeredInfo.text1 = review_string;
contentsList[nbContents].content.centeredInfo.icon = get_app_icon(true);
contentsList[nbContents].content.centeredInfo.style = LARGE_CASE_INFO;
nbContents++;

// Values to be reviewed
contentsList[nbContents].type = TAG_VALUE_LIST;
contentsList[nbContents].content.tagValueList.pairs = pairs;
contentsList[nbContents].content.tagValueList.nbPairs = nbPairs;
nbContents++;

// Approval screen
contentsList[nbContents].type = INFO_LONG_PRESS;
contentsList[nbContents].content.infoLongPress.text = review_string;
contentsList[nbContents].content.infoLongPress.icon = get_app_icon(true);
contentsList[nbContents].content.infoLongPress.longPressText = "Hold to approve";
contentsList[nbContents].content.infoLongPress.longPressToken = TOKEN_APPROVE;
contentsList[nbContents].content.infoLongPress.tuneId = NB_TUNES;
contentsList[nbContents].contentActionCallback = long_press_cb;
nbContents++;

contents.callbackCallNeeded = false;
contents.contentsList = contentsList;
contents.nbContents = nbContents;

nbgl_useCaseGenericReview(&contents, REJECT_BUTTON, reviewReject);
static nbgl_contentTagValueList_t pairsList = {0};

pairs[0].item = "Address";
pairs[0].value = strings.common.toAddress;
pairs[1].item = "Key";
pairs[1].value = strings.common.fullAmount;
pairsList.nbPairs = 2;
pairsList.pairs = pairs;

nbgl_useCaseReview(TYPE_OPERATION,
&pairsList,
get_tx_icon(),
review_string,
NULL,
review_string,
reviewChoice);
}

void ui_display_privacy_public_key(void) {
Expand Down
40 changes: 0 additions & 40 deletions src_nbgl/ui_message_signing.c
Original file line number Diff line number Diff line change
@@ -1,45 +1,5 @@
#include "ui_nbgl.h"
#include "ui_signing.h"
#include "ui_logic.h"
#include "ui_message_signing.h"
#include "glyphs.h"

static void (*g_approved_func)(void) = NULL;
static void (*g_rejected_func)(void) = NULL;

static void ui_message_rejection_handler() {
nbgl_useCaseStatus("Message signing\ncancelled", false, g_rejected_func);
}

static void ui_message_confirm_rejection(void) {
nbgl_useCaseConfirm(REJECT_QUESTION(TEXT_MESSAGE),
NULL,
REJECT_CONFIRM_BUTTON,
RESUME(TEXT_MESSAGE),
ui_message_rejection_handler);
}

void ui_message_review_choice(bool confirm) {
if (confirm) {
nbgl_useCaseStatus("MESSAGE\nSIGNED", true, g_approved_func);
} else {
ui_message_confirm_rejection();
}
}

void ui_message_start(const char *title,
void (*start_func)(void),
void (*approved_func)(void),
void (*rejected_func)(void)) {
g_approved_func = approved_func;
g_rejected_func = rejected_func;
nbgl_useCaseReviewStart(&C_Review_64px,
title,
NULL,
REJECT_BUTTON,
start_func,
ui_message_confirm_rejection);
}

static void ui_message_712_approved(void) {
ui_712_approve();
Expand Down
5 changes: 0 additions & 5 deletions src_nbgl/ui_message_signing.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
#define TEXT_REVIEW_EIP712 REVIEW(TEXT_TYPED_MESSAGE)
#define TEXT_SIGN_EIP712 SIGN(TEXT_TYPED_MESSAGE)

void ui_message_review_choice(bool confirm);
void ui_message_start(const char *title,
void (*start_func)(void),
void (*approved_func)(void),
void (*rejected_func)(void));
void ui_typed_message_review_choice(bool confirm);

#endif // UI_MESSAGE_SIGNING_H_
1 change: 1 addition & 0 deletions src_nbgl/ui_nbgl.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ extern char g_stax_shared_buffer[SHARED_BUFFER_SIZE];
extern nbgl_page_t* pageContext;

const nbgl_icon_details_t* get_app_icon(bool caller_icon);
const nbgl_icon_details_t* get_tx_icon(void);

void ui_idle(void);

Expand Down
Loading
Loading