From c96f6aaf1779619b6c767a7e15a15948f6b84e97 Mon Sep 17 00:00:00 2001 From: Igor Khanin Date: Wed, 20 Dec 2023 17:58:49 +0200 Subject: [PATCH] Build BIP-44 path in caller allocated array --- include/blockchain/mpc/hd_derive.h | 6 ++++-- src/common/blockchain/mpc/hd_derive.cpp | 23 ++++++++--------------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/include/blockchain/mpc/hd_derive.h b/include/blockchain/mpc/hd_derive.h index 1add6ac..778aec8 100644 --- a/include/blockchain/mpc/hd_derive.h +++ b/include/blockchain/mpc/hd_derive.h @@ -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 { @@ -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 diff --git a/src/common/blockchain/mpc/hd_derive.cpp b/src/common/blockchain/mpc/hd_derive.cpp index e8b3365..1430396 100644 --- a/src/common/blockchain/mpc/hd_derive.cpp +++ b/src/common/blockchain/mpc/hd_derive.cpp @@ -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; } @@ -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); @@ -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; }