From 3d9da0dd161c8cad3292f188fec247b56ae0679c Mon Sep 17 00:00:00 2001 From: Francois Beutin Date: Mon, 15 Jan 2024 18:52:12 +0100 Subject: [PATCH] Move basic utility functions from ethUtils to main utils file --- src/ethUtils.c | 267 ------------------ src/ethUtils.h | 74 ----- src/eth_plugin_handler.c | 1 - src/eth_plugin_internal.c | 1 - src/handle_check_address.c | 1 - src/handle_get_printable_amount.c | 1 - src/stark_crypto.c | 1 - src/stark_utils.c | 1 - src/uint128.c | 2 +- src/uint256.c | 1 - src/utils.c | 233 ++++++++++++++- src/utils.h | 72 ++++- src_bagl/ui_flow_signMessage712_v0.c | 1 - src_bagl/ui_flow_signTx.c | 1 - src_bagl/ui_flow_stark_sign.c | 1 - src_features/getPublicKey/cmd_getPublicKey.c | 1 - .../cmd_performPrivacyOperation.c | 1 - src_features/signMessageEIP712/commands_712.c | 1 - src_features/signMessageEIP712/field_hash.c | 1 - src_features/signMessageEIP712/path.c | 1 - src_features/signMessageEIP712/type_hash.c | 1 - src_features/signMessageEIP712/ui_logic.c | 3 +- .../signMessageEIP712_v0/cmd_signMessage712.c | 1 - src_features/signTx/logic_signTx.c | 1 - .../cmd_stark_provideQuantum.c | 1 - src_features/stark_sign/cmd_stark_sign.c | 1 - src_nbgl/ui_approve_tx.c | 1 - src_nbgl/ui_sign_712_v0.c | 1 - src_nbgl/ui_stark_transfer.c | 1 - src_plugins/erc1155/erc1155_plugin.c | 1 - .../erc1155/erc1155_provide_parameters.c | 1 - src_plugins/erc1155/erc1155_ui.c | 1 - src_plugins/erc20/erc20_plugin.c | 1 - src_plugins/erc721/erc721_plugin.c | 1 - src_plugins/erc721/erc721_ui.c | 1 - src_plugins/eth2/eth2_plugin.c | 1 - src_plugins/starkware/starkware_plugin.c | 1 - tools/build_sdk.py | 2 - 38 files changed, 294 insertions(+), 390 deletions(-) delete mode 100644 src/ethUtils.c delete mode 100644 src/ethUtils.h diff --git a/src/ethUtils.c b/src/ethUtils.c deleted file mode 100644 index 02c5797..0000000 --- a/src/ethUtils.c +++ /dev/null @@ -1,267 +0,0 @@ -/******************************************************************************* - * Ledger Ethereum App - * (c) 2016-2019 Ledger - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - ********************************************************************************/ - -/** - * @brief Utilities for an Ethereum Hardware Wallet logic - * @file ethUtils.h - * @author Ledger Firmware Team - * @version 1.0 - * @date 8th of March 2016 - */ - -#include -#include -#include - -#include "shared_context.h" -#include "utils.h" -#include "ethUtils.h" -#include "chainConfig.h" -#include "ethUstream.h" -#include "network.h" - -bool getEthAddressFromKey(cx_ecfp_public_key_t *publicKey, uint8_t *out, cx_sha3_t *sha3Context) { - uint8_t hashAddress[INT256_LENGTH]; - - if (cx_keccak_init_no_throw(sha3Context, 256) != CX_OK) { - return false; - } - - if (cx_hash_no_throw((cx_hash_t *) sha3Context, - CX_LAST, - publicKey->W + 1, - 64, - hashAddress, - 32) != CX_OK) { - return false; - } - - memmove(out, hashAddress + 12, 20); - return true; -} - -bool getEthAddressStringFromKey(cx_ecfp_public_key_t *publicKey, - char *out, - cx_sha3_t *sha3Context, - uint64_t chainId) { - uint8_t hashAddress[INT256_LENGTH]; - - if (cx_keccak_init_no_throw(sha3Context, 256) != CX_OK) { - return false; - } - - if (cx_hash_no_throw((cx_hash_t *) sha3Context, - CX_LAST, - publicKey->W + 1, - 64, - hashAddress, - 32) != CX_OK) { - return false; - } - - if (!getEthAddressStringFromBinary(hashAddress + 12, out, sha3Context, chainId)) { - return false; - } - - return true; -} - -bool 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) { - return false; - } - 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++; - } - return true; -} - -bool getEthAddressStringFromBinary(uint8_t *address, - char *out, - cx_sha3_t *sha3Context, - uint64_t chainId) { - // save some precious stack space - union locals_union { - uint8_t hashChecksum[INT256_LENGTH]; - uint8_t tmp[51]; - } locals_union; - - uint8_t i; - bool eip1191 = false; - uint32_t offset = 0; - switch (chainId) { - case 30: - case 31: - eip1191 = true; - break; - } - if (eip1191) { - if (!u64_to_string(chainId, (char *) locals_union.tmp, sizeof(locals_union.tmp))) { - return false; - } - offset = strnlen((char *) locals_union.tmp, sizeof(locals_union.tmp)); - strlcat((char *) locals_union.tmp + offset, "0x", sizeof(locals_union.tmp) - offset); - offset = strnlen((char *) locals_union.tmp, sizeof(locals_union.tmp)); - } - for (i = 0; i < 20; i++) { - uint8_t digit = address[i]; - locals_union.tmp[offset + 2 * i] = HEXDIGITS[(digit >> 4) & 0x0f]; - locals_union.tmp[offset + 2 * i + 1] = HEXDIGITS[digit & 0x0f]; - } - if (cx_keccak_init_no_throw(sha3Context, 256) != CX_OK) { - return false; - } - - if (cx_hash_no_throw((cx_hash_t *) sha3Context, - CX_LAST, - locals_union.tmp, - offset + 40, - locals_union.hashChecksum, - 32) != CX_OK) { - return false; - } - for (i = 0; i < 40; i++) { - uint8_t digit = address[i / 2]; - if ((i % 2) == 0) { - digit = (digit >> 4) & 0x0f; - } else { - digit = digit & 0x0f; - } - if (digit < 10) { - out[i] = HEXDIGITS[digit]; - } else { - int v = (locals_union.hashChecksum[i / 2] >> (4 * (1 - i % 2))) & 0x0f; - if (v >= 8) { - out[i] = HEXDIGITS[digit] - 'a' + 'A'; - } else { - out[i] = HEXDIGITS[digit]; - } - } - } - out[40] = '\0'; - - return true; -} - -/* Fills the `out` buffer with the lowercase string representation of the pubkey passed in as binary -format by `in`. (eg: uint8_t*:0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB -> -char*:"0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB\0" ) -`sha3` context doesn't have have to be initialized prior to call.*/ -bool getEthDisplayableAddress(uint8_t *in, - char *out, - size_t out_len, - cx_sha3_t *sha3, - uint64_t chainId) { - if (out_len < 43) { - strlcpy(out, "ERROR", out_len); - return false; - } - out[0] = '0'; - out[1] = 'x'; - if (!getEthAddressStringFromBinary(in, out + 2, sha3, chainId)) { - strlcpy(out, "ERROR", out_len); - return false; - } - - return true; -} - -bool adjustDecimals(const char *src, - size_t srcLength, - char *target, - size_t targetLength, - uint8_t decimals) { - uint32_t startOffset; - uint32_t lastZeroOffset = 0; - uint32_t offset = 0; - if ((srcLength == 1) && (*src == '0')) { - if (targetLength < 2) { - return false; - } - target[0] = '0'; - target[1] = '\0'; - return true; - } - if (srcLength <= decimals) { - uint32_t delta = decimals - srcLength; - if (targetLength < srcLength + 1 + 2 + delta) { - return false; - } - target[offset++] = '0'; - target[offset++] = '.'; - for (uint32_t i = 0; i < delta; i++) { - target[offset++] = '0'; - } - startOffset = offset; - for (uint32_t i = 0; i < srcLength; i++) { - target[offset++] = src[i]; - } - target[offset] = '\0'; - } else { - uint32_t sourceOffset = 0; - uint32_t delta = srcLength - decimals; - if (targetLength < srcLength + 1 + 1) { - return false; - } - while (offset < delta) { - target[offset++] = src[sourceOffset++]; - } - if (decimals != 0) { - target[offset++] = '.'; - } - startOffset = offset; - while (sourceOffset < srcLength) { - target[offset++] = src[sourceOffset++]; - } - target[offset] = '\0'; - } - for (uint32_t i = startOffset; i < offset; i++) { - if (target[i] == '0') { - if (lastZeroOffset == 0) { - lastZeroOffset = i; - } - } else { - lastZeroOffset = 0; - } - } - if (lastZeroOffset != 0) { - target[lastZeroOffset] = '\0'; - if (target[lastZeroOffset - 1] == '.') { - target[lastZeroOffset - 1] = '\0'; - } - } - return true; -} diff --git a/src/ethUtils.h b/src/ethUtils.h deleted file mode 100644 index dce493a..0000000 --- a/src/ethUtils.h +++ /dev/null @@ -1,74 +0,0 @@ -/******************************************************************************* - * Ledger Ethereum App - * (c) 2016-2019 Ledger - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - ********************************************************************************/ - -#ifndef _ETHUTILS_H_ -#define _ETHUTILS_H_ - -#include - -#include "cx.h" -#include "chainConfig.h" - -#define KECCAK256_HASH_BYTESIZE 32 - -bool getEthAddressFromKey(cx_ecfp_public_key_t *publicKey, uint8_t *out, cx_sha3_t *sha3Context); - -bool getEthAddressStringFromKey(cx_ecfp_public_key_t *publicKey, - char *out, - cx_sha3_t *sha3Context, - uint64_t chainId); - -bool u64_to_string(uint64_t src, char *dst, uint8_t dst_size); - -bool getEthAddressStringFromBinary(uint8_t *address, - char *out, - cx_sha3_t *sha3Context, - uint64_t chainId); - -bool getEthDisplayableAddress(uint8_t *in, - char *out, - size_t out_len, - cx_sha3_t *sha3, - uint64_t chainId); - -bool adjustDecimals(const char *src, - size_t srcLength, - char *target, - size_t targetLength, - uint8_t decimals); - -static __attribute__((no_instrument_function)) inline int allzeroes(const void *buf, size_t n) { - uint8_t *p = (uint8_t *) buf; - for (size_t i = 0; i < n; ++i) { - if (p[i]) { - return 0; - } - } - return 1; -} -static __attribute__((no_instrument_function)) inline int ismaxint(uint8_t *buf, int n) { - for (int i = 0; i < n; ++i) { - if (buf[i] != 0xff) { - return 0; - } - } - return 1; -} - -static const char HEXDIGITS[] = "0123456789abcdef"; - -#endif // _ETHUTILS_H_ diff --git a/src/eth_plugin_handler.c b/src/eth_plugin_handler.c index bb73e9e..17683f3 100644 --- a/src/eth_plugin_handler.c +++ b/src/eth_plugin_handler.c @@ -3,7 +3,6 @@ #include "eth_plugin_internal.h" #include "shared_context.h" #include "network.h" -#include "ethUtils.h" void eth_plugin_prepare_init(ethPluginInitContract_t *init, const uint8_t *selector, diff --git a/src/eth_plugin_internal.c b/src/eth_plugin_internal.c index 075f2ec..74b25c6 100644 --- a/src/eth_plugin_internal.c +++ b/src/eth_plugin_internal.c @@ -1,6 +1,5 @@ #include #include "eth_plugin_internal.h" -#include "ethUtils.h" // allzeroes bool erc20_plugin_available_check(void); diff --git a/src/handle_check_address.c b/src/handle_check_address.c index 3fdb1f1..45c8586 100644 --- a/src/handle_check_address.c +++ b/src/handle_check_address.c @@ -1,7 +1,6 @@ #include "handle_check_address.h" #include "os.h" #include "shared_context.h" -#include "ethUtils.h" #include "string.h" #define ZERO(x) explicit_bzero(&x, sizeof(x)) diff --git a/src/handle_get_printable_amount.c b/src/handle_get_printable_amount.c index 208b76b..4503abd 100644 --- a/src/handle_get_printable_amount.c +++ b/src/handle_get_printable_amount.c @@ -4,7 +4,6 @@ #include "swap_utils.h" #include "handle_get_printable_amount.h" #include "shared_context.h" -#include "ethUtils.h" #include "utils.h" #include "uint256.h" #include "string.h" diff --git a/src/stark_crypto.c b/src/stark_crypto.c index ca738c9..898b351 100644 --- a/src/stark_crypto.c +++ b/src/stark_crypto.c @@ -3,7 +3,6 @@ #include "shared_context.h" #include "stark_utils.h" #include "utils.h" -#include "ethUtils.h" extraInfo_t *getKnownToken(uint8_t *contractAddress); diff --git a/src/stark_utils.c b/src/stark_utils.c index a585e03..13cf548 100644 --- a/src/stark_utils.c +++ b/src/stark_utils.c @@ -2,7 +2,6 @@ #include "stark_crypto.h" #include "shared_context.h" -#include "ethUtils.h" #include "uint256.h" #include "uint_common.h" diff --git a/src/uint128.c b/src/uint128.c index 0663a24..8837458 100644 --- a/src/uint128.c +++ b/src/uint128.c @@ -21,7 +21,7 @@ #include #include "uint128.h" #include "uint_common.h" -#include "ethUtils.h" // HEXDIGITS +#include "utils.h" // HEXDIGITS void readu128BE(const uint8_t *const buffer, uint128_t *const target) { UPPER_P(target) = readUint64BE(buffer); diff --git a/src/uint256.c b/src/uint256.c index 7851124..f43588a 100644 --- a/src/uint256.c +++ b/src/uint256.c @@ -22,7 +22,6 @@ #include "uint256.h" #include "uint_common.h" #include "ethUstream.h" // INT256_LENGTH -#include "ethUtils.h" // HEXDIGITS void readu256BE(const uint8_t *const buffer, uint256_t *const target) { readu128BE(buffer, &UPPER_P(target)); diff --git a/src/utils.c b/src/utils.c index c88d3c2..1697214 100644 --- a/src/utils.c +++ b/src/utils.c @@ -18,7 +18,6 @@ #include #include -#include "ethUtils.h" #include "uint128.h" #include "uint256.h" #include "tokens.h" @@ -72,6 +71,35 @@ uint64_t u64_from_BE(const uint8_t *in, uint8_t size) { return res; } +bool 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) { + return false; + } + 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++; + } + return true; +} + bool uint256_to_decimal(const uint8_t *value, size_t value_len, char *out, size_t out_len) { if (value_len > INT256_LENGTH) { // value len is bigger than INT256_LENGTH ?! @@ -115,6 +143,73 @@ bool uint256_to_decimal(const uint8_t *value, size_t value_len, char *out, size_ return true; } +bool adjustDecimals(const char *src, + size_t srcLength, + char *target, + size_t targetLength, + uint8_t decimals) { + uint32_t startOffset; + uint32_t lastZeroOffset = 0; + uint32_t offset = 0; + if ((srcLength == 1) && (*src == '0')) { + if (targetLength < 2) { + return false; + } + target[0] = '0'; + target[1] = '\0'; + return true; + } + if (srcLength <= decimals) { + uint32_t delta = decimals - srcLength; + if (targetLength < srcLength + 1 + 2 + delta) { + return false; + } + target[offset++] = '0'; + target[offset++] = '.'; + for (uint32_t i = 0; i < delta; i++) { + target[offset++] = '0'; + } + startOffset = offset; + for (uint32_t i = 0; i < srcLength; i++) { + target[offset++] = src[i]; + } + target[offset] = '\0'; + } else { + uint32_t sourceOffset = 0; + uint32_t delta = srcLength - decimals; + if (targetLength < srcLength + 1 + 1) { + return false; + } + while (offset < delta) { + target[offset++] = src[sourceOffset++]; + } + if (decimals != 0) { + target[offset++] = '.'; + } + startOffset = offset; + while (sourceOffset < srcLength) { + target[offset++] = src[sourceOffset++]; + } + target[offset] = '\0'; + } + for (uint32_t i = startOffset; i < offset; i++) { + if (target[i] == '0') { + if (lastZeroOffset == 0) { + lastZeroOffset = i; + } + } else { + lastZeroOffset = 0; + } + } + if (lastZeroOffset != 0) { + target[lastZeroOffset] = '\0'; + if (target[lastZeroOffset - 1] == '.') { + target[lastZeroOffset - 1] = '\0'; + } + } + return true; +} + bool amountToString(const uint8_t *amount, uint8_t amount_size, uint8_t decimals, @@ -146,3 +241,139 @@ bool amountToString(const uint8_t *amount, out_buffer[out_buffer_size - 1] = '\0'; return true; } + +bool getEthAddressFromKey(cx_ecfp_public_key_t *publicKey, uint8_t *out, cx_sha3_t *sha3Context) { + uint8_t hashAddress[INT256_LENGTH]; + + if (cx_keccak_init_no_throw(sha3Context, 256) != CX_OK) { + return false; + } + + if (cx_hash_no_throw((cx_hash_t *) sha3Context, + CX_LAST, + publicKey->W + 1, + 64, + hashAddress, + 32) != CX_OK) { + return false; + } + + memmove(out, hashAddress + 12, 20); + return true; +} + +bool getEthAddressStringFromKey(cx_ecfp_public_key_t *publicKey, + char *out, + cx_sha3_t *sha3Context, + uint64_t chainId) { + uint8_t hashAddress[INT256_LENGTH]; + + if (cx_keccak_init_no_throw(sha3Context, 256) != CX_OK) { + return false; + } + + if (cx_hash_no_throw((cx_hash_t *) sha3Context, + CX_LAST, + publicKey->W + 1, + 64, + hashAddress, + 32) != CX_OK) { + return false; + } + + if (!getEthAddressStringFromBinary(hashAddress + 12, out, sha3Context, chainId)) { + return false; + } + + return true; +} + +bool getEthAddressStringFromBinary(uint8_t *address, + char *out, + cx_sha3_t *sha3Context, + uint64_t chainId) { + // save some precious stack space + union locals_union { + uint8_t hashChecksum[INT256_LENGTH]; + uint8_t tmp[51]; + } locals_union; + + uint8_t i; + bool eip1191 = false; + uint32_t offset = 0; + switch (chainId) { + case 30: + case 31: + eip1191 = true; + break; + } + if (eip1191) { + if (!u64_to_string(chainId, (char *) locals_union.tmp, sizeof(locals_union.tmp))) { + return false; + } + offset = strnlen((char *) locals_union.tmp, sizeof(locals_union.tmp)); + strlcat((char *) locals_union.tmp + offset, "0x", sizeof(locals_union.tmp) - offset); + offset = strnlen((char *) locals_union.tmp, sizeof(locals_union.tmp)); + } + for (i = 0; i < 20; i++) { + uint8_t digit = address[i]; + locals_union.tmp[offset + 2 * i] = HEXDIGITS[(digit >> 4) & 0x0f]; + locals_union.tmp[offset + 2 * i + 1] = HEXDIGITS[digit & 0x0f]; + } + if (cx_keccak_init_no_throw(sha3Context, 256) != CX_OK) { + return false; + } + + if (cx_hash_no_throw((cx_hash_t *) sha3Context, + CX_LAST, + locals_union.tmp, + offset + 40, + locals_union.hashChecksum, + 32) != CX_OK) { + return false; + } + for (i = 0; i < 40; i++) { + uint8_t digit = address[i / 2]; + if ((i % 2) == 0) { + digit = (digit >> 4) & 0x0f; + } else { + digit = digit & 0x0f; + } + if (digit < 10) { + out[i] = HEXDIGITS[digit]; + } else { + int v = (locals_union.hashChecksum[i / 2] >> (4 * (1 - i % 2))) & 0x0f; + if (v >= 8) { + out[i] = HEXDIGITS[digit] - 'a' + 'A'; + } else { + out[i] = HEXDIGITS[digit]; + } + } + } + out[40] = '\0'; + + return true; +} + +/* Fills the `out` buffer with the lowercase string representation of the pubkey passed in as binary +format by `in`. (eg: uint8_t*:0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB -> +char*:"0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB\0" ) +`sha3` context doesn't have have to be initialized prior to call.*/ +bool getEthDisplayableAddress(uint8_t *in, + char *out, + size_t out_len, + cx_sha3_t *sha3, + uint64_t chainId) { + if (out_len < 43) { + strlcpy(out, "ERROR", out_len); + return false; + } + out[0] = '0'; + out[1] = 'x'; + if (!getEthAddressStringFromBinary(in, out + 2, sha3, chainId)) { + strlcpy(out, "ERROR", out_len); + return false; + } + + return true; +} diff --git a/src/utils.h b/src/utils.h index caf5246..d66c4a0 100644 --- a/src/utils.h +++ b/src/utils.h @@ -15,34 +15,82 @@ * limitations under the License. ********************************************************************************/ -#ifndef _UTILS_H_ -#define _UTILS_H_ +#pragma once +#include #include +#include "os.h" +#include "cx.h" #include "uint256.h" #define ADDRESS_LENGTH 20 #define INT128_LENGTH 16 #define INT256_LENGTH 32 +#define KECCAK256_HASH_BYTESIZE 32 + +static const char HEXDIGITS[] = "0123456789abcdef"; + #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) -void array_hexstr(char* strbuf, const void* bin, unsigned int len); +void array_hexstr(char *strbuf, const void *bin, unsigned int len); -void convertUint128BE(const uint8_t* const data, uint32_t length, uint128_t* const target); -void convertUint256BE(const uint8_t* const data, uint32_t length, uint256_t* const target); -void convertUint64BEto128(const uint8_t* const data, uint32_t length, uint128_t* const target); +void convertUint128BE(const uint8_t *const data, uint32_t length, uint128_t *const target); +void convertUint256BE(const uint8_t *const data, uint32_t length, uint256_t *const target); +void convertUint64BEto128(const uint8_t *const data, uint32_t length, uint128_t *const target); -uint64_t u64_from_BE(const uint8_t* in, uint8_t size); +uint64_t u64_from_BE(const uint8_t *in, uint8_t size); -bool uint256_to_decimal(const uint8_t* value, size_t value_len, char* out, size_t out_len); +bool u64_to_string(uint64_t src, char *dst, uint8_t dst_size); -bool amountToString(const uint8_t* amount, +bool uint256_to_decimal(const uint8_t *value, size_t value_len, char *out, size_t out_len); + +bool amountToString(const uint8_t *amount, uint8_t amount_len, uint8_t decimals, - const char* ticker, - char* out_buffer, + const char *ticker, + char *out_buffer, size_t out_buffer_size); -#endif // _UTILS_H_ +bool adjustDecimals(const char *src, + size_t srcLength, + char *target, + size_t targetLength, + uint8_t decimals); + +bool getEthAddressFromKey(cx_ecfp_public_key_t *publicKey, uint8_t *out, cx_sha3_t *sha3Context); + +bool getEthAddressStringFromKey(cx_ecfp_public_key_t *publicKey, + char *out, + cx_sha3_t *sha3Context, + uint64_t chainId); + +bool getEthAddressStringFromBinary(uint8_t *address, + char *out, + cx_sha3_t *sha3Context, + uint64_t chainId); + +bool getEthDisplayableAddress(uint8_t *in, + char *out, + size_t out_len, + cx_sha3_t *sha3, + uint64_t chainId); + +static __attribute__((no_instrument_function)) inline int allzeroes(const void *buf, size_t n) { + uint8_t *p = (uint8_t *) buf; + for (size_t i = 0; i < n; ++i) { + if (p[i]) { + return 0; + } + } + return 1; +} +static __attribute__((no_instrument_function)) inline int ismaxint(uint8_t *buf, int n) { + for (int i = 0; i < n; ++i) { + if (buf[i] != 0xff) { + return 0; + } + } + return 1; +} diff --git a/src_bagl/ui_flow_signMessage712_v0.c b/src_bagl/ui_flow_signMessage712_v0.c index 230852e..9cdda70 100644 --- a/src_bagl/ui_flow_signMessage712_v0.c +++ b/src_bagl/ui_flow_signMessage712_v0.c @@ -1,7 +1,6 @@ #include "shared_context.h" #include "ui_callbacks.h" #include "common_712.h" -#include "ethUtils.h" void prepare_domain_hash_v0() { snprintf(strings.tmp.tmp, diff --git a/src_bagl/ui_flow_signTx.c b/src_bagl/ui_flow_signTx.c index 137b134..98a5418 100644 --- a/src_bagl/ui_flow_signTx.c +++ b/src_bagl/ui_flow_signTx.c @@ -7,7 +7,6 @@ #include "eth_plugin_handler.h" #include "ui_plugin.h" #include "common_ui.h" -#include "ethUtils.h" #include "plugins.h" #include "domain_name.h" #include "ui_domain_name.h" diff --git a/src_bagl/ui_flow_stark_sign.c b/src_bagl/ui_flow_stark_sign.c index 03174c7..18905e6 100644 --- a/src_bagl/ui_flow_stark_sign.c +++ b/src_bagl/ui_flow_stark_sign.c @@ -2,7 +2,6 @@ #include "shared_context.h" #include "ui_callbacks.h" -#include "ethUtils.h" #include "starkDisplayUtils.h" #include "apdu_constants.h" diff --git a/src_features/getPublicKey/cmd_getPublicKey.c b/src_features/getPublicKey/cmd_getPublicKey.c index eae16d5..d3d149f 100644 --- a/src_features/getPublicKey/cmd_getPublicKey.c +++ b/src_features/getPublicKey/cmd_getPublicKey.c @@ -2,7 +2,6 @@ #include "apdu_constants.h" #include "utils.h" #include "feature_getPublicKey.h" -#include "ethUtils.h" #include "common_ui.h" #include "os_io_seproxyhal.h" diff --git a/src_features/performPrivacyOperation/cmd_performPrivacyOperation.c b/src_features/performPrivacyOperation/cmd_performPrivacyOperation.c index aebb987..c1f0369 100644 --- a/src_features/performPrivacyOperation/cmd_performPrivacyOperation.c +++ b/src_features/performPrivacyOperation/cmd_performPrivacyOperation.c @@ -1,6 +1,5 @@ #include "shared_context.h" #include "apdu_constants.h" -#include "ethUtils.h" #include "feature_performPrivacyOperation.h" #include "common_ui.h" diff --git a/src_features/signMessageEIP712/commands_712.c b/src_features/signMessageEIP712/commands_712.c index a1cc45f..8a21dc3 100644 --- a/src_features/signMessageEIP712/commands_712.c +++ b/src_features/signMessageEIP712/commands_712.c @@ -13,7 +13,6 @@ #include "schema_hash.h" #include "filtering.h" #include "common_712.h" -#include "ethUtils.h" // allzeroes #include "common_ui.h" // ui_idle /** diff --git a/src_features/signMessageEIP712/field_hash.c b/src_features/signMessageEIP712/field_hash.c index cc57dae..f0225d7 100644 --- a/src_features/signMessageEIP712/field_hash.c +++ b/src_features/signMessageEIP712/field_hash.c @@ -8,7 +8,6 @@ #include "mem_utils.h" #include "shared_context.h" #include "ui_logic.h" -#include "ethUtils.h" // KECCAK256_HASH_BYTESIZE #include "context_712.h" // contract_addr #include "utils.h" // u64_from_BE #include "apdu_constants.h" // APDU response codes diff --git a/src_features/signMessageEIP712/path.c b/src_features/signMessageEIP712/path.c index 621d11d..3c823c9 100644 --- a/src_features/signMessageEIP712/path.c +++ b/src_features/signMessageEIP712/path.c @@ -8,7 +8,6 @@ #include "commands_712.h" #include "type_hash.h" #include "shared_context.h" -#include "ethUtils.h" #include "mem_utils.h" #include "ui_logic.h" #include "apdu_constants.h" // APDU response codes diff --git a/src_features/signMessageEIP712/type_hash.c b/src_features/signMessageEIP712/type_hash.c index 1e8da4b..7b9515f 100644 --- a/src_features/signMessageEIP712/type_hash.c +++ b/src_features/signMessageEIP712/type_hash.c @@ -7,7 +7,6 @@ #include "mem_utils.h" #include "type_hash.h" #include "shared_context.h" -#include "ethUtils.h" // KECCAK256_HASH_BYTESIZE #include "format_hash_field_type.h" #include "hash_bytes.h" #include "apdu_constants.h" // APDU response codes diff --git a/src_features/signMessageEIP712/ui_logic.c b/src_features/signMessageEIP712/ui_logic.c index aa7dcd8..45f7832 100644 --- a/src_features/signMessageEIP712/ui_logic.c +++ b/src_features/signMessageEIP712/ui_logic.c @@ -7,8 +7,7 @@ #include "mem_utils.h" #include "os_io.h" #include "shared_context.h" -#include "ethUtils.h" // getEthDisplayableAddress -#include "utils.h" // uint256_to_decimal +#include "utils.h" // uint256_to_decimal #include "common_712.h" #include "context_712.h" // eip712_context_deinit #include "uint256.h" // tostring256 && tostring256_signed diff --git a/src_features/signMessageEIP712_v0/cmd_signMessage712.c b/src_features/signMessageEIP712_v0/cmd_signMessage712.c index deaab84..5de017e 100644 --- a/src_features/signMessageEIP712_v0/cmd_signMessage712.c +++ b/src_features/signMessageEIP712_v0/cmd_signMessage712.c @@ -3,7 +3,6 @@ #include "utils.h" #include "common_ui.h" #include "common_712.h" -#include "ethUtils.h" void handleSignEIP712Message_v0(uint8_t p1, uint8_t p2, diff --git a/src_features/signTx/logic_signTx.c b/src_features/signTx/logic_signTx.c index b5bb875..ec50b71 100644 --- a/src_features/signTx/logic_signTx.c +++ b/src_features/signTx/logic_signTx.c @@ -7,7 +7,6 @@ #endif #include "eth_plugin_handler.h" #include "network.h" -#include "ethUtils.h" #include "common_ui.h" #include "ui_callbacks.h" #include "apdu_constants.h" diff --git a/src_features/stark_provideQuantum/cmd_stark_provideQuantum.c b/src_features/stark_provideQuantum/cmd_stark_provideQuantum.c index 1755c93..614669d 100644 --- a/src_features/stark_provideQuantum/cmd_stark_provideQuantum.c +++ b/src_features/stark_provideQuantum/cmd_stark_provideQuantum.c @@ -2,7 +2,6 @@ #include "shared_context.h" #include "apdu_constants.h" -#include "ethUtils.h" #include "common_ui.h" void handleStarkwareProvideQuantum(uint8_t p1, diff --git a/src_features/stark_sign/cmd_stark_sign.c b/src_features/stark_sign/cmd_stark_sign.c index 311d9d7..7810f77 100644 --- a/src_features/stark_sign/cmd_stark_sign.c +++ b/src_features/stark_sign/cmd_stark_sign.c @@ -4,7 +4,6 @@ #include "apdu_constants.h" #include "stark_utils.h" #include "poorstream.h" -#include "ethUtils.h" #include "common_ui.h" #include "os_io_seproxyhal.h" diff --git a/src_nbgl/ui_approve_tx.c b/src_nbgl/ui_approve_tx.c index ed5e0ae..6d04570 100644 --- a/src_nbgl/ui_approve_tx.c +++ b/src_nbgl/ui_approve_tx.c @@ -3,7 +3,6 @@ #include "shared_context.h" #include "ui_callbacks.h" #include "ui_nbgl.h" -#include "ethUtils.h" #include "ui_signing.h" #include "plugins.h" #include "domain_name.h" diff --git a/src_nbgl/ui_sign_712_v0.c b/src_nbgl/ui_sign_712_v0.c index ec38eea..0681009 100644 --- a/src_nbgl/ui_sign_712_v0.c +++ b/src_nbgl/ui_sign_712_v0.c @@ -2,7 +2,6 @@ #include "ui_nbgl.h" #include "common_712.h" #include "network.h" -#include "ethUtils.h" #include "ui_message_signing.h" #include "ui_signing.h" diff --git a/src_nbgl/ui_stark_transfer.c b/src_nbgl/ui_stark_transfer.c index bb41e1e..40dca23 100644 --- a/src_nbgl/ui_stark_transfer.c +++ b/src_nbgl/ui_stark_transfer.c @@ -4,7 +4,6 @@ #include "ui_nbgl.h" #include "ui_signing.h" #include "starkDisplayUtils.h" -#include "ethUtils.h" #include "network.h" #ifdef HAVE_STARKWARE diff --git a/src_plugins/erc1155/erc1155_plugin.c b/src_plugins/erc1155/erc1155_plugin.c index 1ba22b1..cd79626 100644 --- a/src_plugins/erc1155/erc1155_plugin.c +++ b/src_plugins/erc1155/erc1155_plugin.c @@ -3,7 +3,6 @@ #include #include "erc1155_plugin.h" #include "eth_plugin_internal.h" -#include "ethUtils.h" #include "eth_plugin_handler.h" static const uint8_t ERC1155_APPROVE_FOR_ALL_SELECTOR[SELECTOR_SIZE] = {0xa2, 0x2c, 0xb4, 0x65}; diff --git a/src_plugins/erc1155/erc1155_provide_parameters.c b/src_plugins/erc1155/erc1155_provide_parameters.c index 8b5e1ca..8895396 100644 --- a/src_plugins/erc1155/erc1155_provide_parameters.c +++ b/src_plugins/erc1155/erc1155_provide_parameters.c @@ -4,7 +4,6 @@ #include "erc1155_plugin.h" #include "eth_plugin_internal.h" #include "utils.h" -#include "ethUtils.h" static void handle_safe_transfer(ethPluginProvideParameter_t *msg, erc1155_context_t *context) { uint8_t new_value[INT256_LENGTH]; diff --git a/src_plugins/erc1155/erc1155_ui.c b/src_plugins/erc1155/erc1155_ui.c index 72ea223..888fb23 100644 --- a/src_plugins/erc1155/erc1155_ui.c +++ b/src_plugins/erc1155/erc1155_ui.c @@ -3,7 +3,6 @@ #include #include "erc1155_plugin.h" #include "eth_plugin_interface.h" -#include "ethUtils.h" #include "utils.h" static void set_approval_for_all_ui(ethQueryContractUI_t *msg, erc1155_context_t *context) { diff --git a/src_plugins/erc20/erc20_plugin.c b/src_plugins/erc20/erc20_plugin.c index 33380db..b5424da 100644 --- a/src_plugins/erc20/erc20_plugin.c +++ b/src_plugins/erc20/erc20_plugin.c @@ -2,7 +2,6 @@ #include "eth_plugin_internal.h" #include "eth_plugin_handler.h" #include "shared_context.h" -#include "ethUtils.h" #include "ethUstream.h" #include "utils.h" diff --git a/src_plugins/erc721/erc721_plugin.c b/src_plugins/erc721/erc721_plugin.c index f942fba..3c8a5a6 100644 --- a/src_plugins/erc721/erc721_plugin.c +++ b/src_plugins/erc721/erc721_plugin.c @@ -4,7 +4,6 @@ #include "erc721_plugin.h" #include "eth_plugin_internal.h" #include "eth_plugin_interface.h" -#include "ethUtils.h" #include "eth_plugin_handler.h" static const uint8_t ERC721_APPROVE_SELECTOR[SELECTOR_SIZE] = {0x09, 0x5e, 0xa7, 0xb3}; diff --git a/src_plugins/erc721/erc721_ui.c b/src_plugins/erc721/erc721_ui.c index caaf395..7c13778 100644 --- a/src_plugins/erc721/erc721_ui.c +++ b/src_plugins/erc721/erc721_ui.c @@ -3,7 +3,6 @@ #include #include "erc721_plugin.h" #include "eth_plugin_interface.h" -#include "ethUtils.h" #include "utils.h" static void set_approval_ui(ethQueryContractUI_t *msg, erc721_context_t *context) { diff --git a/src_plugins/eth2/eth2_plugin.c b/src_plugins/eth2/eth2_plugin.c index 352a9f3..24521e3 100644 --- a/src_plugins/eth2/eth2_plugin.c +++ b/src_plugins/eth2/eth2_plugin.c @@ -4,7 +4,6 @@ #include "eth_plugin_internal.h" #include "eth_plugin_handler.h" #include "shared_context.h" -#include "ethUtils.h" #include "utils.h" void getEth2PublicKey(uint32_t *bip32Path, uint8_t bip32PathLength, uint8_t *out); diff --git a/src_plugins/starkware/starkware_plugin.c b/src_plugins/starkware/starkware_plugin.c index d0ee269..d05c877 100644 --- a/src_plugins/starkware/starkware_plugin.c +++ b/src_plugins/starkware/starkware_plugin.c @@ -5,7 +5,6 @@ #include "eth_plugin_internal.h" // TODO : rewrite as independant code #include "stark_utils.h" #include "utils.h" -#include "ethUtils.h" #include "apdu_constants.h" #ifdef HAVE_STARKWARE diff --git a/tools/build_sdk.py b/tools/build_sdk.py index f61535e..3b5c68b 100755 --- a/tools/build_sdk.py +++ b/tools/build_sdk.py @@ -157,7 +157,6 @@ if __name__ == "__main__": "src/tokens.h", "src/utils.h", "src/tx_content.h", - "src/ethUtils.h", "src/shared_context.h", "src/eth_plugin_internal.h", "src/nft.h", @@ -200,7 +199,6 @@ if __name__ == "__main__": # extract and merge function bodies c_files_to_merge = [ "src/utils.c", - "src/ethUtils.c", "src/eth_plugin_internal.c", ] merge_c_files(c_files_to_merge, nodes_to_extract["fn"])