key_from_base58 / pubkey_from_privkey: don't support non-compressed keys.

It just clutters the API, and we don't support them on the wire anyway.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2016-07-01 11:27:57 +09:30
parent 2a03af4486
commit 364c2cd2c0
5 changed files with 9 additions and 20 deletions

View file

@ -313,7 +313,6 @@ bool key_from_base58(secp256k1_context *secpctx,
u8 keybuf[1 + 32 + 1 + 4]; u8 keybuf[1 + 32 + 1 + 4];
u8 csum[4]; u8 csum[4];
BIGNUM bn; BIGNUM bn;
bool compressed;
size_t keylen; size_t keylen;
BN_init(&bn); BN_init(&bn);
@ -321,11 +320,7 @@ bool key_from_base58(secp256k1_context *secpctx,
return false; return false;
keylen = BN_num_bytes(&bn); keylen = BN_num_bytes(&bn);
if (keylen == 1 + 32 + 4) if (keylen != 1 + 32 + 1 + 4)
compressed = false;
else if (keylen == 1 + 32 + 1 + 4)
compressed = true;
else
goto fail_free_bn; goto fail_free_bn;
BN_bn2bin(&bn, keybuf); BN_bn2bin(&bn, keybuf);
@ -334,7 +329,7 @@ bool key_from_base58(secp256k1_context *secpctx,
goto fail_free_bn; goto fail_free_bn;
/* Byte after key should be 1 to represent a compressed key. */ /* Byte after key should be 1 to represent a compressed key. */
if (compressed && keybuf[1 + 32] != 1) if (keybuf[1 + 32] != 1)
goto fail_free_bn; goto fail_free_bn;
if (keybuf[0] == 128) if (keybuf[0] == 128)
@ -350,9 +345,8 @@ bool key_from_base58(secp256k1_context *secpctx,
if (!secp256k1_ec_seckey_verify(secpctx, priv->secret)) if (!secp256k1_ec_seckey_verify(secpctx, priv->secret))
goto fail_free_bn; goto fail_free_bn;
/* Get public key, too, since we know if it's compressed. */ /* Get public key, too. */
if (!pubkey_from_privkey(secpctx, priv, key, if (!pubkey_from_privkey(secpctx, priv, key))
compressed ? SECP256K1_EC_COMPRESSED : 0))
goto fail_free_bn; goto fail_free_bn;
BN_free(&bn); BN_free(&bn);

View file

@ -34,8 +34,7 @@ void pubkey_to_der(secp256k1_context *secpctx, u8 der[PUBKEY_DER_LEN],
/* Pubkey from privkey */ /* Pubkey from privkey */
bool pubkey_from_privkey(secp256k1_context *secpctx, bool pubkey_from_privkey(secp256k1_context *secpctx,
const struct privkey *privkey, const struct privkey *privkey,
struct pubkey *key, struct pubkey *key)
unsigned int compressed_flags)
{ {
if (!secp256k1_ec_pubkey_create(secpctx, &key->pubkey, privkey->secret)) if (!secp256k1_ec_pubkey_create(secpctx, &key->pubkey, privkey->secret))
return false; return false;

View file

@ -25,8 +25,7 @@ char *pubkey_to_hexstr(const tal_t *ctx, secp256k1_context *secpctx,
/* Pubkey from privkey */ /* Pubkey from privkey */
bool pubkey_from_privkey(secp256k1_context *secpctx, bool pubkey_from_privkey(secp256k1_context *secpctx,
const struct privkey *privkey, const struct privkey *privkey,
struct pubkey *key, struct pubkey *key);
unsigned int compressed_flags);
/* Pubkey from DER encoding. */ /* Pubkey from DER encoding. */
bool pubkey_from_der(secp256k1_context *secpctx, bool pubkey_from_der(secp256k1_context *secpctx,

View file

@ -149,8 +149,7 @@ static void new_keypair(struct lightningd_state *dstate,
do { do {
if (RAND_bytes(privkey->secret, sizeof(privkey->secret)) != 1) if (RAND_bytes(privkey->secret, sizeof(privkey->secret)) != 1)
fatal("Could not get random bytes for privkey"); fatal("Could not get random bytes for privkey");
} while (!pubkey_from_privkey(dstate->secpctx, } while (!pubkey_from_privkey(dstate->secpctx, privkey, pubkey));
privkey, pubkey, SECP256K1_EC_COMPRESSED));
} }
void peer_secrets_init(struct peer *peer) void peer_secrets_init(struct peer *peer)
@ -220,8 +219,7 @@ void secrets_init(struct lightningd_state *dstate)
fatal("Failed to read privkey: %s", strerror(errno)); fatal("Failed to read privkey: %s", strerror(errno));
close(fd); close(fd);
if (!pubkey_from_privkey(dstate->secpctx, if (!pubkey_from_privkey(dstate->secpctx,
&dstate->secret->privkey, &dstate->id, &dstate->secret->privkey, &dstate->id))
SECP256K1_EC_COMPRESSED))
fatal("Invalid privkey"); fatal("Invalid privkey");
log_info_struct(dstate->base_log, "ID: %s", struct pubkey, &dstate->id); log_info_struct(dstate->base_log, "ID: %s", struct pubkey, &dstate->id);

View file

@ -27,8 +27,7 @@ static void new_keypair(struct lightningd_state *dstate,
do { do {
if (RAND_bytes(privkey->secret, sizeof(privkey->secret)) != 1) if (RAND_bytes(privkey->secret, sizeof(privkey->secret)) != 1)
fatal("Could not get random bytes for privkey"); fatal("Could not get random bytes for privkey");
} while (!pubkey_from_privkey(dstate->secpctx, } while (!pubkey_from_privkey(dstate->secpctx, privkey, pubkey));
privkey, pubkey, SECP256K1_EC_COMPRESSED));
} }
void wallet_add_signed_input(struct lightningd_state *dstate, void wallet_add_signed_input(struct lightningd_state *dstate,