ABI functions

ABI/RLP functions slightly differ from other functions. For example, when the data needs to be encoded or decoded, only one function is used for both operations and the mode in which the ABI functions should work can be specified during the initialization process. Consider the following call and its representation in C function calls:

balanceOf(0x19CE609af17d094Bcf4143266D2F6dB35231E47c)
// ...
char *func = "balanceOf(address)";
char *addr = "0x19CE609af17d094Bcf4143266D2F6dB35231E47c";

eth_abi_call(&abi, &func, NULL); // balanceOf(
  eth_abi_address(&abi, &addr);  //   0x19CE609af17d094Bcf4143266D2F6dB35231E47c
eth_abi_call_end(&abi);          // )

If the call part is not needed, it can be omitted and it will still work.

Nested data:

If a function accepts a nested array (myfunc(uint8[][])), the encoding process may look like:

// ...
char *func = "myfunc(uint8[][])";
uint8_t a=0x83, b=0xac;

eth_abi_call(&abi, &func, NULL);   // myfunc(
  eth_abi_array(&abi, NULL);       //   [
    eth_abi_array(&abi, NULL);     //     [
      eth_abi_uint8(&abi, &a);     //       0x83
    eth_abi_array_end(&abi, NULL); //     ],
    eth_abi_array(&abi, NULL);     //     [
      eth_abi_uint8(&abi, &a);     //       0xac
    eth_abi_array_end(&abi, NULL); //     ]
  eth_abi_array_end(&abi, NULL);   //   ]
eth_abi_call_end(&abi);            // )

int eth_abi_init(struct eth_abi *abi, int m)

Initializes the given abi struct.

See also

eth_abi_free

Parameters
  • abi[in] Target abi struct that needs to be initialized.

  • m[in] Mode in which the abi functions should work (accepts ETH_ABI_ENCODE or ETH_ABI_DECODE)

Returns

1 on success, -1 otherwise.

int eth_abi_free(struct eth_abi *abi)

Releases internal memory allocated for the given abi struct.

Parameters

abi[in] Target abi

Returns

1 on success, -1 otherwise.

int eth_abi_bool(struct eth_abi *abi, uint8_t *b)

Encodes/decodes “boolean value” (1 or 0)

Parameters
  • abi[in] Target abi.

  • b[inout] Ponter to uint8_t to read/write the data from/to.

Returns

1 on success, -1 otherwise.

int eth_abi_int8(struct eth_abi *abi, int8_t *d)

Encodes/decodes signed 8 bit integer.

Parameters
  • abi[in] Target abi.

  • d[inout] Ponter to int8_t to read/write the data from/to.

Returns

1 on success, -1 otherwise.

int eth_abi_int16(struct eth_abi *abi, int16_t *d)

Encodes/decodes signed 16 bit integer.

Parameters
  • abi[in] Target abi.

  • d[inout] Ponter to int16_t to read/write the data from/to.

Returns

1 on success, -1 otherwise.

int eth_abi_int32(struct eth_abi *abi, int32_t *d)

Encodes/decodes signed 32 bit integer.

Parameters
  • abi[in] Target abi.

  • d[inout] Ponter to int32_t to read/write the data from/to.

Returns

1 on success, -1 otherwise.

int eth_abi_int64(struct eth_abi *abi, int64_t *d)

Encodes/decodes signed 64 bit integer.

Parameters
  • abi[in] Target abi.

  • d[inout] Ponter to int64_t to read/write the data from/to.

Returns

1 on success, -1 otherwise.

int eth_abi_uint8(struct eth_abi *abi, uint8_t *d)

Encodes/decodes unsigned 8 bit integer.

Parameters
  • abi[in] Target abi.

  • d[inout] Ponter to uint8_t to read/write the data from/to.

Returns

1 on success, -1 otherwise.

int eth_abi_uint16(struct eth_abi *abi, uint16_t *d)

Encodes/decodes unsigned 16 bit integer.

Parameters
  • abi[in] Target abi.

  • d[inout] Ponter to uint16_t to read/write the data from/to.

Returns

1 on success, -1 otherwise.

int eth_abi_uint32(struct eth_abi *abi, uint32_t *d)

