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

The sale's arithmetic: keypad cents, base units, wei, fees, calldata. More...

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

Go to the source code of this file.

Macros

#define AMOUNT_CENTS_MAX   999999ULL /* 9999.99 */
#define AMOUNT_UNITS_MAX   (AMOUNT_CENTS_MAX * 10000ULL)
#define WEI_LEN   32U
 How wei is carried: a uint256, 32 bytes big-endian, as on chain.
#define ABI_SELECTOR_LEN   4U /* transfer(address,uint256) selector */
#define ABI_WORD_LEN   32U /* one ABI-encoded argument word */
#define USDC_CALLDATA_LEN   (ABI_SELECTOR_LEN + (2U * ABI_WORD_LEN)) /* 68 */
#define ABI_TO_OFFSET   (ABI_SELECTOR_LEN + (ABI_WORD_LEN - ETH_ADDR_LEN))

Enumerations

enum  evm_funds_t { EVM_FUNDS_OK = 0 , EVM_FUNDS_SHORT_GAS , EVM_FUNDS_SHORT_VALUE , EVM_FUNDS_UNKNOWN }
 What the pre-flight balance check concluded. More...

Functions

static uint64_t amount_key_digit (uint64_t cents, unsigned digit, uint64_t cap)
 A digit shifted in from the right; refused whole if it passes cap.
static uint64_t amount_key_00 (uint64_t cents, uint64_t cap)
 The "00" key: two zeroes shifted in, clamped to cap.
static uint64_t amount_key_back (uint64_t cents)
 The backspace key: the last digit dropped.
static uint64_t amount_cents_to_units (uint64_t cents)
 Keypad cents to 6-decimal base units.
static void amount_format (uint64_t units, char *out, size_t n)
 "12.50": 6-decimal base units to two places, truncated.
static void wei_from_u64 (uint64_t v, uint8_t out[WEI_LEN])
 v as a uint256.
static void wei_add (uint8_t a[WEI_LEN], const uint8_t b[WEI_LEN])
 a += b. A carry out of the top byte is dropped; every caller here stays below 2^80.
static bool wei_lt (const uint8_t a[WEI_LEN], const uint8_t b[WEI_LEN])
 a < b. Equal-length big-endian, so byte order is numeric order.
static bool evm_units_to_wei (uint64_t units, uint8_t wei[WEI_LEN])
 6-decimal keypad units -> wei, for the 18-decimal coins only.
static void evm_fees_from_gwei (uint32_t max_gwei, uint32_t prio_gwei, bool polygon, uint32_t floor_gwei, uint64_t *max_fee, uint64_t *prio_fee)
 The EIP-1559 fees one EVM sale will offer, in wei per gas, from the operator's Gwei settings.
static evm_funds_t evm_funds_check (bool native, const uint8_t have_wei[WEI_LEN], uint64_t gas_cost, uint64_t units)
 Can have_wei pay for this sale?
static void fmt_coin (char *out, size_t n, uint64_t v, unsigned dec, const char *coin)
 "0.0013 ETH": v base units of a dec-decimal coin, to six places, rounded UP — it is a ceiling, and rounding down would understate it.
static void build_usdc_calldata (uint8_t out[USDC_CALLDATA_LEN], const uint8_t to[ETH_ADDR_LEN], uint64_t amount)
 Build the 68-byte ABI-encoded calldata for a USDC transfer call.

Variables

static const uint8_t TRANSFER_SELECTOR [4] = { 0xa9U, 0x05U, 0x9cU, 0xbbU }

Detailed Description

The sale's arithmetic: keypad cents, base units, wei, fees, calldata.

Header-only and free of ESP-IDF dependencies on purpose, like form_parse.h. Every number the customer is charged passes through these few lines, and a mistake in them is silent — a wrapped multiply signs a value nobody entered, a byte out of place in the calldata pays somebody else. So they live where a host test can hold them against vectors from an independent signer (tests/units/test_money.cpp, tools/gen_kat_vectors.py), rather than as statics in main.cpp and ui.cpp where nothing could reach them.

Definition in file money.h.

Macro Definition Documentation

