Skip to content

Commit

Permalink
Fix the CI issues (use no throw functions) and remove "throw" from ET…
Browse files Browse the repository at this point in the history
…H plugin SDK
  • Loading branch information
tdejoigny-ledger committed Jul 11, 2023
1 parent d63f1a9 commit 4fcba0e
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 31 deletions.
7 changes: 4 additions & 3 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ bool uint256_to_decimal(const uint8_t *value, size_t value_len, char *out, size_
return true;
}

void amountToString(const uint8_t *amount,
bool amountToString(const uint8_t *amount,
uint8_t amount_size,
uint8_t decimals,
const char *ticker,
Expand All @@ -125,7 +125,7 @@ void amountToString(const uint8_t *amount,
char tmp_buffer[100] = {0};

if (uint256_to_decimal(amount, amount_size, tmp_buffer, sizeof(tmp_buffer)) == false) {
THROW(EXCEPTION_OVERFLOW);
return false;
}

uint8_t amount_len = strnlen(tmp_buffer, sizeof(tmp_buffer));
Expand All @@ -141,10 +141,11 @@ void amountToString(const uint8_t *amount,
out_buffer + ticker_len,
out_buffer_size - ticker_len - 1,
decimals) == false) {
THROW(EXCEPTION_OVERFLOW);
return false;
}

out_buffer[out_buffer_size - 1] = '\0';
return true;
}

bool parse_swap_config(const uint8_t *config, uint8_t config_len, char *ticker, uint8_t *decimals) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ 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);

void amountToString(const uint8_t* amount,
bool amountToString(const uint8_t* amount,
uint8_t amount_len,
uint8_t decimals,
const char* ticker,
Expand Down
86 changes: 65 additions & 21 deletions src_common/ethUtils.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,30 +117,59 @@ bool rlpDecodeLength(uint8_t *buffer, uint32_t *fieldLength, uint32_t *offset, b
return true;
}

void getEthAddressFromKey(cx_ecfp_public_key_t *publicKey, uint8_t *out, cx_sha3_t *sha3Context) {
bool getEthAddressFromKey(cx_ecfp_public_key_t *publicKey, uint8_t *out, cx_sha3_t *sha3Context) {
uint8_t hashAddress[INT256_LENGTH];
cx_keccak_init(sha3Context, 256);
cx_hash((cx_hash_t *) sha3Context, CX_LAST, publicKey->W + 1, 64, hashAddress, 32);

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;
}

void getEthAddressStringFromKey(cx_ecfp_public_key_t *publicKey,
bool getEthAddressStringFromKey(cx_ecfp_public_key_t *publicKey,
char *out,
cx_sha3_t *sha3Context,
uint64_t chainId) {
uint8_t hashAddress[INT256_LENGTH];
cx_keccak_init(sha3Context, 256);
cx_hash((cx_hash_t *) sha3Context, CX_LAST, publicKey->W + 1, 64, hashAddress, 32);
getEthAddressStringFromBinary(hashAddress + 12, out, sha3Context, chainId);

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;
}

void u64_to_string(uint64_t src, char *dst, uint8_t dst_size) {
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) {
THROW(0x6502);
return false;
}
dst[i] = src % 10 + '0';
src /= 10;
Expand All @@ -160,9 +189,10 @@ void u64_to_string(uint64_t src, char *dst, uint8_t dst_size) {
i--;
j++;
}
return true;
}

void getEthAddressStringFromBinary(uint8_t *address,
bool getEthAddressStringFromBinary(uint8_t *address,
char *out,
cx_sha3_t *sha3Context,
uint64_t chainId) {
Expand All @@ -182,7 +212,9 @@ void getEthAddressStringFromBinary(uint8_t *address,
break;
}
if (eip1191) {
u64_to_string(chainId, (char *) locals_union.tmp, sizeof(locals_union.tmp));
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));
Expand All @@ -192,13 +224,18 @@ void getEthAddressStringFromBinary(uint8_t *address,
locals_union.tmp[offset + 2 * i] = HEXDIGITS[(digit >> 4) & 0x0f];
locals_union.tmp[offset + 2 * i + 1] = HEXDIGITS[digit & 0x0f];
}
cx_keccak_init(sha3Context, 256);
cx_hash((cx_hash_t *) sha3Context,
CX_LAST,
locals_union.tmp,
offset + 40,
locals_union.hashChecksum,
32);
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) {
Expand All @@ -218,24 +255,31 @@ void getEthAddressStringFromBinary(uint8_t *address,
}
}
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.*/
void getEthDisplayableAddress(uint8_t *in,
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;
return false;
}
out[0] = '0';
out[1] = 'x';
getEthAddressStringFromBinary(in, out + 2, sha3, chainId);
if(!getEthAddressStringFromBinary(in, out + 2, sha3, chainId)) {
strlcpy(out, "ERROR", out_len);
return false;
}

return true;
}

bool adjustDecimals(const char *src,
Expand Down
10 changes: 5 additions & 5 deletions src_common/ethUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@ bool rlpDecodeLength(uint8_t *buffer, uint32_t *fieldLength, uint32_t *offset, b

bool rlpCanDecode(uint8_t *buffer, uint32_t bufferLength, bool *valid);

void getEthAddressFromKey(cx_ecfp_public_key_t *publicKey, uint8_t *out, cx_sha3_t *sha3Context);
bool getEthAddressFromKey(cx_ecfp_public_key_t *publicKey, uint8_t *out, cx_sha3_t *sha3Context);

void getEthAddressStringFromKey(cx_ecfp_public_key_t *publicKey,
bool getEthAddressStringFromKey(cx_ecfp_public_key_t *publicKey,
char *out,
cx_sha3_t *sha3Context,
uint64_t chainId);

void u64_to_string(uint64_t src, char *dst, uint8_t dst_size);
bool u64_to_string(uint64_t src, char *dst, uint8_t dst_size);

void getEthAddressStringFromBinary(uint8_t *address,
bool getEthAddressStringFromBinary(uint8_t *address,
char *out,
cx_sha3_t *sha3Context,
uint64_t chainId);

void getEthDisplayableAddress(uint8_t *in,
bool getEthDisplayableAddress(uint8_t *in,
char *out,
size_t out_len,
cx_sha3_t *sha3,
Expand Down
2 changes: 1 addition & 1 deletion tools/build_sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def merge_c_files(sources, nodes_to_extract):
"typedef union": ["extraInfo_t"],
"__attribute__((no_instrument_function)) inline": ["int allzeroes"],
"const": ["HEXDIGITS"],
"fn": ["void getEthAddressStringFromBinary", "void getEthAddressFromKey", "void getEthDisplayableAddress", "bool adjustDecimals", "bool uint256_to_decimal", "void amountToString", "void u64_to_string", "void copy_address", "void copy_parameter", "bool U2BE_from_parameter", "bool U4BE_from_parameter"]
"fn": ["bool getEthAddressStringFromBinary", "bool getEthAddressFromKey", "bool getEthDisplayableAddress", "bool adjustDecimals", "bool uint256_to_decimal", "bool amountToString", "bool u64_to_string", "void copy_address", "void copy_parameter", "bool U2BE_from_parameter", "bool U4BE_from_parameter"]
}
merge_headers(headers_to_merge, nodes_to_extract)

Expand Down

0 comments on commit 4fcba0e

Please sign in to comment.