mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-17 19:03:42 +01:00
protocol: remove support for uncompressed pubkeys.
There's no good reason to support them, and this way every key is 33 bytes. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
8c468c1e15
commit
b6943b9198
@ -1,11 +1,12 @@
|
||||
#include "address.h"
|
||||
#include "pubkey.h"
|
||||
#include <ccan/mem/mem.h>
|
||||
#include <ccan/crypto/sha256/sha256.h>
|
||||
|
||||
void bitcoin_address(const struct pubkey *key, struct bitcoin_address *addr)
|
||||
{
|
||||
struct sha256 h;
|
||||
|
||||
sha256(&h, key->der, pubkey_derlen(key));
|
||||
sha256(&h, memcheck(key->der, sizeof(key->der)), sizeof(key->der));
|
||||
ripemd160(&addr->addr, h.u.u8, sizeof(h));
|
||||
}
|
||||
|
@ -1,41 +1,19 @@
|
||||
#include "privkey.h"
|
||||
#include "pubkey.h"
|
||||
#include <assert.h>
|
||||
#include <ccan/mem/mem.h>
|
||||
#include <ccan/str/hex/hex.h>
|
||||
|
||||
/* Must agree on key validity with bitcoin! Stolen from bitcoin/src/pubkey.h's
|
||||
* GetLen:
|
||||
* // Copyright (c) 2009-2010 Satoshi Nakamoto
|
||||
* // Copyright (c) 2009-2014 The Bitcoin Core developers
|
||||
* // Distributed under the MIT software license, see the accompanying
|
||||
* // file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
*/
|
||||
static unsigned int GetLen(unsigned char chHeader)
|
||||
{
|
||||
if (chHeader == 2 || chHeader == 3)
|
||||
return 33;
|
||||
if (chHeader == 4 || chHeader == 6 || chHeader == 7)
|
||||
return 65;
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t pubkey_derlen(const struct pubkey *key)
|
||||
{
|
||||
size_t len = GetLen(key->der[0]);
|
||||
|
||||
assert(len);
|
||||
return len;
|
||||
}
|
||||
|
||||
bool pubkey_from_der(secp256k1_context *secpctx,
|
||||
const u8 *der, size_t len,
|
||||
struct pubkey *key)
|
||||
{
|
||||
if (len > sizeof(key->der))
|
||||
if (len != sizeof(key->der))
|
||||
return false;
|
||||
|
||||
memcpy(key->der, der, len);
|
||||
if (!secp256k1_ec_pubkey_parse(secpctx, &key->pubkey, key->der, len))
|
||||
memcpy(key->der, memcheck(der, sizeof(key->der)), sizeof(key->der));
|
||||
if (!secp256k1_ec_pubkey_parse(secpctx, &key->pubkey, key->der,
|
||||
sizeof(key->der)))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
@ -55,7 +33,7 @@ bool pubkey_from_privkey(secp256k1_context *secpctx,
|
||||
if (!secp256k1_ec_pubkey_serialize(secpctx, key->der, &outlen,
|
||||
&key->pubkey, compressed_flags))
|
||||
return false;
|
||||
assert(outlen == pubkey_derlen(key));
|
||||
assert(outlen == sizeof(key->der));
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -63,10 +41,10 @@ bool pubkey_from_hexstr(secp256k1_context *secpctx,
|
||||
const char *derstr, size_t slen, struct pubkey *key)
|
||||
{
|
||||
size_t dlen;
|
||||
unsigned char der[65];
|
||||
unsigned char der[sizeof(key->der)];
|
||||
|
||||
dlen = hex_data_size(slen);
|
||||
if (dlen > sizeof(der))
|
||||
if (dlen != sizeof(der))
|
||||
return false;
|
||||
|
||||
if (!hex_decode(derstr, slen, der, dlen))
|
||||
@ -77,6 +55,5 @@ bool pubkey_from_hexstr(secp256k1_context *secpctx,
|
||||
|
||||
bool pubkey_eq(const struct pubkey *a, const struct pubkey *b)
|
||||
{
|
||||
return pubkey_derlen(a) == pubkey_derlen(b)
|
||||
&& memcmp(a->der, b->der, pubkey_derlen(a)) == 0;
|
||||
return memcmp(a->der, b->der, sizeof(a->der)) == 0;
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ struct privkey;
|
||||
|
||||
struct pubkey {
|
||||
/* DER-encoded key (as hashed by bitcoin, for addresses) */
|
||||
u8 der[65];
|
||||
u8 der[33];
|
||||
/* Unpacked pubkey (as used by libsecp256k1 internally) */
|
||||
secp256k1_pubkey pubkey;
|
||||
};
|
||||
@ -28,9 +28,6 @@ bool pubkey_from_privkey(secp256k1_context *secpctx,
|
||||
bool pubkey_from_der(secp256k1_context *secpctx,
|
||||
const u8 *der, size_t len, struct pubkey *key);
|
||||
|
||||
/* How many bytes of key->der are valid. */
|
||||
size_t pubkey_derlen(const struct pubkey *key);
|
||||
|
||||
/* Are these keys equal? */
|
||||
bool pubkey_eq(const struct pubkey *a, const struct pubkey *b);
|
||||
#endif /* LIGHTNING_PUBKEY_H */
|
||||
|
@ -102,7 +102,7 @@ static void add_number(u8 **script, u32 num)
|
||||
|
||||
static void add_push_key(u8 **scriptp, const struct pubkey *key)
|
||||
{
|
||||
add_push_bytes(scriptp, key->der, pubkey_derlen(key));
|
||||
add_push_bytes(scriptp, key->der, sizeof(key->der));
|
||||
}
|
||||
|
||||
static void add_push_sig(u8 **scriptp, const struct bitcoin_signature *sig)
|
||||
@ -129,11 +129,7 @@ static void add_push_sig(u8 **scriptp, const struct bitcoin_signature *sig)
|
||||
/* Is a < b? (If equal we don't care) */
|
||||
static bool key_less(const struct pubkey *a, const struct pubkey *b)
|
||||
{
|
||||
/* Shorter one wins. */
|
||||
if (pubkey_derlen(a) != pubkey_derlen(b))
|
||||
return pubkey_derlen(a) < pubkey_derlen(b);
|
||||
|
||||
return memcmp(a->der, b->der, pubkey_derlen(a)) < 0;
|
||||
return memcmp(a->der, b->der, sizeof(a->der)) < 0;
|
||||
}
|
||||
|
||||
/* tal_count() gives the length of the script. */
|
||||
|
@ -35,7 +35,7 @@ static void dump_tx(const char *str, const struct bitcoin_tx *tx)
|
||||
|
||||
static void dump_key(const char *str, const struct pubkey *key)
|
||||
{
|
||||
printf("%s:%s\n", str, hex_of(NULL, key->der, pubkey_derlen(key)));
|
||||
printf("%s:%s\n", str, hex_of(NULL, key->der, sizeof(key->der)));
|
||||
}
|
||||
|
||||
/* Wrap (and own!) member inside Pkt */
|
||||
|
@ -1363,7 +1363,7 @@ static void json_getpeers(struct command *cmd,
|
||||
/* This is only valid after crypto setup. */
|
||||
if (p->state != STATE_INIT)
|
||||
json_add_hex(response, "peerid",
|
||||
p->id.der, pubkey_derlen(&p->id));
|
||||
p->id.der, sizeof(p->id.der));
|
||||
|
||||
if (p->cstate) {
|
||||
json_object_start(response, "channel");
|
||||
|
@ -176,5 +176,5 @@ void secrets_init(struct lightningd_state *dstate)
|
||||
fatal("Invalid privkey");
|
||||
|
||||
log_info(dstate->base_log, "ID: ");
|
||||
log_add_hex(dstate->base_log, dstate->id.der, pubkey_derlen(&dstate->id));
|
||||
log_add_hex(dstate->base_log, dstate->id.der, sizeof(dstate->id.der));
|
||||
}
|
||||
|
@ -114,7 +114,7 @@ struct _BitcoinPubkey
|
||||
{
|
||||
ProtobufCMessage base;
|
||||
/*
|
||||
* Either 65 or 33 bytes.
|
||||
* Must be 33 bytes.
|
||||
*/
|
||||
ProtobufCBinaryData key;
|
||||
};
|
||||
|
@ -35,7 +35,7 @@ message locktime {
|
||||
|
||||
// Pubkey for commitment transaction input.
|
||||
message bitcoin_pubkey {
|
||||
// Either 65 or 33 bytes.
|
||||
// Must be 33 bytes.
|
||||
required bytes key = 1;
|
||||
}
|
||||
|
||||
|
@ -71,7 +71,7 @@ BitcoinPubkey *pubkey_to_proto(const tal_t *ctx, const struct pubkey *key)
|
||||
struct pubkey check;
|
||||
|
||||
bitcoin_pubkey__init(p);
|
||||
p->key.len = pubkey_derlen(key);
|
||||
p->key.len = sizeof(key->der);
|
||||
p->key.data = tal_dup_arr(p, u8, key->der, p->key.len, 0);
|
||||
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user