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

RLP encoder implementation for EIP-1559 (type 2) transactions. More...

#include "eth_rlp.h"
#include "CW_Utils.h"
Include dependency graph for eth_rlp.cpp:

Go to the source code of this file.

Macros

#define ITEMS_BUF_MAX   320U
#define ITEMS_FIXED_MAX   160U
#define CALLDATA_MAX   (ITEMS_BUF_MAX - ITEMS_FIXED_MAX)

Functions

static uint8_t be_minimal (uint64_t value, uint8_t out[8])
 Write value as a minimal big-endian byte sequence.
static size_t rlp_bytes (const uint8_t *data, size_t data_len, uint8_t *out, size_t out_cap)
 RLP-encode a byte string.
static size_t rlp_uint64 (uint64_t value, uint8_t *out, size_t out_cap)
 RLP-encode a non-negative integer.
static size_t rlp_int256 (const uint8_t data[32], uint8_t *out, size_t out_cap)
 RLP-encode a 32-byte big-endian integer (e.g. ECDSA r or s).
static size_t rlp_list_header (size_t content_len, uint8_t *out, size_t out_cap)
 Write the RLP list header for a list of content_len bytes.
static size_t encode_common_fields (const eth_tx_t *tx, uint8_t *items, size_t items_cap)
 Encode the nine common EIP-1559 fields into the items buffer.
size_t eth_rlp_encode_unsigned (const eth_tx_t *tx, uint8_t *out, size_t out_max)
 Encode an unsigned EIP-1559 transaction: 0x02 || RLP([chainId, nonce, ...]).
size_t eth_rlp_encode_signed (const eth_tx_t *tx, uint8_t v, const uint8_t r[32], const uint8_t s[32], uint8_t *out, size_t out_max)
 Encode a signed EIP-1559 transaction: 0x02 || RLP([..., v, r, s]).

Detailed Description

RLP encoder implementation for EIP-1559 (type 2) transactions.

Definition in file eth_rlp.cpp.

Macro Definition Documentation

◆ CALLDATA_MAX

#define CALLDATA_MAX   (ITEMS_BUF_MAX - ITEMS_FIXED_MAX)

Definition at line 33 of file eth_rlp.cpp.

Referenced by encode_common_fields().

◆ ITEMS_BUF_MAX

#define ITEMS_BUF_MAX   320U

Definition at line 23 of file eth_rlp.cpp.

Referenced by eth_rlp_encode_signed(), and eth_rlp_encode_unsigned().

◆ ITEMS_FIXED_MAX

#define ITEMS_FIXED_MAX   160U

Definition at line 32 of file eth_rlp.cpp.

Function Documentation

◆ be_minimal()

uint8_t be_minimal ( uint64_t value,
uint8_t out[8] )
static

Write value as a minimal big-endian byte sequence.

Value 0 produces one 0x00 byte; the caller is responsible for treating the empty-integer case (value==0 -> RLP 0x80) before calling this function.

Parameters
[in]valueInteger to encode.
[out]outOutput buffer (exactly 8 bytes; up to 8 used).
Returns
Number of bytes written (1-8).

Definition at line 55 of file eth_rlp.cpp.

Referenced by rlp_bytes(), rlp_list_header(), and rlp_uint64().

◆ encode_common_fields()

size_t encode_common_fields ( const eth_tx_t * tx,
uint8_t * items,
size_t items_cap )
static

Encode the nine common EIP-1559 fields into the items buffer.

The calldata length is bounds-checked up front against CALLDATA_MAX so the fixed-size items buffer can never overflow.

Parameters
[in]txTransaction to encode.
[out]itemsScratch buffer.
[in]items_capCapacity of items (>= ITEMS_BUF_MAX).
Returns
Bytes written, or 0 if tx->calldata_len exceeds CALLDATA_MAX or the buffer is too small.

Definition at line 215 of file eth_rlp.cpp.

References eth_tx_t::calldata, eth_tx_t::calldata_len, CALLDATA_MAX, eth_tx_t::chain_id, eth_tx_t::eth_value, eth_tx_t::gas_limit, eth_tx_t::max_fee, eth_tx_t::max_priority_fee, eth_tx_t::nonce, rlp_bytes(), rlp_uint64(), and eth_tx_t::to.