Encodes/decodes unsigned 32 bit integer.

Parameters
  • abi[in] Target abi.

  • d[inout] Ponter to uint32_t to read/write the data from/to.

Returns

1 on success, -1 otherwise.

int eth_abi_uint64(struct eth_abi *abi, uint64_t *d)

Encodes/decodes unsigned 64 bit integer.

Parameters
  • abi[in] Target abi.

  • d[inout] Ponter to uint64_t to read/write the data from/to.

Returns

1 on success, -1 otherwise.

int eth_abi_mpint(struct eth_abi *abi, mpz_t mpz)

Encodes/decodes arbitrarily large integer.

Parameters
  • abi[in] Target abi.

  • mpz[inout] Initialized mpz_t to read/write the data from/to.

Returns

1 on success, -1 otherwise.

int eth_abi_address(struct eth_abi *abi, char **addr)

Encodes/decodes address.

Parameters
  • abi[in] Target abi.

  • addr[inout] Ponter to address to read/write the data from/to.

Returns

1 on success, -1 otherwise.

int eth_abi_bytes8(struct eth_abi *abi, uint8_t *bytes)

Encodes/decodes 8 byte array.

Parameters
  • abi[in] Target abi.

  • bytes[inout] Ponter to uint8_t to read/write the data from/to.

Returns

1 on success, -1 otherwise.

int eth_abi_bytes16(struct eth_abi *abi, uint8_t *bytes)

Encodes/decodes 16 byte array.

Parameters
  • abi[in] Target abi.

  • bytes[inout] Ponter to uint8_t to read/write the data from/to.

Returns

1 on success, -1 otherwise.

int eth_abi_bytes32(struct eth_abi *abi, uint8_t *bytes)

Encodes/decodes 32 byte array.

Parameters
  • abi[in] Target abi.

  • bytes[inout] Ponter to uint8_t to read/write the data from/to.

Returns

1 on success, -1 otherwise.

int eth_abi_bytes(struct eth_abi *abi, uint8_t **bytes, size_t *len)

Encodes/decodes variable length bytes.

Parameters
  • abi[in] Target abi.

  • bytes[inout] Ponter to uint8_t array to read/write the data from/to.

  • len[inout] Length of encoded/decodes bytes.

Returns

1 on success, -1 otherwise.

int eth_abi_to_hex(struct eth_abi *abi, char **hex, size_t *len)

Converts ABI to hex string.

Parameters
  • abi[in] Target abi.

  • hex[out] Pointer to string where the hexadecimal value will be placed.

  • len[out] Pointer to size_to where the length of hexadecimal value will be placed.

Returns

1 on success, -1 otherwise.

int eth_abi_from_hex(struct eth_abi *abi, char *hex, int len)

Loads ABI from hex string.

Parameters
  • abi[in] Target abi.

  • hex[out] Hexadecimal string.

  • len[out] Length of hex

Returns

1 on success, -1 otherwise.

int eth_abi_call(struct eth_abi *abi, char **fn, int *len)

Denotes the start of a call.

// ...
char *func = "balanceOf(address)";
eth_abi_call(&abi, &func, NULL);

See also

eth_abi_call_end

Note

len can be NULL (NULL means the fn is NULL terminated on encode and the length is not needed on decode)

Parameters
  • abi[in] Target abi.

  • fn[inout] Pointer to string to read/write the data from/to.

  • len[inout] Pointer to int to read/write the length from/to.

Returns

1 on success, -1 otherwise.

int eth_abi_call_end(struct eth_abi *abi)

Denotes the end of a call.

Parameters

abi[in] Target abi.

int eth_abi_array(struct eth_abi *abi, uint64_t *len)

Denotes the start of an array.

// ...
eth_abi_call(&abi, NULL);
  eth_abi_uint8(&abi, &myint);
eth_abi_call_end(&abi, NULL);

See also

eth_abi_array_end

Note

len is ignored on encode.

Parameters
  • abi[in] Target abi.

  • len[out] Length of the array.

int eth_abi_array_end(struct eth_abi *abi)

Denotes the end of an array.

Parameters

abi[in] Target abi.