20
20
21
21
#include "asset_info.h"
22
22
#include "common_utils.h"
23
+ #include "lcx_ecfp.h"
24
+ #include "lcx_sha3.h"
23
25
24
26
void array_hexstr (char * strbuf , const void * bin , unsigned int len ) {
25
27
while (len -- ) {
@@ -214,55 +216,23 @@ bool amountToString(const uint8_t *amount,
214
216
return true;
215
217
}
216
218
217
- bool getEthAddressFromKey (cx_ecfp_public_key_t * publicKey , uint8_t * out , cx_sha3_t * sha3Context ) {
218
- uint8_t hashAddress [INT256_LENGTH ];
219
-
220
- if (cx_keccak_init_no_throw (sha3Context , 256 ) != CX_OK ) {
221
- return false;
222
- }
223
-
224
- if (cx_hash_no_throw ((cx_hash_t * ) sha3Context ,
225
- CX_LAST ,
226
- publicKey -> W + 1 ,
227
- 64 ,
228
- hashAddress ,
229
- 32 ) != CX_OK ) {
230
- return false;
231
- }
232
-
233
- memmove (out , hashAddress + 12 , 20 );
234
- return true;
219
+ void getEthAddressFromRawKey (const uint8_t raw_pubkey [static 65 ],
220
+ uint8_t out [static ADDRESS_LENGTH ]) {
221
+ uint8_t hashAddress [CX_KECCAK_256_SIZE ];
222
+ CX_ASSERT (cx_keccak_256_hash (raw_pubkey + 1 , 64 , hashAddress ));
223
+ memmove (out , hashAddress + 12 , ADDRESS_LENGTH );
235
224
}
236
225
237
- bool getEthAddressStringFromKey (cx_ecfp_public_key_t * publicKey ,
238
- char * out ,
239
- cx_sha3_t * sha3Context ,
240
- uint64_t chainId ) {
241
- uint8_t hashAddress [INT256_LENGTH ];
242
-
243
- if (cx_keccak_init_no_throw (sha3Context , 256 ) != CX_OK ) {
244
- return false;
245
- }
246
-
247
- if (cx_hash_no_throw ((cx_hash_t * ) sha3Context ,
248
- CX_LAST ,
249
- publicKey -> W + 1 ,
250
- 64 ,
251
- hashAddress ,
252
- 32 ) != CX_OK ) {
253
- return false;
254
- }
255
-
256
- if (!getEthAddressStringFromBinary (hashAddress + 12 , out , sha3Context , chainId )) {
257
- return false;
258
- }
259
-
260
- return true;
226
+ void getEthAddressStringFromRawKey (const uint8_t raw_pubkey [static 65 ],
227
+ char out [static ADDRESS_LENGTH * 2 ],
228
+ uint64_t chainId ) {
229
+ uint8_t hashAddress [CX_KECCAK_256_SIZE ];
230
+ CX_ASSERT (cx_keccak_256_hash (raw_pubkey + 1 , 64 , hashAddress ));
231
+ getEthAddressStringFromBinary (hashAddress + 12 , out , chainId );
261
232
}
262
233
263
234
bool getEthAddressStringFromBinary (uint8_t * address ,
264
- char * out ,
265
- cx_sha3_t * sha3Context ,
235
+ char out [static ADDRESS_LENGTH * 2 ],
266
236
uint64_t chainId ) {
267
237
// save some precious stack space
268
238
union locals_union {
@@ -292,18 +262,10 @@ bool getEthAddressStringFromBinary(uint8_t *address,
292
262
locals_union .tmp [offset + 2 * i ] = HEXDIGITS [(digit >> 4 ) & 0x0f ];
293
263
locals_union .tmp [offset + 2 * i + 1 ] = HEXDIGITS [digit & 0x0f ];
294
264
}
295
- if (cx_keccak_init_no_throw ( sha3Context , 256 ) != CX_OK ) {
265
+ if (cx_keccak_256_hash ( locals_union . tmp , offset + 40 , locals_union . hashChecksum ) != CX_OK ) {
296
266
return false;
297
267
}
298
268
299
- if (cx_hash_no_throw ((cx_hash_t * ) sha3Context ,
300
- CX_LAST ,
301
- locals_union .tmp ,
302
- offset + 40 ,
303
- locals_union .hashChecksum ,
304
- 32 ) != CX_OK ) {
305
- return false;
306
- }
307
269
for (i = 0 ; i < 40 ; i ++ ) {
308
270
uint8_t digit = address [i / 2 ];
309
271
if ((i % 2 ) == 0 ) {
@@ -329,20 +291,15 @@ bool getEthAddressStringFromBinary(uint8_t *address,
329
291
330
292
/* Fills the `out` buffer with the lowercase string representation of the pubkey passed in as binary
331
293
format by `in`. (eg: uint8_t*:0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB ->
332
- char*:"0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB\0" )
333
- `sha3` context doesn't have have to be initialized prior to call.*/
334
- bool getEthDisplayableAddress (uint8_t * in ,
335
- char * out ,
336
- size_t out_len ,
337
- cx_sha3_t * sha3 ,
338
- uint64_t chainId ) {
294
+ char*:"0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB\0" ).*/
295
+ bool getEthDisplayableAddress (uint8_t * in , char * out , size_t out_len , uint64_t chainId ) {
339
296
if (out_len < 43 ) {
340
297
strlcpy (out , "ERROR" , out_len );
341
298
return false;
342
299
}
343
300
out [0 ] = '0' ;
344
301
out [1 ] = 'x' ;
345
- if (!getEthAddressStringFromBinary (in , out + 2 , sha3 , chainId )) {
302
+ if (!getEthAddressStringFromBinary (in , out + 2 , chainId )) {
346
303
strlcpy (out , "ERROR" , out_len );
347
304
return false;
348
305
}
0 commit comments