From 94363738feac27981534aed1523c86aab0879ecc Mon Sep 17 00:00:00 2001 From: pscott Date: Thu, 26 Aug 2021 13:59:42 +0200 Subject: [PATCH] Move u64_to_string --- src_common/ethUtils.c | 36 +++++++++++++++++++++++++----- src_common/ethUtils.h | 2 ++ src_features/signTx/logic_signTx.c | 29 +----------------------- 3 files changed, 34 insertions(+), 33 deletions(-) diff --git a/src_common/ethUtils.c b/src_common/ethUtils.c index 3ad545e..4a5c98b 100644 --- a/src_common/ethUtils.c +++ b/src_common/ethUtils.c @@ -133,6 +133,34 @@ void getEthAddressStringFromKey(cx_ecfp_public_key_t *publicKey, getEthAddressStringFromBinary(hashAddress + 12, out, sha3Context, chain_config); } +void u64_to_string(uint64_t src, char *dst, uint8_t dst_size) { + // Copy the numbers in ASCII format. + uint8_t i = 0; + do { + // Checking `i + 1` to make sure we have enough space for '\0'. + if (i + 1 >= dst_size) { + THROW(0x6502); + } + dst[i] = src % 10 + '0'; + src /= 10; + i++; + } while (src); + + // Null terminate string + dst[i] = '\0'; + + // Revert the string + i--; + uint8_t j = 0; + while (j < i) { + char tmp = dst[i]; + dst[i] = dst[j]; + dst[j] = tmp; + i--; + j++; + } +} + void getEthAddressStringFromBinary(uint8_t *address, char *out, cx_sha3_t *sha3Context, @@ -153,11 +181,9 @@ void getEthAddressStringFromBinary(uint8_t *address, break; } if (eip1191) { - snprintf((char *) locals_union.tmp, - sizeof(locals_union.tmp), - "%d0x", - chain_config->chainId); - offset = strlen((char *) locals_union.tmp); + u64_to_string(chain_config->chainId, (char *) locals_union.tmp, sizeof(locals_union.tmp)); + offset = strnlen((char *) locals_union.tmp, sizeof(locals_union.tmp)); + strlcat((char *) locals_union.tmp + offset, "0x", sizeof(locals_union.tmp) - offset); } for (i = 0; i < 20; i++) { uint8_t digit = address[i]; diff --git a/src_common/ethUtils.h b/src_common/ethUtils.h index 67712a7..501a2b7 100644 --- a/src_common/ethUtils.h +++ b/src_common/ethUtils.h @@ -44,6 +44,8 @@ void getEthAddressStringFromKey(cx_ecfp_public_key_t *publicKey, cx_sha3_t *sha3Context, chain_config_t *chain_config); +void u64_to_string(uint64_t src, char *dst, uint8_t dst_size); + void getEthAddressStringFromBinary(uint8_t *address, char *out, cx_sha3_t *sha3Context, diff --git a/src_features/signTx/logic_signTx.c b/src_features/signTx/logic_signTx.c index ae310c4..82f640a 100644 --- a/src_features/signTx/logic_signTx.c +++ b/src_features/signTx/logic_signTx.c @@ -8,6 +8,7 @@ #endif #include "eth_plugin_handler.h" #include "network.h" +#include "ethUtils.h" #define ERR_SILENT_MODE_CHECK_FAILED 0x6001 @@ -257,34 +258,6 @@ void prepareFeeDisplay() { sizeof(strings.common.maxFee)); } -static void u64_to_string(uint64_t src, char *dst, uint8_t dst_size) { - // Copy the numbers in ASCII format. - uint8_t i = 0; - do { - // Checking `i + 1` to make sure we have enough space for '\0'. - if (i + 1 >= dst_size) { - THROW(0x6502); - } - dst[i] = src % 10 + '0'; - src /= 10; - i++; - } while (src); - - // Null terminate string - dst[i] = '\0'; - - // Revert the string - i--; - uint8_t j = 0; - while (j < i) { - char tmp = dst[i]; - dst[i] = dst[j]; - dst[j] = tmp; - i--; - j++; - } -} - void prepareNetworkDisplay() { char *name = get_network_name(); if (name == NULL) {