Small simplification/refactoring of EIP712 typed data
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
#include "path.h"
|
||||
#include "field_hash.h"
|
||||
#include "ui_logic.h"
|
||||
#include "typed_data.h"
|
||||
#include "apdu_constants.h" // APDU response codes
|
||||
#include "shared_context.h" // reset_app_context
|
||||
#include "ui_callbacks.h" // ui_idle
|
||||
@@ -50,20 +51,17 @@ bool eip712_context_init(void)
|
||||
return false;
|
||||
}
|
||||
|
||||
// set types pointer
|
||||
if ((eip712_context->structs_array = mem_alloc(sizeof(uint8_t))) == NULL)
|
||||
if (typed_data_init() == false) // this needs to be initialized last !
|
||||
{
|
||||
apdu_response_code = APDU_RESPONSE_INSUFFICIENT_MEMORY;
|
||||
return false;
|
||||
}
|
||||
|
||||
// create len(types)
|
||||
*(eip712_context->structs_array) = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
void eip712_context_deinit(void)
|
||||
{
|
||||
typed_data_deinit();
|
||||
path_deinit();
|
||||
field_hash_deinit();
|
||||
ui_712_deinit();
|
||||
|
||||
@@ -8,8 +8,6 @@
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t *structs_array;
|
||||
uint8_t *current_struct_fields_array;
|
||||
uint8_t contract_addr[ADDRESS_LENGTH];
|
||||
uint8_t schema_hash[224 / 8];
|
||||
} s_eip712_context;
|
||||
|
||||
@@ -61,7 +61,7 @@ static const void *get_nth_field(uint8_t *const fields_count_ptr,
|
||||
if (struct_field_type(field_ptr) == TYPE_CUSTOM)
|
||||
{
|
||||
typename = get_struct_field_typename(field_ptr, &length);
|
||||
if ((struct_ptr = get_structn(eip712_context->structs_array, typename, length)) == NULL)
|
||||
if ((struct_ptr = get_structn(typename, length)) == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
@@ -97,7 +97,7 @@ const void *path_get_nth_field_to_last(uint8_t n)
|
||||
if (field_ptr != NULL)
|
||||
{
|
||||
typename = get_struct_field_typename(field_ptr, &typename_len);
|
||||
struct_ptr = get_structn(eip712_context->structs_array, typename, typename_len);
|
||||
struct_ptr = get_structn(typename, typename_len);
|
||||
}
|
||||
return struct_ptr;
|
||||
}
|
||||
@@ -298,7 +298,7 @@ static bool path_update(void)
|
||||
while (struct_field_type(field_ptr) == TYPE_CUSTOM)
|
||||
{
|
||||
typename = get_struct_field_typename(field_ptr, &typename_len);
|
||||
if ((struct_ptr = get_structn(eip712_context->structs_array, typename, typename_len)) == NULL)
|
||||
if ((struct_ptr = get_structn(typename, typename_len)) == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -318,7 +318,7 @@ static bool path_update(void)
|
||||
}
|
||||
cx_keccak_init(hash_ctx, 256); // initialize it
|
||||
// get the struct typehash
|
||||
if ((thash_ptr = type_hash(eip712_context->structs_array, typename, typename_len)) == NULL)
|
||||
if ((thash_ptr = type_hash(typename, typename_len)) == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -353,7 +353,7 @@ bool path_set_root(const char *const struct_name, uint8_t name_length)
|
||||
return false;
|
||||
}
|
||||
|
||||
path_struct->root_struct = get_structn(eip712_context->structs_array, struct_name, name_length);
|
||||
path_struct->root_struct = get_structn(struct_name, name_length);
|
||||
|
||||
if (path_struct->root_struct == NULL)
|
||||
{
|
||||
@@ -376,7 +376,7 @@ bool path_set_root(const char *const struct_name, uint8_t name_length)
|
||||
return false;
|
||||
}
|
||||
cx_keccak_init(hash_ctx, 256); // init hash
|
||||
if ((thash_ptr = type_hash(eip712_context->structs_array, struct_name, name_length)) == NULL)
|
||||
if ((thash_ptr = type_hash(struct_name, name_length)) == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ bool compute_schema_hash(void)
|
||||
|
||||
cx_sha224_init(&hash_ctx);
|
||||
|
||||
struct_ptr = get_structs_array(eip712_context->structs_array, &structs_count);
|
||||
struct_ptr = get_structs_array(&structs_count);
|
||||
hash_byte('{', (cx_hash_t*)&hash_ctx);
|
||||
while (structs_count-- > 0)
|
||||
{
|
||||
|
||||
@@ -81,7 +81,6 @@ static bool encode_and_hash_type(const void *const struct_ptr)
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param[in] structs_array pointer to structs array
|
||||
* @param[in] deps_count count of how many struct dependencies pointers
|
||||
* @param[in,out] deps pointer to the first dependency pointer
|
||||
*/
|
||||
@@ -119,16 +118,14 @@ static void sort_dependencies(uint8_t deps_count,
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param[in] structs_array pointer to structs array
|
||||
* @param[out] deps_count count of how many struct dependencie pointers
|
||||
* @param[in] first_dep pointer to the first dependency pointer
|
||||
* @param[in] struct_ptr pointer to the struct we are getting the dependencies of
|
||||
* @return \ref false in case of a memory allocation error, \ref true otherwise
|
||||
*/
|
||||
static const void **get_struct_dependencies(const void *const structs_array,
|
||||
uint8_t *const deps_count,
|
||||
const void **first_dep,
|
||||
const void *const struct_ptr)
|
||||
static const void **get_struct_dependencies(uint8_t *const deps_count,
|
||||
const void **first_dep,
|
||||
const void *const struct_ptr)
|
||||
{
|
||||
uint8_t fields_count;
|
||||
const void *field_ptr;
|
||||
@@ -146,7 +143,7 @@ static const void **get_struct_dependencies(const void *const structs_array,
|
||||
// get struct name
|
||||
arg_structname = get_struct_field_typename(field_ptr, &arg_structname_length);
|
||||
// from its name, get the pointer to its definition
|
||||
arg_struct_ptr = get_structn(structs_array, arg_structname, arg_structname_length);
|
||||
arg_struct_ptr = get_structn(arg_structname, arg_structname_length);
|
||||
|
||||
// check if it is not already present in the dependencies array
|
||||
for (dep_idx = 0; dep_idx < *deps_count; ++dep_idx)
|
||||
@@ -172,7 +169,7 @@ static const void **get_struct_dependencies(const void *const structs_array,
|
||||
}
|
||||
*new_dep = arg_struct_ptr;
|
||||
// TODO: Move away from recursive calls
|
||||
get_struct_dependencies(structs_array, deps_count, first_dep, arg_struct_ptr);
|
||||
get_struct_dependencies(deps_count, first_dep, arg_struct_ptr);
|
||||
}
|
||||
}
|
||||
field_ptr = get_next_struct_field(field_ptr);
|
||||
@@ -183,18 +180,15 @@ static const void **get_struct_dependencies(const void *const structs_array,
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param[in] structs_array pointer to structs array
|
||||
* @param[in] struct_name name of the given struct
|
||||
* @param[in] struct_name_length length of the name of the given struct
|
||||
* @param[in] with_deps if hashed typestring should include struct dependencies
|
||||
* @return pointer to encoded string or \ref NULL in case of a memory allocation error
|
||||
*/
|
||||
const uint8_t *type_hash(const void *const structs_array,
|
||||
const char *const struct_name,
|
||||
const uint8_t *type_hash(const char *const struct_name,
|
||||
const uint8_t struct_name_length)
|
||||
{
|
||||
const void *const struct_ptr = get_structn(structs_array,
|
||||
struct_name,
|
||||
const void *const struct_ptr = get_structn(struct_name,
|
||||
struct_name_length);
|
||||
uint8_t deps_count = 0;
|
||||
const void **deps;
|
||||
@@ -202,7 +196,7 @@ const uint8_t *type_hash(const void *const structs_array,
|
||||
void *mem_loc_bak = mem_alloc(0);
|
||||
|
||||
cx_keccak_init(&global_sha3, 256); // init hash
|
||||
deps = get_struct_dependencies(structs_array, &deps_count, NULL, struct_ptr);
|
||||
deps = get_struct_dependencies(&deps_count, NULL, struct_ptr);
|
||||
if ((deps_count > 0) && (deps == NULL))
|
||||
{
|
||||
return NULL;
|
||||
|
||||
@@ -5,8 +5,7 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
const uint8_t *type_hash(const void *const structs_array,
|
||||
const char *const struct_name,
|
||||
const uint8_t *type_hash(const char *const struct_name,
|
||||
const uint8_t struct_name_length);
|
||||
|
||||
#endif // HAVE_EIP712_FULL_SUPPORT
|
||||
|
||||
@@ -7,6 +7,37 @@
|
||||
#include "apdu_constants.h" // APDU response codes
|
||||
#include "context.h"
|
||||
#include "mem.h"
|
||||
#include "mem_utils.h"
|
||||
|
||||
static s_typed_data *typed_data = NULL;
|
||||
|
||||
|
||||
bool typed_data_init(void)
|
||||
{
|
||||
if (typed_data == NULL)
|
||||
{
|
||||
if ((typed_data = MEM_ALLOC_AND_ALIGN_TYPE(*typed_data)) == NULL)
|
||||
{
|
||||
apdu_response_code = APDU_RESPONSE_INSUFFICIENT_MEMORY;
|
||||
return false;
|
||||
}
|
||||
// set types pointer
|
||||
if ((typed_data->structs_array = mem_alloc(sizeof(uint8_t))) == NULL)
|
||||
{
|
||||
apdu_response_code = APDU_RESPONSE_INSUFFICIENT_MEMORY;
|
||||
return false;
|
||||
}
|
||||
|
||||
// create len(types)
|
||||
*(typed_data->structs_array) = 0;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void typed_data_deinit(void)
|
||||
{
|
||||
typed_data = NULL;
|
||||
}
|
||||
|
||||
// lib functions
|
||||
const void *get_array_in_mem(const void *ptr, uint8_t *const array_size)
|
||||
@@ -247,14 +278,13 @@ const uint8_t *get_next_struct(const uint8_t *ptr)
|
||||
}
|
||||
|
||||
// ptr must point to the size of the structs array
|
||||
const uint8_t *get_structs_array(const uint8_t *ptr, uint8_t *const length)
|
||||
const uint8_t *get_structs_array(uint8_t *const length)
|
||||
{
|
||||
return get_array_in_mem(ptr, length);
|
||||
return get_array_in_mem(typed_data->structs_array, length);
|
||||
}
|
||||
|
||||
// Finds struct with a given name
|
||||
const uint8_t *get_structn(const uint8_t *const ptr,
|
||||
const char *const name_ptr,
|
||||
const uint8_t *get_structn(const char *const name_ptr,
|
||||
const uint8_t name_length)
|
||||
{
|
||||
uint8_t structs_count;
|
||||
@@ -262,11 +292,11 @@ const uint8_t *get_structn(const uint8_t *const ptr,
|
||||
const char *name;
|
||||
uint8_t length;
|
||||
|
||||
if ((ptr == NULL) || (name_ptr == NULL))
|
||||
if (name_ptr == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
struct_ptr = get_structs_array(ptr, &structs_count);
|
||||
struct_ptr = get_structs_array(&structs_count);
|
||||
while (structs_count-- > 0)
|
||||
{
|
||||
name = get_struct_name(struct_ptr, &length);
|
||||
@@ -285,12 +315,12 @@ bool set_struct_name(uint8_t length, const uint8_t *const name)
|
||||
uint8_t *length_ptr;
|
||||
char *name_ptr;
|
||||
|
||||
if ((name == NULL) || (eip712_context == NULL))
|
||||
if ((name == NULL) || (typed_data == NULL))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
// increment number of structs
|
||||
*(eip712_context->structs_array) += 1;
|
||||
*(typed_data->structs_array) += 1;
|
||||
|
||||
// copy length
|
||||
if ((length_ptr = mem_alloc(sizeof(uint8_t))) == NULL)
|
||||
@@ -309,12 +339,12 @@ bool set_struct_name(uint8_t length, const uint8_t *const name)
|
||||
memmove(name_ptr, name, length);
|
||||
|
||||
// initialize number of fields
|
||||
if ((eip712_context->current_struct_fields_array = mem_alloc(sizeof(uint8_t))) == NULL)
|
||||
if ((typed_data->current_struct_fields_array = mem_alloc(sizeof(uint8_t))) == NULL)
|
||||
{
|
||||
apdu_response_code = APDU_RESPONSE_INSUFFICIENT_MEMORY;
|
||||
return false;
|
||||
}
|
||||
*(eip712_context->current_struct_fields_array) = 0;
|
||||
*(typed_data->current_struct_fields_array) = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -333,12 +363,12 @@ bool set_struct_field(const uint8_t *const data)
|
||||
uint8_t *fieldname_len_ptr;
|
||||
char *fieldname_ptr;
|
||||
|
||||
if ((data == NULL) || (eip712_context == NULL))
|
||||
if ((data == NULL) || (typed_data == NULL))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
// increment number of struct fields
|
||||
*(eip712_context->current_struct_fields_array) += 1;
|
||||
*(typed_data->current_struct_fields_array) += 1;
|
||||
|
||||
// copy TypeDesc
|
||||
if ((type_desc_ptr = mem_alloc(sizeof(uint8_t))) == NULL)
|
||||
|
||||
@@ -34,6 +34,12 @@ typedef enum
|
||||
TYPES_COUNT
|
||||
} e_type;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t *structs_array;
|
||||
uint8_t *current_struct_fields_array;
|
||||
} s_typed_data;
|
||||
|
||||
const void *get_array_in_mem(const void *ptr, uint8_t *const array_size);
|
||||
const char *get_string_in_mem(const uint8_t *ptr, uint8_t *const string_length);
|
||||
bool struct_field_is_array(const uint8_t *ptr);
|
||||
@@ -57,12 +63,13 @@ const char *get_struct_name(const uint8_t *ptr, uint8_t *const length);
|
||||
const uint8_t *get_struct_fields_array(const uint8_t *ptr,
|
||||
uint8_t *const length);
|
||||
const uint8_t *get_next_struct(const uint8_t *ptr);
|
||||
const uint8_t *get_structs_array(const uint8_t *ptr, uint8_t *const length);
|
||||
const uint8_t *get_structn(const uint8_t *const ptr,
|
||||
const char *const name_ptr,
|
||||
const uint8_t *get_structs_array(uint8_t *const length);
|
||||
const uint8_t *get_structn(const char *const name_ptr,
|
||||
const uint8_t name_length);
|
||||
bool set_struct_name(uint8_t length, const uint8_t *const name);
|
||||
bool set_struct_field(const uint8_t *const data);
|
||||
bool typed_data_init(void);
|
||||
void typed_data_deinit(void);
|
||||
|
||||
#endif // HAVE_EIP712_FULL_SUPPORT
|
||||
|
||||
|
||||
Reference in New Issue
Block a user