Files
app-ethereum/src_features/signMessageEIP712/schema_hash.c
2022-08-05 18:15:52 +02:00

67 lines
2.1 KiB
C

#ifdef HAVE_EIP712_FULL_SUPPORT
#include "schema_hash.h"
#include "hash_bytes.h"
#include "typed_data.h"
#include "format_hash_field_type.h"
#include "context.h"
bool compute_schema_hash(void)
{
const void *struct_ptr;
uint8_t structs_count;
const void *field_ptr;
uint8_t fields_count;
const char *name;
uint8_t name_length;
cx_sha256_t hash_ctx; // sha224
cx_sha224_init(&hash_ctx);
struct_ptr = get_structs_array(&structs_count);
hash_byte('{', (cx_hash_t*)&hash_ctx);
while (structs_count-- > 0)
{
name = get_struct_name(struct_ptr, &name_length);
hash_byte('"', (cx_hash_t*)&hash_ctx);
hash_nbytes((uint8_t*)name, name_length, (cx_hash_t*)&hash_ctx);
hash_nbytes((uint8_t*)"\":[", 3, (cx_hash_t*)&hash_ctx);
field_ptr = get_struct_fields_array(struct_ptr, &fields_count);
while (fields_count-- > 0)
{
hash_nbytes((uint8_t*)"{\"name\":\"", 9, (cx_hash_t*)&hash_ctx);
name = get_struct_field_keyname(field_ptr, &name_length);
hash_nbytes((uint8_t*)name, name_length, (cx_hash_t*)&hash_ctx);
hash_nbytes((uint8_t*)"\",\"type\":\"", 10, (cx_hash_t*)&hash_ctx);
if (!format_hash_field_type(field_ptr, (cx_hash_t*)&hash_ctx))
{
return false;
}
hash_nbytes((uint8_t*)"\"}", 2, (cx_hash_t*)&hash_ctx);
if (fields_count > 0)
{
hash_byte(',', (cx_hash_t*)&hash_ctx);
}
field_ptr = get_next_struct_field(field_ptr);
}
hash_byte(']', (cx_hash_t*)&hash_ctx);
if (structs_count > 0)
{
hash_byte(',', (cx_hash_t*)&hash_ctx);
}
struct_ptr = get_next_struct(struct_ptr);
}
hash_byte('}', (cx_hash_t*)&hash_ctx);
// copy hash into context struct
cx_hash((cx_hash_t*)&hash_ctx,
CX_LAST,
NULL,
0,
eip712_context->schema_hash,
sizeof(eip712_context->schema_hash));
return true;
}
#endif // HAVE_EIP712_FULL_SUPPORT