From 364c2cd2c0b39179cb5d1089129ab019f72d058a Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 1 Jul 2016 11:27:57 +0930 Subject: [PATCH] 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 --- bitcoin/base58.c | 14 ++++---------- bitcoin/pubkey.c | 3 +-- bitcoin/pubkey.h | 3 +-- daemon/secrets.c | 6 ++---- daemon/wallet.c | 3 +-- 5 files changed, 9 insertions(+), 20 deletions(-) diff --git a/bitcoin/base58.c b/bitcoin/base58.c index ca2d80d8b..ab570a4bc 100644 --- a/bitcoin/base58.c +++ b/bitcoin/base58.c @@ -313,7 +313,6 @@ bool key_from_base58(secp256k1_context *secpctx, u8 keybuf[1 + 32 + 1 + 4]; u8 csum[4]; BIGNUM bn; - bool compressed; size_t keylen; BN_init(&bn); @@ -321,11 +320,7 @@ bool key_from_base58(secp256k1_context *secpctx, return false; keylen = BN_num_bytes(&bn); - if (keylen == 1 + 32 + 4) - compressed = false; - else if (keylen == 1 + 32 + 1 + 4) - compressed = true; - else + if (keylen != 1 + 32 + 1 + 4) goto fail_free_bn; BN_bn2bin(&bn, keybuf); @@ -334,7 +329,7 @@ bool key_from_base58(secp256k1_context *secpctx, goto fail_free_bn; /* 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; if (keybuf[0] == 128) @@ -350,9 +345,8 @@ bool key_from_base58(secp256k1_context *secpctx, if (!secp256k1_ec_seckey_verify(secpctx, priv->secret)) goto fail_free_bn; - /* Get public key, too, since we know if it's compressed. */ - if (!pubkey_from_privkey(secpctx, priv, key, - compressed ? SECP256K1_EC_COMPRESSED : 0)) + /* Get public key, too. */ + if (!pubkey_from_privkey(secpctx, priv, key)) goto fail_free_bn; BN_free(&bn); diff --git a/bitcoin/pubkey.c b/bitcoin/pubkey.c index 1cfc9298a..bfe0b0cf2 100644 --- a/bitcoin/pubkey.c +++ b/bitcoin/pubkey.c @@ -34,8 +34,7 @@ void pubkey_to_der(secp256k1_context *secpctx, u8 der[PUBKEY_DER_LEN], /* Pubkey from privkey */ bool pubkey_from_privkey(secp256k1_context *secpctx, const struct privkey *privkey, - struct pubkey *key, - unsigned int compressed_flags) + struct pubkey *key) { if (!secp256k1_ec_pubkey_create(secpctx, &key->pubkey, privkey->secret)) return false; diff --git a/bitcoin/pubkey.h b/bitcoin/pubkey.h index 466fc0b63..f7adfb312 100644 --- a/bitcoin/pubkey.h +++ b/bitcoin/pubkey.h @@ -25,8 +25,7 @@ char *pubkey_to_hexstr(const tal_t *ctx, secp256k1_context *secpctx, /* Pubkey from privkey */ bool pubkey_from_privkey(secp256k1_context *secpctx, const struct privkey *privkey, - struct pubkey *key, - unsigned int compressed_flags); + struct pubkey *key); /* Pubkey from DER encoding. */ bool pubkey_from_der(secp256k1_context *secpctx, diff --git a/daemon/secrets.c b/daemon/secrets.c index 54b9b3913..7e0e23537 100644 --- a/daemon/secrets.c +++ b/daemon/secrets.c @@ -149,8 +149,7 @@ static void new_keypair(struct lightningd_state *dstate, do { if (RAND_bytes(privkey->secret, sizeof(privkey->secret)) != 1) fatal("Could not get random bytes for privkey"); - } while (!pubkey_from_privkey(dstate->secpctx, - privkey, pubkey, SECP256K1_EC_COMPRESSED)); + } while (!pubkey_from_privkey(dstate->secpctx, privkey, pubkey)); } 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)); close(fd); if (!pubkey_from_privkey(dstate->secpctx, - &dstate->secret->privkey, &dstate->id, - SECP256K1_EC_COMPRESSED)) + &dstate->secret->privkey, &dstate->id)) fatal("Invalid privkey"); log_info_struct(dstate->base_log, "ID: %s", struct pubkey, &dstate->id); diff --git a/daemon/wallet.c b/daemon/wallet.c index 27142b527..795e5011a 100644 --- a/daemon/wallet.c +++ b/daemon/wallet.c @@ -27,8 +27,7 @@ static void new_keypair(struct lightningd_state *dstate, do { if (RAND_bytes(privkey->secret, sizeof(privkey->secret)) != 1) fatal("Could not get random bytes for privkey"); - } while (!pubkey_from_privkey(dstate->secpctx, - privkey, pubkey, SECP256K1_EC_COMPRESSED)); + } while (!pubkey_from_privkey(dstate->secpctx, privkey, pubkey)); } void wallet_add_signed_input(struct lightningd_state *dstate,