◆ ABI_SELECTOR_LEN

#define ABI_SELECTOR_LEN   4U /* transfer(address,uint256) selector */

Definition at line 227 of file money.h.

Referenced by build_usdc_calldata().

◆ ABI_TO_OFFSET

#define ABI_TO_OFFSET   (ABI_SELECTOR_LEN + (ABI_WORD_LEN - ETH_ADDR_LEN))

Definition at line 230 of file money.h.

Referenced by build_usdc_calldata().

◆ ABI_WORD_LEN

#define ABI_WORD_LEN   32U /* one ABI-encoded argument word */

Definition at line 228 of file money.h.

◆ AMOUNT_CENTS_MAX

#define AMOUNT_CENTS_MAX   999999ULL /* 9999.99 */

Definition at line 39 of file money.h.

Referenced by amount_kbd_cb().

◆ AMOUNT_UNITS_MAX

#define AMOUNT_UNITS_MAX   (AMOUNT_CENTS_MAX * 10000ULL)

Definition at line 41 of file money.h.

Referenced by evm_units_to_wei().

◆ USDC_CALLDATA_LEN

#define USDC_CALLDATA_LEN   (ABI_SELECTOR_LEN + (2U * ABI_WORD_LEN)) /* 68 */

Definition at line 229 of file money.h.

Referenced by build_usdc_calldata(), and sign_and_broadcast().

◆ WEI_LEN

#define WEI_LEN   32U

How wei is carried: a uint256, 32 bytes big-endian, as on chain.

Definition at line 82 of file money.h.

Referenced by evm_balance_ok(), evm_funds_check(), evm_units_to_wei(), sign_and_broadcast(), wei_add(), wei_from_u64(), and wei_lt().

Enumeration Type Documentation

◆ evm_funds_t

What the pre-flight balance check concluded.

Enumerator
EVM_FUNDS_OK 

Covered (for a token: the gas is).

EVM_FUNDS_SHORT_GAS 

Not even the network fee.

EVM_FUNDS_SHORT_VALUE 

The fee, but not the fee plus the amount.

EVM_FUNDS_UNKNOWN 

Amount past the keypad cap: not ours to say.

Definition at line 172 of file money.h.

Function Documentation

◆ amount_cents_to_units()

uint64_t amount_cents_to_units ( uint64_t cents)
inlinestatic

Keypad cents to 6-decimal base units.

Definition at line 64 of file money.h.

Referenced by amount_update_display().

◆ amount_format()

void amount_format ( uint64_t units,
char * out,
size_t n )
inlinestatic

"12.50": 6-decimal base units to two places, truncated.

Definition at line 70 of file money.h.

◆ amount_key_00()

uint64_t amount_key_00 ( uint64_t cents,
uint64_t cap )
inlinestatic

The "00" key: two zeroes shifted in, clamped to cap.

Definition at line 51 of file money.h.

Referenced by amount_kbd_cb().

◆ amount_key_back()

uint64_t amount_key_back ( uint64_t cents)
inlinestatic

The backspace key: the last digit dropped.

Definition at line 58 of file money.h.

Referenced by amount_kbd_cb().

◆ amount_key_digit()

uint64_t amount_key_digit ( uint64_t cents,
unsigned digit,
uint64_t cap )
inlinestatic

A digit shifted in from the right; refused whole if it passes cap.

Definition at line 44 of file money.h.

Referenced by amount_kbd_cb().

◆ build_usdc_calldata()

void build_usdc_calldata ( uint8_t out[USDC_CALLDATA_LEN],
const uint8_t to[ETH_ADDR_LEN],
uint64_t amount )
inlinestatic

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]outOutput buffer of USDC_CALLDATA_LEN bytes.
[in]toRecipient address, ETH_ADDR_LEN bytes (already parsed/validated).
[in]amountTransfer amount in USDC base units (6 decimals).

Definition at line 246 of file money.h.

References ABI_SELECTOR_LEN, ABI_TO_OFFSET, ETH_ADDR_LEN, TRANSFER_SELECTOR, and USDC_CALLDATA_LEN.

