From 25a37fafae92dabe3a851524cfd845b3b3081276 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 2 Feb 2017 14:35:45 +1030 Subject: [PATCH] type_to_string: add privkey. Signed-off-by: Rusty Russell --- bitcoin/privkey.h | 1 - bitcoin/pubkey.c | 10 ++++++++++ type_to_string.h | 1 + 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/bitcoin/privkey.h b/bitcoin/privkey.h index aee49ac58..5ed4c4937 100644 --- a/bitcoin/privkey.h +++ b/bitcoin/privkey.h @@ -7,5 +7,4 @@ struct privkey { u8 secret[32]; }; - #endif /* LIGHTNING_BITCOIN_PRIVKEY_H */ diff --git a/bitcoin/pubkey.c b/bitcoin/pubkey.c index 3fa578914..4424bda95 100644 --- a/bitcoin/pubkey.c +++ b/bitcoin/pubkey.c @@ -88,3 +88,13 @@ int pubkey_cmp(const struct pubkey *a, const struct pubkey *b) pubkey_to_der(keyb, b); return memcmp(keya, keyb, sizeof(keya)); } + +static char *privkey_to_hexstr(const tal_t *ctx, const struct privkey *secret) +{ + /* Bitcoin appends "01" to indicate the pubkey is compressed. */ + char *str = tal_arr(ctx, char, hex_str_size(sizeof(*secret) + 1)); + hex_encode(secret, sizeof(*secret), str, hex_str_size(sizeof(*secret))); + strcat(str, "01"); + return str; +} +REGISTER_TYPE_TO_STRING(privkey, privkey_to_hexstr); diff --git a/type_to_string.h b/type_to_string.h index 72bb40bd7..83b30a735 100644 --- a/type_to_string.h +++ b/type_to_string.h @@ -20,6 +20,7 @@ union printable_types { const struct netaddr *netaddr; const secp256k1_pubkey *secp256k1_pubkey; const struct channel_id *channel_id; + const struct privkey *privkey; const char *charp_; };