Fix dynamic bytes type encoding
This commit is contained in:
@@ -5,17 +5,23 @@
|
||||
#include "eip712.h"
|
||||
#include "shared_context.h"
|
||||
|
||||
typedef enum
|
||||
{
|
||||
MSB,
|
||||
LSB
|
||||
} e_padding_type;
|
||||
|
||||
/**
|
||||
* Encode a field value to 32 bytes
|
||||
* Encode a field value to 32 bytes (0 padded)
|
||||
*
|
||||
* @param[in] value field value to encode
|
||||
* @param[in] length field length before encoding
|
||||
* @return encoded field value
|
||||
*/
|
||||
static void *field_encode(const uint8_t *const value, uint8_t length)
|
||||
static void *field_encode(const uint8_t *const value, uint8_t length, e_padding_type ptype)
|
||||
{
|
||||
uint8_t *padded_value;
|
||||
uint8_t start_idx;
|
||||
|
||||
if (length > 32) // sanity check
|
||||
{
|
||||
@@ -24,10 +30,22 @@ static void *field_encode(const uint8_t *const value, uint8_t length)
|
||||
// 0-pad the value to 32 bytes
|
||||
if ((padded_value = mem_alloc(EIP_712_ENCODED_FIELD_LENGTH)) != NULL)
|
||||
{
|
||||
explicit_bzero(padded_value, EIP_712_ENCODED_FIELD_LENGTH - length);
|
||||
switch (ptype)
|
||||
{
|
||||
case MSB:
|
||||
explicit_bzero(padded_value, EIP_712_ENCODED_FIELD_LENGTH - length);
|
||||
start_idx = EIP_712_ENCODED_FIELD_LENGTH - length;
|
||||
break;
|
||||
case LSB:
|
||||
explicit_bzero(padded_value + length, EIP_712_ENCODED_FIELD_LENGTH - length);
|
||||
start_idx = 0;
|
||||
break;
|
||||
default:
|
||||
return NULL; // should not be here
|
||||
}
|
||||
for (uint8_t idx = 0; idx < length; ++idx)
|
||||
{
|
||||
padded_value[EIP_712_ENCODED_FIELD_LENGTH - (length - idx)] = value[idx];
|
||||
padded_value[start_idx + idx] = value[idx];
|
||||
}
|
||||
}
|
||||
return padded_value;
|
||||
@@ -38,12 +56,25 @@ static void *field_encode(const uint8_t *const value, uint8_t length)
|
||||
*
|
||||
* @param[in] value pointer to the "packed" integer received
|
||||
* @param[in] length its byte-length
|
||||
* @return the encoded (hashed) value
|
||||
* @return the encoded value
|
||||
*/
|
||||
void *encode_integer(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);
|
||||
return field_encode(value, length, MSB);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode a fixed-size byte array
|
||||
*
|
||||
* @param[in] value pointer to the "packed" bytes array
|
||||
* @param[in] length its byte-length
|
||||
* @return the encoded value
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -51,7 +82,7 @@ void *encode_integer(const uint8_t *const value, uint8_t length)
|
||||
*
|
||||
* @param[in] value pointer to the boolean received
|
||||
* @param[in] length its byte-length
|
||||
* @return the encoded (hashed) value
|
||||
* @return the encoded value
|
||||
*/
|
||||
void *encode_boolean(const bool *const value, uint8_t length)
|
||||
{
|
||||
@@ -67,7 +98,7 @@ void *encode_boolean(const bool *const value, uint8_t length)
|
||||
*
|
||||
* @param[in] value pointer to the address received
|
||||
* @param[in] length its byte-length
|
||||
* @return the encoded (hashed) value
|
||||
* @return the encoded value
|
||||
*/
|
||||
void *encode_address(const uint8_t *const value, uint8_t length)
|
||||
{
|
||||
|
||||
@@ -9,5 +9,6 @@
|
||||
void *encode_integer(const uint8_t *const value, uint8_t length);
|
||||
void *encode_boolean(const bool *const value, uint8_t length);
|
||||
void *encode_address(const uint8_t *const value, uint8_t length);
|
||||
void *encode_bytes(const uint8_t *const value, uint8_t length);
|
||||
|
||||
#endif // ENCODE_FIELD_H_
|
||||
|
||||
@@ -88,9 +88,11 @@ const uint8_t *field_hash(const uint8_t *data,
|
||||
{
|
||||
case TYPE_SOL_INT:
|
||||
case TYPE_SOL_UINT:
|
||||
case TYPE_SOL_BYTES_FIX:
|
||||
value = encode_integer(data, data_length);
|
||||
break;
|
||||
case TYPE_SOL_BYTES_FIX:
|
||||
value = encode_bytes(data, data_length);
|
||||
break;
|
||||
case TYPE_SOL_ADDRESS:
|
||||
value = encode_address(data, data_length);
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user