Skip to content

Commit 3ac3e87

Browse files
Added GTP param_trusted_name
1 parent 27a5e99 commit 3ac3e87

File tree

4 files changed

+164
-0
lines changed

4 files changed

+164
-0
lines changed

src_features/generic_tx_parser/gtp_field.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ typedef union {
3232
#ifdef HAVE_ENUM_VALUE
3333
s_param_enum_context enum_ctx;
3434
#endif
35+
#ifdef HAVE_TRUSTED_NAME
36+
s_param_trusted_name_context trusted_name_ctx;
37+
#endif
3538
} u_param_context;
3639

3740
static bool handle_version(const s_tlv_data *data, s_field_ctx *context) {
@@ -73,6 +76,9 @@ static bool handle_param_type(const s_tlv_data *data, s_field_ctx *context) {
7376
case PARAM_TYPE_UNIT:
7477
#ifdef HAVE_ENUM_VALUE
7578
case PARAM_TYPE_ENUM:
79+
#endif
80+
#ifdef HAVE_TRUSTED_NAME
81+
case PARAM_TYPE_TRUSTED_NAME:
7682
#endif
7783
break;
7884
default:
@@ -131,6 +137,12 @@ static bool handle_param(const s_tlv_data *data, s_field_ctx *context) {
131137
handler = (f_tlv_data_handler) &handle_param_enum_struct;
132138
param_ctx.enum_ctx.param = &context->field->param_enum;
133139
break;
140+
#endif
141+
#ifdef HAVE_TRUSTED_NAME
142+
case PARAM_TYPE_TRUSTED_NAME:
143+
handler = (f_tlv_data_handler) &handle_param_trusted_name_struct;
144+
param_ctx.trusted_name_ctx.param = &context->field->param_trusted_name;
145+
break;
134146
#endif
135147
default:
136148
return false;
@@ -220,6 +232,11 @@ bool format_field(const s_field *field) {
220232
case PARAM_TYPE_ENUM:
221233
ret = format_param_enum(&field->param_enum, field->name);
222234
break;
235+
#endif
236+
#ifdef HAVE_TRUSTED_NAME
237+
case PARAM_TYPE_TRUSTED_NAME:
238+
ret = format_param_trusted_name(&field->param_trusted_name, field->name);
239+
break;
223240
#endif
224241
default:
225242
ret = false;

src_features/generic_tx_parser/gtp_field.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "gtp_param_duration.h"
1313
#include "gtp_param_unit.h"
1414
#include "gtp_param_enum.h"
15+
#include "gtp_param_trusted_name.h"
1516

1617
typedef enum {
1718
PARAM_TYPE_RAW = 0,
@@ -22,6 +23,7 @@ typedef enum {
2223
PARAM_TYPE_DURATION,
2324
PARAM_TYPE_UNIT,
2425
PARAM_TYPE_ENUM,
26+
PARAM_TYPE_TRUSTED_NAME,
2527
} e_param_type;
2628

2729
typedef struct {
@@ -40,6 +42,9 @@ typedef struct {
4042
s_param_unit param_unit;
4143
#ifdef HAVE_ENUM_VALUE
4244
s_param_enum param_enum;
45+
#endif
46+
#ifdef HAVE_TRUSTED_NAME
47+
s_param_trusted_name param_trusted_name;
4348
#endif
4449
};
4550
} s_field;
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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(&param->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
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef GTP_PARAM_TRUSTED_NAME_H_
2+
#define GTP_PARAM_TRUSTED_NAME_H_
3+
4+
#ifdef HAVE_TRUSTED_NAME
5+
6+
#include <stdint.h>
7+
#include <stdbool.h>
8+
#include "tlv.h"
9+
#include "gtp_value.h"
10+
#include "trusted_name.h"
11+
12+
typedef struct {
13+
uint8_t version;
14+
s_value value;
15+
uint8_t type_count;
16+
e_name_type types[TN_TYPE_COUNT];
17+
uint8_t source_count;
18+
e_name_source sources[TN_SOURCE_COUNT];
19+
} s_param_trusted_name;
20+
21+
typedef struct {
22+
s_param_trusted_name *param;
23+
} s_param_trusted_name_context;
24+
25+
bool handle_param_trusted_name_struct(const s_tlv_data *data,
26+
s_param_trusted_name_context *context);
27+
bool format_param_trusted_name(const s_param_trusted_name *param, const char *name);
28+
29+
#endif
30+
31+
#endif // !GTP_PARAM_TRUSTED_NAME_H_

0 commit comments

Comments
 (0)