Skip to content

Commit 97d654a

Browse files
Improved trusted name matching function with sources support
1 parent 642b229 commit 97d654a

File tree

5 files changed

+74
-36
lines changed

5 files changed

+74
-36
lines changed

src_bagl/ui_flow_signTx.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,9 @@ void ux_approve_tx(bool fromPlugin) {
268268
#ifdef HAVE_TRUSTED_NAME
269269
uint64_t chain_id = get_tx_chain_id();
270270
e_name_type type = TN_TYPE_ACCOUNT;
271-
if (has_trusted_name(1, &type, &chain_id, tmpContent.txContent.destination)) {
271+
e_name_source source = TN_SOURCE_ENS;
272+
if (get_trusted_name(1, &type, 1, &source, &chain_id, tmpContent.txContent.destination) !=
273+
NULL) {
272274
ux_approval_tx_flow[step++] = &ux_trusted_name_step;
273275
if (N_storage.verbose_trusted_name) {
274276
ux_approval_tx_flow[step++] = &ux_approval_to_step;

src_features/provideTrustedName/cmd_provide_trusted_name.c

Lines changed: 62 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,56 @@ static s_tlv_payload g_tlv_payload = {0};
110110
static s_trusted_name_info g_trusted_name_info = {0};
111111
char g_trusted_name[TRUSTED_NAME_MAX_LENGTH + 1];
112112

113+
static bool matching_type(e_name_type type, uint8_t type_count, const e_name_type *types) {
114+
for (int i = 0; i < type_count; ++i) {
115+
if (type == types[i]) return true;
116+
}
117+
return false;
118+
}
119+
120+
static bool matching_source(e_name_source source,
121+
uint8_t source_count,
122+
const e_name_source *sources) {
123+
for (int i = 0; i < source_count; ++i) {
124+
if (source == sources[i]) return true;
125+
}
126+
return false;
127+
}
128+
129+
static bool matching_trusted_name(const s_trusted_name_info *trusted_name,
130+
uint8_t type_count,
131+
const e_name_type *types,
132+
uint8_t source_count,
133+
const e_name_source *sources,
134+
const uint64_t *chain_id,
135+
const uint8_t *addr) {
136+
switch (trusted_name->struct_version) {
137+
case 1:
138+
if (!matching_type(TN_TYPE_ACCOUNT, type_count, types)) {
139+
return false;
140+
}
141+
if (!chain_is_ethereum_compatible(chain_id)) {
142+
return false;
143+
}
144+
break;
145+
case 2:
146+
if (!matching_type(trusted_name->name_type, type_count, types)) {
147+
return false;
148+
}
149+
if (!matching_source(trusted_name->name_source, source_count, sources)) {
150+
return false;
151+
}
152+
if (*chain_id != trusted_name->chain_id) {
153+
return false;
154+
}
155+
break;
156+
}
157+
return memcmp(addr, trusted_name->addr, ADDRESS_LENGTH) == 0;
158+
}
159+
113160
/**
114161
* Checks if a trusted name matches the given parameters
115162
*
116-
* Does not care about the trusted name source for now.
117163
* Always wipes the content of \ref g_trusted_name_info
118164
*
119165
* @param[in] types_count number of given trusted name types
@@ -122,36 +168,23 @@ char g_trusted_name[TRUSTED_NAME_MAX_LENGTH + 1];
122168
* @param[in] addr given address
123169
* @return whether there is or not
124170
*/
125-
bool has_trusted_name(uint8_t types_count,
126-
const e_name_type *types,
127-
const uint64_t *chain_id,
128-
const uint8_t *addr) {
129-
bool ret = false;
171+
const char *get_trusted_name(uint8_t type_count,
172+
const e_name_type *types,
173+
uint8_t source_count,
174+
const e_name_source *sources,
175+
const uint64_t *chain_id,
176+
const uint8_t *addr) {
177+
const char *ret = NULL;
130178

131179
if (g_trusted_name_info.rcv_flags != 0) {
132-
for (int i = 0; i < types_count; ++i) {
133-
switch (g_trusted_name_info.struct_version) {
134-
case 1:
135-
if (types[i] == TN_TYPE_ACCOUNT) {
136-
// Check if chain ID is known to be Ethereum-compatible (same derivation
137-
// path)
138-
if ((chain_is_ethereum_compatible(chain_id)) &&
139-
(memcmp(addr, g_trusted_name_info.addr, ADDRESS_LENGTH) == 0)) {
140-
ret = true;
141-
}
142-
}
143-
break;
144-
case 2:
145-
if (types[i] == g_trusted_name_info.name_type) {
146-
if (*chain_id == g_trusted_name_info.chain_id) {
147-
ret = true;
148-
}
149-
}
150-
break;
151-
default:
152-
ret = false;
153-
}
154-
if (ret) break;
180+
if (matching_trusted_name(&g_trusted_name_info,
181+
type_count,
182+
types,
183+
source_count,
184+
sources,
185+
chain_id,
186+
addr)) {
187+
ret = g_trusted_name_info.name;
155188
}
156189
explicit_bzero(&g_trusted_name_info, sizeof(g_trusted_name_info));
157190
}

src_features/provideTrustedName/trusted_name.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ typedef enum {
2828
TN_SOURCE_COUNT,
2929
} e_name_source;
3030

31-
bool has_trusted_name(uint8_t types_count,
32-
const e_name_type *types,
33-
const uint64_t *chain_id,
34-
const uint8_t *addr);
31+
const char *get_trusted_name(uint8_t type_count,
32+
const e_name_type *types,
33+
uint8_t source_count,
34+
const e_name_source *sources,
35+
const uint64_t *chain_id,
36+
const uint8_t *addr);
3537
uint16_t handle_provide_trusted_name(uint8_t p1, const uint8_t *data, uint8_t length);
3638

3739
extern char g_trusted_name[TRUSTED_NAME_MAX_LENGTH + 1];

src_features/signMessageEIP712/ui_logic.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ static bool ui_712_format_trusted_name(const uint8_t *data, uint8_t length) {
543543
}
544544
types_bak >>= 1;
545545
}
546-
if (has_trusted_name(types_count, types, &eip712_context->chain_id, data)) {
546+
if (get_trusted_name(types_count, types, 0, NULL, &eip712_context->chain_id, data) != NULL) {
547547
strlcpy(strings.tmp.tmp, g_trusted_name, sizeof(strings.tmp.tmp));
548548
}
549549
return true;

src_nbgl/ui_approve_tx.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,9 @@ static uint8_t setTagValuePairs(void) {
149149
#ifdef HAVE_TRUSTED_NAME
150150
uint64_t chain_id = get_tx_chain_id();
151151
e_name_type type = TN_TYPE_ACCOUNT;
152+
e_name_source source = TN_SOURCE_ENS;
152153
tx_approval_context.trusted_name_match =
153-
has_trusted_name(1, &type, &chain_id, tmpContent.txContent.destination);
154+
get_trusted_name(1, &type, 1, &source, &chain_id, tmpContent.txContent.destination);
154155
if (tx_approval_context.trusted_name_match) {
155156
pairs[nbPairs].item = "To (domain)";
156157
pairs[nbPairs].value = g_trusted_name;

0 commit comments

Comments
 (0)