Referenced by eth_rlp_encode_signed(), and eth_rlp_encode_unsigned().

◆ eth_rlp_encode_signed()

size_t eth_rlp_encode_signed ( const eth_tx_t * tx,
uint8_t v,
const uint8_t r[32],
const uint8_t s[32],
uint8_t * out,
size_t out_max )

Encode a signed EIP-1559 transaction: 0x02 || RLP([..., v, r, s]).

Parameters
[in]txTransaction parameters.
[in]vSignature parity bit; must be 0 or 1.
[in]r32-byte big-endian ECDSA r component.
[in]s32-byte big-endian ECDSA s component.
[out]outOutput buffer for the encoded bytes.
[in]out_maxCapacity of out.
Returns
Total bytes written, or 0 if the output would overflow out_max or tx->calldata_len exceeds the internal scratch bound.

Definition at line 268 of file eth_rlp.cpp.

References encode_common_fields(), ITEMS_BUF_MAX, rlp_int256(), rlp_list_header(), and rlp_uint64().

Referenced by sign_and_broadcast().

◆ eth_rlp_encode_unsigned()

size_t eth_rlp_encode_unsigned ( const eth_tx_t * tx,
uint8_t * out,
size_t out_max )

Encode an unsigned EIP-1559 transaction: 0x02 || RLP([chainId, nonce, ...]).

Parameters
[in]txTransaction parameters.
[out]outOutput buffer for the encoded bytes.
[in]out_maxCapacity of out.
Returns
Total bytes written, or 0 if the output would overflow out_max or tx->calldata_len exceeds the internal scratch bound.

Definition at line 244 of file eth_rlp.cpp.

References encode_common_fields(), ITEMS_BUF_MAX, and rlp_list_header().

Referenced by sign_and_broadcast().

◆ rlp_bytes()

size_t rlp_bytes ( const uint8_t * data,
size_t data_len,
uint8_t * out,
size_t out_cap )
static

RLP-encode a byte string.

Parameters
[in]dataInput bytes.
[in]data_lenNumber of input bytes.
[out]outOutput buffer.
[in]out_capCapacity of out.
Returns
Bytes written, or 0 if out_cap is too small.

Definition at line 89 of file eth_rlp.cpp.

References be_minimal().

Referenced by encode_common_fields(), rlp_int256(), and rlp_uint64().

◆ rlp_int256()

size_t rlp_int256 ( const uint8_t data[32],
uint8_t * out,
size_t out_cap )
static

RLP-encode a 32-byte big-endian integer (e.g. ECDSA r or s).

Leading zero bytes are stripped so the encoding is minimal.

Parameters
[in]data32-byte big-endian integer.
[out]outOutput buffer.
[in]out_capCapacity of out.
Returns
Bytes written, or 0 if out_cap is too small.

Definition at line 159 of file eth_rlp.cpp.

References rlp_bytes().

Referenced by eth_rlp_encode_signed().

◆ rlp_list_header()

size_t rlp_list_header ( size_t content_len,
uint8_t * out,
size_t out_cap )
static

Write the RLP list header for a list of content_len bytes.

Parameters
[in]content_lenLength of the list content (not written here).
[out]outOutput buffer.
[in]out_capCapacity of out.
Returns
Bytes written (header only), or 0 if out_cap is too small.

Definition at line 182 of file eth_rlp.cpp.

References be_minimal().

Referenced by eth_rlp_encode_signed(), and eth_rlp_encode_unsigned().

◆ rlp_uint64()

size_t rlp_uint64 ( uint64_t value,
uint8_t * out,
size_t out_cap )
static

RLP-encode a non-negative integer.

0 is encoded as the empty byte string (0x80).

Parameters
[in]valueInteger to encode.
[out]outOutput buffer.
[in]out_capCapacity of out.
Returns
Bytes written, or 0 if out_cap is too small.

Definition at line 137 of file eth_rlp.cpp.

References be_minimal(), and rlp_bytes().

Referenced by encode_common_fields(), and eth_rlp_encode_signed().