Skip to content

Commit ee35bb3

Browse files
Merge pull request #1035 from LedgerHQ/fix/apa/clone_get_pk
Fix clone get address
2 parents 537d7a1 + 4d98155 commit ee35bb3

66 files changed

Lines changed: 102 additions & 61 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/features/get_public_key/cmd_get_public_key.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ uint16_t handle_get_public_key(uint8_t p1,
1212
uint8_t dataLength,
1313
unsigned int *tx) {
1414
bip32_path_t bip32;
15-
cx_err_t error = CX_INTERNAL_ERROR;
15+
uint64_t chain_id;
1616

1717
if (!G_called_from_swap) {
1818
reset_app_context();
@@ -33,18 +33,26 @@ uint16_t handle_get_public_key(uint8_t p1,
3333
}
3434

3535
tmpCtx.publicKeyContext.getChaincode = (p2 == P2_CHAINCODE);
36-
CX_CHECK(get_public_key_string(
37-
&bip32,
38-
tmpCtx.publicKeyContext.publicKey.W,
39-
tmpCtx.publicKeyContext.address,
40-
(tmpCtx.publicKeyContext.getChaincode ? tmpCtx.publicKeyContext.chainCode : NULL),
41-
g_chain_config->chain_id));
36+
if (get_public_key_string(
37+
&bip32,
38+
tmpCtx.publicKeyContext.publicKey.W,
39+
tmpCtx.publicKeyContext.address,
40+
(tmpCtx.publicKeyContext.getChaincode ? tmpCtx.publicKeyContext.chainCode : NULL),
41+
g_chain_config->chain_id)) {
42+
return SWO_INCORRECT_DATA;
43+
}
4244

43-
uint64_t chain_id = g_chain_config->chain_id;
4445
if (dataLength >= sizeof(chain_id)) {
4546
chain_id = u64_from_BE(dataBuffer, sizeof(chain_id));
4647
dataLength -= sizeof(chain_id);
4748
dataBuffer += sizeof(chain_id);
49+
if ((g_chain_config->chain_id != ETHEREUM_MAINNET_CHAINID) &&
50+
(chain_id != g_chain_config->chain_id)) {
51+
// clones only accept their own chain ID
52+
return SWO_INCORRECT_DATA;
53+
}
54+
} else {
55+
chain_id = g_chain_config->chain_id;
4856
}
4957

5058
(void) dataBuffer; // to prevent dead increment warning
@@ -62,10 +70,7 @@ uint16_t handle_get_public_key(uint8_t p1,
6270
"0x%.*s",
6371
40,
6472
tmpCtx.publicKeyContext.address);
65-
// don't unnecessarily pass the current app's chain ID
66-
ui_display_public_key(g_chain_config->chain_id == chain_id ? NULL : &chain_id);
73+
ui_display_public_key(&chain_id);
6774
// Return code will be sent after UI approve/cancel
68-
error = 0;
69-
end:
70-
return error;
75+
return SWO_NO_RESPONSE;
7176
}

src/main.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ uint32_t eth2WithdrawalIndex;
7676

7777
const internalStorage_t N_storage_real;
7878

79-
caller_app_t *caller_app = NULL;
79+
const caller_app_t *g_caller_app = NULL;
8080
const chain_config_t *g_chain_config;
8181

8282
void reset_app_context(void) {
@@ -411,12 +411,13 @@ void coin_main(eth_libargs_t *args) {
411411
if (args->chain_config != NULL) {
412412
g_chain_config = args->chain_config;
413413
}
414-
if ((caller_app = args->caller_app) != NULL) {
414+
if (args->caller_app != NULL) {
415415
if (g_chain_config != NULL) {
416-
caller_app->type = CALLER_TYPE_CLONE;
416+
args->caller_app->type = CALLER_TYPE_CLONE;
417417
} else {
418-
caller_app->type = CALLER_TYPE_PLUGIN;
418+
args->caller_app->type = CALLER_TYPE_PLUGIN;
419419
}
420+
g_caller_app = args->caller_app;
420421
}
421422
}
422423
if (g_chain_config == NULL) {

src/nbgl/network_icons.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ const nbgl_icon_details_t *get_network_icon_from_chain_id(const uint64_t *chain_
2828
return PIC(g_network_icons[i].icon);
2929
}
3030
}
31+
#else
32+
// Nano devices don't have the array of icons, fallback on the app's icon
33+
if (*chain_id == ETHEREUM_MAINNET_CHAINID) {
34+
return &ICONGLYPH;
35+
}
3136
#endif
3237
return NULL;
3338
}
39+
40+
const nbgl_icon_details_t *get_clone_network_icon(const caller_app_t *caller_app) {
41+
if ((caller_app == NULL) || (caller_app->type != CALLER_TYPE_CLONE)) {
42+
return NULL;
43+
}
44+
return caller_app->icon;
45+
}

src/nbgl/network_icons.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
#pragma once
22

3+
#include <stdint.h>
4+
#include "nbgl_types.h"
5+
#include "caller_app.h"
6+
37
const nbgl_icon_details_t *get_network_icon_from_chain_id(const uint64_t *chain_id);
8+
const nbgl_icon_details_t *get_clone_network_icon(const caller_app_t *caller_app);

src/nbgl/ui_approve_tx.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "ui_nbgl.h"
88
#include "plugins.h"
99
#include "trusted_name.h"
10-
#include "caller_api.h"
10+
#include "caller_app.h"
1111
#include "network.h"
1212
#include "cmd_get_tx_simulation.h"
1313
#include "cmd_get_gating.h"

src/nbgl/ui_get_public_key.c

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,17 @@ void ui_display_public_key(const uint64_t *chain_id) {
2828
const char *network_name = NULL;
2929
uint8_t title_len = 1; // Initialize lengths to 1 for '\0' character
3030

31-
// - if a chain_id is given and it's - known, we specify its network name
32-
// - unknown, we don't specify anything
33-
// - if no chain_id is given we specify the APPNAME (legacy behaviour)
34-
3531
// Compute the title message length
3632
title_len += strlen(title_prefix);
37-
if (chain_id != NULL) {
38-
if (chain_is_ethereum_compatible(chain_id)) {
39-
network_name = get_network_name_from_chain_id(chain_id);
40-
}
33+
34+
if (g_chain_config->chain_id == ETHEREUM_MAINNET_CHAINID) {
35+
network_name = get_network_name_from_chain_id(chain_id);
36+
icon = get_network_icon_from_chain_id(chain_id);
4137
} else {
42-
network_name = APPNAME;
38+
network_name = get_clone_network_name(g_caller_app);
39+
icon = get_clone_network_icon(g_caller_app);
4340
}
41+
4442
if (network_name != NULL) {
4543
title_len += strlen(network_name);
4644
title_len += 1; // for '\n'
@@ -60,11 +58,6 @@ void ui_display_public_key(const uint64_t *chain_id) {
6058
}
6159
strlcat(g_titleMsg, title_sufix, title_len);
6260

63-
if (chain_id != NULL) {
64-
icon = get_network_icon_from_chain_id(chain_id);
65-
} else {
66-
icon = get_app_icon(false);
67-
}
6861
#ifndef FUZZ
6962
nbgl_useCaseAddressReview(strings.common.toAddress,
7063
NULL,

src/nbgl/ui_home.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "nbgl_use_case.h"
22
#include "app_mem_utils.h"
33
#include "ui_nbgl.h"
4-
#include "caller_api.h"
4+
#include "caller_app.h"
55
#include "network.h"
66
#include "cmd_get_tx_simulation.h"
77
#include "mem_utils.h"
@@ -226,13 +226,13 @@ static void prepare_and_display_home(const char *appname, const char *tagline, u
226226
static void get_appname_and_tagline(const char **appname, const char **tagline) {
227227
uint64_t mainnet_chain_id;
228228

229-
if (caller_app) {
230-
*appname = caller_app->name;
229+
if (g_caller_app) {
230+
*appname = g_caller_app->name;
231231

232-
if (caller_app->type == CALLER_TYPE_PLUGIN) {
233-
size_t name_len = strnlen(caller_app->name, MAX_PLUGIN_NAME_LEN + 1);
232+
if (g_caller_app->type == CALLER_TYPE_PLUGIN) {
233+
size_t name_len = strnlen(g_caller_app->name, MAX_PLUGIN_NAME_LEN + 1);
234234
if (name_len > MAX_PLUGIN_NAME_LEN) {
235-
PRINTF("Plugin caller_app->name exceeds %u bytes; tagline omitted\n",
235+
PRINTF("Plugin g_caller_app->name exceeds %u bytes; tagline omitted\n",
236236
(unsigned) MAX_PLUGIN_NAME_LEN);
237237
return;
238238
}

src/nbgl/ui_icons.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "ui_nbgl.h"
2-
#include "caller_api.h"
2+
#include "caller_app.h"
33
#include "plugins.h"
44
#include "network_icons.h"
55
#include "network.h"
@@ -12,11 +12,11 @@
1212
*/
1313
const nbgl_icon_details_t *get_app_icon(bool caller_icon) {
1414
// Plugin or clone case: prefer the caller app's own icon
15-
if (caller_icon && caller_app) {
16-
if (caller_app->icon) {
17-
return caller_app->icon;
15+
if (caller_icon && g_caller_app) {
16+
if (g_caller_app->icon) {
17+
return g_caller_app->icon;
1818
}
19-
PRINTF("%s: caller_app has no icon\n", __func__);
19+
PRINTF("%s: g_caller_app has no icon\n", __func__);
2020
}
2121
// Default: Ethereum app icon
2222
return &ICONGLYPH;
@@ -29,11 +29,11 @@ const nbgl_icon_details_t *get_app_icon(bool caller_icon) {
2929
*/
3030
const nbgl_icon_details_t *get_home_icon(void) {
3131
// Plugin or clone case: prefer the caller app's own icon
32-
if (caller_app) {
33-
if (caller_app->icon) {
34-
return caller_app->icon;
32+
if (g_caller_app) {
33+
if (g_caller_app->icon) {
34+
return g_caller_app->icon;
3535
}
36-
PRINTF("%s: caller_app has no icon\n", __func__);
36+
PRINTF("%s: g_caller_app has no icon\n", __func__);
3737
}
3838
// Default: Ethereum home icon
3939
return &ICONHOME;
@@ -48,16 +48,16 @@ const nbgl_icon_details_t *get_home_icon(void) {
4848
*/
4949
const nbgl_icon_details_t *get_tx_icon(bool fromPlugin) {
5050
if (fromPlugin && (pluginType == PLUGIN_TYPE_EXTERNAL)) {
51-
if ((caller_app != NULL) && (caller_app->name != NULL)) {
52-
if (strcmp(strings.common.toAddress, caller_app->name) == 0) {
51+
if ((g_caller_app != NULL) && (g_caller_app->name != NULL)) {
52+
if (strcmp(strings.common.toAddress, g_caller_app->name) == 0) {
5353
return get_app_icon(true);
5454
}
5555
}
5656
// icon is NULL in this case
5757
// Check with Alex if this is expected or a bug
5858
return NULL;
5959
}
60-
if ((caller_app != NULL) && !fromPlugin) {
60+
if ((g_caller_app != NULL) && !fromPlugin) {
6161
// Clone case
6262
return get_app_icon(true);
6363
}

src/network.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,10 @@ bool app_compatible_with_chain_id(const uint64_t *chain_id) {
254254
(chain_is_ethereum_compatible(&g_chain_config->chain_id) &&
255255
chain_is_ethereum_compatible(chain_id)));
256256
}
257+
258+
const char *get_clone_network_name(const caller_app_t *caller_app) {
259+
if ((caller_app == NULL) || (caller_app->type != CALLER_TYPE_CLONE)) {
260+
return NULL;
261+
}
262+
return caller_app->name;
263+
}

0 commit comments

Comments
 (0)