2
2
3
3
#include "eth_internals.h"
4
4
5
- void getEthAddressStringFromBinary (uint8_t * address ,
5
+ bool getEthAddressStringFromBinary (uint8_t * address ,
6
6
char * out ,
7
7
cx_sha3_t * sha3Context ,
8
8
uint64_t chainId ) {
@@ -22,7 +22,9 @@ void getEthAddressStringFromBinary(uint8_t *address,
22
22
break ;
23
23
}
24
24
if (eip1191 ) {
25
- u64_to_string (chainId , (char * ) locals_union .tmp , sizeof (locals_union .tmp ));
25
+ if (!u64_to_string (chainId , (char * ) locals_union .tmp , sizeof (locals_union .tmp ))) {
26
+ return false;
27
+ }
26
28
offset = strnlen ((char * ) locals_union .tmp , sizeof (locals_union .tmp ));
27
29
strlcat ((char * ) locals_union .tmp + offset , "0x" , sizeof (locals_union .tmp ) - offset );
28
30
offset = strnlen ((char * ) locals_union .tmp , sizeof (locals_union .tmp ));
@@ -32,13 +34,18 @@ void getEthAddressStringFromBinary(uint8_t *address,
32
34
locals_union .tmp [offset + 2 * i ] = HEXDIGITS [(digit >> 4 ) & 0x0f ];
33
35
locals_union .tmp [offset + 2 * i + 1 ] = HEXDIGITS [digit & 0x0f ];
34
36
}
35
- cx_keccak_init (sha3Context , 256 );
36
- cx_hash ((cx_hash_t * ) sha3Context ,
37
- CX_LAST ,
38
- locals_union .tmp ,
39
- offset + 40 ,
40
- locals_union .hashChecksum ,
41
- 32 );
37
+ if (cx_keccak_init_no_throw (sha3Context , 256 ) != CX_OK ) {
38
+ return false;
39
+ }
40
+
41
+ if (cx_hash_no_throw ((cx_hash_t * ) sha3Context ,
42
+ CX_LAST ,
43
+ locals_union .tmp ,
44
+ offset + 40 ,
45
+ locals_union .hashChecksum ,
46
+ 32 ) != CX_OK ) {
47
+ return false;
48
+ }
42
49
for (i = 0 ; i < 40 ; i ++ ) {
43
50
uint8_t digit = address [i / 2 ];
44
51
if ((i % 2 ) == 0 ) {
@@ -58,27 +65,47 @@ void getEthAddressStringFromBinary(uint8_t *address,
58
65
}
59
66
}
60
67
out [40 ] = '\0' ;
68
+
69
+ return true;
61
70
}
62
71
63
- void getEthAddressFromKey (cx_ecfp_public_key_t * publicKey , uint8_t * out , cx_sha3_t * sha3Context ) {
72
+ bool getEthAddressFromKey (cx_ecfp_public_key_t * publicKey , uint8_t * out , cx_sha3_t * sha3Context ) {
64
73
uint8_t hashAddress [INT256_LENGTH ];
65
- cx_keccak_init (sha3Context , 256 );
66
- cx_hash ((cx_hash_t * ) sha3Context , CX_LAST , publicKey -> W + 1 , 64 , hashAddress , 32 );
74
+
75
+ if (cx_keccak_init_no_throw (sha3Context , 256 ) != CX_OK ) {
76
+ return false;
77
+ }
78
+
79
+ if (cx_hash_no_throw ((cx_hash_t * ) sha3Context ,
80
+ CX_LAST ,
81
+ publicKey -> W + 1 ,
82
+ 64 ,
83
+ hashAddress ,
84
+ 32 ) != CX_OK ) {
85
+ return false;
86
+ }
87
+
67
88
memmove (out , hashAddress + 12 , 20 );
89
+ return true;
68
90
}
69
91
70
- void getEthDisplayableAddress (uint8_t * in ,
92
+ bool getEthDisplayableAddress (uint8_t * in ,
71
93
char * out ,
72
94
size_t out_len ,
73
95
cx_sha3_t * sha3 ,
74
96
uint64_t chainId ) {
75
97
if (out_len < 43 ) {
76
98
strlcpy (out , "ERROR" , out_len );
77
- return ;
99
+ return false ;
78
100
}
79
101
out [0 ] = '0' ;
80
102
out [1 ] = 'x' ;
81
- getEthAddressStringFromBinary (in , out + 2 , sha3 , chainId );
103
+ if (!getEthAddressStringFromBinary (in , out + 2 , sha3 , chainId )) {
104
+ strlcpy (out , "ERROR" , out_len );
105
+ return false;
106
+ }
107
+
108
+ return true;
82
109
}
83
110
84
111
bool adjustDecimals (const char * src ,
@@ -191,7 +218,7 @@ bool uint256_to_decimal(const uint8_t *value, size_t value_len, char *out, size_
191
218
return true;
192
219
}
193
220
194
- void amountToString (const uint8_t * amount ,
221
+ bool amountToString (const uint8_t * amount ,
195
222
uint8_t amount_size ,
196
223
uint8_t decimals ,
197
224
const char * ticker ,
@@ -200,7 +227,7 @@ void amountToString(const uint8_t *amount,
200
227
char tmp_buffer [100 ] = {0 };
201
228
202
229
if (uint256_to_decimal (amount , amount_size , tmp_buffer , sizeof (tmp_buffer )) == false) {
203
- THROW ( EXCEPTION_OVERFLOW ) ;
230
+ return false ;
204
231
}
205
232
206
233
uint8_t amount_len = strnlen (tmp_buffer , sizeof (tmp_buffer ));
@@ -216,19 +243,20 @@ void amountToString(const uint8_t *amount,
216
243
out_buffer + ticker_len ,
217
244
out_buffer_size - ticker_len - 1 ,
218
245
decimals ) == false) {
219
- THROW ( EXCEPTION_OVERFLOW ) ;
246
+ return false ;
220
247
}
221
248
222
249
out_buffer [out_buffer_size - 1 ] = '\0' ;
250
+ return true;
223
251
}
224
252
225
- void u64_to_string (uint64_t src , char * dst , uint8_t dst_size ) {
253
+ bool u64_to_string (uint64_t src , char * dst , uint8_t dst_size ) {
226
254
// Copy the numbers in ASCII format.
227
255
uint8_t i = 0 ;
228
256
do {
229
257
// Checking `i + 1` to make sure we have enough space for '\0'.
230
258
if (i + 1 >= dst_size ) {
231
- THROW ( 0x6502 ) ;
259
+ return false ;
232
260
}
233
261
dst [i ] = src % 10 + '0' ;
234
262
src /= 10 ;
@@ -248,6 +276,7 @@ void u64_to_string(uint64_t src, char *dst, uint8_t dst_size) {
248
276
i -- ;
249
277
j ++ ;
250
278
}
279
+ return true;
251
280
}
252
281
253
282
void copy_address (uint8_t * dst , const uint8_t * parameter , uint8_t dst_size ) {
0 commit comments