Browse Source

bitcoin: create new wrapper type bitcoin_txid, log backward endianness.

It's just a sha256_double, but importantly when we convert it to a
string (in type_to_string, which is used in logging) we use
bitcoin_txid_to_hex() so it's reversed as people expect.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 7 years ago
parent
commit
0237e0b28c
  1. 10
      bitcoin/block.c
  2. 3
      bitcoin/test/run-tx-encode.c
  3. 27
      bitcoin/tx.c
  4. 12
      bitcoin/tx.h
  5. 2
      channeld/channel.c
  6. 2
      channeld/channel_wire.csv
  7. 2
      channeld/commit_tx.c
  8. 3
      channeld/commit_tx.h
  9. 4
      channeld/full_channel.c
  10. 2
      channeld/full_channel.h
  11. 24
      channeld/test/run-full_channel.c
  12. 4
      closingd/closing.c
  13. 2
      closingd/closing_wire.csv
  14. 2
      common/close_tx.c
  15. 3
      common/close_tx.h
  16. 1
      common/funding_tx.h
  17. 6
      common/htlc_tx.c
  18. 5
      common/htlc_tx.h
  19. 2
      common/initial_channel.c
  20. 4
      common/initial_channel.h
  21. 2
      common/initial_commit_tx.c
  22. 3
      common/initial_commit_tx.h
  23. 1
      common/type_to_string.h
  24. 4
      common/utxo.c
  25. 4
      common/utxo.h
  26. 18
      lightningd/chaintopology.c
  27. 8
      lightningd/chaintopology.h
  28. 2
      lightningd/jsonrpc.c
  29. 4
      lightningd/jsonrpc.h
  30. 20
      lightningd/peer_control.c
  31. 2
      lightningd/peer_control.h
  32. 26
      lightningd/test/run-commit_tx.c
  33. 27
      lightningd/watch.c
  34. 18
      lightningd/watch.h
  35. 42
      onchaind/onchain.c
  36. 6
      onchaind/onchain_wire.csv
  37. 6
      openingd/opening.c
  38. 4
      openingd/opening_wire.csv
  39. 2
      tools/generate-wire.py
  40. 2
      wallet/test/run-wallet.c
  41. 6
      wallet/wallet.c
  42. 2
      wallet/wallet.h
  43. 9
      wire/fromwire.c
  44. 2
      wire/test/run-peer-wire.c
  45. 6
      wire/towire.c
  46. 6
      wire/wire.h

10
bitcoin/block.c

@ -41,11 +41,17 @@ struct bitcoin_block *bitcoin_block_from_hex(const tal_t *ctx,
bool bitcoin_blkid_from_hex(const char *hexstr, size_t hexstr_len, bool bitcoin_blkid_from_hex(const char *hexstr, size_t hexstr_len,
struct sha256_double *blockid) struct sha256_double *blockid)
{ {
return bitcoin_txid_from_hex(hexstr, hexstr_len, blockid); struct bitcoin_txid fake_txid;
if (!bitcoin_txid_from_hex(hexstr, hexstr_len, &fake_txid))
return false;
*blockid = fake_txid.shad;
return true;
} }
bool bitcoin_blkid_to_hex(const struct sha256_double *blockid, bool bitcoin_blkid_to_hex(const struct sha256_double *blockid,
char *hexstr, size_t hexstr_len) char *hexstr, size_t hexstr_len)
{ {
return bitcoin_txid_to_hex(blockid, hexstr, hexstr_len); struct bitcoin_txid fake_txid;
fake_txid.shad = *blockid;
return bitcoin_txid_to_hex(&fake_txid, hexstr, hexstr_len);
} }

3
bitcoin/test/run-tx-encode.c

@ -38,7 +38,8 @@ int main(void)
assert(tal_count(tx->input) == 1); assert(tal_count(tx->input) == 1);
assert(tal_count(tx->output) == 1); assert(tal_count(tx->output) == 1);
reverse_bytes(tx->input[0].txid.sha.u.u8, sizeof(tx->input[0].txid)); reverse_bytes(tx->input[0].txid.shad.sha.u.u8,
sizeof(tx->input[0].txid));
hexeq(&tx->input[0].txid, sizeof(tx->input[0].txid), hexeq(&tx->input[0].txid, sizeof(tx->input[0].txid),
"1db36e1306dfc810ea63f0cf866d475cce4e9261a5e8d1581f0d1dc485f4beb5"); "1db36e1306dfc810ea63f0cf866d475cce4e9261a5e8d1581f0d1dc485f4beb5");
assert(tx->input[0].index == 0); assert(tx->input[0].index == 0);

27
bitcoin/tx.c

@ -276,13 +276,13 @@ size_t measure_tx_cost(const struct bitcoin_tx *tx)
return non_witness_len * 4 + witness_len; return non_witness_len * 4 + witness_len;
} }
void bitcoin_txid(const struct bitcoin_tx *tx, struct sha256_double *txid) void bitcoin_txid(const struct bitcoin_tx *tx, struct bitcoin_txid *txid)
{ {
struct sha256_ctx ctx = SHA256_INIT; struct sha256_ctx ctx = SHA256_INIT;
/* For TXID, we never use extended form. */ /* For TXID, we never use extended form. */
push_tx(tx, push_sha, &ctx, false); push_tx(tx, push_sha, &ctx, false);
sha256_double_done(&ctx, txid); sha256_double_done(&ctx, &txid->shad);
} }
struct bitcoin_tx *bitcoin_tx(const tal_t *ctx, varint_t input_count, struct bitcoin_tx *bitcoin_tx(const tal_t *ctx, varint_t input_count,
@ -336,7 +336,7 @@ static void pull_input(const tal_t *ctx, const u8 **cursor, size_t *max,
struct bitcoin_tx_input *input) struct bitcoin_tx_input *input)
{ {
u64 script_len; u64 script_len;
pull_sha256_double(cursor, max, &input->txid); pull_sha256_double(cursor, max, &input->txid.shad);
input->index = pull_le32(cursor, max); input->index = pull_le32(cursor, max);
script_len = pull_length(cursor, max); script_len = pull_length(cursor, max);
if (script_len) if (script_len)
@ -460,9 +460,7 @@ fail:
return NULL; return NULL;
} }
/* <sigh>. Bitcoind represents hashes as little-endian for RPC. This didn't /* <sigh>. Bitcoind represents hashes as little-endian for RPC. */
* stick for blockids (everyone else uses big-endian, eg. block explorers),
* but it did stick for txids. */
static void reverse_bytes(u8 *arr, size_t len) static void reverse_bytes(u8 *arr, size_t len)
{ {
unsigned int i; unsigned int i;
@ -475,18 +473,18 @@ static void reverse_bytes(u8 *arr, size_t len)
} }
bool bitcoin_txid_from_hex(const char *hexstr, size_t hexstr_len, bool bitcoin_txid_from_hex(const char *hexstr, size_t hexstr_len,
struct sha256_double *txid) struct bitcoin_txid *txid)
{ {
if (!hex_decode(hexstr, hexstr_len, txid, sizeof(*txid))) if (!hex_decode(hexstr, hexstr_len, txid, sizeof(*txid)))
return false; return false;
reverse_bytes(txid->sha.u.u8, sizeof(txid->sha.u.u8)); reverse_bytes(txid->shad.sha.u.u8, sizeof(txid->shad.sha.u.u8));
return true; return true;
} }
bool bitcoin_txid_to_hex(const struct sha256_double *txid, bool bitcoin_txid_to_hex(const struct bitcoin_txid *txid,
char *hexstr, size_t hexstr_len) char *hexstr, size_t hexstr_len)
{ {
struct sha256_double rev = *txid; struct sha256_double rev = txid->shad;
reverse_bytes(rev.sha.u.u8, sizeof(rev.sha.u.u8)); reverse_bytes(rev.sha.u.u8, sizeof(rev.sha.u.u8));
return hex_encode(&rev, sizeof(rev), hexstr, hexstr_len); return hex_encode(&rev, sizeof(rev), hexstr, hexstr_len);
} }
@ -499,4 +497,13 @@ static char *fmt_bitcoin_tx(const tal_t *ctx, const struct bitcoin_tx *tx)
return s; return s;
} }
static char *fmt_bitcoin_txid(const tal_t *ctx, const struct bitcoin_txid *txid)
{
char *hexstr = tal_arr(ctx, char, hex_str_size(sizeof(*txid)));
bitcoin_txid_to_hex(txid, hexstr, hex_str_size(sizeof(*txid)));
return hexstr;
}
REGISTER_TYPE_TO_STRING(bitcoin_tx, fmt_bitcoin_tx); REGISTER_TYPE_TO_STRING(bitcoin_tx, fmt_bitcoin_tx);
REGISTER_TYPE_TO_STRING(bitcoin_txid, fmt_bitcoin_txid);

12
bitcoin/tx.h

