@@ -110,10 +110,56 @@ static s_tlv_payload g_tlv_payload = {0};
110
110
static s_trusted_name_info g_trusted_name_info = {0 };
111
111
char g_trusted_name [TRUSTED_NAME_MAX_LENGTH + 1 ];
112
112
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
+
113
160
/**
114
161
* Checks if a trusted name matches the given parameters
115
162
*
116
- * Does not care about the trusted name source for now.
117
163
* Always wipes the content of \ref g_trusted_name_info
118
164
*
119
165
* @param[in] types_count number of given trusted name types
@@ -122,36 +168,23 @@ char g_trusted_name[TRUSTED_NAME_MAX_LENGTH + 1];
122
168
* @param[in] addr given address
123
169
* @return whether there is or not
124
170
*/
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 ;
130
178
131
179
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 ;
155
188
}
156
189
explicit_bzero (& g_trusted_name_info , sizeof (g_trusted_name_info ));
157
190
}
0 commit comments