Referenced by sign_and_broadcast().

◆ evm_fees_from_gwei()

void evm_fees_from_gwei ( uint32_t max_gwei,
uint32_t prio_gwei,
bool polygon,
uint32_t floor_gwei,
uint64_t * max_fee,
uint64_t * prio_fee )
inlinestatic

The EIP-1559 fees one EVM sale will offer, in wei per gas, from the operator's Gwei settings.

Parameters
[in]max_gweiThe Max fee setting.
[in]prio_gweiThe Priority fee setting.
[in]polygontrue on Polygon, which has a tip floor of its own.
[in]floor_gweiThat floor (POLY_MIN_PRIORITY_FEE_GWEI).
[out]max_feeFee cap, wei per gas.
[out]prio_feeTip, wei per gas; never above max_fee.

Definition at line 149 of file money.h.

Referenced by evm_fees_wei().

◆ evm_funds_check()

evm_funds_t evm_funds_check ( bool native,
const uint8_t have_wei[WEI_LEN],
uint64_t gas_cost,
uint64_t units )
inlinestatic

Can have_wei pay for this sale?

Parameters
[in]nativetrue for ETH/POL; for a token only the gas is in wei.
[in]have_weiAccount balance, uint256.
[in]gas_costGas limit * max fee.
[in]unitsSale amount in keypad base units (native only).

Definition at line 187 of file money.h.

References EVM_FUNDS_OK, EVM_FUNDS_SHORT_GAS, EVM_FUNDS_SHORT_VALUE, EVM_FUNDS_UNKNOWN, evm_units_to_wei(), wei_add(), wei_from_u64(), WEI_LEN, and wei_lt().

Referenced by evm_balance_ok().

◆ evm_units_to_wei()

bool evm_units_to_wei ( uint64_t units,
uint8_t wei[WEI_LEN] )
inlinestatic

6-decimal keypad units -> wei, for the 18-decimal coins only.

9999.99 is ~2^73 wei, past a uint64, hence the uint256 result. Under the ceiling units * 10^6 still fits a uint64; the second 10^6 goes over its two 32-bit halves so neither product wraps.

Parameters
[in]unitsSale amount in keypad base units.
[out]weiunits * 10^12; untouched on refusal.
Returns
false past AMOUNT_UNITS_MAX, a value the keypad cannot key.

Definition at line 122 of file money.h.

References AMOUNT_UNITS_MAX, wei_add(), wei_from_u64(), and WEI_LEN.

Referenced by evm_funds_check(), and sign_and_broadcast().

◆ fmt_coin()

void fmt_coin ( char * out,
size_t n,
uint64_t v,
unsigned dec,
const char * coin )
inlinestatic

"0.0013 ETH": v base units of a dec-decimal coin, to six places, rounded UP — it is a ceiling, and rounding down would understate it.

Definition at line 207 of file money.h.

Referenced by sale_fee_text().

◆ wei_add()

void wei_add ( uint8_t a[WEI_LEN],
const uint8_t b[WEI_LEN] )
inlinestatic

a += b. A carry out of the top byte is dropped; every caller here stays below 2^80.

Definition at line 95 of file money.h.

References WEI_LEN.

Referenced by evm_funds_check(), and evm_units_to_wei().

◆ wei_from_u64()

void wei_from_u64 ( uint64_t v,
uint8_t out[WEI_LEN] )
inlinestatic

v as a uint256.

Definition at line 85 of file money.h.

References WEI_LEN.

Referenced by evm_funds_check(), and evm_units_to_wei().

◆ wei_lt()

bool wei_lt ( const uint8_t a[WEI_LEN],
const uint8_t b[WEI_LEN] )
inlinestatic

a < b. Equal-length big-endian, so byte order is numeric order.

Definition at line 106 of file money.h.

References WEI_LEN.

Referenced by evm_funds_check().

Variable Documentation

◆ TRANSFER_SELECTOR

const uint8_t TRANSFER_SELECTOR[4] = { 0xa9U, 0x05U, 0x9cU, 0xbbU }
static

Definition at line 226 of file money.h.

Referenced by build_usdc_calldata().