@ -7,6 +7,10 @@
#include <ccan/short_types/short_types.h> #include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h> #include <ccan/tal/tal.h>
struct bitcoin_txid {
struct sha256_double shad;
};
struct bitcoin_tx { struct bitcoin_tx {
u32 version; u32 version;
struct bitcoin_tx_input *input; struct bitcoin_tx_input *input;
@ -20,7 +24,7 @@ struct bitcoin_tx_output {
}; };
struct bitcoin_tx_input { struct bitcoin_tx_input {
struct sha256_double txid; struct bitcoin_txid txid;
u32 index; /* output number referred to by above */ u32 index; /* output number referred to by above */
u8 *script; u8 *script;
u32 sequence_number; u32 sequence_number;
@ -34,7 +38,7 @@ struct bitcoin_tx_input {
/* SHA256^2 the tx: simpler than sha256_tx */ /* SHA256^2 the tx: simpler than sha256_tx */
void bitcoin_txid(const struct bitcoin_tx *tx, struct sha256_double *txid); void bitcoin_txid(const struct bitcoin_tx *tx, struct bitcoin_txid *txid);
/* Useful for signature code. */ /* Useful for signature code. */
void sha256_tx_for_sig(struct sha256_double *h, const struct bitcoin_tx *tx, void sha256_tx_for_sig(struct sha256_double *h, const struct bitcoin_tx *tx,
@ -57,10 +61,10 @@ struct bitcoin_tx *bitcoin_tx_from_hex(const tal_t *ctx, const char *hex,
/* Parse hex string to get txid (reversed, a-la bitcoind). */ /* Parse hex string to get txid (reversed, a-la bitcoind). */
bool bitcoin_txid_from_hex(const char *hexstr, size_t hexstr_len, bool bitcoin_txid_from_hex(const char *hexstr, size_t hexstr_len,
struct sha256_double *txid); struct bitcoin_txid *txid);
/* Get hex string of txid (reversed, a-la bitcoind). */ /* Get hex string of txid (reversed, a-la bitcoind). */
bool bitcoin_txid_to_hex(const struct sha256_double *txid, bool bitcoin_txid_to_hex(const struct bitcoin_txid *txid,
char *hexstr, size_t hexstr_len); char *hexstr, size_t hexstr_len);
/* Internal de-linearization functions. */ /* Internal de-linearization functions. */

2
channeld/channel.c

@ -2351,7 +2351,7 @@ static void init_channel(struct peer *peer)
u16 funding_txout; u16 funding_txout;
u64 local_msatoshi; u64 local_msatoshi;
struct pubkey funding_pubkey[NUM_SIDES]; struct pubkey funding_pubkey[NUM_SIDES];
struct sha256_double funding_txid; struct bitcoin_txid funding_txid;
enum side funder; enum side funder;
enum htlc_state *hstates; enum htlc_state *hstates;
struct fulfilled_htlc *fulfilled; struct fulfilled_htlc *fulfilled;

2
channeld/channel_wire.csv

@ -7,7 +7,7 @@ channel_normal_operation,11001
# Begin! (passes gossipd-client fd) # Begin! (passes gossipd-client fd)
channel_init,1000 channel_init,1000
channel_init,,chain_hash,struct sha256_double channel_init,,chain_hash,struct sha256_double
channel_init,,funding_txid,struct sha256_double channel_init,,funding_txid,struct bitcoin_txid
channel_init,,funding_txout,u16 channel_init,,funding_txout,u16
channel_init,,funding_satoshi,u64 channel_init,,funding_satoshi,u64
channel_init,,our_config,struct channel_config channel_init,,our_config,struct channel_config

Can't render this file because it has a wrong number of fields in line 2.

2
channeld/commit_tx.c

@ -86,7 +86,7 @@ static void add_received_htlc_out(struct bitcoin_tx *tx, size_t n,
} }
struct bitcoin_tx *commit_tx(const tal_t *ctx, struct bitcoin_tx *commit_tx(const tal_t *ctx,
const struct sha256_double *funding_txid, const struct bitcoin_txid *funding_txid,
unsigned int funding_txout, unsigned int funding_txout,
u64 funding_satoshis, u64 funding_satoshis,
enum side funder, enum side funder,

3
channeld/commit_tx.h

@ -7,7 +7,6 @@
#include <common/initial_commit_tx.h> #include <common/initial_commit_tx.h>
struct keyset; struct keyset;
struct sha256_double;
/** /**
* commit_tx_num_untrimmed: how many of these htlc outputs will commit tx have? * commit_tx_num_untrimmed: how many of these htlc outputs will commit tx have?
@ -43,7 +42,7 @@ size_t commit_tx_num_untrimmed(const struct htlc **htlcs,
* transaction, so we carefully use the terms "self" and "other" here. * transaction, so we carefully use the terms "self" and "other" here.
*/ */
struct bitcoin_tx *commit_tx(const tal_t *ctx, struct bitcoin_tx *commit_tx(const tal_t *ctx,
const struct sha256_double *funding_txid, const struct bitcoin_txid *funding_txid,
unsigned int funding_txout, unsigned int funding_txout,
u64 funding_satoshis, u64 funding_satoshis,
enum side funder, enum side funder,

4
channeld/full_channel.c

@ -20,7 +20,7 @@
#include <string.h> #include <string.h>
struct channel *new_channel(const tal_t *ctx, struct channel *new_channel(const tal_t *ctx,
const struct sha256_double *funding_txid, const struct bitcoin_txid *funding_txid,
unsigned int funding_txout, unsigned int funding_txout,
u64 funding_satoshis, u64 funding_satoshis,
u64 local_msatoshi, u64 local_msatoshi,
@ -178,7 +178,7 @@ static void add_htlcs(struct bitcoin_tx ***txs,
enum side side) enum side side)
{ {
size_t i, n; size_t i, n;
struct sha256_double txid; struct bitcoin_txid txid;
u32 feerate_per_kw = channel->view[side].feerate_per_kw; u32 feerate_per_kw = channel->view[side].feerate_per_kw;
/* Get txid of commitment transaction */ /* Get txid of commitment transaction */

2
channeld/full_channel.h

@ -26,7 +26,7 @@
* Returns state, or NULL if malformed. * Returns state, or NULL if malformed.
*/ */
struct channel *new_channel(const tal_t *ctx, struct channel *new_channel(const tal_t *ctx,
const struct sha256_double *funding_txid, const struct bitcoin_txid *funding_txid,
unsigned int funding_txout, unsigned int funding_txout,
u64 funding_satoshis, u64 funding_satoshis,
u64 local_msatoshi, u64 local_msatoshi,

24
channeld/test/run-full_channel.c

@ -20,26 +20,14 @@
const void *trc; const void *trc;
static struct sha256 sha256_from_hex(const char *hex)
{
struct sha256 sha256;
if (strstarts(hex, "0x"))
hex += 2;
if (!hex_decode(hex, strlen(hex), &sha256, sizeof(sha256)))
abort();
return sha256;
}
/* bitcoind loves its backwards txids! */ /* bitcoind loves its backwards txids! */
static struct sha256_double txid_from_hex(const char *hex) static struct bitcoin_txid txid_from_hex(const char *hex)
{ {
struct sha256_double sha256; struct bitcoin_txid txid;
struct sha256 rev = sha256_from_hex(hex);
size_t i;
for (i = 0; i < sizeof(rev); i++) if (!bitcoin_txid_from_hex(hex, strlen(hex), &txid))
sha256.sha.u.u8[sizeof(sha256) - 1 - i] = rev.u.u8[i]; abort();
return sha256; return txid;
} }
static struct bitcoin_tx *tx_from_hex(const tal_t *ctx, const char *hex) static struct bitcoin_tx *tx_from_hex(const tal_t *ctx, const char *hex)
@ -326,7 +314,7 @@ static void update_feerate(struct channel *channel, u32 feerate)
int main(void) int main(void)
{ {
tal_t *tmpctx = tal_tmpctx(NULL); tal_t *tmpctx = tal_tmpctx(NULL);
struct sha256_double funding_txid; struct bitcoin_txid funding_txid;
/* We test from both sides. */ /* We test from both sides. */
struct channel *lchannel, *rchannel; struct channel *lchannel, *rchannel;
u64 funding_amount_satoshi; u64 funding_amount_satoshi;

4
closingd/closing.c

@ -27,7 +27,7 @@ static struct bitcoin_tx *close_tx(const tal_t *ctx,
struct crypto_state *cs, struct crypto_state *cs,
const struct channel_id *channel_id, const struct channel_id *channel_id,
u8 *scriptpubkey[NUM_SIDES], u8 *scriptpubkey[NUM_SIDES],
const struct sha256_double *funding_txid, const struct bitcoin_txid *funding_txid,
unsigned int funding_txout, unsigned int funding_txout,
u64 funding_satoshi, u64 funding_satoshi,
const u64 satoshi_out[NUM_SIDES], const u64 satoshi_out[NUM_SIDES],
@ -155,7 +155,7 @@ int main(int argc, char *argv[])
u8 *msg; u8 *msg;
struct privkey seed; struct privkey seed;
struct pubkey funding_pubkey[NUM_SIDES]; struct pubkey funding_pubkey[NUM_SIDES];
struct sha256_double funding_txid; struct bitcoin_txid funding_txid;
u16 funding_txout; u16 funding_txout;
u64 funding_satoshi, satoshi_out[NUM_SIDES]; u64 funding_satoshi, satoshi_out[NUM_SIDES];
u64 our_dust_limit; u64 our_dust_limit;

2
closingd/closing_wire.csv

@ -5,7 +5,7 @@ closing_init,2001
closing_init,,crypto_state,struct crypto_state closing_init,,crypto_state,struct crypto_state
closing_init,,gossip_index,u64 closing_init,,gossip_index,u64
closing_init,,seed,struct privkey closing_init,,seed,struct privkey
closing_init,,funding_txid,struct sha256_double closing_init,,funding_txid,struct bitcoin_txid
closing_init,,funding_txout,u16 closing_init,,funding_txout,u16
closing_init,,funding_satoshi,u64 closing_init,,funding_satoshi,u64
closing_init,,remote_fundingkey,struct pubkey closing_init,,remote_fundingkey,struct pubkey

Can't render this file because it has a wrong number of fields in line 3.

2
common/close_tx.c

@ -7,7 +7,7 @@
struct bitcoin_tx *create_close_tx(const tal_t *ctx, struct bitcoin_tx *create_close_tx(const tal_t *ctx,
const u8 *our_script, const u8 *our_script,
const u8 *their_script, const u8 *their_script,
const struct sha256_double *anchor_txid, const struct bitcoin_txid *anchor_txid,
unsigned int anchor_index, unsigned int anchor_index,
u64 anchor_satoshis, u64 anchor_satoshis,
uint64_t to_us, uint64_t to_them, uint64_t to_us, uint64_t to_them,

3
common/close_tx.h

@ -4,7 +4,6 @@
#include <ccan/short_types/short_types.h> #include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h> #include <ccan/tal/tal.h>
struct sha256_double;
struct pubkey; struct pubkey;
/* Create close tx to spend the anchor tx output; doesn't fill in /* Create close tx to spend the anchor tx output; doesn't fill in
@ -12,7 +11,7 @@ struct pubkey;
struct bitcoin_tx *create_close_tx(const tal_t *ctx, struct bitcoin_tx *create_close_tx(const tal_t *ctx,
const u8 *our_script, const u8 *our_script,
const u8 *their_script, const u8 *their_script,
const struct sha256_double *anchor_txid, const struct bitcoin_txid *anchor_txid,
unsigned int anchor_index, unsigned int anchor_index,
u64 anchor_satoshis, u64 anchor_satoshis,
uint64_t to_us, uint64_t to_them, uint64_t to_us, uint64_t to_them,

1
common/funding_tx.h

@ -8,7 +8,6 @@ struct bitcoin_tx;
struct ext_key; struct ext_key;
struct privkey; struct privkey;
struct pubkey; struct pubkey;
struct sha256_double;
struct utxo; struct utxo;
/** /**

6
common/htlc_tx.c

@ -5,7 +5,7 @@
#include <common/keyset.h> #include <common/keyset.h>
static struct bitcoin_tx *htlc_tx(const tal_t *ctx, static struct bitcoin_tx *htlc_tx(const tal_t *ctx,
const struct sha256_double *commit_txid, const struct bitcoin_txid *commit_txid,
unsigned int commit_output_number, unsigned int commit_output_number,
u64 msatoshi, u64 msatoshi,
u16 to_self_delay, u16 to_self_delay,
@ -72,7 +72,7 @@ static struct bitcoin_tx *htlc_tx(const tal_t *ctx,
} }
struct bitcoin_tx *htlc_success_tx(const tal_t *ctx, struct bitcoin_tx *htlc_success_tx(const tal_t *ctx,
const struct sha256_double *commit_txid, const struct bitcoin_txid *commit_txid,
unsigned int commit_output_number, unsigned int commit_output_number,
u64 htlc_msatoshi, u64 htlc_msatoshi,
u16 to_self_delay, u16 to_self_delay,
@ -117,7 +117,7 @@ void htlc_success_tx_add_witness(struct bitcoin_tx *htlc_success,
} }
struct bitcoin_tx *htlc_timeout_tx(const tal_t *ctx, struct bitcoin_tx *htlc_timeout_tx(const tal_t *ctx,
const struct sha256_double *commit_txid, const struct bitcoin_txid *commit_txid,
unsigned int commit_output_number, unsigned int commit_output_number,
u64 htlc_msatoshi, u64 htlc_msatoshi,
u32 cltv_expiry, u32 cltv_expiry,

5
common/htlc_tx.h

@ -6,7 +6,6 @@
struct keyset; struct keyset;
struct preimage; struct preimage;
struct pubkey; struct pubkey;
struct sha256_double;
static inline u64 htlc_timeout_fee(u32 feerate_per_kw) static inline u64 htlc_timeout_fee(u32 feerate_per_kw)
{ {
@ -35,7 +34,7 @@ static inline u64 htlc_success_fee(u32 feerate_per_kw)
/* Create HTLC-success tx to spend a received HTLC commitment tx /* Create HTLC-success tx to spend a received HTLC commitment tx
* output; doesn't fill in input witness. */ * output; doesn't fill in input witness. */
struct bitcoin_tx *htlc_success_tx(const tal_t *ctx, struct bitcoin_tx *htlc_success_tx(const tal_t *ctx,
const struct sha256_double *commit_txid, const struct bitcoin_txid *commit_txid,
unsigned int commit_output_number, unsigned int commit_output_number,
u64 htlc_msatoshi, u64 htlc_msatoshi,
u16 to_self_delay, u16 to_self_delay,
@ -55,7 +54,7 @@ void htlc_success_tx_add_witness(struct bitcoin_tx *htlc_success,
/* Create HTLC-timeout tx to spend an offered HTLC commitment tx /* Create HTLC-timeout tx to spend an offered HTLC commitment tx
* output; doesn't fill in input witness. */ * output; doesn't fill in input witness. */
struct bitcoin_tx *htlc_timeout_tx(const tal_t *ctx, struct bitcoin_tx *htlc_timeout_tx(const tal_t *ctx,
const struct sha256_double *commit_txid, const struct bitcoin_txid *commit_txid,
unsigned int commit_output_number, unsigned int commit_output_number,
u64 htlc_msatoshi, u64 htlc_msatoshi,
u32 cltv_expiry, u32 cltv_expiry,

2
common/initial_channel.c

@ -8,7 +8,7 @@
#include <inttypes.h> #include <inttypes.h>
struct channel *new_initial_channel(const tal_t *ctx, struct channel *new_initial_channel(const tal_t *ctx,
const struct sha256_double *funding_txid, const struct bitcoin_txid *funding_txid,
unsigned int funding_txout, unsigned int funding_txout,
u64 funding_satoshis, u64 funding_satoshis,
u64 local_msatoshi, u64 local_msatoshi,

4
common/initial_channel.h

@ -28,7 +28,7 @@ struct channel_view {
struct channel { struct channel {
/* Funding txid and output. */ /* Funding txid and output. */
struct sha256_double funding_txid; struct bitcoin_txid funding_txid;
unsigned int funding_txout; unsigned int funding_txout;
/* Keys used to spend funding tx. */ /* Keys used to spend funding tx. */
@ -142,7 +142,7 @@ static inline u16 to_self_delay(const struct channel *channel, enum side side)
* Returns channel, or NULL if malformed. * Returns channel, or NULL if malformed.
*/ */
struct channel *new_initial_channel(const tal_t *ctx, struct channel *new_initial_channel(const tal_t *ctx,
const struct sha256_double *funding_txid, const struct bitcoin_txid *funding_txid,
unsigned int funding_txout, unsigned int funding_txout,
u64 funding_satoshis, u64 funding_satoshis,
u64 local_msatoshi, u64 local_msatoshi,

2
common/initial_commit_tx.c

@ -55,7 +55,7 @@ u8 *to_self_wscript(const tal_t *ctx,
} }
struct bitcoin_tx *initial_commit_tx(const tal_t *ctx, struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
const struct sha256_double *funding_txid, const struct bitcoin_txid *funding_txid,
unsigned int funding_txout, unsigned int funding_txout,
u64 funding_satoshis, u64 funding_satoshis,
enum side funder, enum side funder,

3
common/initial_commit_tx.h

@ -6,7 +6,6 @@
#include <common/htlc.h> #include <common/htlc.h>
struct keyset; struct keyset;
struct sha256_double;
/* BOLT #3: /* BOLT #3:
* *
@ -67,7 +66,7 @@ static inline u64 commit_tx_base_fee(u32 feerate_per_kw,
* transaction, so we carefully use the terms "self" and "other" here. * transaction, so we carefully use the terms "self" and "other" here.
*/ */
struct bitcoin_tx *initial_commit_tx(const tal_t *ctx, struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
const struct sha256_double *funding_txid, const struct bitcoin_txid *funding_txid,
unsigned int funding_txout, unsigned int funding_txout,
u64 funding_satoshis, u64 funding_satoshis,
enum side funder, enum side funder,

1
common/type_to_string.h

@ -9,6 +9,7 @@
union printable_types { union printable_types {
const struct pubkey *pubkey; const struct pubkey *pubkey;
const struct sha256_double *sha256_double; const struct sha256_double *sha256_double;
const struct bitcoin_txid *bitcoin_txid;
const struct sha256 *sha256; const struct sha256 *sha256;
const struct ripemd160 *ripemd160; const struct ripemd160 *ripemd160;
const struct rel_locktime *rel_locktime; const struct rel_locktime *rel_locktime;

4
common/utxo.c

@ -3,7 +3,7 @@
void towire_utxo(u8 **pptr, const struct utxo *utxo) void towire_utxo(u8 **pptr, const struct utxo *utxo)
{ {
towire_sha256_double(pptr, &utxo->txid); towire_bitcoin_txid(pptr, &utxo->txid);
towire_u32(pptr, utxo->outnum); towire_u32(pptr, utxo->outnum);
towire_u64(pptr, utxo->amount); towire_u64(pptr, utxo->amount);
towire_u32(pptr, utxo->keyindex); towire_u32(pptr, utxo->keyindex);
@ -12,7 +12,7 @@ void towire_utxo(u8 **pptr, const struct utxo *utxo)
void fromwire_utxo(const u8 **ptr, size_t *max, struct utxo *utxo) void fromwire_utxo(const u8 **ptr, size_t *max, struct utxo *utxo)
{ {
fromwire_sha256_double(ptr, max, &utxo->txid); fromwire_bitcoin_txid(ptr, max, &utxo->txid);
utxo->outnum = fromwire_u32(ptr, max); utxo->outnum = fromwire_u32(ptr, max);
utxo->amount = fromwire_u64(ptr, max); utxo->amount = fromwire_u64(ptr, max);
utxo->keyindex = fromwire_u32(ptr, max); utxo->keyindex = fromwire_u32(ptr, max);

4
common/utxo.h

@ -1,13 +1,13 @@
#ifndef LIGHTNING_COMMON_UTXO_H #ifndef LIGHTNING_COMMON_UTXO_H
#define LIGHTNING_COMMON_UTXO_H #define LIGHTNING_COMMON_UTXO_H
#include "config.h" #include "config.h"
#include <bitcoin/shadouble.h> #include <bitcoin/tx.h>
#include <ccan/short_types/short_types.h> #include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h> #include <ccan/tal/tal.h>
#include <stdbool.h> #include <stdbool.h>
struct utxo { struct utxo {
struct sha256_double txid; struct bitcoin_txid txid;
u32 outnum; u32 outnum;
u64 amount; u64 amount;
u32 keyindex; u32 keyindex;

18
lightningd/chaintopology.c

@ -43,7 +43,7 @@ static void add_tx_to_block(struct block *b,
} }
static bool we_broadcast(const struct chain_topology *topo, static bool we_broadcast(const struct chain_topology *topo,
const struct sha256_double *txid) const struct bitcoin_txid *txid)
{ {
const struct outgoing_tx *otx; const struct outgoing_tx *otx;
@ -74,7 +74,7 @@ static void connect_block(struct chain_topology *topo,
/* Now we see if any of those txs are interesting. */ /* Now we see if any of those txs are interesting. */
for (i = 0; i < tal_count(b->full_txs); i++) { for (i = 0; i < tal_count(b->full_txs); i++) {
const struct bitcoin_tx *tx = b->full_txs[i]; const struct bitcoin_tx *tx = b->full_txs[i];
struct sha256_double txid; struct bitcoin_txid txid;
size_t j; size_t j;
/* Tell them if it spends a txo we care about. */ /* Tell them if it spends a txo we care about. */
@ -108,12 +108,12 @@ static void connect_block(struct chain_topology *topo,
} }
static const struct bitcoin_tx *tx_in_block(const struct block *b, static const struct bitcoin_tx *tx_in_block(const struct block *b,
const struct sha256_double *txid) const struct bitcoin_txid *txid)
{ {
size_t i, n = tal_count(b->txs); size_t i, n = tal_count(b->txs);
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
struct sha256_double this_txid; struct bitcoin_txid this_txid;
bitcoin_txid(b->txs[i], &this_txid); bitcoin_txid(b->txs[i], &this_txid);
if (structeq(&this_txid, txid)) if (structeq(&this_txid, txid))
return b->txs[i]; return b->txs[i];
@ -123,7 +123,7 @@ static const struct bitcoin_tx *tx_in_block(const struct block *b,
/* FIXME: Use hash table. */ /* FIXME: Use hash table. */
static struct block *block_for_tx(const struct chain_topology *topo, static struct block *block_for_tx(const struct chain_topology *topo,
const struct sha256_double *txid, const struct bitcoin_txid *txid,
const struct bitcoin_tx **tx) const struct bitcoin_tx **tx)
{ {
struct block *b; struct block *b;
@ -141,7 +141,7 @@ static struct block *block_for_tx(const struct chain_topology *topo,
} }
size_t get_tx_depth(const struct chain_topology *topo, size_t get_tx_depth(const struct chain_topology *topo,
const struct sha256_double *txid, const struct bitcoin_txid *txid,
const struct bitcoin_tx **tx) const struct bitcoin_tx **tx)
{ {
const struct block *b; const struct block *b;
@ -274,7 +274,7 @@ void broadcast_tx(struct chain_topology *topo,
tal_add_destructor2(peer, clear_otx_peer, otx); tal_add_destructor2(peer, clear_otx_peer, otx);
log_add(topo->log, " (tx %s)", log_add(topo->log, " (tx %s)",
type_to_string(ltmp, struct sha256_double, &otx->txid)); type_to_string(ltmp, struct bitcoin_txid, &otx->txid));
#if DEVELOPER #if DEVELOPER
if (topo->dev_no_broadcast) { if (topo->dev_no_broadcast) {
@ -543,7 +543,7 @@ u32 get_feerate(const struct chain_topology *topo, enum feerate feerate)
} }
struct txlocator *locate_tx(const void *ctx, const struct chain_topology *topo, struct txlocator *locate_tx(const void *ctx, const struct chain_topology *topo,
const struct sha256_double *txid) const struct bitcoin_txid *txid)
{ {
struct block *block = block_for_tx(topo, txid, NULL); struct block *block = block_for_tx(topo, txid, NULL);
if (block == NULL) { if (block == NULL) {
@ -554,7 +554,7 @@ struct txlocator *locate_tx(const void *ctx, const struct chain_topology *topo,
loc->blkheight = block->height; loc->blkheight = block->height;
size_t i, n = tal_count(block->txs); size_t i, n = tal_count(block->txs);
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
struct sha256_double this_txid; struct bitcoin_txid this_txid;
bitcoin_txid(block->txs[i], &this_txid); bitcoin_txid(block->txs[i], &this_txid);
if (structeq(&this_txid, txid)){ if (structeq(&this_txid, txid)){
loc->index = block->txnums[i]; loc->index = block->txnums[i];

8
lightningd/chaintopology.h

@ -2,7 +2,7 @@
#define LIGHTNING_LIGHTNINGD_CHAINTOPOLOGY_H #define LIGHTNING_LIGHTNINGD_CHAINTOPOLOGY_H
#include "config.h" #include "config.h"
#include <bitcoin/block.h> #include <bitcoin/block.h>
#include <bitcoin/shadouble.h> #include <bitcoin/tx.h>
#include <ccan/list/list.h> #include <ccan/list/list.h>
#include <ccan/short_types/short_types.h> #include <ccan/short_types/short_types.h>
#include <ccan/structeq/structeq.h> #include <ccan/structeq/structeq.h>
@ -31,7 +31,7 @@ struct outgoing_tx {
struct list_node list; struct list_node list;
struct peer *peer; struct peer *peer;
const char *hextx; const char *hextx;
struct sha256_double txid; struct bitcoin_txid txid;
void (*failed)(struct peer *peer, int exitstatus, const char *err); void (*failed)(struct peer *peer, int exitstatus, const char *err);
/* FIXME: Remove this. */ /* FIXME: Remove this. */
struct chain_topology *topo; struct chain_topology *topo;
@ -139,7 +139,7 @@ struct txlocator {
/* This is the number of blocks which would have to be mined to invalidate /* This is the number of blocks which would have to be mined to invalidate
* the tx (optional tx is filled in if return is non-zero). */ * the tx (optional tx is filled in if return is non-zero). */
size_t get_tx_depth(const struct chain_topology *topo, size_t get_tx_depth(const struct chain_topology *topo,
const struct sha256_double *txid, const struct bitcoin_txid *txid,
const struct bitcoin_tx **tx); const struct bitcoin_tx **tx);
/* Get highest block number. */ /* Get highest block number. */
@ -161,7 +161,7 @@ void setup_topology(struct chain_topology *topology,
struct timers *timers, struct timers *timers,
struct timerel poll_time, u32 first_peer_block); struct timerel poll_time, u32 first_peer_block);
struct txlocator *locate_tx(const void *ctx, const struct chain_topology *topo, const struct sha256_double *txid); struct txlocator *locate_tx(const void *ctx, const struct chain_topology *topo, const struct bitcoin_txid *txid);
void notify_new_block(struct lightningd *ld, unsigned int height); void notify_new_block(struct lightningd *ld, unsigned int height);
void notify_feerate_change(struct lightningd *ld); void notify_feerate_change(struct lightningd *ld);

2
lightningd/jsonrpc.c

@ -359,7 +359,7 @@ void json_add_pubkey(struct json_result *response,
} }
void json_add_txid(struct json_result *result, const char *fieldname, void json_add_txid(struct json_result *result, const char *fieldname,
const struct sha256_double *txid) const struct bitcoin_txid *txid)
{ {
char hex[hex_str_size(sizeof(*txid))]; char hex[hex_str_size(sizeof(*txid))];

4
lightningd/jsonrpc.h

@ -5,7 +5,7 @@
#include <ccan/list/list.h> #include <ccan/list/list.h>
#include <common/json.h> #include <common/json.h>
struct sha256_double; struct bitcoin_txid;
struct wireaddr; struct wireaddr;
/* Context for a command (from JSON, but might outlive the connection!) /* Context for a command (from JSON, but might outlive the connection!)
@ -69,7 +69,7 @@ void json_add_pubkey(struct json_result *response,
/* '"fieldname" : <hexrev>' or "<hexrev>" if fieldname is NULL */ /* '"fieldname" : <hexrev>' or "<hexrev>" if fieldname is NULL */
void json_add_txid(struct json_result *result, const char *fieldname, void json_add_txid(struct json_result *result, const char *fieldname,
const struct sha256_double *txid); const struct bitcoin_txid *txid);
/* Extract a pubkey from this */ /* Extract a pubkey from this */
bool json_tok_pubkey(const char *buffer, const jsmntok_t *tok, bool json_tok_pubkey(const char *buffer, const jsmntok_t *tok,

20
lightningd/peer_control.c

@ -1108,7 +1108,7 @@ static enum watch_result onchain_tx_watched(struct peer *peer,
void *unused) void *unused)
{ {
u8 *msg; u8 *msg;
struct sha256_double txid; struct bitcoin_txid txid;
if (depth == 0) { if (depth == 0) {
log_unusual(peer->log, "Chain reorganization!"); log_unusual(peer->log, "Chain reorganization!");
@ -1154,7 +1154,7 @@ static enum watch_result onchain_txo_watched(struct peer *peer,
static void watch_tx_and_outputs(struct peer *peer, static void watch_tx_and_outputs(struct peer *peer,
const struct bitcoin_tx *tx) const struct bitcoin_tx *tx)
{ {
struct sha256_double txid; struct bitcoin_txid txid;
struct txwatch *txw; struct txwatch *txw;
bitcoin_txid(tx, &txid); bitcoin_txid(tx, &txid);
@ -1379,7 +1379,7 @@ static enum watch_result funding_spent(struct peer *peer,
void *unused) void *unused)
{ {
u8 *msg, *scriptpubkey; u8 *msg, *scriptpubkey;
struct sha256_double our_last_txid; struct bitcoin_txid our_last_txid;
s64 keyindex; s64 keyindex;
struct pubkey ourkey; struct pubkey ourkey;
struct htlc_stub *stubs; struct htlc_stub *stubs;
@ -1495,13 +1495,13 @@ static enum watch_result funding_lockin_cb(struct peer *peer,
unsigned int depth, unsigned int depth,
void *unused) void *unused)
{ {
struct sha256_double txid; struct bitcoin_txid txid;
const char *txidstr; const char *txidstr;
struct txlocator *loc; struct txlocator *loc;
bool peer_ready; bool peer_ready;
bitcoin_txid(tx, &txid); bitcoin_txid(tx, &txid);
txidstr = type_to_string(peer, struct sha256_double, &txid); txidstr = type_to_string(peer, struct bitcoin_txid, &txid);
log_debug(peer->log, "Funding tx %s depth %u of %u", log_debug(peer->log, "Funding tx %s depth %u of %u",
txidstr, depth, peer->minimum_depth); txidstr, depth, peer->minimum_depth);
tal_free(txidstr); tal_free(txidstr);
@ -2153,7 +2153,7 @@ static void opening_funder_finished(struct subd *opening, const u8 *resp,
u8 *msg; u8 *msg;
struct channel_info *channel_info; struct channel_info *channel_info;
struct utxo *utxos; struct utxo *utxos;
struct sha256_double funding_txid; struct bitcoin_txid funding_txid;
struct pubkey changekey; struct pubkey changekey;
struct pubkey local_fundingkey; struct pubkey local_fundingkey;
struct crypto_state cs; struct crypto_state cs;
@ -2221,11 +2221,11 @@ static void opening_funder_finished(struct subd *opening, const u8 *resp,
log_debug(fc->peer->log, "%zi: %"PRIu64" satoshi (%s) %s\n", log_debug(fc->peer->log, "%zi: %"PRIu64" satoshi (%s) %s\n",
i, fc->utxomap[i]->amount, i, fc->utxomap[i]->amount,
fc->utxomap[i]->is_p2sh ? "P2SH" : "SEGWIT", fc->utxomap[i]->is_p2sh ? "P2SH" : "SEGWIT",
type_to_string(ltmp, struct sha256_double, type_to_string(ltmp, struct bitcoin_txid,
&fc->funding_tx->input[i].txid)); &fc->funding_tx->input[i].txid));
} }
fc->peer->funding_txid = tal(fc->peer, struct sha256_double); fc->peer->funding_txid = tal(fc->peer, struct bitcoin_txid);
bitcoin_txid(fc->funding_tx, fc->peer->funding_txid); bitcoin_txid(fc->funding_tx, fc->peer->funding_txid);
if (!structeq(fc->peer->funding_txid, &funding_txid)) { if (!structeq(fc->peer->funding_txid, &funding_txid)) {
@ -2292,7 +2292,7 @@ static void opening_fundee_finished(struct subd *opening,
/* This is a new channel_info->their_config, set its ID to 0 */ /* This is a new channel_info->their_config, set its ID to 0 */
peer->channel_info->their_config.id = 0; peer->channel_info->their_config.id = 0;
peer->funding_txid = tal(peer, struct sha256_double); peer->funding_txid = tal(peer, struct bitcoin_txid);
if (!fromwire_opening_fundee_reply(tmpctx, reply, NULL, if (!fromwire_opening_fundee_reply(tmpctx, reply, NULL,
&channel_info->their_config, &channel_info->their_config,
remote_commit, remote_commit,
@ -2334,7 +2334,7 @@ static void opening_fundee_finished(struct subd *opening,
} }
log_debug(peer->log, "Watching funding tx %s", log_debug(peer->log, "Watching funding tx %s",
type_to_string(reply, struct sha256_double, type_to_string(reply, struct bitcoin_txid,
peer->funding_txid)); peer->funding_txid));
watch_txid(peer, peer->ld->topology, peer, peer->funding_txid, watch_txid(peer, peer->ld->topology, peer, peer->funding_txid,
funding_lockin_cb, NULL); funding_lockin_cb, NULL);

2
lightningd/peer_control.h

@ -69,7 +69,7 @@ struct peer {
u64 next_htlc_id; u64 next_htlc_id;
/* Funding txid and amounts (once known) */ /* Funding txid and amounts (once known) */
struct sha256_double *funding_txid; struct bitcoin_txid *funding_txid;
u16 funding_outnum; u16 funding_outnum;
u64 funding_satoshi, push_msat; u64 funding_satoshi, push_msat;
bool remote_funding_locked; bool remote_funding_locked;

26
lightningd/test/run-commit_tx.c

@ -20,26 +20,14 @@ static bool print_superverbose;
/* Turn this on to brute-force fee values */ /* Turn this on to brute-force fee values */
/*#define DEBUG */ /*#define DEBUG */
static struct sha256 sha256_from_hex(const char *hex)
{
struct sha256 sha256;
if (strstarts(hex, "0x"))
hex += 2;
if (!hex_decode(hex, strlen(hex), &sha256, sizeof(sha256)))
abort();
return sha256;
}
/* bitcoind loves its backwards txids! */ /* bitcoind loves its backwards txids! */
static struct sha256_double txid_from_hex(const char *hex) static struct bitcoin_txid txid_from_hex(const char *hex)
{ {
struct sha256_double sha256; struct bitcoin_txid txid;
struct sha256 rev = sha256_from_hex(hex);
size_t i;
for (i = 0; i < sizeof(rev); i++) if (!bitcoin_txid_from_hex(hex, strlen(hex), &txid))
sha256.sha.u.u8[sizeof(sha256) - 1 - i] = rev.u.u8[i]; abort();
return sha256; return txid;
} }
static struct secret secret_from_hex(const char *hex) static struct secret secret_from_hex(const char *hex)
@ -204,7 +192,7 @@ static void report_htlcs(const struct bitcoin_tx *tx,
{ {
tal_t *tmpctx = tal_tmpctx(NULL); tal_t *tmpctx = tal_tmpctx(NULL);
size_t i, n; size_t i, n;
struct sha256_double txid; struct bitcoin_txid txid;
struct bitcoin_tx **htlc_tx; struct bitcoin_tx **htlc_tx;
secp256k1_ecdsa_signature *remotehtlcsig; secp256k1_ecdsa_signature *remotehtlcsig;
struct keyset keyset; struct keyset keyset;
@ -439,7 +427,7 @@ static const struct htlc **invert_htlcs(const struct htlc **htlcs)
int main(void) int main(void)
{ {
tal_t *tmpctx = tal_tmpctx(NULL); tal_t *tmpctx = tal_tmpctx(NULL);
struct sha256_double funding_txid; struct bitcoin_txid funding_txid;
u64 funding_amount_satoshi, dust_limit_satoshi; u64 funding_amount_satoshi, dust_limit_satoshi;
u32 feerate_per_kw; u32 feerate_per_kw;
u16 to_self_delay; u16 to_self_delay;

27
lightningd/watch.c

@ -65,17 +65,18 @@ static void destroy_txowatch(struct txowatch *w)
txowatch_hash_del(&w->topo->txowatches, w); txowatch_hash_del(&w->topo->txowatches, w);
} }
const struct sha256_double *txwatch_keyof(const struct txwatch *w) const struct bitcoin_txid *txwatch_keyof(const struct txwatch *w)
{ {
return &w->txid; return &w->txid;
} }
size_t txid_hash(const struct sha256_double *txid) size_t txid_hash(const struct bitcoin_txid *txid)
{ {
return siphash24(siphash_seed(), txid->sha.u.u8, sizeof(txid->sha.u.u8)); return siphash24(siphash_seed(),
txid->shad.sha.u.u8, sizeof(txid->shad.sha.u.u8));
} }
bool txwatch_eq(const struct txwatch *w, const struct sha256_double *txid) bool txwatch_eq(const struct txwatch *w, const struct bitcoin_txid *txid)
{ {
return structeq(&w->txid, txid); return structeq(&w->txid, txid);
} }
@ -88,7 +89,7 @@ static void destroy_txwatch(struct txwatch *w)
struct txwatch *watch_txid_(const tal_t *ctx, struct txwatch *watch_txid_(const tal_t *ctx,
struct chain_topology *topo, struct chain_topology *topo,
struct peer *peer, struct peer *peer,
const struct sha256_double *txid, const struct bitcoin_txid *txid,
enum watch_result (*cb)(struct peer *peer, enum watch_result (*cb)(struct peer *peer,
const struct bitcoin_tx *, const struct bitcoin_tx *,
unsigned int depth, unsigned int depth,
@ -112,7 +113,7 @@ struct txwatch *watch_txid_(const tal_t *ctx,
} }
bool watching_txid(const struct chain_topology *topo, bool watching_txid(const struct chain_topology *topo,
const struct sha256_double *txid) const struct bitcoin_txid *txid)
{ {
return txwatch_hash_get(&topo->txwatches, txid) != NULL; return txwatch_hash_get(&topo->txwatches, txid) != NULL;
} }
@ -127,7 +128,7 @@ struct txwatch *watch_tx_(const tal_t *ctx,
void *arg), void *arg),
void *cb_arg) void *cb_arg)
{ {
struct sha256_double txid; struct bitcoin_txid txid;
bitcoin_txid(tx, &txid); bitcoin_txid(tx, &txid);
return watch_txid(ctx, topo, peer, &txid, cb, cb_arg); return watch_txid(ctx, topo, peer, &txid, cb, cb_arg);
@ -136,7 +137,7 @@ struct txwatch *watch_tx_(const tal_t *ctx,
struct txowatch *watch_txo_(const tal_t *ctx, struct txowatch *watch_txo_(const tal_t *ctx,
struct chain_topology *topo, struct chain_topology *topo,
struct peer *peer, struct peer *peer,
const struct sha256_double *txid, const struct bitcoin_txid *txid,
unsigned int output, unsigned int output,
enum watch_result (*cb)(struct peer *peer, enum watch_result (*cb)(struct peer *peer,
const struct bitcoin_tx *tx, const struct bitcoin_tx *tx,
@ -173,7 +174,7 @@ static bool txw_fire(struct chain_topology *topo,
log_debug(txw->peer->log, log_debug(txw->peer->log,
"Got depth change %u->%u for %s", "Got depth change %u->%u for %s",
txw->depth, depth, txw->depth, depth,
type_to_string(ltmp, struct sha256_double, &txw->txid)); type_to_string(ltmp, struct bitcoin_txid, &txw->txid));
txw->depth = depth; txw->depth = depth;
r = txw->cb(txw->peer, tx, txw->depth, txw->cbdata); r = txw->cb(txw->peer, tx, txw->depth, txw->cbdata);
switch (r) { switch (r) {
@ -190,7 +191,7 @@ void txwatch_fire(struct chain_topology *topo,
const struct bitcoin_tx *tx, const struct bitcoin_tx *tx,
unsigned int depth) unsigned int depth)
{ {
struct sha256_double txid; struct bitcoin_txid txid;
struct txwatch *txw; struct txwatch *txw;
bitcoin_txid(tx, &txid); bitcoin_txid(tx, &txid);
@ -206,15 +207,15 @@ void txowatch_fire(struct chain_topology *topo,
size_t input_num, size_t input_num,
const struct block *block) const struct block *block)
{ {
struct sha256_double txid; struct bitcoin_txid txid;
enum watch_result r; enum watch_result r;
bitcoin_txid(tx, &txid); bitcoin_txid(tx, &txid);
log_debug(txow->peer->log, log_debug(txow->peer->log,
"Got UTXO spend for %s:%u: %s", "Got UTXO spend for %s:%u: %s",
type_to_string(ltmp, struct sha256_double, &txow->out.txid), type_to_string(ltmp, struct bitcoin_txid, &txow->out.txid),
txow->out.index, txow->out.index,
type_to_string(ltmp, struct sha256_double, &txid)); type_to_string(ltmp, struct bitcoin_txid, &txid));
r = txow->cb(txow->peer, tx, input_num, block, txow->cbdata); r = txow->cb(txow->peer, tx, input_num, block, txow->cbdata);
switch (r) { switch (r) {

18
lightningd/watch.h

@ -1,7 +1,7 @@
#ifndef LIGHTNING_LIGHTNINGD_WATCH_H #ifndef LIGHTNING_LIGHTNINGD_WATCH_H
#define LIGHTNING_LIGHTNINGD_WATCH_H #define LIGHTNING_LIGHTNINGD_WATCH_H
#include "config.h" #include "config.h"
#include "bitcoin/shadouble.h" #include <bitcoin/tx.h>
#include <ccan/crypto/ripemd160/ripemd160.h> #include <ccan/crypto/ripemd160/ripemd160.h>
#include <ccan/htable/htable_type.h> #include <ccan/htable/htable_type.h>
#include <ccan/list/list.h> #include <ccan/list/list.h>
@ -17,7 +17,7 @@ enum watch_result {
}; };
struct txwatch_output { struct txwatch_output {
struct sha256_double txid; struct bitcoin_txid txid;
unsigned int index; unsigned int index;
}; };
@ -55,7 +55,7 @@ struct txwatch {
struct peer *peer; struct peer *peer;
/* Transaction to watch. */ /* Transaction to watch. */
struct sha256_double txid; struct bitcoin_txid txid;
unsigned int depth; unsigned int depth;
/* A new depth (0 if kicked out, otherwise 1 = tip, etc.) */ /* A new depth (0 if kicked out, otherwise 1 = tip, etc.) */
@ -67,9 +67,9 @@ struct txwatch {
void *cbdata; void *cbdata;
}; };
const struct sha256_double *txwatch_keyof(const struct txwatch *w); const struct bitcoin_txid *txwatch_keyof(const struct txwatch *w);
size_t txid_hash(const struct sha256_double *txid); size_t txid_hash(const struct bitcoin_txid *txid);
bool txwatch_eq(const struct txwatch *w, const struct sha256_double *txid); bool txwatch_eq(const struct txwatch *w, const struct bitcoin_txid *txid);
HTABLE_DEFINE_TYPE(struct txwatch, txwatch_keyof, txid_hash, txwatch_eq, HTABLE_DEFINE_TYPE(struct txwatch, txwatch_keyof, txid_hash, txwatch_eq,
txwatch_hash); txwatch_hash);
@ -77,7 +77,7 @@ HTABLE_DEFINE_TYPE(struct txwatch, txwatch_keyof, txid_hash, txwatch_eq,
struct txwatch *watch_txid_(const tal_t *ctx, struct txwatch *watch_txid_(const tal_t *ctx,
struct chain_topology *topo, struct chain_topology *topo,
struct peer *peer, struct peer *peer,
const struct sha256_double *txid, const struct bitcoin_txid *txid,
enum watch_result (*cb)(struct peer *peer, enum watch_result (*cb)(struct peer *peer,
const struct bitcoin_tx *, const struct bitcoin_tx *,
unsigned int depth, unsigned int depth,
@ -115,7 +115,7 @@ struct txwatch *watch_tx_(const tal_t *ctx,
struct txowatch *watch_txo_(const tal_t *ctx, struct txowatch *watch_txo_(const tal_t *ctx,
struct chain_topology *topo, struct chain_topology *topo,
struct peer *peer, struct peer *peer,
const struct sha256_double *txid, const struct bitcoin_txid *txid,
unsigned int output, unsigned int output,
enum watch_result (*cb)(struct peer *peer, enum watch_result (*cb)(struct peer *peer,
const struct bitcoin_tx *tx, const struct bitcoin_tx *tx,
@ -144,7 +144,7 @@ void txowatch_fire(struct chain_topology *topo,
const struct block *block); const struct block *block);
bool watching_txid(const struct chain_topology *topo, bool watching_txid(const struct chain_topology *topo,
const struct sha256_double *txid); const struct bitcoin_txid *txid);
void watch_topology_changed(struct chain_topology *topo); void watch_topology_changed(struct chain_topology *topo);
#endif /* LIGHTNING_LIGHTNINGD_WATCH_H */ #endif /* LIGHTNING_LIGHTNINGD_WATCH_H */

42
onchaind/onchain.c

@ -68,14 +68,14 @@ struct proposed_resolution {
/* How it actually got resolved. */ /* How it actually got resolved. */
struct resolution { struct resolution {
struct sha256_double txid; struct bitcoin_txid txid;
unsigned int depth; unsigned int depth;
enum tx_type tx_type; enum tx_type tx_type;
}; };
struct tracked_output { struct tracked_output {
enum tx_type tx_type; enum tx_type tx_type;
struct sha256_double txid; struct bitcoin_txid txid;
u32 tx_blockheight; u32 tx_blockheight;
u32 outnum; u32 outnum;
u64 satoshi; u64 satoshi;
@ -257,7 +257,7 @@ static struct bitcoin_tx *tx_to_us(const tal_t *ctx,
static struct tracked_output * static struct tracked_output *
new_tracked_output(struct tracked_output ***outs, new_tracked_output(struct tracked_output ***outs,
const struct sha256_double *txid, const struct bitcoin_txid *txid,
u32 tx_blockheight, u32 tx_blockheight,
enum tx_type tx_type, enum tx_type tx_type,
u32 outnum, u32 outnum,
@ -272,7 +272,7 @@ static struct tracked_output *
status_trace("Tracking output %u of %s: %s/%s", status_trace("Tracking output %u of %s: %s/%s",
outnum, outnum,
type_to_string(trc, struct sha256_double, txid), type_to_string(trc, struct bitcoin_txid, txid),
tx_type_name(tx_type), tx_type_name(tx_type),
output_type_name(output_type)); output_type_name(output_type));
@ -298,7 +298,7 @@ static void ignore_output(struct tracked_output *out)
{ {
status_trace("Ignoring output %u of %s: %s/%s", status_trace("Ignoring output %u of %s: %s/%s",
out->outnum, out->outnum,
type_to_string(trc, struct sha256_double, &out->txid), type_to_string(trc, struct bitcoin_txid, &out->txid),
tx_type_name(out->tx_type), tx_type_name(out->tx_type),
output_type_name(out->output_type)); output_type_name(out->output_type));
@ -366,7 +366,7 @@ static void propose_resolution_at_block(struct tracked_output *out,
/* This simple case: true if this was resolved by our proposal. */ /* This simple case: true if this was resolved by our proposal. */
static bool resolved_by_proposal(struct tracked_output *out, static bool resolved_by_proposal(struct tracked_output *out,
const struct sha256_double *txid) const struct bitcoin_txid *txid)
{ {
/* If there's no TX associated, it's not us. */ /* If there's no TX associated, it's not us. */
if (!out->proposal->tx) if (!out->proposal->tx)
@ -394,7 +394,7 @@ static bool resolved_by_proposal(struct tracked_output *out,
/* Otherwise, we figure out what happened and then call this. */ /* Otherwise, we figure out what happened and then call this. */
static void resolved_by_other(struct tracked_output *out, static void resolved_by_other(struct tracked_output *out,
const struct sha256_double *txid, const struct bitcoin_txid *txid,
enum tx_type tx_type) enum tx_type tx_type)
{ {
out->resolved = tal(out, struct resolution); out->resolved = tal(out, struct resolution);
@ -406,7 +406,7 @@ static void resolved_by_other(struct tracked_output *out,
tx_type_name(out->tx_type), tx_type_name(out->tx_type),
output_type_name(out->output_type), output_type_name(out->output_type),
tx_type_name(tx_type), tx_type_name(tx_type),
type_to_string(trc, struct sha256_double, txid)); type_to_string(trc, struct bitcoin_txid, txid));
} }
static void unknown_spend(struct tracked_output *out, static void unknown_spend(struct tracked_output *out,
@ -479,8 +479,8 @@ static bool is_mutual_close(const struct bitcoin_tx *tx,
} }
/* We only ever send out one, so matching it is easy. */ /* We only ever send out one, so matching it is easy. */
static bool is_local_commitment(const struct sha256_double *txid, static bool is_local_commitment(const struct bitcoin_txid *txid,
const struct sha256_double *our_broadcast_txid) const struct bitcoin_txid *our_broadcast_txid)
{ {
return structeq(txid, our_broadcast_txid); return structeq(txid, our_broadcast_txid);
} }
@ -505,7 +505,7 @@ static bool all_irrevocably_resolved(struct tracked_output **outs)
static void unwatch_tx(const struct bitcoin_tx *tx) static void unwatch_tx(const struct bitcoin_tx *tx)
{ {
u8 *msg; u8 *msg;
struct sha256_double txid; struct bitcoin_txid txid;
bitcoin_txid(tx, &txid); bitcoin_txid(tx, &txid);
@ -591,7 +591,7 @@ static void handle_htlc_onchain_fulfill(struct tracked_output *out,
static void resolve_htlc_tx(struct tracked_output ***outs, static void resolve_htlc_tx(struct tracked_output ***outs,
size_t out_index, size_t out_index,
const struct bitcoin_tx *htlc_tx, const struct bitcoin_tx *htlc_tx,
const struct sha256_double *htlc_txid, const struct bitcoin_txid *htlc_txid,
u32 tx_blockheight) u32 tx_blockheight)
{ {
struct tracked_output *out; struct tracked_output *out;
@ -670,7 +670,7 @@ static void output_spent(struct tracked_output ***outs,
u32 input_num, u32 input_num,
u32 tx_blockheight) u32 tx_blockheight)
{ {
struct sha256_double txid; struct bitcoin_txid txid;
bitcoin_txid(tx, &txid); bitcoin_txid(tx, &txid);
@ -742,7 +742,7 @@ static void output_spent(struct tracked_output ***outs,
/* Not interesting to us, so unwatch the tx and all its outputs */ /* Not interesting to us, so unwatch the tx and all its outputs */
status_trace("Notified about tx %s output %u spend, but we don't care", status_trace("Notified about tx %s output %u spend, but we don't care",
type_to_string(trc, struct sha256_double, type_to_string(trc, struct bitcoin_txid,
&tx->input[input_num].txid), &tx->input[input_num].txid),
tx->input[input_num].index); tx->input[input_num].index);
unwatch_tx(tx); unwatch_tx(tx);
@ -783,7 +783,7 @@ static void update_resolution_depth(struct tracked_output *out, u32 depth)
} }
static void tx_new_depth(struct tracked_output **outs, static void tx_new_depth(struct tracked_output **outs,
const struct sha256_double *txid, u32 depth) const struct bitcoin_txid *txid, u32 depth)
{ {
size_t i; size_t i;
@ -932,7 +932,7 @@ static void wait_for_resolved(struct tracked_output **outs)
{ {
while (!all_irrevocably_resolved(outs)) { while (!all_irrevocably_resolved(outs)) {
u8 *msg = wire_sync_read(outs, REQ_FD); u8 *msg = wire_sync_read(outs, REQ_FD);
struct sha256_double txid; struct bitcoin_txid txid;
struct bitcoin_tx *tx = tal(msg, struct bitcoin_tx); struct bitcoin_tx *tx = tal(msg, struct bitcoin_tx);
u32 input_num, depth, tx_blockheight; u32 input_num, depth, tx_blockheight;
struct preimage preimage; struct preimage preimage;
@ -962,7 +962,7 @@ static void set_state(enum peer_state state)
} }
static void handle_mutual_close(const struct bitcoin_tx *tx, static void handle_mutual_close(const struct bitcoin_tx *tx,
const struct sha256_double *txid, const struct bitcoin_txid *txid,
struct tracked_output **outs) struct tracked_output **outs)
{ {
set_state(ONCHAIND_MUTUAL); set_state(ONCHAIND_MUTUAL);
@ -1151,7 +1151,7 @@ static void note_missing_htlcs(u8 **htlc_scripts,
static void handle_our_unilateral(const struct bitcoin_tx *tx, static void handle_our_unilateral(const struct bitcoin_tx *tx,
u32 tx_blockheight, u32 tx_blockheight,
const struct sha256_double *txid, const struct bitcoin_txid *txid,
const struct secrets *secrets, const struct secrets *secrets,
const struct sha256 *shaseed, const struct sha256 *shaseed,
const struct pubkey *remote_revocation_basepoint, const struct pubkey *remote_revocation_basepoint,
@ -1450,7 +1450,7 @@ static void steal_htlc(struct tracked_output *out)
* claim all the funds. * claim all the funds.
*/ */
static void handle_their_cheat(const struct bitcoin_tx *tx, static void handle_their_cheat(const struct bitcoin_tx *tx,
const struct sha256_double *txid, const struct bitcoin_txid *txid,
u32 tx_blockheight, u32 tx_blockheight,
const struct sha256 *revocation_preimage, const struct sha256 *revocation_preimage,
const struct secrets *secrets, const struct secrets *secrets,
@ -1714,7 +1714,7 @@ static void handle_their_cheat(const struct bitcoin_tx *tx,
static void handle_their_unilateral(const struct bitcoin_tx *tx, static void handle_their_unilateral(const struct bitcoin_tx *tx,
u32 tx_blockheight, u32 tx_blockheight,
const struct sha256_double *txid, const struct bitcoin_txid *txid,
const struct secrets *secrets, const struct secrets *secrets,
const struct sha256 *shaseed, const struct sha256 *shaseed,
const struct pubkey *remote_per_commitment_point, const struct pubkey *remote_per_commitment_point,
@ -1950,7 +1950,7 @@ int main(int argc, char *argv[])
struct secrets secrets; struct secrets secrets;
struct sha256 shaseed; struct sha256 shaseed;
struct tracked_output **outs; struct tracked_output **outs;
struct sha256_double our_broadcast_txid, txid; struct bitcoin_txid our_broadcast_txid, txid;
secp256k1_ecdsa_signature *remote_htlc_sigs; secp256k1_ecdsa_signature *remote_htlc_sigs;
u64 funding_amount_satoshi, num_htlcs; u64 funding_amount_satoshi, num_htlcs;
u8 *scriptpubkey[NUM_SIDES]; u8 *scriptpubkey[NUM_SIDES];

6
onchaind/onchain_wire.csv

@ -14,7 +14,7 @@ onchain_init,,feerate_per_kw,u32
onchain_init,,local_dust_limit_satoshi,u64 onchain_init,,local_dust_limit_satoshi,u64
onchain_init,,remote_revocation_basepoint,struct pubkey onchain_init,,remote_revocation_basepoint,struct pubkey
# Gives an easy way to tell if it's our unilateral close or theirs... # Gives an easy way to tell if it's our unilateral close or theirs...
onchain_init,,our_broadcast_txid,struct sha256_double onchain_init,,our_broadcast_txid,struct bitcoin_txid
onchain_init,,local_scriptpubkey_len,u16 onchain_init,,local_scriptpubkey_len,u16
onchain_init,,local_scriptpubkey,local_scriptpubkey_len*u8 onchain_init,,local_scriptpubkey,local_scriptpubkey_len*u8
onchain_init,,remote_scriptpubkey_len,u16 onchain_init,,remote_scriptpubkey_len,u16
@ -56,12 +56,12 @@ onchain_spent,,blockheight,u32
# master->onchaind: We will receive more than one of these, as depth changes. # master->onchaind: We will receive more than one of these, as depth changes.
onchain_depth,5005 onchain_depth,5005
onchain_depth,,txid,struct sha256_double onchain_depth,,txid,struct bitcoin_txid
onchain_depth,,depth,u32 onchain_depth,,depth,u32
# onchaind->master: We don't want to watch this tx, or its outputs # onchaind->master: We don't want to watch this tx, or its outputs
onchain_unwatch_tx,5006 onchain_unwatch_tx,5006
onchain_unwatch_tx,,txid,struct sha256_double onchain_unwatch_tx,,txid,struct bitcoin_txid
onchain_unwatch_tx,,num_outputs,u32 onchain_unwatch_tx,,num_outputs,u32
# master->onchaind: We know HTLC preimage # master->onchaind: We know HTLC preimage

Can't render this file because it has a wrong number of fields in line 2.

6
openingd/opening.c

@ -47,7 +47,7 @@ struct state {
/* Funding and feerate: set by opening peer. */ /* Funding and feerate: set by opening peer. */
u64 funding_satoshis, push_msat; u64 funding_satoshis, push_msat;
u32 feerate_per_kw; u32 feerate_per_kw;
struct sha256_double funding_txid; struct bitcoin_txid funding_txid;
u16 funding_txout; u16 funding_txout;
/* Secret keys and basepoint secrets. */ /* Secret keys and basepoint secrets. */
@ -436,7 +436,7 @@ static u8 *funder_channel(struct state *state,
type_to_string(trc, struct pubkey, our_funding_pubkey)); type_to_string(trc, struct pubkey, our_funding_pubkey));
msg = towire_funding_created(state, &state->channel_id, msg = towire_funding_created(state, &state->channel_id,
&state->funding_txid.sha, &state->funding_txid,
state->funding_txout, state->funding_txout,
&sig); &sig);
if (!sync_crypto_write(&state->cs, PEER_FD, msg)) if (!sync_crypto_write(&state->cs, PEER_FD, msg))
@ -643,7 +643,7 @@ static u8 *fundee_channel(struct state *state,
"Reading funding_created: %s", strerror(errno)); "Reading funding_created: %s", strerror(errno));
if (!fromwire_funding_created(msg, NULL, &id_in, if (!fromwire_funding_created(msg, NULL, &id_in,
&state->funding_txid.sha, &state->funding_txid,
&state->funding_txout, &state->funding_txout,
&theirsig)) &theirsig))
peer_failed(PEER_FD, &state->cs, &state->channel_id, peer_failed(PEER_FD, &state->cs, &state->channel_id,

4
openingd/opening_wire.csv

@ -43,7 +43,7 @@ opening_funder_reply,,delayed_payment_basepoint,struct pubkey
opening_funder_reply,,their_per_commit_point,struct pubkey opening_funder_reply,,their_per_commit_point,struct pubkey
opening_funder_reply,,minimum_depth,u32 opening_funder_reply,,minimum_depth,u32
opening_funder_reply,,remote_fundingkey,struct pubkey opening_funder_reply,,remote_fundingkey,struct pubkey
opening_funder_reply,,funding_txid,struct sha256_double opening_funder_reply,,funding_txid,struct bitcoin_txid
opening_funder_reply,,feerate_per_kw,u32 opening_funder_reply,,feerate_per_kw,u32
# This means they offer the open (contains their offer packet) # This means they offer the open (contains their offer packet)
@ -67,7 +67,7 @@ opening_fundee_reply,,htlc_basepoint,struct pubkey
opening_fundee_reply,,delayed_payment_basepoint,struct pubkey opening_fundee_reply,,delayed_payment_basepoint,struct pubkey
opening_fundee_reply,,their_per_commit_point,struct pubkey opening_fundee_reply,,their_per_commit_point,struct pubkey
opening_fundee_reply,,remote_fundingkey,struct pubkey opening_fundee_reply,,remote_fundingkey,struct pubkey
opening_fundee_reply,,funding_txid,struct sha256_double opening_fundee_reply,,funding_txid,struct bitcoin_txid
opening_fundee_reply,,funding_txout,u16 opening_fundee_reply,,funding_txout,u16
opening_fundee_reply,,funding_satoshis,u64 opening_fundee_reply,,funding_satoshis,u64
opening_fundee_reply,,push_msat,u64 opening_fundee_reply,,push_msat,u64

Can't render this file because it has a wrong number of fields in line 3.

2
tools/generate-wire.py

@ -19,6 +19,7 @@ type2size = {
'struct pubkey': 33, 'struct pubkey': 33,
'struct sha256': 32, 'struct sha256': 32,
'struct sha256_double': 32, 'struct sha256_double': 32,
'struct bitcoin_txid': 32,
'u64': 8, 'u64': 8,
'u32': 4, 'u32': 4,
'u16': 2, 'u16': 2,
@ -78,6 +79,7 @@ partialtypemap = {
'features': FieldType('u8'), 'features': FieldType('u8'),
'channel_id': FieldType('struct channel_id'), 'channel_id': FieldType('struct channel_id'),
'chain_hash': FieldType('struct sha256_double'), 'chain_hash': FieldType('struct sha256_double'),
'funding_txid': FieldType('struct bitcoin_txid'),
'pad': FieldType('pad'), 'pad': FieldType('pad'),
} }

2
wallet/test/run-wallet.c

@ -255,7 +255,7 @@ static bool test_channel_crud(const tal_t *ctx)
struct wallet_channel c1, *c2 = tal(w, struct wallet_channel); struct wallet_channel c1, *c2 = tal(w, struct wallet_channel);
struct peer p; struct peer p;
struct channel_info ci; struct channel_info ci;
struct sha256_double *hash = tal(w, struct sha256_double); struct bitcoin_txid *hash = tal(w, struct bitcoin_txid);
struct pubkey pk; struct pubkey pk;
struct changed_htlc last_commit; struct changed_htlc last_commit;
secp256k1_ecdsa_signature *sig = tal(w, secp256k1_ecdsa_signature); secp256k1_ecdsa_signature *sig = tal(w, secp256k1_ecdsa_signature);

6
wallet/wallet.c

@ -55,7 +55,7 @@ static bool wallet_stmt2output(sqlite3_stmt *stmt, struct utxo *utxo)
} }
bool wallet_update_output_status(struct wallet *w, bool wallet_update_output_status(struct wallet *w,
const struct sha256_double *txid, const struct bitcoin_txid *txid,
const u32 outnum, enum output_status oldstatus, const u32 outnum, enum output_status oldstatus,
enum output_status newstatus) enum output_status newstatus)
{ {
@ -475,7 +475,7 @@ static bool wallet_stmt2channel(struct wallet *w, sqlite3_stmt *stmt,
if (sqlite3_column_type(stmt, 12) != SQLITE_NULL) { if (sqlite3_column_type(stmt, 12) != SQLITE_NULL) {
assert(sqlite3_column_bytes(stmt, 12) == 32); assert(sqlite3_column_bytes(stmt, 12) == 32);
chan->peer->funding_txid = tal(chan->peer, struct sha256_double); chan->peer->funding_txid = tal(chan->peer, struct bitcoin_txid);
memcpy(chan->peer->funding_txid, sqlite3_column_blob(stmt, 12), 32); memcpy(chan->peer->funding_txid, sqlite3_column_blob(stmt, 12), 32);
} else { } else {
chan->peer->funding_txid = NULL; chan->peer->funding_txid = NULL;
@ -829,7 +829,7 @@ int wallet_extract_owned_outputs(struct wallet *w, const struct bitcoin_tx *tx,
log_debug(w->log, "Owning output %zu %"PRIu64" (%s) txid %s", log_debug(w->log, "Owning output %zu %"PRIu64" (%s) txid %s",
output, tx->output[output].amount, output, tx->output[output].amount,
is_p2sh ? "P2SH" : "SEGWIT", is_p2sh ? "P2SH" : "SEGWIT",
type_to_string(ltmp, struct sha256_double, type_to_string(ltmp, struct bitcoin_txid,
&utxo->txid)); &utxo->txid));
if (!wallet_add_utxo(w, utxo, is_p2sh ? p2sh_wpkh : our_change)) { if (!wallet_add_utxo(w, utxo, is_p2sh ? p2sh_wpkh : our_change)) {

2
wallet/wallet.h

@ -118,7 +118,7 @@ bool wallet_add_utxo(struct wallet *w, struct utxo *utxo,
* `output_state_any` as @oldstatus. * `output_state_any` as @oldstatus.
*/ */
bool wallet_update_output_status(struct wallet *w, bool wallet_update_output_status(struct wallet *w,
const struct sha256_double *txid, const struct bitcoin_txid *txid,
const u32 outnum, enum output_status oldstatus, const u32 outnum, enum output_status oldstatus,
enum output_status newstatus); enum output_status newstatus);

9
wire/fromwire.c

@ -2,6 +2,7 @@
#include <bitcoin/preimage.h> #include <bitcoin/preimage.h>
#include <bitcoin/pubkey.h> #include <bitcoin/pubkey.h>
#include <bitcoin/shadouble.h> #include <bitcoin/shadouble.h>
#include <bitcoin/tx.h>
#include <ccan/build_assert/build_assert.h> #include <ccan/build_assert/build_assert.h>
#include <ccan/endian/endian.h> #include <ccan/endian/endian.h>
#include <ccan/mem/mem.h> #include <ccan/mem/mem.h>
@ -176,6 +177,12 @@ void fromwire_sha256_double(const u8 **cursor, size_t *max,
fromwire_sha256(cursor, max, &sha256d->sha); fromwire_sha256(cursor, max, &sha256d->sha);
} }
void fromwire_bitcoin_txid(const u8 **cursor, size_t *max,
struct bitcoin_txid *txid)
{
fromwire_sha256_double(cursor, max, &txid->shad);
}
void fromwire_preimage(const u8 **cursor, size_t *max, struct preimage *preimage) void fromwire_preimage(const u8 **cursor, size_t *max, struct preimage *preimage)
{ {
fromwire(cursor, max, preimage, sizeof(*preimage)); fromwire(cursor, max, preimage, sizeof(*preimage));
@ -207,7 +214,7 @@ REGISTER_TYPE_TO_HEXSTR(channel_id);
* (ie. `funding_output_index` alters the last two bytes). * (ie. `funding_output_index` alters the last two bytes).
*/ */
void derive_channel_id(struct channel_id *channel_id, void derive_channel_id(struct channel_id *channel_id,
struct sha256_double *txid, u16 txout) struct bitcoin_txid *txid, u16 txout)
{ {
BUILD_ASSERT(sizeof(*channel_id) == sizeof(*txid)); BUILD_ASSERT(sizeof(*channel_id) == sizeof(*txid));
memcpy(channel_id, txid, sizeof(*channel_id)); memcpy(channel_id, txid, sizeof(*channel_id));

2
wire/test/run-peer-wire.c

@ -82,7 +82,7 @@ struct msg_closing_signed {
}; };
struct msg_funding_created { struct msg_funding_created {
struct channel_id temporary_channel_id; struct channel_id temporary_channel_id;
struct sha256 txid; struct bitcoin_txid txid;
u16 output_index; u16 output_index;
secp256k1_ecdsa_signature signature; secp256k1_ecdsa_signature signature;
}; };

6
wire/towire.c

@ -1,6 +1,7 @@
#include "wire.h" #include "wire.h"
#include <bitcoin/preimage.h> #include <bitcoin/preimage.h>
#include <bitcoin/shadouble.h> #include <bitcoin/shadouble.h>
#include <bitcoin/tx.h>
#include <ccan/crypto/ripemd160/ripemd160.h> #include <ccan/crypto/ripemd160/ripemd160.h>
#include <ccan/endian/endian.h> #include <ccan/endian/endian.h>
#include <ccan/mem/mem.h> #include <ccan/mem/mem.h>
@ -119,6 +120,11 @@ void towire_sha256_double(u8 **pptr, const struct sha256_double *sha256d)
towire_sha256(pptr, &sha256d->sha); towire_sha256(pptr, &sha256d->sha);
} }
void towire_bitcoin_txid(u8 **pptr, const struct bitcoin_txid *txid)
{
towire_sha256_double(pptr, &txid->shad);
}
void towire_preimage(u8 **pptr, const struct preimage *preimage) void towire_preimage(u8 **pptr, const struct preimage *preimage)
{ {
towire(pptr, preimage, sizeof(*preimage)); towire(pptr, preimage, sizeof(*preimage));

6
wire/wire.h

@ -15,11 +15,12 @@ struct channel_id {
u8 id[32]; u8 id[32];
}; };
struct bitcoin_txid;
struct preimage; struct preimage;
struct ripemd160; struct ripemd160;
void derive_channel_id(struct channel_id *channel_id, void derive_channel_id(struct channel_id *channel_id,
struct sha256_double *txid, u16 txout); struct bitcoin_txid *txid, u16 txout);
/* Read the type; returns -1 if not long enough. cursor is a tal ptr. */ /* Read the type; returns -1 if not long enough. cursor is a tal ptr. */
int fromwire_peektype(const u8 *cursor); int fromwire_peektype(const u8 *cursor);
@ -38,6 +39,7 @@ void towire_short_channel_id(u8 **pptr,
const struct short_channel_id *short_channel_id); const struct short_channel_id *short_channel_id);
void towire_sha256(u8 **pptr, const struct sha256 *sha256); void towire_sha256(u8 **pptr, const struct sha256 *sha256);
void towire_sha256_double(u8 **pptr, const struct sha256_double *sha256d); void towire_sha256_double(u8 **pptr, const struct sha256_double *sha256d);
void towire_bitcoin_txid(u8 **pptr, const struct bitcoin_txid *txid);
void towire_preimage(u8 **pptr, const struct preimage *preimage); void towire_preimage(u8 **pptr, const struct preimage *preimage);
void towire_ripemd160(u8 **pptr, const struct ripemd160 *ripemd); void towire_ripemd160(u8 **pptr, const struct ripemd160 *ripemd);
void towire_u8(u8 **pptr, u8 v); void towire_u8(u8 **pptr, u8 v);
@ -70,6 +72,8 @@ void fromwire_short_channel_id(const u8 **cursor, size_t *max,
void fromwire_sha256(const u8 **cursor, size_t *max, struct sha256 *sha256); void fromwire_sha256(const u8 **cursor, size_t *max, struct sha256 *sha256);
void fromwire_sha256_double(const u8 **cursor, size_t *max, void fromwire_sha256_double(const u8 **cursor, size_t *max,
struct sha256_double *sha256d); struct sha256_double *sha256d);
void fromwire_bitcoin_txid(const u8 **cursor, size_t *max,
struct bitcoin_txid *txid);
void fromwire_preimage(const u8 **cursor, size_t *max, struct preimage *preimage); void fromwire_preimage(const u8 **cursor, size_t *max, struct preimage *preimage);
void fromwire_ripemd160(const u8 **cursor, size_t *max, struct ripemd160 *ripemd); void fromwire_ripemd160(const u8 **cursor, size_t *max, struct ripemd160 *ripemd);
void fromwire_pad(const u8 **cursor, size_t *max, size_t num); void fromwire_pad(const u8 **cursor, size_t *max, size_t num);

Loading…
Cancel
Save