diff --git a/bitcoin/block.c b/bitcoin/block.c index d12a97a77..7abbd7c88 100644 --- a/bitcoin/block.c +++ b/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, 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, 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); } diff --git a/bitcoin/test/run-tx-encode.c b/bitcoin/test/run-tx-encode.c index 1223bd46c..a04be0311 100644 --- a/bitcoin/test/run-tx-encode.c +++ b/bitcoin/test/run-tx-encode.c @@ -38,7 +38,8 @@ int main(void) assert(tal_count(tx->input) == 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), "1db36e1306dfc810ea63f0cf866d475cce4e9261a5e8d1581f0d1dc485f4beb5"); assert(tx->input[0].index == 0); diff --git a/bitcoin/tx.c b/bitcoin/tx.c index 1f7c806ce..c31d31333 100644 --- a/bitcoin/tx.c +++ b/bitcoin/tx.c @@ -276,13 +276,13 @@ size_t measure_tx_cost(const struct bitcoin_tx *tx) 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; /* For TXID, we never use extended form. */ 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, @@ -336,7 +336,7 @@ static void pull_input(const tal_t *ctx, const u8 **cursor, size_t *max, struct bitcoin_tx_input *input) { u64 script_len; - pull_sha256_double(cursor, max, &input->txid); + pull_sha256_double(cursor, max, &input->txid.shad); input->index = pull_le32(cursor, max); script_len = pull_length(cursor, max); if (script_len) @@ -460,9 +460,7 @@ fail: return NULL; } -/* . Bitcoind represents hashes as little-endian for RPC. This didn't - * stick for blockids (everyone else uses big-endian, eg. block explorers), - * but it did stick for txids. */ +/* . Bitcoind represents hashes as little-endian for RPC. */ static void reverse_bytes(u8 *arr, size_t len) { 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, - struct sha256_double *txid) + struct bitcoin_txid *txid) { if (!hex_decode(hexstr, hexstr_len, txid, sizeof(*txid))) 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; } -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) { - struct sha256_double rev = *txid; + struct sha256_double rev = txid->shad; reverse_bytes(rev.sha.u.u8, sizeof(rev.sha.u.u8)); 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; } +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_txid, fmt_bitcoin_txid); diff --git a/bitcoin/tx.h b/bitcoin/tx.h index 79323fd4a..987c2e80e 100644 --- a/bitcoin/tx.h +++ b/bitcoin/tx.h @@ -7,6 +7,10 @@ #include #include +struct bitcoin_txid { + struct sha256_double shad; +}; + struct bitcoin_tx { u32 version; struct bitcoin_tx_input *input; @@ -20,7 +24,7 @@ struct bitcoin_tx_output { }; struct bitcoin_tx_input { - struct sha256_double txid; + struct bitcoin_txid txid; u32 index; /* output number referred to by above */ u8 *script; u32 sequence_number; @@ -34,7 +38,7 @@ struct bitcoin_tx_input { /* 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. */ 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). */ 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). */ -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); /* Internal de-linearization functions. */ diff --git a/channeld/channel.c b/channeld/channel.c index d1a1684fc..460b0c645 100644 --- a/channeld/channel.c +++ b/channeld/channel.c @@ -2351,7 +2351,7 @@ static void init_channel(struct peer *peer) u16 funding_txout; u64 local_msatoshi; struct pubkey funding_pubkey[NUM_SIDES]; - struct sha256_double funding_txid; + struct bitcoin_txid funding_txid; enum side funder; enum htlc_state *hstates; struct fulfilled_htlc *fulfilled; diff --git a/channeld/channel_wire.csv b/channeld/channel_wire.csv index 5aff8d4e0..11d8e8df6 100644 --- a/channeld/channel_wire.csv +++ b/channeld/channel_wire.csv @@ -7,7 +7,7 @@ channel_normal_operation,11001 # Begin! (passes gossipd-client fd) channel_init,1000 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_satoshi,u64 channel_init,,our_config,struct channel_config diff --git a/channeld/commit_tx.c b/channeld/commit_tx.c index 83ea6b5a7..90bf23565 100644 --- a/channeld/commit_tx.c +++ b/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, - const struct sha256_double *funding_txid, + const struct bitcoin_txid *funding_txid, unsigned int funding_txout, u64 funding_satoshis, enum side funder, diff --git a/channeld/commit_tx.h b/channeld/commit_tx.h index 9b8fcbac6..534d5938f 100644 --- a/channeld/commit_tx.h +++ b/channeld/commit_tx.h @@ -7,7 +7,6 @@ #include struct keyset; -struct sha256_double; /** * 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. */ 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, u64 funding_satoshis, enum side funder, diff --git a/channeld/full_channel.c b/channeld/full_channel.c index 220ccb6d9..1c2c824cb 100644 --- a/channeld/full_channel.c +++ b/channeld/full_channel.c @@ -20,7 +20,7 @@ #include struct channel *new_channel(const tal_t *ctx, - const struct sha256_double *funding_txid, + const struct bitcoin_txid *funding_txid, unsigned int funding_txout, u64 funding_satoshis, u64 local_msatoshi, @@ -178,7 +178,7 @@ static void add_htlcs(struct bitcoin_tx ***txs, enum side side) { size_t i, n; - struct sha256_double txid; + struct bitcoin_txid txid; u32 feerate_per_kw = channel->view[side].feerate_per_kw; /* Get txid of commitment transaction */ diff --git a/channeld/full_channel.h b/channeld/full_channel.h index 596b19a07..67a1f3a1b 100644 --- a/channeld/full_channel.h +++ b/channeld/full_channel.h @@ -26,7 +26,7 @@ * Returns state, or NULL if malformed. */ struct channel *new_channel(const tal_t *ctx, - const struct sha256_double *funding_txid, + const struct bitcoin_txid *funding_txid, unsigned int funding_txout, u64 funding_satoshis, u64 local_msatoshi, diff --git a/channeld/test/run-full_channel.c b/channeld/test/run-full_channel.c index 5521d6e6a..9e2773b3c 100644 --- a/channeld/test/run-full_channel.c +++ b/channeld/test/run-full_channel.c @@ -20,26 +20,14 @@ 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! */ -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 sha256 rev = sha256_from_hex(hex); - size_t i; + struct bitcoin_txid txid; - for (i = 0; i < sizeof(rev); i++) - sha256.sha.u.u8[sizeof(sha256) - 1 - i] = rev.u.u8[i]; - return sha256; + if (!bitcoin_txid_from_hex(hex, strlen(hex), &txid)) + abort(); + return txid; } 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) { tal_t *tmpctx = tal_tmpctx(NULL); - struct sha256_double funding_txid; + struct bitcoin_txid funding_txid; /* We test from both sides. */ struct channel *lchannel, *rchannel; u64 funding_amount_satoshi; diff --git a/closingd/closing.c b/closingd/closing.c index 98ba6423d..ba47ded6c 100644 --- a/closingd/closing.c +++ b/closingd/closing.c @@ -27,7 +27,7 @@ static struct bitcoin_tx *close_tx(const tal_t *ctx, struct crypto_state *cs, const struct channel_id *channel_id, u8 *scriptpubkey[NUM_SIDES], - const struct sha256_double *funding_txid, + const struct bitcoin_txid *funding_txid, unsigned int funding_txout, u64 funding_satoshi, const u64 satoshi_out[NUM_SIDES], @@ -155,7 +155,7 @@ int main(int argc, char *argv[]) u8 *msg; struct privkey seed; struct pubkey funding_pubkey[NUM_SIDES]; - struct sha256_double funding_txid; + struct bitcoin_txid funding_txid; u16 funding_txout; u64 funding_satoshi, satoshi_out[NUM_SIDES]; u64 our_dust_limit; diff --git a/closingd/closing_wire.csv b/closingd/closing_wire.csv index 662e6c620..4e4ad01d4 100644 --- a/closingd/closing_wire.csv +++ b/closingd/closing_wire.csv @@ -5,7 +5,7 @@ closing_init,2001 closing_init,,crypto_state,struct crypto_state closing_init,,gossip_index,u64 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_satoshi,u64 closing_init,,remote_fundingkey,struct pubkey diff --git a/common/close_tx.c b/common/close_tx.c index d13f08f75..9f880da50 100644 --- a/common/close_tx.c +++ b/common/close_tx.c @@ -7,7 +7,7 @@ struct bitcoin_tx *create_close_tx(const tal_t *ctx, const u8 *our_script, const u8 *their_script, - const struct sha256_double *anchor_txid, + const struct bitcoin_txid *anchor_txid, unsigned int anchor_index, u64 anchor_satoshis, uint64_t to_us, uint64_t to_them, diff --git a/common/close_tx.h b/common/close_tx.h index 8a94f846a..40746dabc 100644 --- a/common/close_tx.h +++ b/common/close_tx.h @@ -4,7 +4,6 @@ #include #include -struct sha256_double; struct pubkey; /* 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, const u8 *our_script, const u8 *their_script, - const struct sha256_double *anchor_txid, + const struct bitcoin_txid *anchor_txid, unsigned int anchor_index, u64 anchor_satoshis, uint64_t to_us, uint64_t to_them, diff --git a/common/funding_tx.h b/common/funding_tx.h index 083057018..7fd89616b 100644 --- a/common/funding_tx.h +++ b/common/funding_tx.h @@ -8,7 +8,6 @@ struct bitcoin_tx; struct ext_key; struct privkey; struct pubkey; -struct sha256_double; struct utxo; /** diff --git a/common/htlc_tx.c b/common/htlc_tx.c index 220883430..93c03f84f 100644 --- a/common/htlc_tx.c +++ b/common/htlc_tx.c @@ -5,7 +5,7 @@ #include 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, u64 msatoshi, 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, - const struct sha256_double *commit_txid, + const struct bitcoin_txid *commit_txid, unsigned int commit_output_number, u64 htlc_msatoshi, 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, - const struct sha256_double *commit_txid, + const struct bitcoin_txid *commit_txid, unsigned int commit_output_number, u64 htlc_msatoshi, u32 cltv_expiry, diff --git a/common/htlc_tx.h b/common/htlc_tx.h index 18f35c61d..21774e6b5 100644 --- a/common/htlc_tx.h +++ b/common/htlc_tx.h @@ -6,7 +6,6 @@ struct keyset; struct preimage; struct pubkey; -struct sha256_double; 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 * output; doesn't fill in input witness. */ 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, u64 htlc_msatoshi, 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 * output; doesn't fill in input witness. */ 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, u64 htlc_msatoshi, u32 cltv_expiry, diff --git a/common/initial_channel.c b/common/initial_channel.c index 57801060e..4ac04dad6 100644 --- a/common/initial_channel.c +++ b/common/initial_channel.c @@ -8,7 +8,7 @@ #include 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, u64 funding_satoshis, u64 local_msatoshi, diff --git a/common/initial_channel.h b/common/initial_channel.h index 904fb5758..3354b01c2 100644 --- a/common/initial_channel.h +++ b/common/initial_channel.h @@ -28,7 +28,7 @@ struct channel_view { struct channel { /* Funding txid and output. */ - struct sha256_double funding_txid; + struct bitcoin_txid funding_txid; unsigned int funding_txout; /* 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. */ 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, u64 funding_satoshis, u64 local_msatoshi, diff --git a/common/initial_commit_tx.c b/common/initial_commit_tx.c index 014ec7669..2f15f4da9 100644 --- a/common/initial_commit_tx.c +++ b/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, - const struct sha256_double *funding_txid, + const struct bitcoin_txid *funding_txid, unsigned int funding_txout, u64 funding_satoshis, enum side funder, diff --git a/common/initial_commit_tx.h b/common/initial_commit_tx.h index 4744ac20d..858ede797 100644 --- a/common/initial_commit_tx.h +++ b/common/initial_commit_tx.h @@ -6,7 +6,6 @@ #include struct keyset; -struct sha256_double; /* 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. */ 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, u64 funding_satoshis, enum side funder, diff --git a/common/type_to_string.h b/common/type_to_string.h index c12258283..221ad66c9 100644 --- a/common/type_to_string.h +++ b/common/type_to_string.h @@ -9,6 +9,7 @@ union printable_types { const struct pubkey *pubkey; const struct sha256_double *sha256_double; + const struct bitcoin_txid *bitcoin_txid; const struct sha256 *sha256; const struct ripemd160 *ripemd160; const struct rel_locktime *rel_locktime; diff --git a/common/utxo.c b/common/utxo.c index 8da8a77bc..edd86741f 100644 --- a/common/utxo.c +++ b/common/utxo.c @@ -3,7 +3,7 @@ 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_u64(pptr, utxo->amount); 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) { - fromwire_sha256_double(ptr, max, &utxo->txid); + fromwire_bitcoin_txid(ptr, max, &utxo->txid); utxo->outnum = fromwire_u32(ptr, max); utxo->amount = fromwire_u64(ptr, max); utxo->keyindex = fromwire_u32(ptr, max); diff --git a/common/utxo.h b/common/utxo.h index 5461541ee..634143630 100644 --- a/common/utxo.h +++ b/common/utxo.h @@ -1,13 +1,13 @@ #ifndef LIGHTNING_COMMON_UTXO_H #define LIGHTNING_COMMON_UTXO_H #include "config.h" -#include +#include #include #include #include struct utxo { - struct sha256_double txid; + struct bitcoin_txid txid; u32 outnum; u64 amount; u32 keyindex; diff --git a/lightningd/chaintopology.c b/lightningd/chaintopology.c index 86232ec9d..8ec5d627f 100644 --- a/lightningd/chaintopology.c +++ b/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, - const struct sha256_double *txid) + const struct bitcoin_txid *txid) { 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. */ for (i = 0; i < tal_count(b->full_txs); i++) { const struct bitcoin_tx *tx = b->full_txs[i]; - struct sha256_double txid; + struct bitcoin_txid txid; size_t j; /* 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, - const struct sha256_double *txid) + const struct bitcoin_txid *txid) { size_t i, n = tal_count(b->txs); for (i = 0; i < n; i++) { - struct sha256_double this_txid; + struct bitcoin_txid this_txid; bitcoin_txid(b->txs[i], &this_txid); if (structeq(&this_txid, txid)) return b->txs[i]; @@ -123,7 +123,7 @@ static const struct bitcoin_tx *tx_in_block(const struct block *b, /* FIXME: Use hash table. */ 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) { 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, - const struct sha256_double *txid, + const struct bitcoin_txid *txid, const struct bitcoin_tx **tx) { const struct block *b; @@ -274,7 +274,7 @@ void broadcast_tx(struct chain_topology *topo, tal_add_destructor2(peer, clear_otx_peer, otx); 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 (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, - const struct sha256_double *txid) + const struct bitcoin_txid *txid) { struct block *block = block_for_tx(topo, txid, NULL); if (block == NULL) { @@ -554,7 +554,7 @@ struct txlocator *locate_tx(const void *ctx, const struct chain_topology *topo, loc->blkheight = block->height; size_t i, n = tal_count(block->txs); for (i = 0; i < n; i++) { - struct sha256_double this_txid; + struct bitcoin_txid this_txid; bitcoin_txid(block->txs[i], &this_txid); if (structeq(&this_txid, txid)){ loc->index = block->txnums[i]; diff --git a/lightningd/chaintopology.h b/lightningd/chaintopology.h index 09a4dbc3a..42a189b5c 100644 --- a/lightningd/chaintopology.h +++ b/lightningd/chaintopology.h @@ -2,7 +2,7 @@ #define LIGHTNING_LIGHTNINGD_CHAINTOPOLOGY_H #include "config.h" #include -#include +#include #include #include #include @@ -31,7 +31,7 @@ struct outgoing_tx { struct list_node list; struct peer *peer; const char *hextx; - struct sha256_double txid; + struct bitcoin_txid txid; void (*failed)(struct peer *peer, int exitstatus, const char *err); /* FIXME: Remove this. */ struct chain_topology *topo; @@ -139,7 +139,7 @@ struct txlocator { /* 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). */ 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); /* Get highest block number. */ @@ -161,7 +161,7 @@ void setup_topology(struct chain_topology *topology, struct timers *timers, 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_feerate_change(struct lightningd *ld); diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c index 7dd1ad88d..e17fd698b 100644 --- a/lightningd/jsonrpc.c +++ b/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, - const struct sha256_double *txid) + const struct bitcoin_txid *txid) { char hex[hex_str_size(sizeof(*txid))]; diff --git a/lightningd/jsonrpc.h b/lightningd/jsonrpc.h index 6187e0652..b8a0d808b 100644 --- a/lightningd/jsonrpc.h +++ b/lightningd/jsonrpc.h @@ -5,7 +5,7 @@ #include #include -struct sha256_double; +struct bitcoin_txid; struct wireaddr; /* Context for a command (from JSON, but might outlive the connection!) @@ -69,7 +69,7 @@ void json_add_pubkey(struct json_result *response, /* '"fieldname" : ' or "" if fieldname is NULL */ 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 */ bool json_tok_pubkey(const char *buffer, const jsmntok_t *tok, diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index a552024c0..31daaffd8 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -1108,7 +1108,7 @@ static enum watch_result onchain_tx_watched(struct peer *peer, void *unused) { u8 *msg; - struct sha256_double txid; + struct bitcoin_txid txid; if (depth == 0) { 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, const struct bitcoin_tx *tx) { - struct sha256_double txid; + struct bitcoin_txid txid; struct txwatch *txw; bitcoin_txid(tx, &txid); @@ -1379,7 +1379,7 @@ static enum watch_result funding_spent(struct peer *peer, void *unused) { u8 *msg, *scriptpubkey; - struct sha256_double our_last_txid; + struct bitcoin_txid our_last_txid; s64 keyindex; struct pubkey ourkey; struct htlc_stub *stubs; @@ -1495,13 +1495,13 @@ static enum watch_result funding_lockin_cb(struct peer *peer, unsigned int depth, void *unused) { - struct sha256_double txid; + struct bitcoin_txid txid; const char *txidstr; struct txlocator *loc; bool peer_ready; 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", txidstr, depth, peer->minimum_depth); tal_free(txidstr); @@ -2153,7 +2153,7 @@ static void opening_funder_finished(struct subd *opening, const u8 *resp, u8 *msg; struct channel_info *channel_info; struct utxo *utxos; - struct sha256_double funding_txid; + struct bitcoin_txid funding_txid; struct pubkey changekey; struct pubkey local_fundingkey; 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", i, fc->utxomap[i]->amount, 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->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); 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 */ 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, &channel_info->their_config, remote_commit, @@ -2334,7 +2334,7 @@ static void opening_fundee_finished(struct subd *opening, } 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)); watch_txid(peer, peer->ld->topology, peer, peer->funding_txid, funding_lockin_cb, NULL); diff --git a/lightningd/peer_control.h b/lightningd/peer_control.h index 25e6276b5..d90a8b7d7 100644 --- a/lightningd/peer_control.h +++ b/lightningd/peer_control.h @@ -69,7 +69,7 @@ struct peer { u64 next_htlc_id; /* Funding txid and amounts (once known) */ - struct sha256_double *funding_txid; + struct bitcoin_txid *funding_txid; u16 funding_outnum; u64 funding_satoshi, push_msat; bool remote_funding_locked; diff --git a/lightningd/test/run-commit_tx.c b/lightningd/test/run-commit_tx.c index dfad78680..51faee2bb 100644 --- a/lightningd/test/run-commit_tx.c +++ b/lightningd/test/run-commit_tx.c @@ -20,26 +20,14 @@ static bool print_superverbose; /* Turn this on to brute-force fee values */ /*#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! */ -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 sha256 rev = sha256_from_hex(hex); - size_t i; + struct bitcoin_txid txid; - for (i = 0; i < sizeof(rev); i++) - sha256.sha.u.u8[sizeof(sha256) - 1 - i] = rev.u.u8[i]; - return sha256; + if (!bitcoin_txid_from_hex(hex, strlen(hex), &txid)) + abort(); + return txid; } 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); size_t i, n; - struct sha256_double txid; + struct bitcoin_txid txid; struct bitcoin_tx **htlc_tx; secp256k1_ecdsa_signature *remotehtlcsig; struct keyset keyset; @@ -439,7 +427,7 @@ static const struct htlc **invert_htlcs(const struct htlc **htlcs) int main(void) { tal_t *tmpctx = tal_tmpctx(NULL); - struct sha256_double funding_txid; + struct bitcoin_txid funding_txid; u64 funding_amount_satoshi, dust_limit_satoshi; u32 feerate_per_kw; u16 to_self_delay; diff --git a/lightningd/watch.c b/lightningd/watch.c index 8ed676c32..d23499c6e 100644 --- a/lightningd/watch.c +++ b/lightningd/watch.c @@ -65,17 +65,18 @@ static void destroy_txowatch(struct txowatch *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; } -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); } @@ -88,7 +89,7 @@ static void destroy_txwatch(struct txwatch *w) struct txwatch *watch_txid_(const tal_t *ctx, struct chain_topology *topo, struct peer *peer, - const struct sha256_double *txid, + const struct bitcoin_txid *txid, enum watch_result (*cb)(struct peer *peer, const struct bitcoin_tx *, unsigned int depth, @@ -112,7 +113,7 @@ struct txwatch *watch_txid_(const tal_t *ctx, } 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; } @@ -127,7 +128,7 @@ struct txwatch *watch_tx_(const tal_t *ctx, void *arg), void *cb_arg) { - struct sha256_double txid; + struct bitcoin_txid txid; bitcoin_txid(tx, &txid); 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 chain_topology *topo, struct peer *peer, - const struct sha256_double *txid, + const struct bitcoin_txid *txid, unsigned int output, enum watch_result (*cb)(struct peer *peer, const struct bitcoin_tx *tx, @@ -173,7 +174,7 @@ static bool txw_fire(struct chain_topology *topo, log_debug(txw->peer->log, "Got depth change %u->%u for %s", txw->depth, depth, - type_to_string(ltmp, struct sha256_double, &txw->txid)); + type_to_string(ltmp, struct bitcoin_txid, &txw->txid)); txw->depth = depth; r = txw->cb(txw->peer, tx, txw->depth, txw->cbdata); switch (r) { @@ -190,7 +191,7 @@ void txwatch_fire(struct chain_topology *topo, const struct bitcoin_tx *tx, unsigned int depth) { - struct sha256_double txid; + struct bitcoin_txid txid; struct txwatch *txw; bitcoin_txid(tx, &txid); @@ -206,15 +207,15 @@ void txowatch_fire(struct chain_topology *topo, size_t input_num, const struct block *block) { - struct sha256_double txid; + struct bitcoin_txid txid; enum watch_result r; bitcoin_txid(tx, &txid); log_debug(txow->peer->log, "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, - 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); switch (r) { diff --git a/lightningd/watch.h b/lightningd/watch.h index 6a4526409..e4ba2809a 100644 --- a/lightningd/watch.h +++ b/lightningd/watch.h @@ -1,7 +1,7 @@ #ifndef LIGHTNING_LIGHTNINGD_WATCH_H #define LIGHTNING_LIGHTNINGD_WATCH_H #include "config.h" -#include "bitcoin/shadouble.h" +#include #include #include #include @@ -17,7 +17,7 @@ enum watch_result { }; struct txwatch_output { - struct sha256_double txid; + struct bitcoin_txid txid; unsigned int index; }; @@ -55,7 +55,7 @@ struct txwatch { struct peer *peer; /* Transaction to watch. */ - struct sha256_double txid; + struct bitcoin_txid txid; unsigned int depth; /* A new depth (0 if kicked out, otherwise 1 = tip, etc.) */ @@ -67,9 +67,9 @@ struct txwatch { void *cbdata; }; -const struct sha256_double *txwatch_keyof(const struct txwatch *w); -size_t txid_hash(const struct sha256_double *txid); -bool txwatch_eq(const struct txwatch *w, const struct sha256_double *txid); +const struct bitcoin_txid *txwatch_keyof(const struct txwatch *w); +size_t txid_hash(const struct bitcoin_txid *txid); +bool txwatch_eq(const struct txwatch *w, const struct bitcoin_txid *txid); HTABLE_DEFINE_TYPE(struct txwatch, txwatch_keyof, txid_hash, txwatch_eq, 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 chain_topology *topo, struct peer *peer, - const struct sha256_double *txid, + const struct bitcoin_txid *txid, enum watch_result (*cb)(struct peer *peer, const struct bitcoin_tx *, unsigned int depth, @@ -115,7 +115,7 @@ struct txwatch *watch_tx_(const tal_t *ctx, struct txowatch *watch_txo_(const tal_t *ctx, struct chain_topology *topo, struct peer *peer, - const struct sha256_double *txid, + const struct bitcoin_txid *txid, unsigned int output, enum watch_result (*cb)(struct peer *peer, const struct bitcoin_tx *tx, @@ -144,7 +144,7 @@ void txowatch_fire(struct chain_topology *topo, const struct block *block); 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); #endif /* LIGHTNING_LIGHTNINGD_WATCH_H */ diff --git a/onchaind/onchain.c b/onchaind/onchain.c index f4533653f..b2a90e65b 100644 --- a/onchaind/onchain.c +++ b/onchaind/onchain.c @@ -68,14 +68,14 @@ struct proposed_resolution { /* How it actually got resolved. */ struct resolution { - struct sha256_double txid; + struct bitcoin_txid txid; unsigned int depth; enum tx_type tx_type; }; struct tracked_output { enum tx_type tx_type; - struct sha256_double txid; + struct bitcoin_txid txid; u32 tx_blockheight; u32 outnum; u64 satoshi; @@ -257,7 +257,7 @@ static struct bitcoin_tx *tx_to_us(const tal_t *ctx, static struct tracked_output * new_tracked_output(struct tracked_output ***outs, - const struct sha256_double *txid, + const struct bitcoin_txid *txid, u32 tx_blockheight, enum tx_type tx_type, u32 outnum, @@ -272,7 +272,7 @@ static struct tracked_output * status_trace("Tracking output %u of %s: %s/%s", outnum, - type_to_string(trc, struct sha256_double, txid), + type_to_string(trc, struct bitcoin_txid, txid), tx_type_name(tx_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", 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), 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. */ 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 (!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. */ static void resolved_by_other(struct tracked_output *out, - const struct sha256_double *txid, + const struct bitcoin_txid *txid, enum tx_type tx_type) { 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), output_type_name(out->output_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, @@ -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. */ -static bool is_local_commitment(const struct sha256_double *txid, - const struct sha256_double *our_broadcast_txid) +static bool is_local_commitment(const struct bitcoin_txid *txid, + const struct bitcoin_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) { u8 *msg; - struct sha256_double txid; + struct bitcoin_txid 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, size_t out_index, const struct bitcoin_tx *htlc_tx, - const struct sha256_double *htlc_txid, + const struct bitcoin_txid *htlc_txid, u32 tx_blockheight) { struct tracked_output *out; @@ -670,7 +670,7 @@ static void output_spent(struct tracked_output ***outs, u32 input_num, u32 tx_blockheight) { - struct sha256_double txid; + struct bitcoin_txid 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 */ 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].index); 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, - const struct sha256_double *txid, u32 depth) + const struct bitcoin_txid *txid, u32 depth) { size_t i; @@ -932,7 +932,7 @@ static void wait_for_resolved(struct tracked_output **outs) { while (!all_irrevocably_resolved(outs)) { 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); u32 input_num, depth, tx_blockheight; 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, - const struct sha256_double *txid, + const struct bitcoin_txid *txid, struct tracked_output **outs) { 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, u32 tx_blockheight, - const struct sha256_double *txid, + const struct bitcoin_txid *txid, const struct secrets *secrets, const struct sha256 *shaseed, const struct pubkey *remote_revocation_basepoint, @@ -1450,7 +1450,7 @@ static void steal_htlc(struct tracked_output *out) * claim all the funds. */ static void handle_their_cheat(const struct bitcoin_tx *tx, - const struct sha256_double *txid, + const struct bitcoin_txid *txid, u32 tx_blockheight, const struct sha256 *revocation_preimage, 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, u32 tx_blockheight, - const struct sha256_double *txid, + const struct bitcoin_txid *txid, const struct secrets *secrets, const struct sha256 *shaseed, const struct pubkey *remote_per_commitment_point, @@ -1950,7 +1950,7 @@ int main(int argc, char *argv[]) struct secrets secrets; struct sha256 shaseed; struct tracked_output **outs; - struct sha256_double our_broadcast_txid, txid; + struct bitcoin_txid our_broadcast_txid, txid; secp256k1_ecdsa_signature *remote_htlc_sigs; u64 funding_amount_satoshi, num_htlcs; u8 *scriptpubkey[NUM_SIDES]; diff --git a/onchaind/onchain_wire.csv b/onchaind/onchain_wire.csv index 38383f7ee..16a392a9d 100644 --- a/onchaind/onchain_wire.csv +++ b/onchaind/onchain_wire.csv @@ -14,7 +14,7 @@ onchain_init,,feerate_per_kw,u32 onchain_init,,local_dust_limit_satoshi,u64 onchain_init,,remote_revocation_basepoint,struct pubkey # 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,local_scriptpubkey_len*u8 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. onchain_depth,5005 -onchain_depth,,txid,struct sha256_double +onchain_depth,,txid,struct bitcoin_txid onchain_depth,,depth,u32 # onchaind->master: We don't want to watch this tx, or its outputs onchain_unwatch_tx,5006 -onchain_unwatch_tx,,txid,struct sha256_double +onchain_unwatch_tx,,txid,struct bitcoin_txid onchain_unwatch_tx,,num_outputs,u32 # master->onchaind: We know HTLC preimage diff --git a/openingd/opening.c b/openingd/opening.c index 3357c3779..b60595e1c 100644 --- a/openingd/opening.c +++ b/openingd/opening.c @@ -47,7 +47,7 @@ struct state { /* Funding and feerate: set by opening peer. */ u64 funding_satoshis, push_msat; u32 feerate_per_kw; - struct sha256_double funding_txid; + struct bitcoin_txid funding_txid; u16 funding_txout; /* 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)); msg = towire_funding_created(state, &state->channel_id, - &state->funding_txid.sha, + &state->funding_txid, state->funding_txout, &sig); 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)); if (!fromwire_funding_created(msg, NULL, &id_in, - &state->funding_txid.sha, + &state->funding_txid, &state->funding_txout, &theirsig)) peer_failed(PEER_FD, &state->cs, &state->channel_id, diff --git a/openingd/opening_wire.csv b/openingd/opening_wire.csv index 598336869..306d6b7fd 100644 --- a/openingd/opening_wire.csv +++ b/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,,minimum_depth,u32 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 # 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,,their_per_commit_point,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_satoshis,u64 opening_fundee_reply,,push_msat,u64 diff --git a/tools/generate-wire.py b/tools/generate-wire.py index a130c0381..809d858af 100755 --- a/tools/generate-wire.py +++ b/tools/generate-wire.py @@ -19,6 +19,7 @@ type2size = { 'struct pubkey': 33, 'struct sha256': 32, 'struct sha256_double': 32, + 'struct bitcoin_txid': 32, 'u64': 8, 'u32': 4, 'u16': 2, @@ -78,6 +79,7 @@ partialtypemap = { 'features': FieldType('u8'), 'channel_id': FieldType('struct channel_id'), 'chain_hash': FieldType('struct sha256_double'), + 'funding_txid': FieldType('struct bitcoin_txid'), 'pad': FieldType('pad'), } diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index cbae8ae87..0a34e3047 100644 --- a/wallet/test/run-wallet.c +++ b/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 peer p; 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 changed_htlc last_commit; secp256k1_ecdsa_signature *sig = tal(w, secp256k1_ecdsa_signature); diff --git a/wallet/wallet.c b/wallet/wallet.c index 6975b67f3..7c44f3982 100644 --- a/wallet/wallet.c +++ b/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, - const struct sha256_double *txid, + const struct bitcoin_txid *txid, const u32 outnum, enum output_status oldstatus, 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) { 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); } else { 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", output, tx->output[output].amount, is_p2sh ? "P2SH" : "SEGWIT", - type_to_string(ltmp, struct sha256_double, + type_to_string(ltmp, struct bitcoin_txid, &utxo->txid)); if (!wallet_add_utxo(w, utxo, is_p2sh ? p2sh_wpkh : our_change)) { diff --git a/wallet/wallet.h b/wallet/wallet.h index 74d66a6c6..5c8ff2a62 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -118,7 +118,7 @@ bool wallet_add_utxo(struct wallet *w, struct utxo *utxo, * `output_state_any` as @oldstatus. */ 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, enum output_status newstatus); diff --git a/wire/fromwire.c b/wire/fromwire.c index 4d4badd77..39300e814 100644 --- a/wire/fromwire.c +++ b/wire/fromwire.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -176,6 +177,12 @@ void fromwire_sha256_double(const u8 **cursor, size_t *max, 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) { 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). */ 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)); memcpy(channel_id, txid, sizeof(*channel_id)); diff --git a/wire/test/run-peer-wire.c b/wire/test/run-peer-wire.c index a0bdc0015..280c7bf2b 100644 --- a/wire/test/run-peer-wire.c +++ b/wire/test/run-peer-wire.c @@ -82,7 +82,7 @@ struct msg_closing_signed { }; struct msg_funding_created { struct channel_id temporary_channel_id; - struct sha256 txid; + struct bitcoin_txid txid; u16 output_index; secp256k1_ecdsa_signature signature; }; diff --git a/wire/towire.c b/wire/towire.c index 4ca3fd7e7..c377ec147 100644 --- a/wire/towire.c +++ b/wire/towire.c @@ -1,6 +1,7 @@ #include "wire.h" #include #include +#include #include #include #include @@ -119,6 +120,11 @@ void towire_sha256_double(u8 **pptr, const struct sha256_double *sha256d) 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) { towire(pptr, preimage, sizeof(*preimage)); diff --git a/wire/wire.h b/wire/wire.h index f89abe6e5..1b24d55bd 100644 --- a/wire/wire.h +++ b/wire/wire.h @@ -15,11 +15,12 @@ struct channel_id { u8 id[32]; }; +struct bitcoin_txid; struct preimage; struct ripemd160; 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. */ 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); void towire_sha256(u8 **pptr, const struct sha256 *sha256); 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_ripemd160(u8 **pptr, const struct ripemd160 *ripemd); 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_double(const u8 **cursor, size_t *max, 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_ripemd160(const u8 **cursor, size_t *max, struct ripemd160 *ripemd); void fromwire_pad(const u8 **cursor, size_t *max, size_t num);