2015-09-30 10:54:54 +09:30
|
|
|
#include "privkey.h"
|
2016-01-22 06:38:08 +10:30
|
|
|
#include "pubkey.h"
|
|
|
|
#include <assert.h>
|
2016-03-30 16:54:16 +10:30
|
|
|
#include <ccan/mem/mem.h>
|
2016-01-22 06:38:08 +10:30
|
|
|
#include <ccan/str/hex/hex.h>
|
2016-07-01 11:19:28 +09:30
|
|
|
#include <ccan/structeq/structeq.h>
|
2017-08-29 01:32:01 +09:30
|
|
|
#include <common/type_to_string.h>
|
|
|
|
#include <common/utils.h>
|
2015-06-02 11:53:59 +09:30
|
|
|
|
2016-12-02 18:12:58 +10:30
|
|
|
bool pubkey_from_der(const u8 *der, size_t len, struct pubkey *key)
|
2015-06-02 11:53:59 +09:30
|
|
|
{
|
2016-07-01 11:19:28 +09:30
|
|
|
if (len != PUBKEY_DER_LEN)
|
2016-01-22 06:41:47 +10:30
|
|
|
return false;
|
2015-09-30 10:54:54 +09:30
|
|
|
|
2016-12-02 18:12:58 +10:30
|
|
|
if (!secp256k1_ec_pubkey_parse(secp256k1_ctx, &key->pubkey,
|
2016-07-01 11:19:28 +09:30
|
|
|
memcheck(der, len), len))
|
2016-01-22 06:41:47 +10:30
|
|
|
return false;
|
2015-09-30 10:54:54 +09:30
|
|
|
|
|
|
|
return true;
|
2015-06-02 11:53:59 +09:30
|
|
|
}
|
|
|
|
|
2016-12-02 18:12:58 +10:30
|
|
|
void pubkey_to_der(u8 der[PUBKEY_DER_LEN], const struct pubkey *key)
|
2016-07-01 11:19:28 +09:30
|
|
|
{
|
|
|
|
size_t outlen = PUBKEY_DER_LEN;
|
2016-12-02 18:12:58 +10:30
|
|
|
if (!secp256k1_ec_pubkey_serialize(secp256k1_ctx, der, &outlen,
|
2016-07-01 11:19:28 +09:30
|
|
|
&key->pubkey,
|
|
|
|
SECP256K1_EC_COMPRESSED))
|
|
|
|
abort();
|
|
|
|
assert(outlen == PUBKEY_DER_LEN);
|
|
|
|
}
|
|
|
|
|
2015-09-30 10:54:54 +09:30
|
|
|
/* Pubkey from privkey */
|
2016-12-02 18:12:58 +10:30
|
|
|
bool pubkey_from_privkey(const struct privkey *privkey,
|
2016-07-01 11:27:57 +09:30
|
|
|
struct pubkey *key)
|
2015-06-05 12:24:58 +09:30
|
|
|
{
|
2016-12-02 18:12:58 +10:30
|
|
|
if (!secp256k1_ec_pubkey_create(secp256k1_ctx,
|
2017-05-06 11:49:44 +09:30
|
|
|
&key->pubkey, privkey->secret.data))
|
2016-01-22 06:41:47 +10:30
|
|
|
return false;
|
2015-09-30 10:54:54 +09:30
|
|
|
return true;
|
|
|
|
}
|
2016-11-11 09:32:04 +10:30
|
|
|
|
2016-12-02 18:12:58 +10:30
|
|
|
bool pubkey_from_hexstr(const char *derstr, size_t slen, struct pubkey *key)
|
2015-09-30 10:54:54 +09:30
|
|
|
{
|
2016-01-22 06:41:47 +10:30
|
|
|
size_t dlen;
|
2016-07-01 11:19:28 +09:30
|
|
|
unsigned char der[PUBKEY_DER_LEN];
|
2015-09-30 10:54:54 +09:30
|
|
|
|
|
|
|
dlen = hex_data_size(slen);
|
2016-03-30 16:54:16 +10:30
|
|
|
if (dlen != sizeof(der))
|
2015-06-05 12:24:58 +09:30
|
|
|
return false;
|
2015-09-30 10:54:54 +09:30
|
|
|
|
|
|
|
if (!hex_decode(derstr, slen, der, dlen))
|
2015-06-05 12:24:58 +09:30
|
|
|
return false;
|
2015-09-30 10:54:54 +09:30
|
|
|
|
2016-12-02 18:12:58 +10:30
|
|
|
return pubkey_from_der(der, dlen, key);
|
2015-09-30 10:54:54 +09:30
|
|
|
}
|
|
|
|
|
2016-12-02 18:12:58 +10:30
|
|
|
char *pubkey_to_hexstr(const tal_t *ctx, const struct pubkey *key)
|
2016-07-01 11:19:28 +09:30
|
|
|
{
|
|
|
|
unsigned char der[PUBKEY_DER_LEN];
|
|
|
|
|
2016-12-02 18:12:58 +10:30
|
|
|
pubkey_to_der(der, key);
|
2016-07-01 11:19:28 +09:30
|
|
|
return tal_hexstr(ctx, der, sizeof(der));
|
|
|
|
}
|
2016-11-11 09:32:04 +10:30
|
|
|
|
2017-01-25 10:02:43 +10:30
|
|
|
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);
|
|
|
|
|
2015-09-30 10:54:54 +09:30
|
|
|
bool pubkey_eq(const struct pubkey *a, const struct pubkey *b)
|
|
|
|
{
|
2016-07-01 11:19:28 +09:30
|
|
|
return structeq(&a->pubkey, &b->pubkey);
|
2015-06-05 12:24:58 +09:30
|
|
|
}
|
2016-12-12 13:12:58 +01:00
|
|
|
|
2017-01-04 14:07:15 +10:30
|
|
|
REGISTER_TYPE_TO_STRING(pubkey, pubkey_to_hexstr);
|
|
|
|
|
2016-12-12 13:12:58 +01:00
|
|
|
int pubkey_cmp(const struct pubkey *a, const struct pubkey *b)
|
|
|
|
{
|
|
|
|
u8 keya[33], keyb[33];
|
|
|
|
pubkey_to_der(keya, a);
|
|
|
|
pubkey_to_der(keyb, b);
|
|
|
|
return memcmp(keya, keyb, sizeof(keya));
|
|
|
|
}
|
2017-02-02 14:35:45 +10:30
|
|
|
|
|
|
|
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);
|
2017-05-06 11:49:44 +09:30
|
|
|
REGISTER_TYPE_TO_HEXSTR(secret);
|
2017-06-14 10:29:10 +02:00
|
|
|
|
|
|
|
void pubkey_to_hash160(const struct pubkey *pk, struct ripemd160 *hash)
|
|
|
|
{
|
|
|
|
u8 der[PUBKEY_DER_LEN];
|
|
|
|
struct sha256 h;
|
|
|
|
|
|
|
|
pubkey_to_der(der, pk);
|
|
|
|
sha256(&h, der, sizeof(der));
|
|
|
|
ripemd160(hash, h.u.u8, sizeof(h));
|
|
|
|
}
|