Skip to content

Commit

Permalink
list of changes
Browse files Browse the repository at this point in the history
- renaming hdkey formatting functions: better more expressive names
- refactor return values of such functions to copy snprintf behaviour
- BIP32_SERIALIZED_LEN rather than magic number 78
  • Loading branch information
gministeri committed Sep 20, 2023
1 parent 316aa37 commit 0760f2c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 77 deletions.
13 changes: 8 additions & 5 deletions include/urc/crypto_hdkey.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,11 @@ typedef struct {
} crypto_hdkey;

urc_error parse_hdkey(size_t size, const uint8_t *buffer, crypto_hdkey *out);
bool hdkey2bip32(const crypto_hdkey *hdkey, uint8_t out[78]);
// -1 --> out of memory
// -2 --> internal error / invalid hdkey
int hdkey2keypathstr(const crypto_hdkey *hdkey, size_t size, char *out);
int hdkeytrail(const crypto_hdkey *hdkey, size_t size, char *out);
#define BIP32_SERIALIZED_LEN 78
bool bip32_serialize(const crypto_hdkey *hdkey, uint8_t out[BIP32_SERIALIZED_LEN]);
// return values of following functions follow the snprintf convention:
// any value >= of size means that the output has been truncated
// any value >= 0 indicates the number of characters written to ``out`` buffer (excluding the null-terminator)
// otherwise, any value <0 means an error occurred
int format_keyorigin(const crypto_hdkey *hdkey, size_t size, char *out);
int format_keyderivationpath(const crypto_hdkey *hdkey, size_t size, char *out);
30 changes: 14 additions & 16 deletions src/hdkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ urc_error internal_parse_pair_component(CborValue *iter, child_pair_component *o
return result;
}

bool hdkey2bip32(const crypto_hdkey *hdkey, uint8_t out[78]) {
bool bip32_serialize(const crypto_hdkey *hdkey, uint8_t out[BIP32_SERIALIZED_LEN]) {
size_t cursor = 0;

uint32_t *version = (uint32_t *)&out[cursor]; // 0 - 3
Expand Down Expand Up @@ -629,12 +629,12 @@ bool hdkey2bip32(const crypto_hdkey *hdkey, uint8_t out[78]) {
return true;
}

int hdkey2keypathstr(const crypto_hdkey *hdkey, size_t size, char *out) {
int format_keyorigin(const crypto_hdkey *hdkey, size_t size, char *out) {
switch (hdkey->type) {
case hdkey_type_master:
return 0;
case hdkey_type_na:
return -2;
return -1;
default:
break;
}
Expand All @@ -645,8 +645,9 @@ int hdkey2keypathstr(const crypto_hdkey *hdkey, size_t size, char *out) {
fpr = hdkey->key.derived.parent_fingerprint;
}
len += snprintf(out, size, "[%x", fpr);
// either an error or an out-of-space
if (len < 0 || (size_t)len >= size) {
return -1;
return len;
}
for (size_t idx = 0; idx < hdkey->key.derived.origin.components_count; idx++) {
const path_component *comp = &hdkey->key.derived.origin.components[idx];
Expand All @@ -659,25 +660,24 @@ int hdkey2keypathstr(const crypto_hdkey *hdkey, size_t size, char *out) {
}
break;
default:
return -2;
return -1;
}
// either an error or an out-of-space
if (len < 0 || (size_t)len >= size) {
return -1;
return len;
}
}
len += snprintf(&out[len], size - len, "]");
if (len < 0 || (size_t)len >= size) {
return -1;
}
// it includes error or out-of-space cases
return len;
}

int hdkeytrail(const crypto_hdkey *hdkey, size_t size, char *out) {
int format_keyderivationpath(const crypto_hdkey *hdkey, size_t size, char *out) {
switch (hdkey->type) {
case hdkey_type_master:
return 0;
case hdkey_type_na:
return -2;
return -1;
default:
break;
}
Expand All @@ -697,15 +697,13 @@ int hdkeytrail(const crypto_hdkey *hdkey, size_t size, char *out) {
len += snprintf(&out[len], size - len, "/*");
break;
default:
return -2;
return -1;
}
// either an error or an out-of-space
if (len < 0 || (size_t)len >= size) {
return -1;
return len;
}
}

if (len < 0 || (size_t)len >= size) {
return -1;
}
return len;
}
Loading

0 comments on commit 0760f2c

Please sign in to comment.