|
cryptnox-sdk-arduino 1.0.0
Arduino library for Cryptnox Hardware Wallet
|
#include <Arduino.h>#include <SPI.h>#include "keccak256.h"#include <WiFiS3.h>#include <ArduinoHttpClient.h>#include "util.h"#include "config.h"#include <CryptnoxWallet.h>Go to the source code of this file.
Classes | |
| struct | Tx2 |
| Ethereum EIP-1559 transaction structure. More... | |
Macros | |
| #define | PN532_SS_PIN (10U) |
| PN532 SPI slave-select pin. | |
| #define | CARD_PIN "000000000" |
| #define | CARD_PIN_LEN (9U) |
| #define | RPC_PATH "/" |
| #define | WIFI_CA_CERT |
| #define | ERC20_TRANSFER_SEL_0 0xa9U |
| #define | ERC20_TRANSFER_SEL_1 0x05U |
| #define | ERC20_TRANSFER_SEL_2 0x9cU |
| #define | ERC20_TRANSFER_SEL_3 0xbbU |
| #define | ERC20_INDEX_OFFSET 64U |
| #define | YPARITY_UNKNOWN 0xFFU |
| Sentinel returned by determineYParity() when recovery fails. | |
| #define | HTTP_OK 200 |
| Expected HTTP 200 OK status code. | |
| #define | TX_MAX_RETRIES 3U |
| Maximum number of send-transaction attempts before giving up. | |
| #define | TX_RETRY_DELAY_MS 2000U |
| Delay in ms between send-transaction retry attempts. | |
| #define | WIFI_RETRY_MAX 20U |
| Maximum WiFi reconnect poll iterations (each iteration waits 500 ms). | |
| #define | HEX_CHAR_BUF_SIZE 3U |
| Buffer size for a two-hex-char + NUL string used in byte-to-hex conversion. | |
| #define | ECRECOVER_V_PAD_CHARS 62U |
| Number of leading zero hex characters in the ecrecover v-field padding. | |
| #define | ECRECOVER_V_BASE 27U |
| Base value for Ethereum ecrecover v parameter (yParity=0 → v=27, yParity=1 → v=28). | |
| #define | RLP_ITEM_OR_FAIL(BUF, CAP, OFF, IN, IN_LEN) |
Functions | |
| PN532Adapter | nfc (serialAdapter, PN532_SS_PIN, &SPI) |
| static void | printHex (const char *label, const uint8_t *data, size_t len) |
| static bool | ensureWiFi () |
| static size_t | rlpEncodeTxBody (uint8_t *buf, size_t bufCap, const Tx2 &tx) |
| static size_t | rlpFinalize (uint8_t *out, size_t outCap, const uint8_t *buf, size_t off) |
| size_t | rlpEncodeUnsignedTx (const Tx2 &tx, uint8_t *out, size_t outCap) |
| size_t | rlpEncodeSignedTx (const Tx2 &tx, const uint8_t *r, const uint8_t *s, const uint8_t *v, uint8_t *out, size_t outCap) |
| size_t | encodeERC20Transfer (uint8_t *out) |
| Encode calldata for ERC-20 transfer(address to, uint256 amount). | |
| bool | sendRawTx (const uint8_t *raw, size_t len) |
| Send a raw signed transaction via JSON-RPC. | |
| uint8_t | determineYParity (const uint8_t *hash, const uint8_t *r, const uint8_t *s) |
| Determine EIP-1559 yParity by calling the Ethereum ecrecover precompile. | |
| uint8_t | fetchNonce (uint64_t *nonce) |
| Fetch the current nonce for ADDR_FROM via eth_getTransactionCount. | |
| void | setup () |
| Arduino setup: init PN532, connect WiFi, build tx, sign with Cryptnox card, send. | |
| void | loop () |
| Arduino loop (empty — transaction is sent once in setup). | |
Variables | |
| ArduinoLoggerAdapter | serialAdapter |
| ArduinoCryptoProvider | cryptoProvider |
| ArduinoPlatform | platform |
| CryptnoxWallet | wallet (nfc, serialAdapter, cryptoProvider, platform) |
| static const char | hexChars [] = "0123456789abcdef" |
| #define CARD_PIN "000000000" |
Definition at line 37 of file UsdcSigning.ino.
| #define CARD_PIN_LEN (9U) |
Definition at line 38 of file UsdcSigning.ino.
| #define ECRECOVER_V_BASE 27U |
Base value for Ethereum ecrecover v parameter (yParity=0 → v=27, yParity=1 → v=28).
Definition at line 128 of file UsdcSigning.ino.
Referenced by determineYParity().
| #define ECRECOVER_V_PAD_CHARS 62U |
Number of leading zero hex characters in the ecrecover v-field padding.
Definition at line 126 of file UsdcSigning.ino.
Referenced by determineYParity().
| #define ERC20_INDEX_OFFSET 64U |
Definition at line 109 of file UsdcSigning.ino.
Referenced by encodeERC20Transfer().
| #define ERC20_TRANSFER_SEL_0 0xa9U |
ERC-20 transfer(address,uint256) function selector: keccak256("transfer(address,uint256)")[0..3]
Definition at line 104 of file UsdcSigning.ino.
Referenced by encodeERC20Transfer().
| #define ERC20_TRANSFER_SEL_1 0x05U |
Definition at line 105 of file UsdcSigning.ino.
Referenced by encodeERC20Transfer().
| #define ERC20_TRANSFER_SEL_2 0x9cU |
Definition at line 106 of file UsdcSigning.ino.
Referenced by encodeERC20Transfer().
| #define ERC20_TRANSFER_SEL_3 0xbbU |
Definition at line 107 of file UsdcSigning.ino.
Referenced by encodeERC20Transfer().
| #define HEX_CHAR_BUF_SIZE 3U |
Buffer size for a two-hex-char + NUL string used in byte-to-hex conversion.
Definition at line 124 of file UsdcSigning.ino.
Referenced by sendRawTx().
| #define HTTP_OK 200 |
Expected HTTP 200 OK status code.
Definition at line 115 of file UsdcSigning.ino.
Referenced by determineYParity(), fetchNonce(), and sendRawTx().
| #define PN532_SS_PIN (10U) |
PN532 SPI slave-select pin.
Definition at line 31 of file UsdcSigning.ino.
| #define RLP_ITEM_OR_FAIL | ( | BUF, | |
| CAP, | |||
| OFF, | |||
| IN, | |||
| IN_LEN ) |
Definition at line 252 of file UsdcSigning.ino.
Referenced by rlpEncodeTxBody().
| #define RPC_PATH "/" |
Definition at line 44 of file UsdcSigning.ino.
| #define TX_MAX_RETRIES 3U |
Maximum number of send-transaction attempts before giving up.
Definition at line 118 of file UsdcSigning.ino.
Referenced by fetchNonce(), and sendRawTx().
| #define TX_RETRY_DELAY_MS 2000U |
Delay in ms between send-transaction retry attempts.
Definition at line 120 of file UsdcSigning.ino.
Referenced by sendRawTx().
| #define WIFI_CA_CERT |
Definition at line 59 of file UsdcSigning.ino.
| #define WIFI_RETRY_MAX 20U |
Maximum WiFi reconnect poll iterations (each iteration waits 500 ms).
Definition at line 122 of file UsdcSigning.ino.
Referenced by ensureWiFi().
| #define YPARITY_UNKNOWN 0xFFU |
Sentinel returned by determineYParity() when recovery fails.
Definition at line 112 of file UsdcSigning.ino.
Referenced by determineYParity(), and setup().
| uint8_t determineYParity | ( | const uint8_t * | hash, |
| const uint8_t * | r, | ||
| const uint8_t * | s ) |
Determine EIP-1559 yParity by calling the Ethereum ecrecover precompile.
Tries v=27 (yParity=0) and v=28 (yParity=1). Compares the recovered address with ADDR_FROM (defined in config.h) to pick the correct value.
| hash | Keccak-256 transaction hash (32 bytes). |
| r | Signature r component (32 bytes). |
| s | Signature s component (32 bytes). |
Definition at line 471 of file UsdcSigning.ino.
References ADDR_FROM, ECRECOVER_V_BASE, ECRECOVER_V_PAD_CHARS, ensureWiFi(), F, hexChars, HTTP_OK, RPC_HOST, RPC_PATH, RPC_PORT, WIFI_CA_CERT, and YPARITY_UNKNOWN.
Referenced by setup().
| size_t encodeERC20Transfer | ( | uint8_t * | out | ) |
Encode calldata for ERC-20 transfer(address to, uint256 amount).
| out | Output buffer (at least 68 bytes) |
Definition at line 346 of file UsdcSigning.ino.
References ADDR_TO, AMOUNT_USDC, ERC20_INDEX_OFFSET, ERC20_TRANSFER_SEL_0, ERC20_TRANSFER_SEL_1, ERC20_TRANSFER_SEL_2, ERC20_TRANSFER_SEL_3, F, hexToBytes(), and CW_Utils::secure_wipe().
Referenced by setup().
|
static |
Definition at line 239 of file UsdcSigning.ino.
References WIFI_PASSWORD, WIFI_RETRY_MAX, and WIFI_SSID.
Referenced by determineYParity(), fetchNonce(), and sendRawTx().
| uint8_t fetchNonce | ( | uint64_t * | nonce | ) |
Fetch the current nonce for ADDR_FROM via eth_getTransactionCount.
| nonce | Output: nonce value on success (note: 0 is a valid nonce for a fresh address). |
Definition at line 586 of file UsdcSigning.ino.
References ADDR_FROM, ensureWiFi(), F, fromHex(), HTTP_OK, RPC_HOST, RPC_PATH, RPC_PORT, TX_MAX_RETRIES, and WIFI_CA_CERT.
Referenced by setup().
| void loop | ( | ) |
Arduino loop (empty — transaction is sent once in setup).
Definition at line 840 of file UsdcSigning.ino.
| PN532Adapter nfc | ( | serialAdapter | , |
| PN532_SS_PIN | , | ||
| & | SPI ) |
References PN532_SS_PIN, and serialAdapter.
|
static |
| size_t rlpEncodeSignedTx | ( | const Tx2 & | tx, |
| const uint8_t * | r, | ||
| const uint8_t * | s, | ||
| const uint8_t * | v, | ||
| uint8_t * | out, | ||
| size_t | outCap ) |
Definition at line 311 of file UsdcSigning.ino.
References RlpEncodeItem(), rlpEncodeTxBody(), rlpFinalize(), CW_Utils::secure_wipe(), and trimLeadingZeros().
Referenced by setup().
|
static |
Definition at line 260 of file UsdcSigning.ino.
References Tx2::chainId, ConvertNumberToUintArray(), Tx2::data, Tx2::dataLen, F, Tx2::gasLimit, hexToBytes(), Tx2::maxFeePerGas, Tx2::maxPriorityFeePerGas, Tx2::nonce, RLP_ITEM_OR_FAIL, Tx2::to, and Tx2::value.
Referenced by rlpEncodeSignedTx(), and rlpEncodeUnsignedTx().
| size_t rlpEncodeUnsignedTx | ( | const Tx2 & | tx, |
| uint8_t * | out, | ||
| size_t | outCap ) |
Definition at line 304 of file UsdcSigning.ino.
References rlpEncodeTxBody(), and rlpFinalize().
Referenced by setup().
|
static |
Definition at line 288 of file UsdcSigning.ino.
References RlpEncodeWholeHeader(), and CW_Utils::safe_memcpy().
Referenced by rlpEncodeSignedTx(), and rlpEncodeUnsignedTx().
| bool sendRawTx | ( | const uint8_t * | raw, |
| size_t | len ) |
Send a raw signed transaction via JSON-RPC.
| raw | Signed transaction bytes |
| len | Length of raw transaction in bytes |
Definition at line 370 of file UsdcSigning.ino.
References ensureWiFi(), F, HEX_CHAR_BUF_SIZE, hexChars, HTTP_OK, RPC_HOST, RPC_PATH, RPC_PORT, TX_MAX_RETRIES, TX_RETRY_DELAY_MS, and WIFI_CA_CERT.
Referenced by setup().
| void setup | ( | ) |
Arduino setup: init PN532, connect WiFi, build tx, sign with Cryptnox card, send.
Definition at line 672 of file UsdcSigning.ino.
References ADDR_USDC, CARD_PIN, CARD_PIN_LEN, CHAIN_ID_SEPOLIA, Tx2::chainId, CW_HASH_SIZE, CW_OK, CW_SIGN_DERIVE_K1, CW_SIGN_NO_KEY_LOADED, CW_SIGN_PIN_INCORRECT, CW_SIGN_SIG_ECDSA_LOW_S, CW_SIGN_WITH_PIN, Tx2::data, Tx2::dataLen, CW_SignRequest::derivePath, CW_SignRequest::derivePathLength, determineYParity(), encodeERC20Transfer(), CW_SignResult::errorCode, F, fetchNonce(), GAS_LIMIT_ERC20, Tx2::gasLimit, CW_SignRequest::hash, CW_SignRequest::hashLength, HEX, keccak256(), MAX_FEE, MAX_PRIORITY_FEE, Tx2::maxFeePerGas, Tx2::maxPriorityFeePerGas, Tx2::nonce, CW_SignRequest::pin, printHex(), rlpEncodeSignedTx(), rlpEncodeUnsignedTx(), CW_Utils::safe_memcpy(), CW_Utils::secure_wipe(), sendRawTx(), CW_SignResult::signature, Tx2::to, Tx2::value, wallet, WIFI_PASSWORD, WIFI_SSID, and YPARITY_UNKNOWN.
| ArduinoCryptoProvider cryptoProvider |
Definition at line 98 of file UsdcSigning.ino.
|
static |
Definition at line 145 of file UsdcSigning.ino.
Referenced by determineYParity(), printHex(), and sendRawTx().
| ArduinoPlatform platform |
Definition at line 99 of file UsdcSigning.ino.
| ArduinoLoggerAdapter serialAdapter |
Definition at line 97 of file UsdcSigning.ino.
| CryptnoxWallet wallet(nfc, serialAdapter, cryptoProvider, platform) | ( | nfc | , |
| serialAdapter | , | ||
| cryptoProvider | , | ||
| platform | ) |