From a25e2816e998df7fa7fa73c8f0e2cf70d5d36c8a Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 25 Jan 2017 10:02:43 +1030 Subject: [PATCH] type_to_string: add secp256k1_pubkey Signed-off-by: Rusty Russell --- bitcoin/pubkey.c | 12 ++++++++++++ bitcoin/pubkey.h | 3 +++ type_to_string.h | 2 ++ 3 files changed, 17 insertions(+) diff --git a/bitcoin/pubkey.c b/bitcoin/pubkey.c index a56bf526b..3fa578914 100644 --- a/bitcoin/pubkey.c +++ b/bitcoin/pubkey.c @@ -62,6 +62,18 @@ char *pubkey_to_hexstr(const tal_t *ctx, const struct pubkey *key) return tal_hexstr(ctx, der, sizeof(der)); } +char *secp256k1_pubkey_to_hexstr(const tal_t *ctx, const secp256k1_pubkey *key) +{ + unsigned char der[PUBKEY_DER_LEN]; + size_t outlen = sizeof(der); + if (!secp256k1_ec_pubkey_serialize(secp256k1_ctx, der, &outlen, key, + SECP256K1_EC_COMPRESSED)) + abort(); + assert(outlen == sizeof(der)); + return tal_hexstr(ctx, der, sizeof(der)); +} +REGISTER_TYPE_TO_STRING(secp256k1_pubkey, secp256k1_pubkey_to_hexstr); + bool pubkey_eq(const struct pubkey *a, const struct pubkey *b) { return structeq(&a->pubkey, &b->pubkey); diff --git a/bitcoin/pubkey.h b/bitcoin/pubkey.h index 6e63f2ab4..af8bfe9b0 100644 --- a/bitcoin/pubkey.h +++ b/bitcoin/pubkey.h @@ -20,6 +20,9 @@ bool pubkey_from_hexstr(const char *derstr, size_t derlen, struct pubkey *key); /* Convert from hex string of DER (scriptPubKey from validateaddress) */ char *pubkey_to_hexstr(const tal_t *ctx, const struct pubkey *key); +/* Convenience wrapper for a raw secp256k1_pubkey */ +char *secp256k1_pubkey_to_hexstr(const tal_t *ctx, const secp256k1_pubkey *key); + /* Pubkey from privkey */ bool pubkey_from_privkey(const struct privkey *privkey, struct pubkey *key); diff --git a/type_to_string.h b/type_to_string.h index 3cd359557..146a42a64 100644 --- a/type_to_string.h +++ b/type_to_string.h @@ -3,6 +3,7 @@ #include "config.h" #include "utils.h" #include +#include /* This must match the type_to_string_ cases. */ union printable_types { @@ -17,6 +18,7 @@ union printable_types { const struct channel_state *channel_state; const struct channel_oneside *channel_oneside; const struct netaddr *netaddr; + const secp256k1_pubkey *secp256k1_pubkey; const char *charp_; };