|
| 1 | +#ifdef HAVE_GENERIC_TX_PARSER |
| 2 | +#ifdef HAVE_TRUSTED_NAME |
| 3 | + |
| 4 | +#include "gtp_param_trusted_name.h" |
| 5 | +#include "network.h" |
| 6 | +#include "trusted_name.h" |
| 7 | +#include "gtp_field_table.h" |
| 8 | +#include "utils.h" |
| 9 | +#include "shared_context.h" |
| 10 | + |
| 11 | +enum { |
| 12 | + TAG_VERSION = 0x00, |
| 13 | + TAG_VALUE = 0x01, |
| 14 | + TAG_TYPES = 0x02, |
| 15 | + TAG_SOURCES = 0x03, |
| 16 | +}; |
| 17 | + |
| 18 | +static bool handle_version(const s_tlv_data *data, s_param_trusted_name_context *context) { |
| 19 | + if (data->length != sizeof(context->param->version)) { |
| 20 | + return false; |
| 21 | + } |
| 22 | + context->param->version = data->value[0]; |
| 23 | + return true; |
| 24 | +} |
| 25 | + |
| 26 | +static bool handle_value(const s_tlv_data *data, s_param_trusted_name_context *context) { |
| 27 | + s_value_context ctx = {0}; |
| 28 | + |
| 29 | + ctx.value = &context->param->value; |
| 30 | + explicit_bzero(ctx.value, sizeof(*ctx.value)); |
| 31 | + return tlv_parse(data->value, data->length, (f_tlv_data_handler) &handle_value_struct, &ctx); |
| 32 | +} |
| 33 | + |
| 34 | +static bool handle_types(const s_tlv_data *data, s_param_trusted_name_context *context) { |
| 35 | + if (data->length > sizeof(context->param->types)) { |
| 36 | + return false; |
| 37 | + } |
| 38 | + memcpy(context->param->types, data->value, data->length); |
| 39 | + context->param->type_count = data->length; |
| 40 | + return true; |
| 41 | +} |
| 42 | + |
| 43 | +static bool handle_sources(const s_tlv_data *data, s_param_trusted_name_context *context) { |
| 44 | + if (data->length > sizeof(context->param->sources)) { |
| 45 | + return false; |
| 46 | + } |
| 47 | + memcpy(context->param->sources, data->value, data->length); |
| 48 | + context->param->source_count = data->length; |
| 49 | + return true; |
| 50 | +} |
| 51 | + |
| 52 | +bool handle_param_trusted_name_struct(const s_tlv_data *data, |
| 53 | + s_param_trusted_name_context *context) { |
| 54 | + bool ret; |
| 55 | + |
| 56 | + switch (data->tag) { |
| 57 | + case TAG_VERSION: |
| 58 | + ret = handle_version(data, context); |
| 59 | + break; |
| 60 | + case TAG_VALUE: |
| 61 | + ret = handle_value(data, context); |
| 62 | + break; |
| 63 | + case TAG_TYPES: |
| 64 | + ret = handle_types(data, context); |
| 65 | + break; |
| 66 | + case TAG_SOURCES: |
| 67 | + ret = handle_sources(data, context); |
| 68 | + break; |
| 69 | + default: |
| 70 | + PRINTF(TLV_TAG_ERROR_MSG, data->tag); |
| 71 | + ret = false; |
| 72 | + } |
| 73 | + return ret; |
| 74 | +} |
| 75 | + |
| 76 | +bool format_param_trusted_name(const s_param_trusted_name *param, const char *name) { |
| 77 | + s_parsed_value_collection values; |
| 78 | + char *buf = strings.tmp.tmp; |
| 79 | + size_t buf_size = sizeof(strings.tmp.tmp); |
| 80 | + uint64_t chain_id; |
| 81 | + uint8_t addr[ADDRESS_LENGTH]; |
| 82 | + const char *tname; |
| 83 | + e_param_type param_type; |
| 84 | + |
| 85 | + if (!value_get(¶m->value, &values)) { |
| 86 | + return false; |
| 87 | + } |
| 88 | + chain_id = get_tx_chain_id(); |
| 89 | + for (int i = 0; i < values.size; ++i) { |
| 90 | + buf_shrink_expand(values.value[i].ptr, values.value[i].length, addr, sizeof(addr)); |
| 91 | + if ((tname = get_trusted_name(param->type_count, |
| 92 | + param->types, |
| 93 | + param->source_count, |
| 94 | + param->sources, |
| 95 | + &chain_id, |
| 96 | + addr)) != NULL) { |
| 97 | + strlcpy(buf, tname, buf_size); |
| 98 | + param_type = PARAM_TYPE_TRUSTED_NAME; |
| 99 | + } else { |
| 100 | + getEthDisplayableAddress(addr, buf, buf_size, chainConfig->chainId); |
| 101 | + param_type = PARAM_TYPE_RAW; |
| 102 | + } |
| 103 | + if (!add_to_field_table(param_type, name, buf)) { |
| 104 | + return false; |
| 105 | + } |
| 106 | + } |
| 107 | + return true; |
| 108 | +} |
| 109 | + |
| 110 | +#endif // HAVE_TRUSTED_NAME |
| 111 | +#endif // HAVE_GENERIC_TX_PARSER |
0 commit comments