-
Notifications
You must be signed in to change notification settings - Fork 267
Expand file tree
/
Copy patherc20_plugin.c
More file actions
285 lines (260 loc) · 12.6 KB
/
Copy patherc20_plugin.c
File metadata and controls
285 lines (260 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include <string.h>
#include "eth_plugin_internal.h"
#include "eth_plugin_handler.h"
#include "shared_context.h"
#include "plugin_utils.h"
#include "common_utils.h"
#include "format.h"
#include "utils.h"
#include "calldata.h"
#include "manage_asset_info.h"
#include "eth_swap_utils.h"
#include "erc20_plugin.h"
typedef enum { ERC20_TRANSFER = 0, ERC20_APPROVE } erc20Selector_t;
static const uint8_t ERC20_TRANSFER_SELECTOR[SELECTOR_SIZE] = {0xa9, 0x05, 0x9c, 0xbb};
static const uint8_t ERC20_APPROVE_SELECTOR[SELECTOR_SIZE] = {0x09, 0x5e, 0xa7, 0xb3};
const uint8_t *const ERC20_SELECTORS[NUM_ERC20_SELECTORS] = {ERC20_TRANSFER_SELECTOR,
ERC20_APPROVE_SELECTOR};
#define MAX_CONTRACT_NAME_LEN 15
#define MAX_EXTRA_DATA_CHUNKS 2
typedef struct erc20_parameters_t {
uint8_t selectorIndex;
uint8_t destinationAddress[ADDRESS_LENGTH];
uint8_t amount[INT256_LENGTH];
char ticker[MAX_TICKER_LEN];
uint8_t decimals;
char contract_name[MAX_CONTRACT_NAME_LEN];
// data not part of the ABI (usually for tracking purposes)
char extra_data[MAX_EXTRA_DATA_CHUNKS * CALLDATA_CHUNK_SIZE];
uint8_t extra_data_len;
} erc20_parameters_t;
void erc20_plugin_call(eth_plugin_msg_t message, void *parameters) {
switch (message) {
case ETH_PLUGIN_INIT_CONTRACT: {
ethPluginInitContract_t *msg = (ethPluginInitContract_t *) parameters;
erc20_parameters_t *context = (erc20_parameters_t *) msg->pluginContext;
explicit_bzero(context->extra_data, sizeof(context->extra_data));
context->extra_data_len = 0;
// enforce that ETH amount should be 0
if (!allzeroes(msg->txContent->value.value, CALLDATA_CHUNK_SIZE)) {
PRINTF("Err: Transaction amount is not 0\n");
msg->result = ETH_PLUGIN_RESULT_ERROR;
} else {
size_t i;
for (i = 0; i < NUM_ERC20_SELECTORS; i++) {
if (memcmp((uint8_t *) PIC(ERC20_SELECTORS[i]),
msg->selector,
CALLDATA_SELECTOR_SIZE) == 0) {
context->selectorIndex = i;
break;
}
}
if (i == NUM_ERC20_SELECTORS) {
PRINTF("Unknown selector %.*H\n", CALLDATA_SELECTOR_SIZE, msg->selector);
msg->result = ETH_PLUGIN_RESULT_ERROR;
break;
}
PRINTF("erc20 plugin init\n");
msg->result = ETH_PLUGIN_RESULT_OK;
}
} break;
case ETH_PLUGIN_PROVIDE_PARAMETER: {
ethPluginProvideParameter_t *msg = (ethPluginProvideParameter_t *) parameters;
erc20_parameters_t *context = (erc20_parameters_t *) msg->pluginContext;
PRINTF("erc20 plugin provide parameter %d %.*H\n",
msg->parameterOffset,
32,
msg->parameter);
switch (msg->parameterOffset) {
// function transfer(address _to, uint256 _value)
// ^^^^^^^^^^^
// function approve(address _spender, uint256 _value)
// ^^^^^^^^^^^^^^^^
case CALLDATA_SELECTOR_SIZE:
memmove(context->destinationAddress,
msg->parameter + (CALLDATA_CHUNK_SIZE - ADDRESS_LENGTH),
ADDRESS_LENGTH);
msg->result = ETH_PLUGIN_RESULT_OK;
break;
// function transfer(address _to, uint256 _value)
// ^^^^^^^^^^^^^^
// function approve(address _spender, uint256 _value)
// ^^^^^^^^^^^^^^
case CALLDATA_SELECTOR_SIZE + CALLDATA_CHUNK_SIZE:
memmove(context->amount, msg->parameter, CALLDATA_CHUNK_SIZE);
msg->result = ETH_PLUGIN_RESULT_OK;
break;
default:
if (msg->parameterOffset <= CALLDATA_SELECTOR_SIZE + CALLDATA_CHUNK_SIZE +
(MAX_EXTRA_DATA_CHUNKS * CALLDATA_CHUNK_SIZE)) {
// store extra data for possible later use
size_t extra_data_offset =
msg->parameterOffset -
(CALLDATA_SELECTOR_SIZE + (CALLDATA_CHUNK_SIZE * 2));
memmove(context->extra_data + extra_data_offset,
msg->parameter,
CALLDATA_CHUNK_SIZE);
if (msg->parameter_size <= CALLDATA_CHUNK_SIZE) {
context->extra_data_len += msg->parameter_size;
msg->result = ETH_PLUGIN_RESULT_OK;
} else {
PRINTF("Error: wrong parameter size!\n");
msg->result = ETH_PLUGIN_RESULT_ERROR;
}
} else {
PRINTF("Extra data too long to buffer\n");
context->extra_data_len = 0;
msg->result = ETH_PLUGIN_RESULT_ERROR;
}
break;
}
} break;
case ETH_PLUGIN_FINALIZE: {
ethPluginFinalize_t *msg = (ethPluginFinalize_t *) parameters;
erc20_parameters_t *context = (erc20_parameters_t *) msg->pluginContext;
PRINTF("erc20 plugin finalize %u\n", pluginType);
msg->tokenLookup1 = msg->txContent->destination;
msg->numScreens = 2;
if (context->extra_data_len > 0) {
msg->numScreens += 1;
}
if (G_called_from_swap) {
char buf[sizeof(strings.common.fullAmount)];
const tokenDefinition_t *token_def;
if (!getEthDisplayableAddress(context->destinationAddress,
buf,
sizeof(buf),
chainConfig->chainId)) {
msg->result = ETH_PLUGIN_RESULT_ERROR;
break;
}
swap_check_destination(buf);
if ((token_def = (const tokenDefinition_t *) get_asset_info_by_addr(
msg->tokenLookup1)) == NULL) {
msg->result = ETH_PLUGIN_RESULT_ERROR;
break;
}
if (!amountToString(context->amount,
sizeof(context->amount),
token_def->decimals,
token_def->ticker,
buf,
sizeof(buf))) {
msg->result = ETH_PLUGIN_RESULT_ERROR;
break;
}
swap_check_amount(buf);
G_swap_checked = true;
}
msg->uiType = ETH_UI_TYPE_GENERIC;
msg->result = ETH_PLUGIN_RESULT_OK;
} break;
case ETH_PLUGIN_PROVIDE_INFO: {
ethPluginProvideInfo_t *msg = (ethPluginProvideInfo_t *) parameters;
erc20_parameters_t *context = (erc20_parameters_t *) msg->pluginContext;
PRINTF("erc20 plugin provide token 1: %d - 2: %d\n",
(msg->item1 != NULL),
(msg->item2 != NULL));
if (msg->item1 != NULL) {
strlcpy(context->ticker, msg->item1->token.ticker, MAX_TICKER_LEN);
context->decimals = msg->item1->token.decimals;
msg->result = ETH_PLUGIN_RESULT_OK;
} else {
msg->result = ETH_PLUGIN_RESULT_FALLBACK;
}
} break;
case ETH_PLUGIN_QUERY_CONTRACT_ID: {
ethQueryContractID_t *msg = (ethQueryContractID_t *) parameters;
erc20_parameters_t *context = (erc20_parameters_t *) msg->pluginContext;
strlcpy(msg->name, "ERC20 token", msg->nameLength);
if (context->selectorIndex == ERC20_TRANSFER) {
strlcpy(msg->version, "Send", msg->versionLength);
} else {
strlcpy(msg->version, "Approve", msg->versionLength);
}
msg->result = ETH_PLUGIN_RESULT_OK;
} break;
case ETH_PLUGIN_QUERY_CONTRACT_UI: {
ethQueryContractUI_t *msg = (ethQueryContractUI_t *) parameters;
erc20_parameters_t *context = (erc20_parameters_t *) msg->pluginContext;
switch (msg->screenIndex) {
case 0:
if (context->selectorIndex == ERC20_TRANSFER) {
strlcpy(msg->title, "Send", msg->titleLength);
} else {
strlcpy(msg->title, "Approve", msg->titleLength);
}
if (ismaxint(context->amount, sizeof(context->amount))) {
strlcpy(msg->msg, "Unlimited ", msg->msgLength);
strlcat(msg->msg, context->ticker, msg->msgLength);
} else {
if (!amountToString(context->amount,
sizeof(context->amount),
context->decimals,
context->ticker,
msg->msg,
msg->msgLength)) {
msg->result = ETH_PLUGIN_RESULT_ERROR;
break;
}
}
msg->result = ETH_PLUGIN_RESULT_OK;
break;
case 1:
if (context->selectorIndex == ERC20_TRANSFER) {
strlcpy(msg->title, "To", msg->titleLength);
} else {
strlcpy(msg->title, "Approve to", msg->titleLength);
}
if (!getEthDisplayableAddress(context->destinationAddress,
msg->msg,
msg->msgLength,
chainConfig->chainId)) {
msg->result = ETH_PLUGIN_RESULT_ERROR;
}
msg->result = ETH_PLUGIN_RESULT_OK;
break;
case 2: {
PRINTF("Extra Data Length %d\n", context->extra_data_len);
PRINTF("Message Length %d\n", msg->msgLength);
if (context->extra_data_len == 0) {
// should not happen
msg->result = ETH_PLUGIN_RESULT_ERROR;
break;
}
strlcpy(msg->title, "Extra Data", msg->titleLength);
if (is_printable(context->extra_data, context->extra_data_len)) {
// display as string
PRINTF("Display as ASCII string\n");
// should never trigger unless the msg->msg buffer has been downsized
if (context->extra_data_len >= msg->msgLength) {
msg->result = ETH_PLUGIN_RESULT_ERROR;
break;
}
memmove(msg->msg, context->extra_data, context->extra_data_len);
msg->msg[context->extra_data_len] = '\0';
} else {
char hex_buf[2 + (sizeof(context->extra_data) * 2) + 1];
// display as hex
PRINTF("Display as Hex string\n");
memmove(hex_buf, "0x", 2);
if (format_hex((const uint8_t *) context->extra_data,
context->extra_data_len,
&hex_buf[2],
sizeof(hex_buf) - 2) < 0) {
msg->result = ETH_PLUGIN_RESULT_ERROR;
break;
}
str_cpy_explicit_trunc(hex_buf, strlen(hex_buf), msg->msg, msg->msgLength);
}
msg->result = ETH_PLUGIN_RESULT_OK;
break;
}
default:
break;
}
} break;
default:
PRINTF("Unhandled message %d\n", message);
}
}