BasicUsage ESP32 example: initialise PN532, connect to a Cryptnox card, and sign a hash.
BasicUsage ESP32 example: initialise PN532, connect to a Cryptnox card, and sign a hash.This is the ESP32-IDF port of the Arduino BasicUsage.ino sketch. It demonstrates the complete wallet flow with minimal code:
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "driver/spi_master.h"
#include "esp_log.h"
#include "esp_wifi.h"
#include "esp_netif.h"
#include "esp_event.h"
#include "nvs_flash.h"
#include "CryptnoxWallet.h"
#include "CW_Utils.h"
#include "config.h"
#define SPI_ENABLED 1
#define I2C_ENABLED 0
#if SPI_ENABLED
#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
#endif
#if I2C_ENABLED
#define PN532_I2C_PORT 0
#define PN532_SDA 27
#define PN532_SCL 22
#define PN532_IRQ (-1)
#define PN532_RST (-1)
#define PN532_I2C_HZ 100000U
#endif
static const char *
const TAG =
"basic_usage";
#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1
int32_t event_id, void *event_data)
{
(void)arg;
(void)event_data;
if ((event_base == WIFI_EVENT) && (event_id == WIFI_EVENT_STA_START)) {
esp_wifi_connect();
} else if ((event_base == WIFI_EVENT) &&
(event_id == WIFI_EVENT_STA_DISCONNECTED)) {
esp_wifi_connect();
} else {
}
} else if ((event_base == IP_EVENT) && (event_id == IP_EVENT_STA_GOT_IP)) {
} else {
}
}
{
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
(void)esp_netif_create_default_wifi_sta();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
esp_event_handler_instance_t h_any;
esp_event_handler_instance_t h_ip;
ESP_ERROR_CHECK(esp_event_handler_instance_register(
ESP_ERROR_CHECK(esp_event_handler_instance_register(
wifi_config_t wifi_cfg;
(void)memset(&wifi_cfg, 0, sizeof(wifi_cfg));
(void)strncpy((
char *)wifi_cfg.sta.ssid,
WIFI_SSID,
sizeof(wifi_cfg.sta.ssid) - 1U);
sizeof(wifi_cfg.sta.password) - 1U);
wifi_cfg.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_cfg));
ESP_ERROR_CHECK(esp_wifi_start());
pdFALSE, pdFALSE,
ESP_LOGI(
TAG,
"WiFi connected");
} else {
ESP_LOGW(
TAG,
"WiFi connect failed — TRNG entropy may be reduced");
}
(void)esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, h_ip);
(void)esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, h_any);
}
{
bool keep_running = true;
while (keep_running) {
CW_SecureSession session{};
if (!wallet.connect(session)) {
ESP_LOGW(
TAG,
"Card not detected — hold card to reader");
continue;
}
ESP_LOGI(
TAG,
"Card connected and secure channel established");
ESP_LOGI(
TAG,
"Signing test hash...");
uint8_t testHash[CW_HASH_SIZE];
(void)memset(testHash, 0x01, sizeof(testHash));
CW_SignRequest signRequest(session,
CW_SIGN_CURR_K1,
CW_SIGN_SIG_ECDSA_LOW_S,
CW_SIGN_WITH_PIN);
signRequest.hash = testHash;
signRequest.hashLength = static_cast<uint8_t>(CW_HASH_SIZE);
(void)CW_Utils::safe_memcpy(signRequest.pin, sizeof(signRequest.pin),
CW_SignResult signResult = wallet.sign(signRequest);
if (signResult.errorCode == CW_OK) {
ESP_LOGI(
TAG,
"Signature received (64 bytes raw r||s)");
ESP_LOG_BUFFER_HEX_LEVEL(
TAG,
&signResult.signature[CW_SIG_R_OFFSET], 8U, ESP_LOG_INFO);
ESP_LOG_BUFFER_HEX_LEVEL(
TAG,
&signResult.signature[CW_SIG_S_OFFSET], 8U, ESP_LOG_INFO);
ESP_LOGI(
TAG,
"Card processed successfully");
} else if (signResult.errorCode == CW_SIGN_PIN_INCORRECT) {
ESP_LOGE(
TAG,
"Wrong PIN — halting to protect retry counter");
CW_Utils::secure_wipe(testHash, sizeof(testHash));
CW_Utils::secure_wipe(signResult.signature, sizeof(signResult.signature));
wallet.disconnect(session);
keep_running = false;
continue;
} else {
ESP_LOGE(
TAG,
"Sign failed: errorCode = 0x%02X",
static_cast<unsigned int>(signResult.errorCode));
}
CW_Utils::secure_wipe(testHash, sizeof(testHash));
CW_Utils::secure_wipe(signResult.signature, sizeof(signResult.signature));
wallet.disconnect(session);
}
while (true) {
}
}
{
esp_err_t nvs_ret = nvs_flash_init();
if ((nvs_ret == ESP_ERR_NVS_NO_FREE_PAGES) ||
(nvs_ret == ESP_ERR_NVS_NEW_VERSION_FOUND)) {
ESP_ERROR_CHECK(nvs_flash_erase());
nvs_ret = nvs_flash_init();
}
ESP_ERROR_CHECK(nvs_ret);
#if SPI_ENABLED
spi_bus_config_t buscfg = {};
ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO));
#endif
#if I2C_ENABLED
#endif
if (nfc_ret != ESP_OK) {
ESP_LOGE(
TAG,
"PN532 init failed — check wiring and interface selection");
return;
}
(void)logger.
begin(115200UL);
CryptnoxWallet wallet(nfcTransport, logger, cryptoProvider, platform);
if (!wallet.begin()) {
ESP_LOGE(
TAG,
"Wallet init failed (SAMConfig)");
return;
}
(void)nfcTransport.printFirmwareVersion();
ESP_LOGI(
TAG,
"Ready — hold Cryptnox card to reader");
}
static const size_t DEFAULT_PIN_LEN
void app_main(void)
ESP-IDF application entry point.
static EventGroupHandle_t s_wifi_event_group
#define SPI_MAX_TRANSFER_SZ
static void run_basic_usage_loop(CryptnoxWallet &wallet)
Main application loop: connect, sign a test hash, wipe buffers, disconnect.
static const uint32_t LOOP_DELAY_MS
static const uint8_t DEFAULT_PIN[]
Demo PIN — replace with the PIN used during card initialisation.
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
static void wifi_start(void)
Initialise Wi-Fi station mode and block until connected or timeout.
CW_Logger implementation that writes to ESP32 UART0 via printf.
CW_NfcTransport adapter wrapping the ESP-IDF PN532 NFC driver.
CW_CryptoProvider backed by mbedTLS and the ESP32 hardware TRNG.
CW_Logger backed by ESP32 UART0.
bool begin(unsigned long baudRate=115200UL) override
Initialise UART0 at the given baud rate.
CW_NfcTransport implementation backed by the ESP-IDF PN532 driver.
CW_CryptoProvider implementation for ESP32 using mbedTLS and the hardware TRNG.
static const char *const TAG
esp_err_t pn532_init(pn532_t *dev, const pn532_config_t *config)
Initialise the PN532 and bring it to a ready state.
Low-level PN532 NFC controller driver for ESP-IDF (SPI and I²C).
Compile-time configuration passed to pn532_init.
spi_host_device_t spi_host
pn532_transport_t transport
Opaque-like runtime state for a single PN532 instance.