Skip to content

Commit

Permalink
wire: move remaining bitcoin functions out to bitcoin/ files.
Browse files Browse the repository at this point in the history
Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell authored and cdecker committed May 18, 2020
1 parent 197d1bc commit cfb320c
Show file tree
Hide file tree
Showing 80 changed files with 434 additions and 377 deletions.
7 changes: 4 additions & 3 deletions bitcoin/block.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "bitcoin/block.h"
#include "bitcoin/pullpush.h"
#include "bitcoin/tx.h"
#include <bitcoin/block.h>
#include <bitcoin/chainparams.h>
#include <bitcoin/pullpush.h>
#include <bitcoin/tx.h>
#include <ccan/str/hex/hex.h>
#include <common/type_to_string.h>
#include <wire/wire.h>
Expand Down
6 changes: 1 addition & 5 deletions bitcoin/chainparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@
#include <ccan/short_types/short_types.h>
#include <ccan/tal/str/str.h>
#include <common/amount.h>
#include <common/bip32.h>
#include <stdbool.h>

struct bip32_key_version {
u32 bip32_pubkey_version;
u32 bip32_privkey_version;
};

struct chainparams {
const char *network_name;
const char *bip173_name;
Expand Down
21 changes: 21 additions & 0 deletions bitcoin/privkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <ccan/mem/mem.h>
#include <ccan/str/hex/hex.h>
#include <common/type_to_string.h>
#include <wire/wire.h>

static char *privkey_to_hexstr(const tal_t *ctx, const struct privkey *secret)
{
Expand All @@ -22,3 +23,23 @@ bool secret_eq_consttime(const struct secret *a, const struct secret *b)
result |= a->data[i] ^ b->data[i];
return result == 0;
}

void towire_privkey(u8 **pptr, const struct privkey *privkey)
{
towire_secret(pptr, &privkey->secret);
}

void towire_secret(u8 **pptr, const struct secret *secret)
{
towire(pptr, secret->data, sizeof(secret->data));
}

void fromwire_secret(const u8 **cursor, size_t *max, struct secret *secret)
{
fromwire(cursor, max, secret->data, sizeof(secret->data));
}

void fromwire_privkey(const u8 **cursor, size_t *max, struct privkey *privkey)
{
fromwire_secret(cursor, max, &privkey->secret);
}
7 changes: 7 additions & 0 deletions bitcoin/privkey.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,11 @@ bool secret_eq_consttime(const struct secret *a, const struct secret *b);
struct privkey {
struct secret secret;
};

/* marshal/unmarshal functions */
void fromwire_secret(const u8 **cursor, size_t *max, struct secret *secret);
void fromwire_privkey(const u8 **cursor, size_t *max, struct privkey *privkey);
void towire_privkey(u8 **pptr, const struct privkey *privkey);
void towire_secret(u8 **pptr, const struct secret *secret);

#endif /* LIGHTNING_BITCOIN_PRIVKEY_H */
30 changes: 30 additions & 0 deletions bitcoin/pubkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
#include <ccan/str/hex/hex.h>
#include <common/type_to_string.h>
#include <common/utils.h>
#include <wire/wire.h>

#ifndef SUPERVERBOSE
#define SUPERVERBOSE(...)
#endif

bool pubkey_from_der(const u8 *der, size_t len, struct pubkey *key)
{
Expand Down Expand Up @@ -95,3 +100,28 @@ void pubkey_to_hash160(const struct pubkey *pk, struct ripemd160 *hash)
sha256(&h, der, sizeof(der));
ripemd160(hash, h.u.u8, sizeof(h));
}

void fromwire_pubkey(const u8 **cursor, size_t *max, struct pubkey *pubkey)
{
u8 der[PUBKEY_CMPR_LEN];

if (!fromwire(cursor, max, der, sizeof(der)))
return;

if (!pubkey_from_der(der, sizeof(der), pubkey)) {
SUPERVERBOSE("not a valid point");
fromwire_fail(cursor, max);
}
}

void towire_pubkey(u8 **pptr, const struct pubkey *pubkey)
{
u8 output[PUBKEY_CMPR_LEN];
size_t outputlen = sizeof(output);

secp256k1_ec_pubkey_serialize(secp256k1_ctx, output, &outputlen,
&pubkey->pubkey,
SECP256K1_EC_COMPRESSED);

towire(pptr, output, outputlen);
}
5 changes: 5 additions & 0 deletions bitcoin/pubkey.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,9 @@ static inline int pubkey_idx(const struct pubkey *id1, const struct pubkey *id2)
* pubkey_to_hash160 - Get the hash for p2pkh payments for a given pubkey
*/
void pubkey_to_hash160(const struct pubkey *pk, struct ripemd160 *hash);

/* marshal/unmarshal functions */
void towire_pubkey(u8 **pptr, const struct pubkey *pubkey);
void fromwire_pubkey(const u8 **cursor, size_t *max, struct pubkey *pubkey);

#endif /* LIGHTNING_BITCOIN_PUBKEY_H */
12 changes: 12 additions & 0 deletions bitcoin/shadouble.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "shadouble.h"
#include <ccan/mem/mem.h>
#include <common/type_to_string.h>
#include <wire/wire.h>

void sha256_double(struct sha256_double *shadouble, const void *p, size_t len)
{
Expand All @@ -14,3 +15,14 @@ void sha256_double_done(struct sha256_ctx *shactx, struct sha256_double *res)
sha256(&res->sha, &res->sha, sizeof(res->sha));
}
REGISTER_TYPE_TO_HEXSTR(sha256_double);

void towire_sha256_double(u8 **pptr, const struct sha256_double *sha256d)
{
towire_sha256(pptr, &sha256d->sha);
}

void fromwire_sha256_double(const u8 **cursor, size_t *max,
struct sha256_double *sha256d)
{
fromwire_sha256(cursor, max, &sha256d->sha);
}
6 changes: 6 additions & 0 deletions bitcoin/shadouble.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define LIGHTNING_BITCOIN_SHADOUBLE_H
#include "config.h"
#include <ccan/crypto/sha256/sha256.h>
#include <ccan/short_types/short_types.h>

/* To explicitly distinguish between single sha and bitcoin's standard double */
struct sha256_double {
Expand All @@ -11,4 +12,9 @@ struct sha256_double {
void sha256_double(struct sha256_double *shadouble, const void *p, size_t len);

void sha256_double_done(struct sha256_ctx *sha256, struct sha256_double *res);

/* marshal/unmarshal functions */
void fromwire_sha256_double(const u8 **cursor, size_t *max,
struct sha256_double *sha256d);
void towire_sha256_double(u8 **pptr, const struct sha256_double *sha256d);
#endif /* LIGHTNING_BITCOIN_SHADOUBLE_H */
13 changes: 6 additions & 7 deletions bitcoin/test/run-bitcoin_block_from_hex.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@ struct amount_sat fromwire_amount_sat(const u8 **cursor UNNEEDED, size_t *max UN
/* Generated stub for fromwire_fail */
const void *fromwire_fail(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_fail called!\n"); abort(); }
/* Generated stub for fromwire_sha256_double */
void fromwire_sha256_double(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
struct sha256_double *sha256d UNNEEDED)
{ fprintf(stderr, "fromwire_sha256_double called!\n"); abort(); }
/* Generated stub for fromwire_sha256 */
void fromwire_sha256(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct sha256 *sha256 UNNEEDED)
{ fprintf(stderr, "fromwire_sha256 called!\n"); abort(); }
/* Generated stub for fromwire_tal_arrn */
u8 *fromwire_tal_arrn(const tal_t *ctx UNNEEDED,
const u8 **cursor UNNEEDED, size_t *max UNNEEDED, size_t num UNNEEDED)
Expand All @@ -45,9 +44,9 @@ u16 fromwire_u16(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
/* Generated stub for towire_amount_sat */
void towire_amount_sat(u8 **pptr UNNEEDED, const struct amount_sat sat UNNEEDED)
{ fprintf(stderr, "towire_amount_sat called!\n"); abort(); }
/* Generated stub for towire_sha256_double */
void towire_sha256_double(u8 **pptr UNNEEDED, const struct sha256_double *sha256d UNNEEDED)
{ fprintf(stderr, "towire_sha256_double called!\n"); abort(); }
/* Generated stub for towire_sha256 */
void towire_sha256(u8 **pptr UNNEEDED, const struct sha256 *sha256 UNNEEDED)
{ fprintf(stderr, "towire_sha256 called!\n"); abort(); }
/* Generated stub for towire_u16 */
void towire_u16(u8 **pptr UNNEEDED, u16 v UNNEEDED)
{ fprintf(stderr, "towire_u16 called!\n"); abort(); }
Expand Down
6 changes: 6 additions & 0 deletions bitcoin/test/run-secret_eq_consttime.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
#include <unistd.h>

/* AUTOGENERATED MOCKS START */
/* Generated stub for fromwire */
const u8 *fromwire(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, void *copy UNNEEDED, size_t n UNNEEDED)
{ fprintf(stderr, "fromwire called!\n"); abort(); }
/* Generated stub for towire */
void towire(u8 **pptr UNNEEDED, const void *data UNNEEDED, size_t len UNNEEDED)
{ fprintf(stderr, "towire called!\n"); abort(); }
/* AUTOGENERATED MOCKS END */
static bool verbose = false;

Expand Down
13 changes: 6 additions & 7 deletions bitcoin/test/run-tx-encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ struct amount_sat fromwire_amount_sat(const u8 **cursor UNNEEDED, size_t *max UN
/* Generated stub for fromwire_fail */
const void *fromwire_fail(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_fail called!\n"); abort(); }
/* Generated stub for fromwire_sha256_double */
void fromwire_sha256_double(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
struct sha256_double *sha256d UNNEEDED)
{ fprintf(stderr, "fromwire_sha256_double called!\n"); abort(); }
/* Generated stub for fromwire_sha256 */
void fromwire_sha256(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct sha256 *sha256 UNNEEDED)
{ fprintf(stderr, "fromwire_sha256 called!\n"); abort(); }
/* Generated stub for fromwire_tal_arrn */
u8 *fromwire_tal_arrn(const tal_t *ctx UNNEEDED,
const u8 **cursor UNNEEDED, size_t *max UNNEEDED, size_t num UNNEEDED)
Expand All @@ -46,9 +45,9 @@ u16 fromwire_u16(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
/* Generated stub for towire_amount_sat */
void towire_amount_sat(u8 **pptr UNNEEDED, const struct amount_sat sat UNNEEDED)
{ fprintf(stderr, "towire_amount_sat called!\n"); abort(); }
/* Generated stub for towire_sha256_double */
void towire_sha256_double(u8 **pptr UNNEEDED, const struct sha256_double *sha256d UNNEEDED)
{ fprintf(stderr, "towire_sha256_double called!\n"); abort(); }
/* Generated stub for towire_sha256 */
void towire_sha256(u8 **pptr UNNEEDED, const struct sha256 *sha256 UNNEEDED)
{ fprintf(stderr, "towire_sha256 called!\n"); abort(); }
/* Generated stub for towire_u16 */
void towire_u16(u8 **pptr UNNEEDED, u16 v UNNEEDED)
{ fprintf(stderr, "towire_u16 called!\n"); abort(); }
Expand Down
1 change: 1 addition & 0 deletions bitcoin/tx.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <assert.h>
#include <bitcoin/block.h>
#include <bitcoin/chainparams.h>
#include <bitcoin/pullpush.h>
#include <bitcoin/tx.h>
#include <ccan/cast/cast.h>
Expand Down
18 changes: 12 additions & 6 deletions channeld/test/run-commit_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,31 @@ static bool print_superverbose;
/*#define DEBUG */

/* AUTOGENERATED MOCKS START */
/* Generated stub for bigsize_get */
size_t bigsize_get(const u8 *p UNNEEDED, size_t max UNNEEDED, bigsize_t *val UNNEEDED)
{ fprintf(stderr, "bigsize_get called!\n"); abort(); }
/* Generated stub for bigsize_put */
size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN] UNNEEDED, bigsize_t v UNNEEDED)
{ fprintf(stderr, "bigsize_put called!\n"); abort(); }
/* Generated stub for fromwire_bigsize */
bigsize_t fromwire_bigsize(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_bigsize called!\n"); abort(); }
/* Generated stub for fromwire_channel_id */
void fromwire_channel_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
struct channel_id *channel_id UNNEEDED)
{ fprintf(stderr, "fromwire_channel_id called!\n"); abort(); }
/* Generated stub for fromwire_node_id */
void fromwire_node_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct node_id *id UNNEEDED)
{ fprintf(stderr, "fromwire_node_id called!\n"); abort(); }
/* Generated stub for status_fmt */
void status_fmt(enum log_level level UNNEEDED,
const struct node_id *peer UNNEEDED,
const char *fmt UNNEEDED, ...)

{ fprintf(stderr, "status_fmt called!\n"); abort(); }
/* Generated stub for towire_bigsize */
void towire_bigsize(u8 **pptr UNNEEDED, const bigsize_t val UNNEEDED)
{ fprintf(stderr, "towire_bigsize called!\n"); abort(); }
/* Generated stub for towire_channel_id */
void towire_channel_id(u8 **pptr UNNEEDED, const struct channel_id *channel_id UNNEEDED)
{ fprintf(stderr, "towire_channel_id called!\n"); abort(); }
/* Generated stub for towire_node_id */
void towire_node_id(u8 **pptr UNNEEDED, const struct node_id *id UNNEEDED)
{ fprintf(stderr, "towire_node_id called!\n"); abort(); }
/* AUTOGENERATED MOCKS END */

/* bitcoind loves its backwards txids! */
Expand Down
18 changes: 12 additions & 6 deletions channeld/test/run-full_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
#include <wally_core.h>

/* AUTOGENERATED MOCKS START */
/* Generated stub for bigsize_get */
size_t bigsize_get(const u8 *p UNNEEDED, size_t max UNNEEDED, bigsize_t *val UNNEEDED)
{ fprintf(stderr, "bigsize_get called!\n"); abort(); }
/* Generated stub for bigsize_put */
size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN] UNNEEDED, bigsize_t v UNNEEDED)
{ fprintf(stderr, "bigsize_put called!\n"); abort(); }
/* Generated stub for fromwire_bigsize */
bigsize_t fromwire_bigsize(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_bigsize called!\n"); abort(); }
/* Generated stub for fromwire_channel_id */
void fromwire_channel_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
struct channel_id *channel_id UNNEEDED)
{ fprintf(stderr, "fromwire_channel_id called!\n"); abort(); }
/* Generated stub for fromwire_node_id */
void fromwire_node_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct node_id *id UNNEEDED)
{ fprintf(stderr, "fromwire_node_id called!\n"); abort(); }
/* Generated stub for memleak_add_helper_ */
void memleak_add_helper_(const tal_t *p UNNEEDED, void (*cb)(struct htable *memtable UNNEEDED,
const tal_t *)){ }
Expand All @@ -36,9 +36,15 @@ void memleak_remove_htable(struct htable *memtable UNNEEDED, const struct htable
void status_failed(enum status_failreason code UNNEEDED,
const char *fmt UNNEEDED, ...)
{ fprintf(stderr, "status_failed called!\n"); abort(); }
/* Generated stub for towire_bigsize */
void towire_bigsize(u8 **pptr UNNEEDED, const bigsize_t val UNNEEDED)
{ fprintf(stderr, "towire_bigsize called!\n"); abort(); }
/* Generated stub for towire_channel_id */
void towire_channel_id(u8 **pptr UNNEEDED, const struct channel_id *channel_id UNNEEDED)
{ fprintf(stderr, "towire_channel_id called!\n"); abort(); }
/* Generated stub for towire_node_id */
void towire_node_id(u8 **pptr UNNEEDED, const struct node_id *id UNNEEDED)
{ fprintf(stderr, "towire_node_id called!\n"); abort(); }
/* AUTOGENERATED MOCKS END */

void status_fmt(enum log_level level UNUSED,
Expand Down
19 changes: 13 additions & 6 deletions cli/test/run-large-input.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <common/bigsize.h>
#include <common/channel_id.h>
#include <common/json_stream.h>
#include <common/node_id.h>
#include <common/wireaddr.h>
#include <fcntl.h>
#include <sys/socket.h>
Expand Down Expand Up @@ -51,22 +52,22 @@ bool amount_sat_eq(struct amount_sat a UNNEEDED, struct amount_sat b UNNEEDED)
struct amount_sat a UNNEEDED,
struct amount_sat b UNNEEDED)
{ fprintf(stderr, "amount_sat_sub called!\n"); abort(); }
/* Generated stub for bigsize_get */
size_t bigsize_get(const u8 *p UNNEEDED, size_t max UNNEEDED, bigsize_t *val UNNEEDED)
{ fprintf(stderr, "bigsize_get called!\n"); abort(); }
/* Generated stub for bigsize_put */
size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN] UNNEEDED, bigsize_t v UNNEEDED)
{ fprintf(stderr, "bigsize_put called!\n"); abort(); }
/* Generated stub for fromwire_amount_msat */
struct amount_msat fromwire_amount_msat(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_amount_msat called!\n"); abort(); }
/* Generated stub for fromwire_amount_sat */
struct amount_sat fromwire_amount_sat(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_amount_sat called!\n"); abort(); }
/* Generated stub for fromwire_bigsize */
bigsize_t fromwire_bigsize(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_bigsize called!\n"); abort(); }
/* Generated stub for fromwire_channel_id */
void fromwire_channel_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
struct channel_id *channel_id UNNEEDED)
{ fprintf(stderr, "fromwire_channel_id called!\n"); abort(); }
/* Generated stub for fromwire_node_id */
void fromwire_node_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct node_id *id UNNEEDED)
{ fprintf(stderr, "fromwire_node_id called!\n"); abort(); }
/* Generated stub for json_add_member */
void json_add_member(struct json_stream *js UNNEEDED,
const char *fieldname UNNEEDED,
Expand Down Expand Up @@ -95,9 +96,15 @@ void towire_amount_msat(u8 **pptr UNNEEDED, const struct amount_msat msat UNNEED
/* Generated stub for towire_amount_sat */
void towire_amount_sat(u8 **pptr UNNEEDED, const struct amount_sat sat UNNEEDED)
{ fprintf(stderr, "towire_amount_sat called!\n"); abort(); }
/* Generated stub for towire_bigsize */
void towire_bigsize(u8 **pptr UNNEEDED, const bigsize_t val UNNEEDED)
{ fprintf(stderr, "towire_bigsize called!\n"); abort(); }
/* Generated stub for towire_channel_id */
void towire_channel_id(u8 **pptr UNNEEDED, const struct channel_id *channel_id UNNEEDED)
{ fprintf(stderr, "towire_channel_id called!\n"); abort(); }
/* Generated stub for towire_node_id */
void towire_node_id(u8 **pptr UNNEEDED, const struct node_id *id UNNEEDED)
{ fprintf(stderr, "towire_node_id called!\n"); abort(); }
/* Generated stub for version_and_exit */
char *version_and_exit(const void *unused UNNEEDED)
{ fprintf(stderr, "version_and_exit called!\n"); abort(); }
Expand Down
Loading

0 comments on commit cfb320c

Please sign in to comment.