|
23 | 23 | #include "lcx_ecfp.h"
|
24 | 24 | #include "lcx_sha3.h"
|
25 | 25 |
|
26 |
| -void array_hexstr(char *strbuf, const void *bin, unsigned int len) { |
27 |
| - while (len--) { |
28 |
| - *strbuf++ = HEXDIGITS[((*((char *) bin)) >> 4) & 0xF]; |
29 |
| - *strbuf++ = HEXDIGITS[(*((char *) bin)) & 0xF]; |
30 |
| - bin = (const void *) ((unsigned int) bin + 1); |
| 26 | +int array_bytes_string(char *out, size_t outl, const void *value, size_t len) { |
| 27 | + if (outl <= 2) { |
| 28 | + // Need at least '0x' and 1 digit |
| 29 | + return -1; |
31 | 30 | }
|
32 |
| - *strbuf = 0; // EOS |
| 31 | + if (strlcpy(out, "0x", outl) != 2) { |
| 32 | + goto err; |
| 33 | + } |
| 34 | + if (format_hex(value, len, out + 2, outl - 2) < 0) { |
| 35 | + goto err; |
| 36 | + } |
| 37 | + return 0; |
| 38 | +err: |
| 39 | + *out = '\0'; |
| 40 | + return -1; |
33 | 41 | }
|
34 | 42 |
|
35 | 43 | uint64_t u64_from_BE(const uint8_t *in, uint8_t size) {
|
@@ -227,15 +235,15 @@ void getEthAddressFromRawKey(const uint8_t raw_pubkey[static 65],
|
227 | 235 | }
|
228 | 236 |
|
229 | 237 | void getEthAddressStringFromRawKey(const uint8_t raw_pubkey[static 65],
|
230 |
| - char out[static ADDRESS_LENGTH * 2], |
| 238 | + char out[static (ADDRESS_LENGTH * 2) + 1], |
231 | 239 | uint64_t chainId) {
|
232 | 240 | uint8_t hashAddress[CX_KECCAK_256_SIZE];
|
233 | 241 | CX_ASSERT(cx_keccak_256_hash(raw_pubkey + 1, 64, hashAddress));
|
234 | 242 | getEthAddressStringFromBinary(hashAddress + 12, out, chainId);
|
235 | 243 | }
|
236 | 244 |
|
237 | 245 | bool getEthAddressStringFromBinary(uint8_t *address,
|
238 |
| - char out[static ADDRESS_LENGTH * 2], |
| 246 | + char out[static (ADDRESS_LENGTH * 2) + 1], |
239 | 247 | uint64_t chainId) {
|
240 | 248 | // save some precious stack space
|
241 | 249 | union locals_union {
|
@@ -287,7 +295,7 @@ bool getEthAddressStringFromBinary(uint8_t *address,
|
287 | 295 | }
|
288 | 296 | }
|
289 | 297 | }
|
290 |
| - out[40] = '\0'; |
| 298 | + out[ADDRESS_LENGTH * 2] = '\0'; |
291 | 299 |
|
292 | 300 | return true;
|
293 | 301 | }
|
|
0 commit comments