Skip to content

Commit a56ea73

Browse files
committed
Merge #31: Add function to export a pubkey as hex representation
f46fc21 Add function to export a pubkey as hex representation (Jonas Schnelli)
2 parents a05c6f5 + f46fc21 commit a56ea73

File tree

6 files changed

+45
-0
lines changed

6 files changed

+45
-0
lines changed

include/btc/bip32.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ LIBBTC_API void btc_hdnode_fill_public_key(btc_hdnode* node);
5959
LIBBTC_API void btc_hdnode_serialize_public(const btc_hdnode* node, const btc_chain* chain, char* str, int strsize);
6060
LIBBTC_API void btc_hdnode_serialize_private(const btc_hdnode* node, const btc_chain* chain, char* str, int strsize);
6161
LIBBTC_API void btc_hdnode_get_p2pkh_address(const btc_hdnode* node, const btc_chain* chain, char* str, int strsize);
62+
LIBBTC_API btc_bool btc_hdnode_get_pub_hex(const btc_hdnode* node, char* str, size_t *strsize);
6263
LIBBTC_API btc_bool btc_hdnode_deserialize(const char* str, const btc_chain* chain, btc_hdnode* node);
6364

6465
//!derive btc_hdnode including private key from master private key

include/btc/ecc_key.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ LIBBTC_API void btc_pubkey_from_key(btc_key* privkey, btc_pubkey* pubkey_inout);
5959
//get the hash160 (single SHA256 + RIPEMD160)
6060
LIBBTC_API void btc_pubkey_get_hash160(const btc_pubkey* pubkey, uint8_t* hash160);
6161

62+
//get the hex representation of a pubkey, strsize must be at leat 66 bytes
63+
LIBBTC_API btc_bool btc_pubkey_get_hex(const btc_pubkey* pubkey, char* str, size_t *strsize);
64+
6265
//sign a 32byte message/hash and returns a DER encoded signature (through *sigout)
6366
LIBBTC_API btc_bool btc_key_sign_hash(const btc_key* privkey, const uint8_t* hash, unsigned char* sigout, size_t* outlen);
6467

src/bip32.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "btc/base58.h"
3535
#include "btc/hash.h"
3636
#include "btc/ecc.h"
37+
#include "btc/ecc_key.h"
3738

3839
#include "ripemd160.h"
3940
#include "sha2.h"
@@ -223,6 +224,16 @@ void btc_hdnode_get_p2pkh_address(const btc_hdnode* node, const btc_chain* chain
223224
btc_base58_encode_check(hash160, 21, str, strsize);
224225
}
225226

227+
btc_bool btc_hdnode_get_pub_hex(const btc_hdnode* node, char* str, size_t *strsize)
228+
{
229+
btc_pubkey pubkey;
230+
btc_pubkey_init(&pubkey);
231+
memcpy(&pubkey.pubkey, node->public_key, BTC_ECKEY_COMPRESSED_LENGTH);
232+
pubkey.compressed = true;
233+
234+
return btc_pubkey_get_hex(&pubkey, str, strsize);
235+
}
236+
226237

227238
// check for validity of curve point in case of public data not performed
228239
btc_bool btc_hdnode_deserialize(const char* str, const btc_chain* chain, btc_hdnode* node)

src/ecc_key.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,16 @@ void btc_pubkey_get_hash160(const btc_pubkey* pubkey, uint8_t* hash160)
118118
}
119119

120120

121+
btc_bool btc_pubkey_get_hex(const btc_pubkey* pubkey, char* str, size_t *strsize)
122+
{
123+
if (*strsize < BTC_ECKEY_COMPRESSED_LENGTH*2)
124+
return false;
125+
utils_bin_to_hex((unsigned char *)pubkey->pubkey, BTC_ECKEY_COMPRESSED_LENGTH, str);
126+
*strsize = BTC_ECKEY_COMPRESSED_LENGTH*2;
127+
return true;
128+
}
129+
130+
121131
void btc_pubkey_from_key(btc_key* privkey, btc_pubkey* pubkey_inout)
122132
{
123133
if (pubkey_inout == NULL || privkey == NULL)
@@ -135,6 +145,7 @@ btc_bool btc_key_sign_hash(const btc_key* privkey, const uint8_t* hash, unsigned
135145
return ecc_sign(privkey->privkey, hash, sigout, outlen);
136146
}
137147

148+
138149
btc_bool btc_key_sign_hash_compact(const btc_key* privkey, const uint8_t* hash, unsigned char* sigout, size_t* outlen)
139150
{
140151
return ecc_sign_compact(privkey->privkey, hash, sigout, outlen);

test/bip32_tests.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,14 @@ void test_bip32()
226226
u_assert_int_eq(r, true);
227227
btc_hdnode_get_p2pkh_address(&node4, &btc_chain_test, str, sizeof(str));
228228
u_assert_str_eq(str, "mp4VkLBrnetj5LkhyNqtgkBzJwRBqhTbaa");
229+
size_t size = sizeof(str);
230+
size_t sizeSmall = 55;
231+
r = btc_hdnode_get_pub_hex(&node4, str, &sizeSmall);
232+
u_assert_int_eq(r, false);
233+
r = btc_hdnode_get_pub_hex(&node4, str, &size);
234+
u_assert_int_eq(size, 66);
235+
u_assert_int_eq(r, true);
236+
u_assert_str_eq(str, "0391a9964e79f39cebf9b59eb2151b500bd462e589682d6ceebe8e15970bfebf8b");
229237
btc_hdnode_serialize_public(&node4, &btc_chain_test, str, sizeof(str));
230238
u_assert_str_eq(str, "tpubD8MQJFN9LVzG8pktwoQ7ApWWKLfUUhonQkeXe8gqi9tFMtMdC34g6Ntj5K6V1hdzR3to2z7dGnQbXaoZSsFkVky7TFWZjmC9Ez4Gog6ujaD");
231239
}

test/eckey_tests.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include <btc/ecc_key.h>
3434

35+
#include "utest.h"
3536
#include "utils.h"
3637

3738
void test_eckey()
@@ -65,6 +66,16 @@ void test_eckey()
6566

6667
btc_pubkey_verify_sig(&pubkey, hash, sig, outlen);
6768

69+
70+
size_t size = 66;
71+
char str[size];
72+
int r = btc_pubkey_get_hex(&pubkey, str, &size);
73+
u_assert_int_eq(r, true);
74+
u_assert_int_eq(size, 66);
75+
76+
size = 50;
77+
r = btc_pubkey_get_hex(&pubkey, str, &size);
78+
u_assert_int_eq(r, false);
6879
btc_privkey_cleanse(&key);
6980
btc_pubkey_cleanse(&pubkey);
7081
}

0 commit comments

Comments
 (0)