Skip to content

Commit

Permalink
Build BIP-44 path in caller allocated array
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkh-fb committed Dec 20, 2023
1 parent ebff055 commit c96f6aa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
6 changes: 4 additions & 2 deletions include/blockchain/mpc/hd_derive.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ const unsigned int EDDSA_PUBLIC_KEY_SIZE = 32;
const unsigned int PRIVATE_KEY_SIZE = 32;
const unsigned int CHAIN_CODE_SIZE_BYTES = 32;
const unsigned int BIP44_PATH_LENGTH = 5;

typedef unsigned char HDChaincode[CHAIN_CODE_SIZE_BYTES];
typedef unsigned char PubKey[COMPRESSED_PUBLIC_KEY_SIZE];
typedef unsigned char PrivKey[PRIVATE_KEY_SIZE];
typedef uint32_t Bip44Path[BIP44_PATH_LENGTH];

typedef enum
{
Expand All @@ -41,12 +43,12 @@ inline uint32_t bip32_hardened_index(uint32_t idx)
return (1U<<31) + idx;
}

// TODO: Refactor receive allocated int[BIP44_PATH_LENGTH] path
hd_derive_status build_bip44_path(uint32_t** path, uint32_t* path_len, uint32_t asset_num, uint32_t account, uint32_t change = 0, uint32_t addr_index = 0);
hd_derive_status derive_public_key_generic(const elliptic_curve256_algebra_ctx_t *ctx, PubKey derived_key, const PubKey pubkey, const HDChaincode chaincode, const uint32_t* path, const uint32_t path_len);
hd_derive_status derive_private_key_generic(const elliptic_curve256_algebra_ctx_t *ctx, PrivKey derived_privkey, const PubKey pubkey, const PrivKey privkey, const HDChaincode chaincode, const uint32_t* path, const uint32_t path_len);
hd_derive_status derive_private_and_public_keys(const elliptic_curve256_algebra_ctx_t *ctx, PrivKey derived_privkey, PubKey derived_pubkey, const PubKey pubkey, const PrivKey privkey, const HDChaincode chaincode, const uint32_t* path, const uint32_t path_len);

hd_derive_status build_bip44_path(Bip44Path path, uint32_t asset_num, uint32_t account, uint32_t change = 0, uint32_t addr_index = 0);

#ifdef __cplusplus
}
#endif //__cplusplus
Expand Down
23 changes: 8 additions & 15 deletions src/common/blockchain/mpc/hd_derive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static hd_derive_status BIP32Hash(hmac_sha_result output, const HDChaincode chai
if (1 != HMAC_Update(ctx, num, 4)) {
goto end_bip32_hash;
}

if (1 != HMAC_Final(ctx, output, &output_len)) {
goto end_bip32_hash;
}
Expand Down Expand Up @@ -142,7 +142,7 @@ hd_derive_status derive_private_key_generic(const elliptic_curve256_algebra_ctx_
hd_derive_status derive_private_and_public_keys(const elliptic_curve256_algebra_ctx_t *ctx, PrivKey derived_privkey, PubKey derived_pubkey, const PubKey pubkey, const PrivKey privkey, const HDChaincode chaincode,
const uint32_t* path, const uint32_t path_len) {
PubKey temp_pubkey;

if (!path || !path_len)
{
memcpy(derived_privkey, privkey, PRIVATE_KEY_SIZE);
Expand Down Expand Up @@ -170,18 +170,11 @@ hd_derive_status derive_private_and_public_keys(const elliptic_curve256_algebra_
return HD_DERIVE_SUCCESS;
}

hd_derive_status build_bip44_path(uint32_t** path, uint32_t* path_len, uint32_t asset_num, uint32_t account, uint32_t change, uint32_t addr_index) {
*path_len = 5;
*path = (uint32_t*)malloc(BIP44_PATH_LENGTH * sizeof(uint32_t));
if (NULL == *path){
return HD_DERIVE_ERROR_OUT_OF_MEMORY;
}

(*path)[0] = BIP44; //purpose
(*path)[1] = asset_num;
(*path)[2] = account;
(*path)[3] = change;
(*path)[4] = addr_index;

hd_derive_status build_bip44_path(Bip44Path path, uint32_t asset_num, uint32_t account, uint32_t change, uint32_t addr_index) {
path[0] = BIP44; //purpose
path[1] = asset_num;
path[2] = account;
path[3] = change;
path[4] = addr_index;
return HD_DERIVE_SUCCESS;
}

0 comments on commit c96f6aa

Please sign in to comment.