Listing and exporting keys using C API

Hello!

My actual goal is to write a safe rust wrapper around the C API, but I am having issues with the C API itself. Currently I am unable to list or export keys and all I get is random data. I know the python examples are working, so everything is wired up correctly.

This here is a minimal repro for the problem, compilable via gcc main.c -lzk_app_utils:

#include <stdio.h>
#include <zymkey/zk_app_utils.h>

const int MAX_NUM_KEYS = 32;

int list_keys(zkCTX ctx) {
    int max_num_keys = MAX_NUM_KEYS;
    int alloc_key_list[MAX_NUM_KEYS];
    int alloc_key_list_sz = 0;
    if (zkGetAllocSlotsList(ctx, false, &max_num_keys, (int**)&alloc_key_list, &alloc_key_list_sz) < 0) {
        printf("zk_get_alloc_slots_list failed\n");
        return -1;
    }
    printf("alloc_key_list_sz: %d\n", alloc_key_list_sz);
    for (int i = 0; i < alloc_key_list_sz; i++) {
        printf("alloc_key_list[%d]: %d\n", i, alloc_key_list[i]);
    }
}

int export_pubkey(zkCTX ctx, int slot) {
    uint8_t pk[64];
    int pk_sz = 64;
    if (zkExportPubKey(ctx, (uint8_t**)&pk, &pk_sz, slot, false) < 0) {
        printf("zk_export_pubkey failed\n");
        return -1;
    }
    printf("pubkey:\n");
    for (int i = 0; i < pk_sz; i++) {
        printf("%02x", pk[i]);
    }
    printf("\n");
}

int main(int argc, char** argv) {
    zkCTX ctx;
    if (zkOpen(&ctx) < 0) {
        printf("zk_open failed\n");
        return -1;
    }
    
    list_keys(ctx);
    export_pubkey(ctx, 0);
    
    if( zkClose(ctx) < 0) {
        printf("zk_close failed\n");
        return -1;
    }
}

Does anybody have a working usage example of those APIs or knows whats wrong about the code above?

Thanks in advance!

Tino

I found out whats wrong: The APIs are expecting you to really just give a reference to a pointer (which can be NULL) and then the library will allocate memory for the results and put a pointer to that memory in the reference the user passed in. The application layer is then responsible for freeing that memory.