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

Implementation of the hex Ethereum-address parser, with EIP-55 checksum verification (HARDENING.md §7.1). More...

#include "eth_addr.h"
#include <string.h>
#include <stdbool.h>
#include "keccak256.h"
#include "CW_Utils.h"
Include dependency graph for eth_addr.cpp:

Go to the source code of this file.

Functions

static int hex_nibble_val (char c)
 Decode a single ASCII hex digit.
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

Implementation of the hex Ethereum-address parser, with EIP-55 checksum verification (HARDENING.md §7.1).

Definition in file eth_addr.cpp.

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().

◆ hex_nibble_val()

int hex_nibble_val ( char c)
static

Decode a single ASCII hex digit.

Parameters
[in]cCharacter to decode.
Returns
Nibble value 0–15, or -1 if c is not in [0-9a-fA-F].

Definition at line 26 of file eth_addr.cpp.

Referenced by eth_addr_parse().