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

Implementation of the JSON-RPC response parsing helpers. More...

#include "eth_json.h"
#include <string.h>
#include "CW_Utils.h"
#include "cJSON.h"
Include dependency graph for eth_json.cpp:

Go to the source code of this file.

Functions

bool eth_json_result_string (const char *resp, char *out, size_t out_size)
 Extract the top-level "result" string from a JSON-RPC response.
bool eth_json_error_message (const char *resp, char *out, size_t out_size)
 Extract the JSON-RPC error.message from a response body.
eth_json_receipt_t eth_json_receipt_status (const char *resp)
 Classify an eth_getTransactionReceipt response body.
static char lc (char c)
static bool str_is (const cJSON *j, const char *want)
 true if a JSON string equals want, ignoring hex case.
static void addr_hex (const uint8_t a[20], bool word, char *out)
 "0x" + the 20 bytes as hex, or left-padded to a 32-byte word.
static void word_hex (uint64_t v, char *out)
 "0x" + v as a 32-byte big-endian word.
static bool has_transfer (const cJSON *logs, const char *token, const char *payee_word, const char *amount_word)
 true if logs holds a Transfer(*, payee, amount) from token.
eth_json_receipt_t eth_json_receipt_check (const char *resp, const eth_receipt_expect_t *want)
 Classify a receipt AND check it is the payment that was asked for.

Variables

static const char TRANSFER_TOPIC []

Detailed Description

Implementation of the JSON-RPC response parsing helpers.

Definition in file eth_json.cpp.

Function Documentation

◆ addr_hex()

void addr_hex ( const uint8_t a[20],
bool word,
char * out )
static

"0x" + the 20 bytes as hex, or left-padded to a 32-byte word.

Definition at line 114 of file eth_json.cpp.

Referenced by eth_json_receipt_check().

◆ eth_json_error_message()

bool eth_json_error_message ( const char * resp,
char * out,
size_t out_size )

Extract the JSON-RPC error.message from a response body.

The counterpart to eth_json_result_string, and the only place a node ever says why it refused a transaction: "insufficient funds for gas * price + value", "nonce too low", "transaction underpriced". Dropping it on the floor is what turns every one of those into an identical "Broadcast failed" on the panel, which tells an operator nothing they can act on.

A message longer than out_size is truncated rather than rejected — half a reason beats none, and the caller's buffer is a panel line, not a log.

Parameters
[in]respNUL-terminated response body.
[out]outNUL-terminated message on success; untouched otherwise.
[in]out_sizeCapacity of out.
Returns
true if the body parsed and carried a non-empty error message.

Definition at line 39 of file eth_json.cpp.

References ok().

Referenced by eth_rpc_send_raw_tx().

◆ eth_json_receipt_check()

eth_json_receipt_t eth_json_receipt_check ( const char * resp,
const eth_receipt_expect_t * want )

Classify a receipt AND check it is the payment that was asked for.

A status of 0x1 alone proves nothing: a node that never broadcast our transaction can answer with the receipt of any other successful one. So on top of eth_json_receipt_status this requires transactionHash to be the hash computed on the device, to to be the expected contract (or payee), and — for a token — a Transfer log from that contract to payee for exactly amount. A mined receipt failing any of those is ETH_JSON_RECEIPT_MISMATCH, never SUCCESS or REVERTED.

Parameters
[in]respNUL-terminated response body.
[in]wantWhat the receipt must show; NULL yields ERROR.
Returns
One of eth_json_receipt_t.

Definition at line 162 of file eth_json.cpp.

References addr_hex(), eth_receipt_expect_t::amount, ETH_JSON_RECEIPT_ERROR, ETH_JSON_RECEIPT_MISMATCH, ETH_JSON_RECEIPT_REVERTED, eth_json_receipt_status(), ETH_JSON_RECEIPT_SUCCESS, has_transfer(), eth_receipt_expect_t::payee, str_is(), eth_receipt_expect_t::to, eth_receipt_expect_t::tx_hash, and word_hex().

Referenced by eth_rpc_get_tx_receipt().

◆ eth_json_receipt_status()

eth_json_receipt_t eth_json_receipt_status ( const char * resp)

Classify an eth_getTransactionReceipt response body.

Parses the JSON and inspects the top-level result: null means the tx is still pending; an object's status field ("0x1" = success, anything else = reverted) gives the final verdict. Any other shape (invalid JSON, result neither null nor object, missing/non-string status) yields ETH_JSON_RECEIPT_ERROR.

Parameters
[in]respNUL-terminated response body.
Returns
One of eth_json_receipt_t.

Definition at line 69 of file eth_json.cpp.

References ETH_JSON_RECEIPT_ERROR, ETH_JSON_RECEIPT_PENDING, ETH_JSON_RECEIPT_REVERTED, and ETH_JSON_RECEIPT_SUCCESS.

Referenced by eth_json_receipt_check().

◆ eth_json_result_string()

bool eth_json_result_string ( const char * resp,
char * out,
size_t out_size )

Extract the top-level "result" string from a JSON-RPC response.

Uses a real JSON parser (cJSON) instead of strstr so that JSON-RPC error objects, HTTP error bodies, or look-alike substrings are rejected.

Parameters
[in]respNUL-terminated response body.
[out]outNUL-terminated "result" value on success; untouched on failure.
[in]out_sizeCapacity of out.
Returns
true on success; false if the body is not valid JSON, "result" is absent or not a string, or the value does not fit in out.

Definition at line 18 of file eth_json.cpp.

References ok().

Referenced by eth_rpc_get_balance(), eth_rpc_get_nonce(), eth_rpc_get_token_balance(), eth_rpc_get_token_decimals(), and eth_rpc_send_raw_tx().

◆ has_transfer()

bool has_transfer ( const cJSON * logs,
const char * token,
const char * payee_word,
const char * amount_word )
static

true if logs holds a Transfer(*, payee, amount) from token.

Definition at line 143 of file eth_json.cpp.

References str_is(), and TRANSFER_TOPIC.

Referenced by eth_json_receipt_check().

◆ lc()

char lc ( char c)
static

Definition at line 96 of file eth_json.cpp.

Referenced by str_is().

◆ str_is()

bool str_is ( const cJSON * j,
const char * want )
static

true if a JSON string equals want, ignoring hex case.

Definition at line 102 of file eth_json.cpp.

References lc().

Referenced by eth_json_receipt_check(), and has_transfer().

◆ word_hex()

void word_hex ( uint64_t v,
char * out )
static

"0x" + v as a 32-byte big-endian word.

Definition at line 129 of file eth_json.cpp.

Referenced by eth_json_receipt_check().

Variable Documentation

◆ TRANSFER_TOPIC

const char TRANSFER_TOPIC[]
static
Initial value:
=
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"

Definition at line 93 of file eth_json.cpp.

Referenced by has_transfer().