Move basic utility functions from ethUtils to main utils file
This commit is contained in:
267
src/ethUtils.c
267
src/ethUtils.c
@@ -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 <hello@ledger.fr>
|
||||
* @version 1.0
|
||||
* @date 8th of March 2016
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
@@ -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 <stdint.h>
|
||||
|
||||
#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_
|
||||
@@ -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,
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#include <string.h>
|
||||
#include "eth_plugin_internal.h"
|
||||
#include "ethUtils.h" // allzeroes
|
||||
|
||||
bool erc20_plugin_available_check(void);
|
||||
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
#include "shared_context.h"
|
||||
#include "stark_utils.h"
|
||||
#include "utils.h"
|
||||
#include "ethUtils.h"
|
||||
|
||||
extraInfo_t *getKnownToken(uint8_t *contractAddress);
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
#include "stark_crypto.h"
|
||||
#include "shared_context.h"
|
||||
#include "ethUtils.h"
|
||||
#include "uint256.h"
|
||||
#include "uint_common.h"
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
#include <string.h>
|
||||
#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);
|
||||
|
||||
@@ -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));
|
||||
|
||||
233
src/utils.c
233
src/utils.c
@@ -18,7 +18,6 @@
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
72
src/utils.h
72
src/utils.h
@@ -15,34 +15,82 @@
|
||||
* limitations under the License.
|
||||
********************************************************************************/
|
||||
|
||||
#ifndef _UTILS_H_
|
||||
#define _UTILS_H_
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
#include "shared_context.h"
|
||||
#include "ui_callbacks.h"
|
||||
#include "ethUtils.h"
|
||||
#include "starkDisplayUtils.h"
|
||||
#include "apdu_constants.h"
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#include "shared_context.h"
|
||||
#include "apdu_constants.h"
|
||||
#include "ethUtils.h"
|
||||
|
||||
#include "feature_performPrivacyOperation.h"
|
||||
#include "common_ui.h"
|
||||
|
||||
@@ -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
|
||||
|
||||
/**
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
#include "shared_context.h"
|
||||
#include "apdu_constants.h"
|
||||
#include "ethUtils.h"
|
||||
#include "common_ui.h"
|
||||
|
||||
void handleStarkwareProvideQuantum(uint8_t p1,
|
||||
|
||||
@@ -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"
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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"
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
#include "ui_nbgl.h"
|
||||
#include "ui_signing.h"
|
||||
#include "starkDisplayUtils.h"
|
||||
#include "ethUtils.h"
|
||||
#include "network.h"
|
||||
|
||||
#ifdef HAVE_STARKWARE
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
#include <string.h>
|
||||
#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};
|
||||
|
||||
@@ -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];
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
#include <string.h>
|
||||
#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) {
|
||||
|
||||
@@ -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"
|
||||
|
||||
|
||||
@@ -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};
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
#include <string.h>
|
||||
#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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"])
|
||||
|
||||
Reference in New Issue
Block a user