Skip to content

Commit

Permalink
bitcoin: remove unused functions, or make static.
Browse files Browse the repository at this point in the history
Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Dec 5, 2021
1 parent f64df6f commit d9968bb
Show file tree
Hide file tree
Showing 48 changed files with 28 additions and 574 deletions.
55 changes: 0 additions & 55 deletions bitcoin/base58.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,63 +65,8 @@ static bool from_base58(u8 *version,
return true;
}

bool bitcoin_from_base58(u8 *version, struct bitcoin_address *addr,
const char *base58, size_t len)
{
return from_base58(version, &addr->addr, base58, len);
}


bool p2sh_from_base58(u8 *version, struct ripemd160 *p2sh, const char *base58,
size_t len)
{

return from_base58(version, p2sh, base58, len);
}

bool ripemd160_from_base58(u8 *version, struct ripemd160 *rmd,
const char *base58, size_t base58_len)
{
return from_base58(version, rmd, base58, base58_len);
}

bool key_from_base58(const char *base58, size_t base58_len,
bool *test_net, struct privkey *priv, struct pubkey *key)
{
// 1 byte version, 32 byte private key, 1 byte compressed, 4 byte checksum
u8 keybuf[1 + 32 + 1 + 4];
char *terminated_base58 = tal_dup_arr(NULL, char, base58, base58_len, 1);
terminated_base58[base58_len] = '\0';
size_t keybuflen = sizeof(keybuf);


size_t written = 0;
int r = wally_base58_to_bytes(terminated_base58, BASE58_FLAG_CHECKSUM, keybuf, keybuflen, &written);
wally_bzero(terminated_base58, base58_len + 1);
tal_free(terminated_base58);
if (r != WALLY_OK || written > keybuflen)
return false;

/* Byte after key should be 1 to represent a compressed key. */
if (keybuf[1 + 32] != 1)
return false;

if (keybuf[0] == 128)
*test_net = false;
else if (keybuf[0] == 239)
*test_net = true;
else
return false;

/* Copy out secret. */
memcpy(priv->secret.data, keybuf + 1, sizeof(priv->secret.data));

if (!secp256k1_ec_seckey_verify(secp256k1_ctx, priv->secret.data))
return false;

/* Get public key, too. */
if (!pubkey_from_privkey(priv, key))
return false;

return true;
}
7 changes: 0 additions & 7 deletions bitcoin/base58.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,10 @@ struct bitcoin_address;
/* Bitcoin address encoded in base58, with version and checksum */
char *bitcoin_to_base58(const tal_t *ctx, const struct chainparams *chainparams,
const struct bitcoin_address *addr);
bool bitcoin_from_base58(u8 *version, struct bitcoin_address *addr,
const char *base58, size_t len);

/* P2SH address encoded as base58, with version and checksum */
char *p2sh_to_base58(const tal_t *ctx, const struct chainparams *chainparams,
const struct ripemd160 *p2sh);
bool p2sh_from_base58(u8 *version, struct ripemd160 *p2sh, const char *base58,
size_t len);

bool key_from_base58(const char *base58, size_t base58_len,
bool *test_net, struct privkey *priv, struct pubkey *key);

