EIP-712 code linting

This commit is contained in:
Alexandre Paillier
2022-07-19 11:49:18 +02:00
parent 0cf21cdf73
commit de9e895ad9
44 changed files with 778 additions and 1402 deletions

View File

@@ -5,13 +5,9 @@
#include "encode_field.h"
#include "mem.h"
#include "shared_context.h"
#include "apdu_constants.h" // APDU response codes
#include "apdu_constants.h" // APDU response codes
typedef enum
{
MSB,
LSB
} e_padding_type;
typedef enum { MSB, LSB } e_padding_type;
/**
* Encode a field value to 32 bytes (padded)
@@ -25,20 +21,17 @@ typedef enum
static void *field_encode(const uint8_t *const value,
uint8_t length,
e_padding_type ptype,
uint8_t pval)
{
uint8_t pval) {
uint8_t *padded_value;
uint8_t start_idx;
if (length > EIP_712_ENCODED_FIELD_LENGTH) // sanity check
if (length > EIP_712_ENCODED_FIELD_LENGTH) // sanity check
{
apdu_response_code = APDU_RESPONSE_INVALID_DATA;
return NULL;
}
if ((padded_value = mem_alloc(EIP_712_ENCODED_FIELD_LENGTH)) != NULL)
{
switch (ptype)
{
if ((padded_value = mem_alloc(EIP_712_ENCODED_FIELD_LENGTH)) != NULL) {
switch (ptype) {
case MSB:
memset(padded_value, pval, EIP_712_ENCODED_FIELD_LENGTH - length);
start_idx = EIP_712_ENCODED_FIELD_LENGTH - length;
@@ -49,12 +42,10 @@ static void *field_encode(const uint8_t *const value,
break;
default:
apdu_response_code = APDU_RESPONSE_CONDITION_NOT_SATISFIED;
return NULL; // should not be here
return NULL; // should not be here
}
memcpy(&padded_value[start_idx], value, length);
}
else
{
} else {
apdu_response_code = APDU_RESPONSE_INSUFFICIENT_MEMORY;
}
return padded_value;
@@ -67,8 +58,7 @@ static void *field_encode(const uint8_t *const value,
* @param[in] length its byte-length
* @return the encoded value
*/
void *encode_uint(const uint8_t *const value, uint8_t length)
{
void *encode_uint(const uint8_t *const value, uint8_t length) {
// no length check here since it will be checked by field_encode
return field_encode(value, length, MSB, 0x00);
}
@@ -81,16 +71,13 @@ void *encode_uint(const uint8_t *const value, uint8_t length)
* @param[in] typesize the type size in bytes
* @return the encoded value
*/
void *encode_int(const uint8_t *const value, uint8_t length, uint8_t typesize)
{
void *encode_int(const uint8_t *const value, uint8_t length, uint8_t typesize) {
uint8_t padding_value;
if ((length == typesize) && (value[0] & (1 << 7))) // negative number
if ((length == typesize) && (value[0] & (1 << 7))) // negative number
{
padding_value = 0xFF;
}
else
{
} else {
padding_value = 0x00;
}
// no length check here since it will be checked by field_encode
@@ -104,8 +91,7 @@ void *encode_int(const uint8_t *const value, uint8_t length, uint8_t typesize
* @param[in] length its byte-length
* @return the encoded value
*/
void *encode_bytes(const uint8_t *const value, uint8_t length)
{
void *encode_bytes(const uint8_t *const value, uint8_t length) {
// no length check here since it will be checked by field_encode
return field_encode(value, length, LSB, 0x00);
}
@@ -117,14 +103,13 @@ void *encode_bytes(const uint8_t *const value, uint8_t length)
* @param[in] length its byte-length
* @return the encoded value
*/
void *encode_boolean(const bool *const value, uint8_t length)
{
if (length != 1) // sanity check
void *encode_boolean(const bool *const value, uint8_t length) {
if (length != 1) // sanity check
{
apdu_response_code = APDU_RESPONSE_INVALID_DATA;
return NULL;
}
return encode_uint((uint8_t*)value, length);
return encode_uint((uint8_t *) value, length);
}
/**
@@ -134,9 +119,8 @@ void *encode_boolean(const bool *const value, uint8_t length)
* @param[in] length its byte-length
* @return the encoded value
*/
void *encode_address(const uint8_t *const value, uint8_t length)
{
if (length != ADDRESS_LENGTH) // sanity check
void *encode_address(const uint8_t *const value, uint8_t length) {
if (length != ADDRESS_LENGTH) // sanity check
{
apdu_response_code = APDU_RESPONSE_INVALID_DATA;
return NULL;
@@ -144,4 +128,4 @@ void *encode_address(const uint8_t *const value, uint8_t length)
return encode_uint(value, length);
}
#endif // HAVE_EIP712_FULL_SUPPORT
#endif // HAVE_EIP712_FULL_SUPPORT