|
| 1 | +#include <string.h> |
| 2 | +#include "gtp_param_token.h" |
| 3 | +#include "shared_context.h" |
| 4 | +#include "utils.h" |
| 5 | +#include "manage_asset_info.h" |
| 6 | +#include "network.h" |
| 7 | +#include "gtp_field_table.h" |
| 8 | + |
| 9 | +enum { |
| 10 | + TAG_VERSION = 0x00, |
| 11 | + TAG_ADDRESS = 0x01, |
| 12 | + TAG_NATIVE_CURRENCY = 0x02, |
| 13 | +}; |
| 14 | + |
| 15 | +static bool handle_version(const s_tlv_data *data, s_param_token_context *context) { |
| 16 | + if (data->length != sizeof(context->param->version)) { |
| 17 | + return false; |
| 18 | + } |
| 19 | + context->param->version = data->value[0]; |
| 20 | + return true; |
| 21 | +} |
| 22 | + |
| 23 | +static bool handle_address(const s_tlv_data *data, s_param_token_context *context) { |
| 24 | + s_value_context ctx = {0}; |
| 25 | + |
| 26 | + ctx.value = &context->param->address; |
| 27 | + explicit_bzero(ctx.value, sizeof(*ctx.value)); |
| 28 | + return tlv_parse(data->value, data->length, (f_tlv_data_handler) &handle_value_struct, &ctx); |
| 29 | +} |
| 30 | + |
| 31 | +static bool handle_native_currency(const s_tlv_data *data, s_param_token_context *context) { |
| 32 | + if (data->length > ADDRESS_LENGTH) { |
| 33 | + return false; |
| 34 | + } |
| 35 | + if (context->param->native_addr_count == MAX_NATIVE_ADDRS) { |
| 36 | + return false; |
| 37 | + } |
| 38 | + memcpy(&context->param |
| 39 | + ->native_addrs[context->param->native_addr_count][ADDRESS_LENGTH - data->length], |
| 40 | + data->value, |
| 41 | + data->length); |
| 42 | + context->param->native_addr_count += 1; |
| 43 | + return true; |
| 44 | +} |
| 45 | + |
| 46 | +bool handle_param_token_struct(const s_tlv_data *data, s_param_token_context *context) { |
| 47 | + bool ret; |
| 48 | + |
| 49 | + switch (data->tag) { |
| 50 | + case TAG_VERSION: |
| 51 | + ret = handle_version(data, context); |
| 52 | + break; |
| 53 | + case TAG_ADDRESS: |
| 54 | + ret = handle_address(data, context); |
| 55 | + break; |
| 56 | + case TAG_NATIVE_CURRENCY: |
| 57 | + ret = handle_native_currency(data, context); |
| 58 | + break; |
| 59 | + default: |
| 60 | + PRINTF(TLV_TAG_ERROR_MSG, data->tag); |
| 61 | + ret = false; |
| 62 | + } |
| 63 | + return ret; |
| 64 | +} |
| 65 | + |
| 66 | +static bool match_native(const uint8_t *addr, const s_param_token *param) { |
| 67 | + for (int i = 0; i < param->native_addr_count; ++i) { |
| 68 | + if (memcmp(addr, param->native_addrs[i], ADDRESS_LENGTH) == 0) { |
| 69 | + return true; |
| 70 | + } |
| 71 | + } |
| 72 | + return false; |
| 73 | +} |
| 74 | + |
| 75 | +bool format_param_token(const s_param_token *param, const char *name) { |
| 76 | + bool ret; |
| 77 | + s_parsed_value_collection collec = {0}; |
| 78 | + uint8_t addr[ADDRESS_LENGTH]; |
| 79 | + const tokenDefinition_t *token_def; |
| 80 | + uint64_t chain_id; |
| 81 | + const char *ticker = NULL; |
| 82 | + |
| 83 | + chain_id = get_tx_chain_id(); |
| 84 | + if ((ret = value_get(¶m->address, &collec))) { |
| 85 | + for (int i = 0; i < collec.size; ++i) { |
| 86 | + buf_shrink_expand(collec.value[i].ptr, collec.value[i].length, addr, sizeof(addr)); |
| 87 | + if (match_native(addr, param)) { |
| 88 | + ticker = get_displayable_ticker(&chain_id, chainConfig); |
| 89 | + } else if ((token_def = (const tokenDefinition_t *) get_asset_info_by_addr(addr))) { |
| 90 | + ticker = token_def->ticker; |
| 91 | + } |
| 92 | + if (ticker == NULL) { |
| 93 | + ret = false; |
| 94 | + break; |
| 95 | + } |
| 96 | + if (!(ret = add_to_field_table(PARAM_TYPE_TOKEN, name, ticker))) { |
| 97 | + break; |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + value_cleanup(¶m->address, &collec); |
| 102 | + return ret; |
| 103 | +} |
0 commit comments