85static bool do_post(
const char *body,
char *resp_buf,
size_t resp_buf_size)
92 esp_http_client_config_t cfg;
93 (void)memset(&cfg, 0,
sizeof(cfg));
95 cfg.method = HTTP_METHOD_POST;
96 cfg.timeout_ms = 15000;
97 cfg.crt_bundle_attach = esp_crt_bundle_attach;
101 cfg.auth_type = HTTP_AUTH_TYPE_BASIC;
104 esp_http_client_handle_t client = esp_http_client_init(&cfg);
105 if (client == NULL) {
106 ESP_LOGE(
TAG,
"HTTP client init failed");
110 (void)esp_http_client_set_header(client,
"Content-Type",
"application/json");
112 int body_len = (int)strlen(body);
113 esp_err_t err = esp_http_client_open(client, body_len);
115 ESP_LOGE(
TAG,
"HTTP open: %s", esp_err_to_name(err));
119 if (esp_http_client_write(client, body, body_len) != body_len) {
120 ESP_LOGE(
TAG,
"HTTP write incomplete");
125 int64_t content_length = esp_http_client_fetch_headers(client);
126 (void)content_length;
131 int space = (int)(resp_buf_size - 1U) - total;
132 if (space <= 0) {
break; }
133 read = esp_http_client_read(client, resp_buf + total, space);
134 if (read > 0) { total += read; }
137 resp_buf[total] =
'\0';
138 success = (total > 0);
142 esp_http_client_close(client);
143 esp_http_client_cleanup(client);
186 ESP_ERROR_CHECK(esp_netif_init());
187 ESP_ERROR_CHECK(esp_event_loop_create_default());
188 (void)esp_netif_create_default_wifi_sta();
190 wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
191 ESP_ERROR_CHECK(esp_wifi_init(&cfg));
193 esp_event_handler_instance_t h_any;
194 esp_event_handler_instance_t h_ip;
195 ESP_ERROR_CHECK(esp_event_handler_instance_register(
197 ESP_ERROR_CHECK(esp_event_handler_instance_register(
200 wifi_config_t wifi_cfg;
201 (void)memset(&wifi_cfg, 0,
sizeof(wifi_cfg));
202 (void)strncpy((
char *)wifi_cfg.sta.ssid, ssid,
sizeof(wifi_cfg.sta.ssid) - 1U);
203 (void)strncpy((
char *)wifi_cfg.sta.password, password,
sizeof(wifi_cfg.sta.password) - 1U);
204 wifi_cfg.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
206 ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
207 ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg));
208 ESP_ERROR_CHECK(esp_wifi_start());
210 ESP_LOGI(
TAG,
"Connecting to \"%s\"...", ssid);
219 ESP_LOGI(
TAG,
"WiFi connected");
221 ESP_LOGE(
TAG,
"WiFi connect failed");
224 (void)esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, h_ip);
225 (void)esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, h_any);
263 (void)memset(input, 0,
sizeof(input));
264 (void)memcpy(input, hash, 32U);
266 (void)memcpy(input + 64U, r, 32U);
267 (void)memcpy(input + 96U, s, 32U);
272 input_hex[256] =
'\0';
276 if ((from_hex[0] ==
'0') && ((from_hex[1] ==
'x') || (from_hex[1] ==
'X'))) {
281 for (v_raw = 0U; v_raw < 2U; v_raw++) {
287 (void)snprintf(body,
sizeof(body),
288 "{\"jsonrpc\":\"2.0\",\"method\":\"eth_call\","
289 "\"params\":[{\"to\":"
290 "\"0x0000000000000000000000000000000000000001\","
291 "\"data\":\"0x%s\"},\"latest\"],\"id\":3}",
295 if (!
do_post(body, resp,
sizeof(resp))) {
301 char *result = strstr(resp,
"\"result\":\"0x");
302 if (result == NULL) {
continue; }
305 size_t result_hex_len = 0U;
307 const char *p = result;
308 while ((*p !=
'"') && (*p !=
'\0') && (*p !=
',')) {
314 if (result_hex_len < 64U) {
320 const char *recovered_hex = result + 24U;
322 if (strncasecmp(recovered_hex, from_hex, 40U) == 0) {
323 ESP_LOGI(
TAG,
"v=%u matched ecrecover", v_raw);
328 ESP_LOGW(
TAG,
"ecrecover did not match either parity, defaulting v=0");
333 char *tx_hash_out,
size_t tx_hash_max)
337 char *tx_hex = (
char *)malloc(hex_str_size);
338 if (tx_hex == NULL) {
return false; }
343 tx_hex[hex_str_size - 1U] =
'\0';
346 size_t body_size = hex_str_size + 128U;
347 char *body = (
char *)malloc(body_size);
348 if (body == NULL) { free(tx_hex);
return false; }
350 (void)snprintf(body, body_size,
351 "{\"jsonrpc\":\"2.0\",\"method\":\"eth_sendRawTransaction\","
352 "\"params\":[\"%s\"],\"id\":2}",
357 bool ok =
do_post(body, resp,
sizeof(resp));
360 if (!ok) {
return false; }
363 char *result = strstr(resp,
"\"result\":\"");
364 if (result == NULL) {
365 ESP_LOGE(
TAG,
"send_raw_tx: no result in: %s", resp);
370 char *end = strchr(result,
'"');
371 if (end == NULL) {
return false; }
373 size_t hash_len = (size_t)(end - result);
374 if ((hash_len + 1U) > tx_hash_max) {
return false; }
376 (void)memcpy(tx_hash_out, result, hash_len);
377 tx_hash_out[hash_len] =
'\0';
static EventGroupHandle_t s_wifi_event_group
static void wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
FreeRTOS event handler driving the Wi-Fi station state machine.
#define WIFI_CONNECTED_BIT