cryptnox-pos 1.0.0
Standalone USDC payment terminal firmware (ESP32 + Cryptnox smart card)
Loading...
Searching...
No Matches
eth_addr.h File Reference

Hex Ethereum-address parsing — a pure, dependency-free unit so it can be unit-tested and fuzzed on the host (see fuzz/fuzz_parse_address.cpp). More...

#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
Include dependency graph for eth_addr.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define ETH_ADDR_LEN   20U
 Length of a raw (binary) Ethereum address, in bytes.
#define ETH_ADDR_HEX_LEN   ((size_t)ETH_ADDR_LEN * 2U)
 Length of the same address in hex characters (no 0x prefix).

Functions

bool eth_addr_parse (const char *hex, uint8_t out[ETH_ADDR_LEN])
 Parse a 20-byte Ethereum address from a hex string.
bool eth_addr_format (const uint8_t addr[ETH_ADDR_LEN], char *out, size_t n)
 Render a 20-byte address as an EIP-55 mixed-case "0x..." string.

Detailed Description

Hex Ethereum-address parsing — a pure, dependency-free unit so it can be unit-tested and fuzzed on the host (see fuzz/fuzz_parse_address.cpp).

Definition in file eth_addr.h.

Macro Definition Documentation

◆ ETH_ADDR_HEX_LEN

#define ETH_ADDR_HEX_LEN   ((size_t)ETH_ADDR_LEN * 2U)

Length of the same address in hex characters (no 0x prefix).

Two hex chars per byte. The multiply is performed in size_t because every use site is a size_t context (strlen result, array bound, loop bound): multiplying in unsigned int and widening at the use site trips bugprone-implicit-widening-of-multiplication-result on a 64-bit host.

Definition at line 34 of file eth_addr.h.

Referenced by eth_addr_format(), and eth_addr_parse().

◆ ETH_ADDR_LEN

#define ETH_ADDR_LEN   20U

Function Documentation

◆ eth_addr_format()

bool eth_addr_format ( const uint8_t addr[ETH_ADDR_LEN],
char * out,
size_t n )

Render a 20-byte address as an EIP-55 mixed-case "0x..." string.

The inverse of eth_addr_parse, and it produces the checksummed form on purpose: an address derived on the device (from a card's public key, say) is then displayed exactly as the operator's own wallet displays it, which is the whole point of showing it to them. It also means the value that goes into storage carries a checksum, so the boot-time parse is a real check on it rather than the no-op it is for an all-lowercase string.

Parameters
[in]addr20-byte address.
[out]outBuffer for "0x" + 40 hex + NUL.
[in]nCapacity of out; needs at least 43 bytes.
Returns
true on success, false if out is too small (and then left empty).

Definition at line 109 of file eth_addr.cpp.

References ETH_ADDR_HEX_LEN, ETH_ADDR_LEN, and keccak256().

Referenced by card_read_payouts(), and sign_and_broadcast().

◆ eth_addr_parse()

bool eth_addr_parse ( const char * hex,
uint8_t out[ETH_ADDR_LEN] )

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

Accepts an optional 0x / 0X prefix, then requires exactly 40 hex characters and rejects any non-hex input instead of silently decoding garbage. A mixed-case input is treated as EIP-55 checksummed and its checksum is verified (keccak256 recomputed, rejected on case mismatch); an all-lower/all-upper input is accepted as un-checksummed.

Parameters
[in]hexAddress string (with or without 0x prefix).
[out]out20-byte decoded address; zeroed then left partially written on failure — must not be used unless true is returned.
Returns
true on success, false on wrong length, non-hex character, the zero address, or a failed EIP-55 checksum.

Definition at line 34 of file eth_addr.cpp.

References ETH_ADDR_HEX_LEN, ETH_ADDR_LEN, hex_nibble_val(), and keccak256().

Referenced by addr_plausible(), resolve_evm_payout(), and token_parse().