cryptnox-sdk-esp32 1.0.0
ESP32 SDK for Cryptnox Hardware Wallet
Loading...
Searching...
No Matches
main.cpp File Reference
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/spi_master.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "CryptnoxWallet.h"
#include "CW_Utils.h"
#include "Pn532NfcTransport.h"
#include "ESP32Logger.h"
#include "esp32_crypto_provider.h"
#include "ESP32Platform.h"
#include "pn532.h"
#include "keccak256.h"
#include "eth_rlp.h"
#include "eth_rpc.h"
#include "config.h"
Include dependency graph for main.cpp:

Go to the source code of this file.

Macros

#define SPI_ENABLED   1
#define I2C_ENABLED   0
#define SPI_MOSI   11
#define SPI_MISO   13
#define SPI_SCLK   12
#define SPI_MAX_TRANSFER_SZ   4096
#define SPI_PIN_UNUSED   (-1)
#define NFC_CS   10
#define TX_BUF_SIZE   300U

Functions

static void parse_address (const char *hex, uint8_t out[20])
 Parse a hex string into a 20-byte Ethereum address.
static void build_usdc_calldata (uint8_t out[68], const char *to_hex, uint64_t amount)
 Build the 68-byte ABI-encoded calldata for a USDC transfer call.
static void signing_loop (CryptnoxWallet &wallet)
 Main application loop: sign and broadcast a USDC transfer each card tap.
void app_main (void)
 ESP-IDF application entry point.

Variables

static const char *const TAG = "usdc_signing"
static const uint8_t TRANSFER_SELECTOR [4] = { 0xa9U, 0x05U, 0x9cU, 0xbbU }

Macro Definition Documentation

◆ I2C_ENABLED

#define I2C_ENABLED   0

Definition at line 66 of file main.cpp.

◆ NFC_CS

#define NFC_CS   10

Definition at line 75 of file main.cpp.

◆ SPI_ENABLED

#define SPI_ENABLED   1

Definition at line 65 of file main.cpp.

◆ SPI_MAX_TRANSFER_SZ

#define SPI_MAX_TRANSFER_SZ   4096

Definition at line 73 of file main.cpp.

◆ SPI_MISO

#define SPI_MISO   13

Definition at line 71 of file main.cpp.

◆ SPI_MOSI

#define SPI_MOSI   11

Definition at line 70 of file main.cpp.

◆ SPI_PIN_UNUSED

#define SPI_PIN_UNUSED   (-1)

Definition at line 74 of file main.cpp.

◆ SPI_SCLK

#define SPI_SCLK   12

Definition at line 72 of file main.cpp.

◆ TX_BUF_SIZE

#define TX_BUF_SIZE   300U
Examples
UsdcSigning/main/main.cpp.

Definition at line 92 of file main.cpp.

Referenced by signing_loop().

Function Documentation

◆ app_main()

◆ build_usdc_calldata()

void build_usdc_calldata ( uint8_t out[68],
const char * to_hex,
uint64_t amount )
static

Build the 68-byte ABI-encoded calldata for a USDC transfer call.

Encodes the ERC-20 transfer(address,uint256) selector followed by the ABI-encoded arguments:

selector(4) | zeroes(12) | to(20) | zeroes(24) | amount_be(8)
Parameters
[out]out68-byte output buffer for the calldata.
[in]to_hexRecipient address as a hex string (with or without 0x).
[in]amountTransfer amount in USDC base units (6 decimals).
Examples
UsdcSigning/main/main.cpp.

Definition at line 140 of file main.cpp.

References parse_address(), and TRANSFER_SELECTOR.

Referenced by signing_loop().

◆ parse_address()

void parse_address ( const char * hex,
uint8_t out[20] )
static

Parse a hex string into a 20-byte Ethereum address.

Accepts an optional 0x or 0X prefix. If the source is shorter than 20 bytes the remaining output bytes are zeroed; if longer, the excess is silently discarded.

Parameters
[in]hexHex string (with or without 0x prefix).
[out]out20-byte output buffer for the decoded address.
Examples
UsdcSigning/main/main.cpp.

Definition at line 108 of file main.cpp.

Referenced by build_usdc_calldata(), and signing_loop().

◆ signing_loop()

void signing_loop ( CryptnoxWallet & wallet)
static

Main application loop: sign and broadcast a USDC transfer each card tap.

Each iteration waits for a card, fetches the current nonce, builds and hashes an EIP-1559 USDC transfer transaction, signs it on the card via CryptnoxWallet::sign, recovers the v parity, and broadcasts the signed transaction via eth_rpc_send_raw_tx.

Parameters
[in]walletInitialised wallet instance.
Examples
UsdcSigning/main/main.cpp.

Definition at line 173 of file main.cpp.

References ADDR_TO, ADDR_USDC, AMOUNT_USDC, build_usdc_calldata(), eth_tx_t::calldata, eth_tx_t::calldata_len, CARD_PIN, CARD_PIN_LEN, eth_tx_t::chain_id, CHAIN_ID_SEPOLIA, eth_rlp_encode_signed(), eth_rlp_encode_unsigned(), eth_rpc_ecrecover_parity(), eth_rpc_get_nonce(), eth_rpc_send_raw_tx(), eth_tx_t::eth_value, eth_tx_t::gas_limit, GAS_LIMIT_ERC20, keccak256(), MAX_FEE, eth_tx_t::max_fee, MAX_PRIORITY_FEE, eth_tx_t::max_priority_fee, eth_tx_t::nonce, parse_address(), TAG, eth_tx_t::to, and TX_BUF_SIZE.

Referenced by app_main().

Variable Documentation

◆ TAG

const char* const TAG = "usdc_signing"
static

Definition at line 61 of file main.cpp.

◆ TRANSFER_SELECTOR

const uint8_t TRANSFER_SELECTOR[4] = { 0xa9U, 0x05U, 0x9cU, 0xbbU }
static
Examples
UsdcSigning/main/main.cpp.

Definition at line 89 of file main.cpp.

Referenced by build_usdc_calldata().