cryptnox-sdk-arduino 1.0.0
Arduino library for Cryptnox Hardware Wallet
Loading...
Searching...
No Matches
util.cpp File Reference
#include <Arduino.h>
#include "util.h"
Include dependency graph for util.cpp:

Go to the source code of this file.

Functions

int fromHex (char c)
 Convert a hexadecimal character to a byte value.
bool hexToBytes (const char *hex, uint8_t *out, size_t len)
 Convert a hex string to a byte array.
uint32_t RlpEncodeWholeHeader (uint8_t *header_output, size_t header_cap, uint32_t total_len)
 Encodes the RLP list header for a sequence of items.
uint32_t RlpEncodeItem (uint8_t *output, size_t output_cap, const uint8_t *input, uint32_t input_len)
 Encodes a single RLP item.
uint32_t ConvertNumberToUintArray (uint8_t *str, uint64_t val)
 Converts an unsigned integer into a big-endian byte array.
size_t trimLeadingZeros (uint8_t *out, size_t out_cap, const uint8_t *in, size_t in_len)
 Trims leading zeros from a byte array.

Function Documentation

◆ ConvertNumberToUintArray()

uint32_t ConvertNumberToUintArray ( uint8_t * str,
uint64_t val )

Converts an unsigned integer into a big-endian byte array.

Converts an unsigned integer to a big-endian byte array.

NEW-5: widened to uint64_t. Previously truncated Tx2 uint64 fields (nonce/fees/gasLimit/value) at 4 bytes, producing wrong-amount tx.

Parameters
[out]strOutput buffer (at least 8 bytes).
[in]valUnsigned 64-bit integer to convert.
Returns
Number of bytes written to the buffer (1..8).
Examples
UsdcSigning.ino.

Definition at line 190 of file util.cpp.

Referenced by rlpEncodeTxBody().

◆ fromHex()

int fromHex ( char c)

Convert a hexadecimal character to a byte value.

M-07: returns -1 on any non-hex character so callers can distinguish a true '0' from a parse error.

Examples
UsdcSigning.ino.

Definition at line 15 of file util.cpp.

Referenced by fetchNonce(), and hexToBytes().

◆ hexToBytes()

bool hexToBytes ( const char * hex,
uint8_t * out,
size_t len )

Convert a hex string to a byte array.

H-03: validate input length before any read. Without this guard a truncated or malicious RPC response shorter than 2*len would cause out-of-bounds reads past the NUL terminator.

Returns
true on success, false on NULL input or insufficient length.
Examples
UsdcSigning.ino.

Definition at line 32 of file util.cpp.

References fromHex().

Referenced by encodeERC20Transfer(), and rlpEncodeTxBody().

◆ RlpEncodeItem()

uint32_t RlpEncodeItem ( uint8_t * output,
size_t output_cap,
const uint8_t * input,
uint32_t input_len )

Encodes a single RLP item.

Encodes a single item in RLP format.

Parameters
[out]outputBuffer where the encoded RLP item will be written.
[in]inputInput data to encode.
[in]input_lenLength of input data.
Returns
Number of bytes written to output.
Examples
UsdcSigning.ino.

Definition at line 123 of file util.cpp.

Referenced by rlpEncodeSignedTx().

◆ RlpEncodeWholeHeader()

uint32_t RlpEncodeWholeHeader ( uint8_t * header_output,
size_t header_cap,
uint32_t total_len )

Encodes the RLP list header for a sequence of items.

Encodes the total length into an RLP list header.

This function generates the RLP "whole header" for a list payload of length total_len, according to Ethereum RLP specification.

RLP encoding rules:

  • For a list with total payload < 55 bytes: 0xC0 + total_len (single-byte header)
  • For a list with payload >= 55 bytes: 0xF7 + length_of_length_field, followed by the big-endian encoded total_len

Example: total_len = 10 → header = C0 + 0x0A → C0 0A total_len = 100 → total_len = 0x64 (1 byte) → F8 64 total_len = 1024 → total_len = 0x0400 (2 bytes) → F9 04 00

This header is intended to be placed immediately before the concatenated items in an RLP list. For EIP-1559 typed transactions, it must appear after the type byte (0x02).

Parameters
[out]header_outputPointer where the encoded header will be written.
[in]total_lenLength in bytes of all list items concatenated.
Returns
Number of bytes written to header_output.
Examples
UsdcSigning.ino.

Definition at line 76 of file util.cpp.

Referenced by rlpFinalize().

◆ trimLeadingZeros()

size_t trimLeadingZeros ( uint8_t * out,
size_t out_cap,
const uint8_t * in,
size_t in_len )

Trims leading zeros from a byte array.

Removes leading zeros from a byte array.

Parameters
[out]outOutput buffer.
[in]inInput buffer.
[in]in_lenLength of input buffer.
Returns
Number of bytes written to the output buffer.
Examples
UsdcSigning.ino.

Definition at line 220 of file util.cpp.

Referenced by rlpEncodeSignedTx().