Skip to content

Commit b823365

Browse files
Added PARAM_TOKEN handling to GCS
1 parent 906bb16 commit b823365

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed

src_features/generic_tx_parser/gtp_field.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ typedef union {
3535
#ifdef HAVE_TRUSTED_NAME
3636
s_param_trusted_name_context trusted_name_ctx;
3737
#endif
38+
s_param_token_context token_ctx;
3839
} u_param_context;
3940

4041
static bool handle_version(const s_tlv_data *data, s_field_ctx *context) {
@@ -80,6 +81,7 @@ static bool handle_param_type(const s_tlv_data *data, s_field_ctx *context) {
8081
#ifdef HAVE_TRUSTED_NAME
8182
case PARAM_TYPE_TRUSTED_NAME:
8283
#endif
84+
case PARAM_TYPE_TOKEN:
8385
break;
8486
default:
8587
PRINTF("Error: Unsupported param type (%u)\n", context->field->param_type);
@@ -144,6 +146,10 @@ static bool handle_param(const s_tlv_data *data, s_field_ctx *context) {
144146
param_ctx.trusted_name_ctx.param = &context->field->param_trusted_name;
145147
break;
146148
#endif
149+
case PARAM_TYPE_TOKEN:
150+
handler = (f_tlv_data_handler) &handle_param_token_struct;
151+
param_ctx.token_ctx.param = &context->field->param_token;
152+
break;
147153
default:
148154
return false;
149155
}
@@ -238,6 +244,9 @@ bool format_field(const s_field *field) {
238244
ret = format_param_trusted_name(&field->param_trusted_name, field->name);
239245
break;
240246
#endif
247+
case PARAM_TYPE_TOKEN:
248+
ret = format_param_token(&field->param_token, field->name);
249+
break;
241250
default:
242251
ret = false;
243252
}

src_features/generic_tx_parser/gtp_field.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "gtp_param_unit.h"
1414
#include "gtp_param_enum.h"
1515
#include "gtp_param_trusted_name.h"
16+
#include "gtp_param_token.h"
1617

1718
typedef enum {
1819
PARAM_TYPE_RAW = 0,
@@ -24,6 +25,8 @@ typedef enum {
2425
PARAM_TYPE_UNIT,
2526
PARAM_TYPE_ENUM,
2627
PARAM_TYPE_TRUSTED_NAME,
28+
PARAM_TYPE_CALLDATA,
29+
PARAM_TYPE_TOKEN,
2730
} e_param_type;
2831

2932
typedef struct {
@@ -46,6 +49,7 @@ typedef struct {
4649
#ifdef HAVE_TRUSTED_NAME
4750
s_param_trusted_name param_trusted_name;
4851
#endif
52+
s_param_token param_token;
4953
};
5054
} s_field;
5155

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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(&param->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(&param->address, &collec);
102+
return ret;
103+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef GTP_PARAM_TOKEN_
2+
#define GTP_PARAM_TOKEN_
3+
4+
#include <stdint.h>
5+
#include <stdbool.h>
6+
#include "common_utils.h"
7+
#include "gtp_value.h"
8+
#include "tlv.h"
9+
10+
#define MAX_NATIVE_ADDRS 4
11+
12+
typedef struct {
13+
uint8_t version;
14+
s_value address;
15+
uint8_t native_addr_count;
16+
uint8_t native_addrs[MAX_NATIVE_ADDRS][ADDRESS_LENGTH];
17+
} s_param_token;
18+
19+
typedef struct {
20+
s_param_token *param;
21+
} s_param_token_context;
22+
23+
bool handle_param_token_struct(const s_tlv_data *data, s_param_token_context *context);
24+
bool format_param_token(const s_param_token *param, const char *name);
25+
26+
#endif // !GTP_PARAM_TOKEN_

0 commit comments

Comments
 (0)