cryptnox-pos
1.0.0
Standalone USDC payment terminal firmware (ESP32 + Cryptnox smart card)
Toggle main menu visibility
Loading...
Searching...
No Matches
eth_json.h
Go to the documentation of this file.
1
/*
2
* SPDX-License-Identifier: LGPL-3.0-or-later
3
* Copyright (c) 2026 Cryptnox SA
4
*/
5
13
14
#ifndef ETH_JSON_H
15
#define ETH_JSON_H
16
17
#include <stdbool.h>
18
#include <stddef.h>
19
#include <stdint.h>
20
21
#ifdef __cplusplus
22
extern
"C"
{
23
#endif
24
26
typedef
enum
{
27
ETH_JSON_RECEIPT_ERROR
= 0,
30
ETH_JSON_RECEIPT_PENDING
,
31
ETH_JSON_RECEIPT_SUCCESS
,
32
ETH_JSON_RECEIPT_REVERTED
,
33
ETH_JSON_RECEIPT_MISMATCH
,
36
}
eth_json_receipt_t
;
37
40
typedef
struct
{
41
const
char
*
tx_hash
;
43
const
uint8_t *
to
;
45
const
uint8_t *
payee
;
46
uint64_t
amount
;
47
}
eth_receipt_expect_t
;
48
62
bool
eth_json_result_string
(
const
char
*resp,
char
*out,
size_t
out_size);
63
81
bool
eth_json_error_message
(
const
char
*resp,
char
*out,
size_t
out_size);
82
95
eth_json_receipt_t
eth_json_receipt_status
(
const
char
*resp);
96
112
eth_json_receipt_t
eth_json_receipt_check
(
const
char
*resp,
113
const
eth_receipt_expect_t
*want);
114
135
static
inline
bool
eth_json_hex_quantity
(
const
char
*hex, uint64_t *out)
136
{
137
if
((hex == NULL) || (out == NULL)) {
return
false
; }
138
if
((hex[0] !=
'0'
) || ((hex[1] !=
'x'
) && (hex[1] !=
'X'
))) {
return
false
; }
139
140
const
char
*p = &hex[2];
141
if
(*p ==
'\0'
) {
return
false
; }
/* "0x" on its own is not a quantity */
142
143
/* Every digit is checked, leading zeros included: validating only the
144
* significant ones would read "0x00zz" as zero rather than as garbage. */
145
size_t
len = 0U;
146
while
(p[len] !=
'\0'
) {
147
const
char
c = p[len];
148
const
bool
is_hex = ((c >=
'0'
) && (c <=
'9'
)) ||
149
((c >=
'a'
) && (c <=
'f'
)) ||
150
((c >=
'A'
) && (c <=
'F'
));
151
if
(!is_hex) {
return
false
; }
152
len++;
153
}
154
155
/* Nodes pad a uint256 out to 64 characters; strip down to the significant
156
* digits so the width test below is about the value, not the encoding.
157
* One digit always survives, so "0x000" stays parseable as zero. */
158
while
((*p ==
'0'
) && (p[1] !=
'\0'
)) { p++; len--; }
159
160
if
(len > 16U) { *out = UINT64_MAX;
return
true
; }
161
162
uint64_t v = 0U;
163
for
(
size_t
i = 0U; i < len; i++) {
164
const
char
c = p[i];
165
uint8_t n;
166
if
((c >=
'0'
) && (c <=
'9'
)) { n = (uint8_t)(c -
'0'
); }
167
else
if
((c >=
'a'
) && (c <=
'f'
)) { n = (uint8_t)((c -
'a'
) + 10); }
168
else
{ n = (uint8_t)((c -
'A'
) + 10); }
169
v = (v << 4) | (uint64_t)n;
170
}
171
*out = v;
172
return
true
;
173
}
174
175
#ifdef __cplusplus
176
}
177
#endif
178
179
#endif
/* ETH_JSON_H */
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.
Definition
eth_json.cpp:39
eth_json_hex_quantity
static bool eth_json_hex_quantity(const char *hex, uint64_t *out)
Parse a JSON-RPC QUANTITY ("0x0", "0x1a", 64 hex chars) into a uint64, saturating instead of wrapping...
Definition
eth_json.h:135
eth_json_receipt_t
eth_json_receipt_t
Verdict for an eth_getTransactionReceipt response body.
Definition
eth_json.h:26
ETH_JSON_RECEIPT_REVERTED
@ ETH_JSON_RECEIPT_REVERTED
Definition
eth_json.h:32
ETH_JSON_RECEIPT_PENDING
@ ETH_JSON_RECEIPT_PENDING
Definition
eth_json.h:30
ETH_JSON_RECEIPT_SUCCESS
@ ETH_JSON_RECEIPT_SUCCESS
Definition
eth_json.h:31
ETH_JSON_RECEIPT_ERROR
@ ETH_JSON_RECEIPT_ERROR
Definition
eth_json.h:27
ETH_JSON_RECEIPT_MISMATCH
@ ETH_JSON_RECEIPT_MISMATCH
Definition
eth_json.h:33
eth_json_receipt_status
eth_json_receipt_t eth_json_receipt_status(const char *resp)
Classify an eth_getTransactionReceipt response body.
Definition
eth_json.cpp:69
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.
Definition
eth_json.cpp:18
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.
Definition
eth_json.cpp:162
eth_receipt_expect_t
What a receipt must show for the payment to count — see eth_json_receipt_check.
Definition
eth_json.h:40
eth_receipt_expect_t::amount
uint64_t amount
Definition
eth_json.h:46
eth_receipt_expect_t::to
const uint8_t * to
Definition
eth_json.h:43
eth_receipt_expect_t::payee
const uint8_t * payee
Definition
eth_json.h:45
eth_receipt_expect_t::tx_hash
const char * tx_hash
Definition
eth_json.h:41
main
eth_json.h
Generated by
1.17.0