Skip to content

Commit

Permalink
add <>_format functions
Browse files Browse the repository at this point in the history
- urc_crypto_eckey_format
- urc_crypto_hdkey_format
- urc_crypto_output_format
- urc_crypto_account_format

- refactor tests to test new functions as well
  • Loading branch information
gministeri committed Jul 23, 2024
1 parent fcc1b41 commit cb2db06
Show file tree
Hide file tree
Showing 22 changed files with 804 additions and 960 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ option(URC_FETCH_DEPS "tell cmake to go fetch dependencies itself" OFF)
option(URC_ENABLE_TESTS "enable tests" OFF)
option(URC_ENABLE_FUZZ_TESTS "enable fuzzy tests" OFF)
option(URC_ENABLE_COVERAGE "enable code coverage" OFF)
option(URC_ENABLE_VALGRIND "enable valgrind tests" OFF)

### dependencies
include(cmake/dependencies.cmake)
Expand Down
4 changes: 4 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
"type": "BOOL",
"value": "ON"
},
"URC_ENABLE_VALGRIND": {
"type": "BOOL",
"value": "ON"
},
"CMAKE_BUILD_TYPE": {
"type": "STRING",
"value": "Debug"
Expand Down
1 change: 1 addition & 0 deletions include/urc/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extern "C" {

void urc_free(void *ptr);
void urc_string_free(char *str);
void urc_string_array_free(char *str_array[]);

#ifdef __cplusplus
}
Expand Down
4 changes: 4 additions & 0 deletions include/urc/crypto_account.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ int urc_crypto_account_deserialize(const uint8_t *cbor_buffer, size_t cbor_len,
// parse an account in jade format, descriptors are not introduced by tag 308
int urc_jade_account_deserialize(const uint8_t *cbor_buffer, size_t len, crypto_account *out);

// *out[] must be freed using urc_string_array_free()
// last element of *out[] is NULL
int urc_crypto_account_format(const crypto_account *account, urc_crypto_output_format_mode mode, char **out[]);

#ifdef __cplusplus
}
#endif
3 changes: 3 additions & 0 deletions include/urc/crypto_eckey.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ typedef struct {

int urc_crypto_eckey_deserialize(const uint8_t *cbor_buffer, size_t cbor_len, crypto_eckey *out);

// ``out`` must be freed by caller using urc_string_free function
int urc_crypto_eckey_format(const crypto_eckey *eckey, char **out);

#ifdef __cplusplus
}
#endif
6 changes: 1 addition & 5 deletions include/urc/crypto_hdkey.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,9 @@ typedef struct {
} crypto_hdkey;

int urc_crypto_hdkey_deserialize(const uint8_t *cbor_buffer, size_t cbor_len, crypto_hdkey *out);
#define BIP32_SERIALIZED_LEN 78
bool bip32_serialize(const crypto_hdkey *hdkey, uint8_t out[BIP32_SERIALIZED_LEN]);

// ``out`` must be freed by caller using urc_string_free function
int format_keyorigin(const crypto_hdkey *hdkey, char **out);
int format_keyderivationpath(const crypto_hdkey *hdkey, char **out);
int urc_bip32_tobase58(const crypto_hdkey *hdkey, char **out);
int urc_crypto_hdkey_format(const crypto_hdkey *hdkey, char **out);

#ifdef __cplusplus
}
Expand Down
12 changes: 10 additions & 2 deletions include/urc/crypto_output.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ typedef struct {
} keytype;
} output_keyexp;


#define URC_RAWSCRIPT_LEN 32
typedef struct {
union {
output_keyexp key; // p2pkh
output_keyexp key; // p2pkh
uint8_t raw[URC_RAWSCRIPT_LEN];
} output;
enum {
Expand All @@ -51,6 +50,15 @@ typedef struct {

int urc_crypto_output_deserialize(const uint8_t *cbor_buffer, size_t cbor_len, crypto_output *out);

typedef enum {
// output descriptor represented as is
urc_crypto_output_format_mode_default,
// if derivation path is empty, and key origin counts to 2, an extra ``/0/*`` is added as derivation path
urc_crypto_output_format_mode_BIP44_compatible,
} urc_crypto_output_format_mode;
// ``out`` must be freed by caller using urc_string_free function
int urc_crypto_output_format(const crypto_output *output, urc_crypto_output_format_mode mode, char **out);

#ifdef __cplusplus
}
#endif
28 changes: 26 additions & 2 deletions src/account.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

#include <stdlib.h>
#include "wally_core.h"

#include "urc/core.h"
#include "urc/crypto_account.h"
#include "urc/error.h"
#include "urc/tags.h"

#include "internals.h"
Expand Down Expand Up @@ -97,3 +97,27 @@ int urc_crypto_account_deserialize_impl(CborValue *iter, crypto_account *out)
}
return result;
}

int urc_crypto_account_format(const crypto_account *account, urc_crypto_output_format_mode mode, char **out[])
{
if (!account || !out) {
return URC_EINVALIDARG;
}

size_t array_size = sizeof(char *) * (account->descriptors_count + 1);
*out = wally_malloc(array_size);
if (!*out) {
return URC_ENOMEM;
}
(*out)[account->descriptors_count] = NULL;

for (size_t idx = 0; idx < account->descriptors_count; idx++) {
int result = urc_crypto_output_format(&account->descriptors[idx], mode, &(*out)[idx]);
if (result != URC_OK) {
urc_string_array_free(*out);
*out = NULL;
return result;
}
}
return URC_OK;
}
11 changes: 11 additions & 0 deletions src/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,14 @@
void urc_free(void *ptr) { wally_free(ptr); }

void urc_string_free(char *str) { wally_free_string(str); }


void urc_string_array_free(char *str_array[]) {
size_t idx = 0;
while (str_array[idx]) {
urc_string_free(str_array[idx]);
str_array[idx] = NULL;
idx++;
}
wally_free(str_array);
}
28 changes: 28 additions & 0 deletions src/eckey.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "internals.h"
#include "macros.h"
#include "utils.h"
#include <wally_core.h>

int urc_crypto_eckey_deserialize(const uint8_t *buffer, size_t len, crypto_eckey *out)
{
Expand Down Expand Up @@ -99,3 +100,30 @@ int urc_crypto_eckey_deserialize_impl(CborValue *iter, crypto_eckey *out)
exit:
return result;
}

int urc_crypto_eckey_format(const crypto_eckey *eckey, char **out)
{
if (!eckey || !out) {
return URC_EINVALIDARG;
}

*out = NULL;
int wallyerr = WALLY_OK;
switch (eckey->type) {
case eckey_type_private:
wallyerr = wally_hex_from_bytes(eckey->key.prvate, CRYPTO_ECKEY_PRIVATE_SIZE, out);
break;
case eckey_type_public_compressed:
wallyerr = wally_hex_from_bytes(eckey->key.public_compressed, CRYPTO_ECKEY_PUBLIC_COMPRESSED_SIZE, out);
break;
case eckey_type_public_uncompressed:
wallyerr = wally_hex_from_bytes(eckey->key.public_uncompressed, CRYPTO_ECKEY_PUBLIC_UNCOMPRESSED_SIZE, out);
break;
default:
return URC_EINVALIDARG;
}
if (wallyerr != WALLY_OK) {
return URC_EWALLYINTERNALERROR;
}
return URC_OK;
}
Loading

0 comments on commit cb2db06

Please sign in to comment.