/* Decode a p2pkh or p2sh into the ripemd160 hash */
bool ripemd160_from_base58(u8 *version, struct ripemd160 *rmd,
Expand Down
15 changes: 2 additions & 13 deletions bitcoin/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,19 +229,8 @@ void bitcoin_block_blkid(const struct bitcoin_block *b,
*out = b->hdr.hash;
}

/* We do the same hex-reversing crud as txids. */
bool bitcoin_blkid_from_hex(const char *hexstr, size_t hexstr_len,
struct bitcoin_blkid *blockid)
{
struct bitcoin_txid fake_txid;
if (!bitcoin_txid_from_hex(hexstr, hexstr_len, &fake_txid))
return false;
blockid->shad = fake_txid.shad;
return true;
}

bool bitcoin_blkid_to_hex(const struct bitcoin_blkid *blockid,
char *hexstr, size_t hexstr_len)
static bool bitcoin_blkid_to_hex(const struct bitcoin_blkid *blockid,
char *hexstr, size_t hexstr_len)
{
struct bitcoin_txid fake_txid;
fake_txid.shad = blockid->shad;
Expand Down
8 changes: 0 additions & 8 deletions bitcoin/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,6 @@ bitcoin_block_from_hex(const tal_t *ctx, const struct chainparams *chainparams,
void bitcoin_block_blkid(const struct bitcoin_block *block,
struct bitcoin_blkid *out);

/* Parse hex string to get blockid (reversed, a-la bitcoind). */
bool bitcoin_blkid_from_hex(const char *hexstr, size_t hexstr_len,
struct bitcoin_blkid *blockid);

/* Get hex string of blockid (reversed, a-la bitcoind). */
bool bitcoin_blkid_to_hex(const struct bitcoin_blkid *blockid,
char *hexstr, size_t hexstr_len);

/* Marshalling/unmarshaling over the wire */
void towire_bitcoin_blkid(u8 **pptr, const struct bitcoin_blkid *blkid);
void fromwire_bitcoin_blkid(const u8 **cursor, size_t *max,
Expand Down
8 changes: 0 additions & 8 deletions bitcoin/chainparams.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,6 @@ const struct chainparams *chainparams_for_network(const char *network_name)
return NULL;
}

const struct chainparams **chainparams_for_networks(const tal_t *ctx)
{
const struct chainparams **params = tal_arr(ctx, const struct chainparams*, 0);
for (size_t i = 0; i < ARRAY_SIZE(networks); i++)
tal_arr_expand(&params, &networks[i]);
return params;
}

const struct chainparams *chainparams_by_chainhash(const struct bitcoin_blkid *chain_hash)
{
for (size_t i = 0; i < ARRAY_SIZE(networks); i++) {
Expand Down
6 changes: 0 additions & 6 deletions bitcoin/chainparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ struct chainparams {
*/
const struct chainparams *chainparams_for_network(const char *network_name);

/**
* chainparams_for_networks - Get blockchain parameters for all known networks,
* as a tal array.
*/
const struct chainparams **chainparams_for_networks(const tal_t *ctx);

/**
* chainparams_by_bip173 - Helper to get a network by its bip173 name
*
Expand Down
49 changes: 1 addition & 48 deletions bitcoin/locktime.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,60 +23,13 @@ static bool abs_is_seconds(u32 locktime)
return locktime >= SECONDS_POINT;
}

bool rel_locktime_is_seconds(const struct rel_locktime *rel)
{
return rel->locktime & BIP68_SECONDS_FLAG;
}

u32 rel_locktime_to_seconds(const struct rel_locktime *rel)
{
assert(rel_locktime_is_seconds(rel));
return (rel->locktime & BIP68_LOCKTIME_MASK) << BIP68_SECONDS_SHIFT;
}

u32 rel_locktime_to_blocks(const struct rel_locktime *rel)
{
assert(!rel_locktime_is_seconds(rel));
return rel->locktime & BIP68_LOCKTIME_MASK;
}

bool blocks_to_abs_locktime(u32 blocks, struct abs_locktime *abs)
{
return abs_blocks_to_locktime(blocks, &abs->locktime);
}

bool abs_locktime_is_seconds(const struct abs_locktime *abs)
{
return abs_is_seconds(abs->locktime);
}

u32 abs_locktime_to_seconds(const struct abs_locktime *abs)
{
assert(abs_locktime_is_seconds(abs));
return abs->locktime;
}

u32 abs_locktime_to_blocks(const struct abs_locktime *abs)
{
assert(!abs_locktime_is_seconds(abs));
assert(!abs_is_seconds(abs->locktime));
return abs->locktime;
}

static char *fmt_rel_locktime(const tal_t *ctx, const struct rel_locktime *rl)
{
if (rel_locktime_is_seconds(rl))
return tal_fmt(ctx, "+%usec", rel_locktime_to_seconds(rl));
else
return tal_fmt(ctx, "+%ublocks", rel_locktime_to_blocks(rl));
}

static char *fmt_abs_locktime(const tal_t *ctx, const struct abs_locktime *al)
{
if (abs_locktime_is_seconds(al))
return tal_fmt(ctx, "%usec", abs_locktime_to_seconds(al));
else
return tal_fmt(ctx, "%ublocks", abs_locktime_to_blocks(al));
}

REGISTER_TYPE_TO_STRING(rel_locktime, fmt_rel_locktime);
REGISTER_TYPE_TO_STRING(abs_locktime, fmt_abs_locktime);
11 changes: 0 additions & 11 deletions bitcoin/locktime.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,12 @@
#include <ccan/short_types/short_types.h>
#include <stdbool.h>

/* As used by nSequence and OP_CHECKSEQUENCEVERIFY (BIP68) */
struct rel_locktime {
u32 locktime;
};

bool rel_locktime_is_seconds(const struct rel_locktime *rel);
u32 rel_locktime_to_seconds(const struct rel_locktime *rel);
u32 rel_locktime_to_blocks(const struct rel_locktime *rel);

/* As used by nLocktime and OP_CHECKLOCKTIMEVERIFY (BIP65) */
struct abs_locktime {
u32 locktime;
};

bool blocks_to_abs_locktime(u32 blocks, struct abs_locktime *abs);
bool abs_locktime_is_seconds(const struct abs_locktime *abs);
u32 abs_locktime_to_seconds(const struct abs_locktime *abs);
u32 abs_locktime_to_blocks(const struct abs_locktime *abs);

#endif /* LIGHTNING_BITCOIN_LOCKTIME_H */
19 changes: 4 additions & 15 deletions bitcoin/psbt.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <wire/wire.h>


void psbt_destroy(struct wally_psbt *psbt)
static void psbt_destroy(struct wally_psbt *psbt)
{
wally_psbt_free(psbt);
}
Expand Down Expand Up @@ -430,17 +430,6 @@ bool psbt_has_input(const struct wally_psbt *psbt,
return false;
}

bool psbt_input_set_redeemscript(struct wally_psbt *psbt, size_t in,
const u8 *redeemscript)
{
int wally_err;
assert(psbt->num_inputs > in);
wally_err = wally_psbt_input_set_redeem_script(&psbt->inputs[in],
redeemscript,
tal_bytelen(redeemscript));
return wally_err == WALLY_OK;
}

struct amount_sat psbt_input_get_amount(const struct wally_psbt *psbt,
size_t in)
{
Expand Down Expand Up @@ -558,9 +547,9 @@ void psbt_input_set_unknown(const tal_t *ctx,
abort();
}

void *psbt_get_unknown(const struct wally_map *map,
const u8 *key,
size_t *val_len)
static void *psbt_get_unknown(const struct wally_map *map,
const u8 *key,
size_t *val_len)
{
size_t index;

Expand Down
33 changes: 1 addition & 32 deletions bitcoin/psbt.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,6 @@ struct bitcoin_signature;
struct bitcoin_txid;
struct pubkey;

/** psbt_destroy - Destroy a PSBT that is not tal-allocated
*
* @psbt - the PSBT to destroy
*
* WARNING Do NOT call this function directly if you got the
* PSBT from create_psbt, new_psbt, psbt_from_bytes,
* psbt_from_b64, or fromwire_wally_psbt.
* Those functions register this function as a `tal_destructor`
* automatically.
*/
void psbt_destroy(struct wally_psbt *psbt);

/**
* create_psbt - Create a new psbt object
*
Expand Down Expand Up @@ -172,16 +160,7 @@ WARN_UNUSED_RESULT bool psbt_input_set_signature(struct wally_psbt *psbt, size_t
const struct bitcoin_signature *sig);

void psbt_input_set_witscript(struct wally_psbt *psbt, size_t in, const u8 *wscript);
void psbt_elements_input_init(struct wally_psbt *psbt, size_t in,
const u8 *scriptPubkey,
struct amount_asset *asset,
const u8 *nonce);
void psbt_elements_input_init_witness(struct wally_psbt *psbt, size_t in,
const u8 *witscript,
struct amount_asset *asset,
const u8 *nonce);
bool psbt_input_set_redeemscript(struct wally_psbt *psbt, size_t in,
const u8 *redeemscript);

/* psbt_input_set_unknown - Set the given Key-Value in the psbt's input keymap
* @ctx - tal context for allocations
* @in - psbt input to set key-value on
Expand All @@ -194,16 +173,6 @@ void psbt_input_set_unknown(const tal_t *ctx,
const u8 *key,
const void *value,
size_t value_len);
/* psbt_get_unknown - Fetch the value from the given map at key
*
* @map - map of unknowns to search for key
* @key - key of key-value pair to return value for
* @value_len - (out) length of value (if found)
*
* Returns: value at @key, or NULL if not found */
void *psbt_get_unknown(const struct wally_map *map,
const u8 *key,
size_t *val_len);

/* psbt_get_lightning - Fetch a proprietary lightning value from the given map
*
Expand Down
4 changes: 2 additions & 2 deletions bitcoin/pubkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ char *pubkey_to_hexstr(const tal_t *ctx, const struct pubkey *key)
}
REGISTER_TYPE_TO_STRING(pubkey, pubkey_to_hexstr);

char *secp256k1_pubkey_to_hexstr(const tal_t *ctx, const secp256k1_pubkey *key)
static char *secp256k1_pubkey_to_hexstr(const tal_t *ctx, const secp256k1_pubkey *key)
{
unsigned char der[PUBKEY_CMPR_LEN];
size_t outlen = sizeof(der);
Expand Down Expand Up @@ -150,7 +150,7 @@ void towire_point32(u8 **pptr, const struct point32 *point32)
towire(pptr, output, sizeof(output));
}

char *point32_to_hexstr(const tal_t *ctx, const struct point32 *point32)
static char *point32_to_hexstr(const tal_t *ctx, const struct point32 *point32)
{
u8 output[32];

Expand Down
4 changes: 0 additions & 4 deletions bitcoin/pubkey.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ bool pubkey_from_hexstr(const char *derstr, size_t derlen, struct pubkey *key);
/* Convert from hex string of DER (scriptPubKey from validateaddress) */
char *pubkey_to_hexstr(const tal_t *ctx, const struct pubkey *key);

/* Convenience wrapper for a raw secp256k1_pubkey */
char *secp256k1_pubkey_to_hexstr(const tal_t *ctx, const secp256k1_pubkey *key);

/* Point from secret */
bool pubkey_from_secret(const struct secret *secret, struct pubkey *key);

Expand Down Expand Up @@ -75,5 +72,4 @@ void fromwire_pubkey(const u8 **cursor, size_t *max, struct pubkey *pubkey);
void towire_point32(u8 **pptr, const struct point32 *pubkey);
void fromwire_point32(const u8 **cursor, size_t *max, struct point32 *pubkey);

char *point32_to_hexstr(const tal_t *ctx, const struct point32 *point32);
#endif /* LIGHTNING_BITCOIN_PUBKEY_H */
7 changes: 0 additions & 7 deletions bitcoin/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,6 @@ u8 *scriptpubkey_p2pkh(const tal_t *ctx, const struct bitcoin_address *addr)
return script;
}

u8 *scriptpubkey_opreturn(const tal_t *ctx)
{
u8 *script = tal_arr(ctx, u8, 0);

add_op(&script, OP_RETURN);
return script;
}
u8 *scriptpubkey_opreturn_padded(const tal_t *ctx)
{
u8 *script = tal_arr(ctx, u8, 0);
Expand Down
2 changes: 0 additions & 2 deletions bitcoin/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ u8 *scriptpubkey_p2sh_hash(const tal_t *ctx, const struct ripemd160 *redeemhash)
/* Create an output script using p2pkh */
u8 *scriptpubkey_p2pkh(const tal_t *ctx, const struct bitcoin_address *addr);

/* Create a prunable output script */
u8 *scriptpubkey_opreturn(const tal_t *ctx);
/* Create a prunable output script with 20 random bytes.
* This is needed since a spend from a p2wpkh to an `OP_RETURN` without
* any other outputs would result in a transaction smaller than the
Expand Down
Loading

0 comments on commit d9968bb

Please sign in to comment.