-
Notifications
You must be signed in to change notification settings - Fork 265
Expand file tree
/
Copy patheth2_plugin.c
More file actions
230 lines (208 loc) · 9.73 KB
/
eth2_plugin.c
File metadata and controls
230 lines (208 loc) · 9.73 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
#ifdef HAVE_ETH2
#include <string.h>
#include "eth_plugin_internal.h"
#include "eth_plugin_handler.h"
#include "shared_context.h"
#include "common_utils.h"
#include "eth2_plugin.h"
static const uint8_t ETH2_DEPOSIT_SELECTOR[SELECTOR_SIZE] = {0x22, 0x89, 0x51, 0x18};
const uint8_t *const ETH2_SELECTORS[NUM_ETH2_SELECTORS] = {ETH2_DEPOSIT_SELECTOR};
void getEth2PublicKey(uint32_t *bip32Path, uint8_t bip32PathLength, uint8_t *out);
#define WITHDRAWAL_KEY_PATH_1 12381
#define WITHDRAWAL_KEY_PATH_2 3600
#define WITHDRAWAL_KEY_PATH_4 0
#define ETH2_DEPOSIT_PUBKEY_OFFSET 0x80
#define ETH2_WITHDRAWAL_CREDENTIALS_OFFSET 0xE0
#define ETH2_SIGNATURE_OFFSET 0x120
#define ETH2_DEPOSIT_PUBKEY_LENGTH 0x30
#define ETH2_WITHDRAWAL_CREDENTIALS_LENGTH 0x20
#define ETH2_SIGNATURE_LENGTH 0x60
static const uint8_t deposit_contract_address[ADDRESS_LENGTH] = {
0x00, 0x00, 0x00, 0x00, 0x21, 0x9a, 0xb5, 0x40, 0x35, 0x6c,
0xbb, 0x83, 0x9c, 0xbe, 0x05, 0x30, 0x3d, 0x77, 0x05, 0xfa,
};
const uint8_t *const ETH2_ADDRESSES[NUM_ETH2_ADDRESSES] = {deposit_contract_address};
// Highest index for withdrawal derivation path.
#define INDEX_MAX 65536 // 2 ^ 16 : arbitrary value to protect from path attacks.
typedef struct eth2_deposit_parameters_t {
uint8_t valid;
char deposit_address[ETH2_DEPOSIT_PUBKEY_LENGTH];
} eth2_deposit_parameters_t;
void eth2_plugin_call(eth_plugin_msg_t message, void *parameters) {
if (parameters == NULL) {
return;
}
switch (message) {
case ETH_PLUGIN_INIT_CONTRACT: {
ethPluginInitContract_t *msg = (ethPluginInitContract_t *) parameters;
eth2_deposit_parameters_t *context = (eth2_deposit_parameters_t *) msg->pluginContext;
context->valid = 1;
msg->result = ETH_PLUGIN_RESULT_OK;
} break;
case ETH_PLUGIN_PROVIDE_PARAMETER: {
ethPluginProvideParameter_t *msg = (ethPluginProvideParameter_t *) parameters;
eth2_deposit_parameters_t *context = (eth2_deposit_parameters_t *) msg->pluginContext;
uint32_t index;
PRINTF("eth2 plugin provide parameter %d %.*H\n",
msg->parameterOffset,
32,
msg->parameter);
switch (msg->parameterOffset) {
case 4 + (32 * 0): // pubkey offset
case 4 + (32 * 1): // withdrawal credentials offset
case 4 + (32 * 2): // signature offset
case 4 + (32 * 4): // deposit pubkey length
case 4 + (32 * 7): // withdrawal credentials length
case 4 + (32 * 9): // signature length
{
uint32_t check = 0;
switch (msg->parameterOffset) {
case 4 + (32 * 0):
check = ETH2_DEPOSIT_PUBKEY_OFFSET;
break;
case 4 + (32 * 1):
check = ETH2_WITHDRAWAL_CREDENTIALS_OFFSET;
break;
case 4 + (32 * 2):
check = ETH2_SIGNATURE_OFFSET;
break;
case 4 + (32 * 4):
check = ETH2_DEPOSIT_PUBKEY_LENGTH;
break;
case 4 + (32 * 7):
check = ETH2_WITHDRAWAL_CREDENTIALS_LENGTH;
break;
case 4 + (32 * 9):
check = ETH2_SIGNATURE_LENGTH;
break;
default:
break;
}
index = U4BE(msg->parameter, 32 - 4);
if (index != check) {
PRINTF("eth2 plugin parameter check %d failed, expected %d got %d\n",
msg->parameterOffset,
check,
index);
context->valid = 0;
}
msg->result = ETH_PLUGIN_RESULT_OK;
} break;
case 4 + (32 * 5): // deposit pubkey 1
{
memcpy(context->deposit_address, msg->parameter, PARAMETER_LENGTH);
msg->result = ETH_PLUGIN_RESULT_OK;
break;
}
case 4 + (32 * 6): // deposit pubkey 2
{
// Copy the last 16 bytes.
memcpy(context->deposit_address + 32,
msg->parameter,
sizeof(context->deposit_address) - 32);
// Use a temporary buffer to store the string representation.
char tmp[ETH2_DEPOSIT_PUBKEY_LENGTH];
if (!getEthDisplayableAddress((uint8_t *) context->deposit_address,
tmp,
sizeof(tmp),
chainConfig->chainId)) {
msg->result = ETH_PLUGIN_RESULT_ERROR;
return;
}
// Copy back the string to the global variable.
strlcpy(context->deposit_address, tmp, ETH2_DEPOSIT_PUBKEY_LENGTH);
msg->result = ETH_PLUGIN_RESULT_OK;
break;
}
case 4 + (32 * 3): // deposit data root
case 4 + (32 * 10): // signature
case 4 + (32 * 11):
case 4 + (32 * 12):
msg->result = ETH_PLUGIN_RESULT_OK;
break;
case 4 + (32 * 8): // withdrawal credentials
{
uint8_t tmp[48] = {0};
uint32_t withdrawalKeyPath[4];
withdrawalKeyPath[0] = WITHDRAWAL_KEY_PATH_1;
withdrawalKeyPath[1] = WITHDRAWAL_KEY_PATH_2;
if (eth2WithdrawalIndex > INDEX_MAX) {
PRINTF("eth2 plugin: withdrawal index is too big\n");
PRINTF("Got %u which is higher than INDEX_MAX (%u)\n",
eth2WithdrawalIndex,
INDEX_MAX);
msg->result = ETH_PLUGIN_RESULT_ERROR;
context->valid = 0;
}
withdrawalKeyPath[2] = eth2WithdrawalIndex;
withdrawalKeyPath[3] = WITHDRAWAL_KEY_PATH_4;
getEth2PublicKey(withdrawalKeyPath, 4, tmp);
PRINTF("eth2 plugin computed withdrawal public key %.*H\n", 48, tmp);
cx_hash_sha256(tmp, 48, tmp, 32);
tmp[0] = 0;
if (memcmp(tmp, msg->parameter, 32) != 0) {
PRINTF("eth2 plugin invalid withdrawal credentials\n");
PRINTF("Got %.*H\n", 32, msg->parameter);
PRINTF("Expected %.*H\n", 32, tmp);
msg->result = ETH_PLUGIN_RESULT_ERROR;
context->valid = 0;
} else {
msg->result = ETH_PLUGIN_RESULT_OK;
}
} break;
default:
PRINTF("Unhandled parameter offset\n");
break;
}
} break;
case ETH_PLUGIN_FINALIZE: {
ethPluginFinalize_t *msg = (ethPluginFinalize_t *) parameters;
eth2_deposit_parameters_t *context = (eth2_deposit_parameters_t *) msg->pluginContext;
PRINTF("eth2 plugin finalize\n");
if (context->valid) {
msg->numScreens = 2;
msg->uiType = ETH_UI_TYPE_GENERIC;
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;
strlcpy(msg->name, "ETH2", msg->nameLength);
strlcpy(msg->version, "Deposit", msg->versionLength);
msg->result = ETH_PLUGIN_RESULT_OK;
} break;
case ETH_PLUGIN_QUERY_CONTRACT_UI: {
ethQueryContractUI_t *msg = (ethQueryContractUI_t *) parameters;
eth2_deposit_parameters_t *context = (eth2_deposit_parameters_t *) msg->pluginContext;
switch (msg->screenIndex) {
case 0: { // Amount screen
uint8_t decimals = WEI_TO_ETHER;
const char *ticker = chainConfig->coinName;
strlcpy(msg->title, "Amount", msg->titleLength);
if (!amountToString(tmpContent.txContent.value.value,
tmpContent.txContent.value.length,
decimals,
ticker,
msg->msg,
msg->msgLength)) {
msg->result = ETH_PLUGIN_RESULT_ERROR;
break;
}
msg->result = ETH_PLUGIN_RESULT_OK;
} break;
case 1: { // Deposit pubkey screen
strlcpy(msg->title, "Validator", msg->titleLength);
strlcpy(msg->msg, context->deposit_address, msg->msgLength);
msg->result = ETH_PLUGIN_RESULT_OK;
} break;
default:
break;
}
} break;
default:
PRINTF("Unhandled message %d\n", message);
}
}
#endif