diff --git a/bitcoin/tx.c b/bitcoin/tx.c index 68b03abe4..41d2c887e 100644 --- a/bitcoin/tx.c +++ b/bitcoin/tx.c @@ -276,10 +276,12 @@ static void bitcoin_tx_destroy(struct bitcoin_tx *tx) wally_tx_free(tx->wtx); } -struct bitcoin_tx *bitcoin_tx(const tal_t *ctx, varint_t input_count, - varint_t output_count) +struct bitcoin_tx *bitcoin_tx(const tal_t *ctx, + const struct chainparams *chainparams, + varint_t input_count, varint_t output_count) { struct bitcoin_tx *tx = tal(ctx, struct bitcoin_tx); + wally_tx_init_alloc(WALLY_TX_VERSION_2, 0, input_count, output_count, &tx->wtx); tal_add_destructor(tx, bitcoin_tx_destroy); @@ -287,6 +289,7 @@ struct bitcoin_tx *bitcoin_tx(const tal_t *ctx, varint_t input_count, tx->input_amounts = tal_arrz(tx, struct amount_sat*, input_count); tx->wtx->locktime = 0; tx->wtx->version = 2; + tx->chainparams = chainparams; return tx; } @@ -305,6 +308,7 @@ struct bitcoin_tx *pull_bitcoin_tx(const tal_t *ctx, const u8 **cursor, /* We don't know the input amounts yet, so set them all to NULL */ tx->input_amounts = tal_arrz(tx, struct amount_sat *, tx->wtx->inputs_allocation_len); + tx->chainparams = NULL; *cursor += wsize; *max -= wsize; diff --git a/bitcoin/tx.h b/bitcoin/tx.h index 785fdde65..8cdeb6f42 100644 --- a/bitcoin/tx.h +++ b/bitcoin/tx.h @@ -23,6 +23,9 @@ struct bitcoin_tx { * unknown) */ struct amount_sat **input_amounts; struct wally_tx *wtx; + + /* Keep a reference to the ruleset we have to abide by */ + const struct chainparams *chainparams; }; struct bitcoin_tx_output { @@ -52,8 +55,9 @@ size_t measure_tx_weight(const struct bitcoin_tx *tx); /* Allocate a tx: you just need to fill in inputs and outputs (they're * zeroed with inputs' sequence_number set to FFFFFFFF) */ -struct bitcoin_tx *bitcoin_tx(const tal_t *ctx, varint_t input_count, - varint_t output_count); +struct bitcoin_tx *bitcoin_tx(const tal_t *ctx, + const struct chainparams *chainparams, + varint_t input_count, varint_t output_count); /* This takes a raw bitcoin tx in hex. */ struct bitcoin_tx *bitcoin_tx_from_hex(const tal_t *ctx, const char *hex, diff --git a/channeld/channeld.c b/channeld/channeld.c index ac8509dbd..e7df12412 100644 --- a/channeld/channeld.c +++ b/channeld/channeld.c @@ -979,10 +979,9 @@ static secp256k1_ecdsa_signature *calc_commitsigs(const tal_t *ctx, const u8 *msg; secp256k1_ecdsa_signature *htlc_sigs; - txs = channel_txs(tmpctx, &htlc_map, &wscripts, peer->channel, - &peer->remote_per_commit, - commit_index, - REMOTE); + txs = channel_txs(tmpctx, peer->channel->chainparams, &htlc_map, + &wscripts, peer->channel, &peer->remote_per_commit, + commit_index, REMOTE); msg = towire_hsm_sign_remote_commitment_tx(NULL, txs[0], &peer->channel->funding_pubkey[REMOTE], @@ -1398,9 +1397,10 @@ static void handle_peer_commit_sig(struct peer *peer, const u8 *msg) /* SIGHASH_ALL is implied. */ commit_sig.sighash_type = SIGHASH_ALL; - txs = channel_txs(tmpctx, &htlc_map, &wscripts, peer->channel, - &peer->next_local_per_commit, - peer->next_index[LOCAL], LOCAL); + txs = + channel_txs(tmpctx, peer->channel->chainparams, &htlc_map, + &wscripts, peer->channel, &peer->next_local_per_commit, + peer->next_index[LOCAL], LOCAL); if (!derive_simple_key(&peer->channel->basepoints[REMOTE].htlc, &peer->next_local_per_commit, &remote_htlckey)) diff --git a/channeld/commit_tx.c b/channeld/commit_tx.c index ca2d232ce..ccc99fd6f 100644 --- a/channeld/commit_tx.c +++ b/channeld/commit_tx.c @@ -76,6 +76,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 chainparams *chainparams, const struct bitcoin_txid *funding_txid, unsigned int funding_txout, struct amount_sat funding, @@ -146,7 +147,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx, #endif /* Worst-case sizing: both to-local and to-remote outputs. */ - tx = bitcoin_tx(ctx, 1, untrimmed + 2); + tx = bitcoin_tx(ctx, chainparams, 1, untrimmed + 2); /* We keep track of which outputs have which HTLCs */ *htlcmap = tal_arr(tx, const struct htlc *, tx->wtx->outputs_allocation_len); diff --git a/channeld/commit_tx.h b/channeld/commit_tx.h index 748a81ae7..2ad7c72fa 100644 --- a/channeld/commit_tx.h +++ b/channeld/commit_tx.h @@ -1,6 +1,7 @@ #ifndef LIGHTNING_CHANNELD_COMMIT_TX_H #define LIGHTNING_CHANNELD_COMMIT_TX_H #include "config.h" +#include #include #include #include @@ -43,6 +44,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 chainparams *chainparams, const struct bitcoin_txid *funding_txid, unsigned int funding_txout, struct amount_sat funding, diff --git a/channeld/full_channel.c b/channeld/full_channel.c index c030af787..4ec343c54 100644 --- a/channeld/full_channel.c +++ b/channeld/full_channel.c @@ -187,7 +187,8 @@ static bool sum_offered_msatoshis(struct amount_msat *total, return true; } -static void add_htlcs(struct bitcoin_tx ***txs, +static void add_htlcs(const struct chainparams *chainparams, + struct bitcoin_tx ***txs, const u8 ***wscripts, const struct htlc **htlcmap, const struct channel *channel, @@ -210,7 +211,7 @@ static void add_htlcs(struct bitcoin_tx ***txs, continue; if (htlc_owner(htlc) == side) { - tx = htlc_timeout_tx(*txs, &txid, i, + tx = htlc_timeout_tx(*txs, chainparams, &txid, i, htlc->amount, htlc->expiry.locktime, channel->config[!side].to_self_delay, @@ -222,7 +223,7 @@ static void add_htlcs(struct bitcoin_tx ***txs, &htlc->rhash, &keyset->self_revocation_key); } else { - tx = htlc_success_tx(*txs, &txid, i, + tx = htlc_success_tx(*txs, chainparams, &txid, i, htlc->amount, channel->config[!side].to_self_delay, feerate_per_kw, @@ -245,6 +246,7 @@ static void add_htlcs(struct bitcoin_tx ***txs, /* FIXME: We could cache these. */ struct bitcoin_tx **channel_txs(const tal_t *ctx, + const struct chainparams *chainparams, const struct htlc ***htlcmap, const u8 ***wscripts, const struct channel *channel, @@ -266,27 +268,21 @@ struct bitcoin_tx **channel_txs(const tal_t *ctx, gather_htlcs(ctx, channel, side, &committed, NULL, NULL); txs = tal_arr(ctx, struct bitcoin_tx *, 1); - txs[0] = commit_tx(ctx, &channel->funding_txid, - channel->funding_txout, - channel->funding, - channel->funder, - channel->config[!side].to_self_delay, - &keyset, - channel->view[side].feerate_per_kw, - channel->config[side].dust_limit, - channel->view[side].owed[side], - channel->view[side].owed[!side], - committed, - htlcmap, - commitment_number ^ channel->commitment_number_obscurer, - side); + txs[0] = commit_tx( + ctx, chainparams, &channel->funding_txid, channel->funding_txout, + channel->funding, channel->funder, + channel->config[!side].to_self_delay, &keyset, + channel->view[side].feerate_per_kw, + channel->config[side].dust_limit, channel->view[side].owed[side], + channel->view[side].owed[!side], committed, htlcmap, + commitment_number ^ channel->commitment_number_obscurer, side); *wscripts = tal_arr(ctx, const u8 *, 1); (*wscripts)[0] = bitcoin_redeem_2of2(*wscripts, &channel->funding_pubkey[side], &channel->funding_pubkey[!side]); - add_htlcs(&txs, wscripts, *htlcmap, channel, &keyset, side); + add_htlcs(chainparams, &txs, wscripts, *htlcmap, channel, &keyset, side); tal_free(committed); return txs; diff --git a/channeld/full_channel.h b/channeld/full_channel.h index 3c2e6e7bb..e55daf3be 100644 --- a/channeld/full_channel.h +++ b/channeld/full_channel.h @@ -47,6 +47,7 @@ struct channel *new_full_channel(const tal_t *ctx, /** * channel_txs: Get the current commitment and htlc txs for the channel. * @ctx: tal context to allocate return value from. + * @chainparams: Parameters for the generated transactions. * @channel: The channel to evaluate * @htlc_map: Pointer to htlcs for each tx output (allocated off @ctx). * @wscripts: Pointer to array of wscript for each tx returned (alloced off @ctx) @@ -59,6 +60,7 @@ struct channel *new_full_channel(const tal_t *ctx, * fills in @htlc_map, or NULL on key derivation failure. */ struct bitcoin_tx **channel_txs(const tal_t *ctx, + const struct chainparams *chainparams, const struct htlc ***htlcmap, const u8 ***wscripts, const struct channel *channel, diff --git a/channeld/test/run-commit_tx.c b/channeld/test/run-commit_tx.c index 906981ee9..ccaf0d6c4 100644 --- a/channeld/test/run-commit_tx.c +++ b/channeld/test/run-commit_tx.c @@ -222,7 +222,8 @@ static void report_htlcs(const struct bitcoin_tx *tx, continue; if (htlc_owner(htlc) == LOCAL) { - htlc_tx[i] = htlc_timeout_tx(htlc_tx, &txid, i, + htlc_tx[i] = htlc_timeout_tx(htlc_tx, tx->chainparams, + &txid, i, htlc->amount, htlc->expiry.locktime, to_self_delay, @@ -234,7 +235,8 @@ static void report_htlcs(const struct bitcoin_tx *tx, &htlc->rhash, remote_revocation_key); } else { - htlc_tx[i] = htlc_success_tx(htlc_tx, &txid, i, + htlc_tx[i] = htlc_success_tx(htlc_tx, tx->chainparams, + &txid, i, htlc->amount, to_self_delay, feerate_per_kw, @@ -457,6 +459,7 @@ int main(void) u64 commitment_number, cn_obscurer; struct amount_msat to_local, to_remote; const struct htlc **htlcs, **htlc_map, **htlc_map2, **inv_htlcs; + const struct chainparams *chainparams = chainparams_for_network("bitcoin"); secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN); @@ -712,7 +715,8 @@ int main(void) keyset.other_htlc_key = remote_htlckey; print_superverbose = true; - tx = commit_tx(tmpctx, &funding_txid, funding_output_index, + tx = commit_tx(tmpctx, chainparams, + &funding_txid, funding_output_index, funding_amount, LOCAL, to_self_delay, &keyset, @@ -723,7 +727,8 @@ int main(void) NULL, &htlc_map, commitment_number ^ cn_obscurer, LOCAL); print_superverbose = false; - tx2 = commit_tx(tmpctx, &funding_txid, funding_output_index, + tx2 = commit_tx(tmpctx, chainparams, + &funding_txid, funding_output_index, funding_amount, REMOTE, to_self_delay, &keyset, @@ -766,7 +771,8 @@ int main(void) to_local.millisatoshis, to_remote.millisatoshis, feerate_per_kw); print_superverbose = true; - tx = commit_tx(tmpctx, &funding_txid, funding_output_index, + tx = commit_tx(tmpctx, chainparams, + &funding_txid, funding_output_index, funding_amount, LOCAL, to_self_delay, &keyset, @@ -777,7 +783,8 @@ int main(void) htlcs, &htlc_map, commitment_number ^ cn_obscurer, LOCAL); print_superverbose = false; - tx2 = commit_tx(tmpctx, &funding_txid, funding_output_index, + tx2 = commit_tx(tmpctx, chainparams, + &funding_txid, funding_output_index, funding_amount, REMOTE, to_self_delay, &keyset, @@ -808,7 +815,8 @@ int main(void) feerate_per_kw = increase(feerate_per_kw); print_superverbose = false; - newtx = commit_tx(tmpctx, &funding_txid, funding_output_index, + newtx = commit_tx(tmpctx, chainparams, + &funding_txid, funding_output_index, funding_amount, LOCAL, to_self_delay, &keyset, @@ -820,7 +828,8 @@ int main(void) commitment_number ^ cn_obscurer, LOCAL); /* This is what it would look like for peer generating it! */ - tx2 = commit_tx(tmpctx, &funding_txid, funding_output_index, + tx2 = commit_tx(tmpctx, chainparams, + &funding_txid, funding_output_index, funding_amount, REMOTE, to_self_delay, &keyset, @@ -851,7 +860,8 @@ int main(void) to_local.millisatoshis, to_remote.millisatoshis, feerate_per_kw-1); /* Recalc with verbosity on */ print_superverbose = true; - tx = commit_tx(tmpctx, &funding_txid, funding_output_index, + tx = commit_tx(tmpctx, chainparams, + &funding_txid, funding_output_index, funding_amount, LOCAL, to_self_delay, &keyset, @@ -887,7 +897,8 @@ int main(void) to_local.millisatoshis, to_remote.millisatoshis, feerate_per_kw); /* Recalc with verbosity on */ print_superverbose = true; - newtx = commit_tx(tmpctx, &funding_txid, funding_output_index, + newtx = commit_tx(tmpctx, chainparams, + &funding_txid, funding_output_index, funding_amount, LOCAL, to_self_delay, &keyset, @@ -945,7 +956,8 @@ int main(void) "to_remote_msat: %"PRIu64"\n" "local_feerate_per_kw: %u\n", to_local.millisatoshis, to_remote.millisatoshis, feerate_per_kw); - tx = commit_tx(tmpctx, &funding_txid, funding_output_index, + tx = commit_tx(tmpctx, chainparams, + &funding_txid, funding_output_index, funding_amount, LOCAL, to_self_delay, &keyset, diff --git a/channeld/test/run-full_channel.c b/channeld/test/run-full_channel.c index 26be3ceb1..304ed075c 100644 --- a/channeld/test/run-full_channel.c +++ b/channeld/test/run-full_channel.c @@ -494,7 +494,8 @@ int main(void) keyset.self_htlc_key = keyset.self_payment_key; keyset.other_htlc_key = keyset.other_payment_key; - raw_tx = commit_tx(tmpctx, &funding_txid, funding_output_index, + raw_tx = commit_tx(tmpctx, chainparams, + &funding_txid, funding_output_index, funding_amount, LOCAL, remote_config->to_self_delay, &keyset, @@ -504,7 +505,8 @@ int main(void) to_remote, NULL, &htlc_map, 0x2bb038521914 ^ 42, LOCAL); - txs = channel_txs(tmpctx, &htlc_map, &wscripts, + txs = channel_txs(tmpctx, chainparams, + &htlc_map, &wscripts, lchannel, &local_per_commitment_point, 42, LOCAL); assert(tal_count(txs) == 1); assert(tal_count(htlc_map) == 2); @@ -512,7 +514,8 @@ int main(void) assert(scripteq(wscripts[0], funding_wscript)); tx_must_be_eq(txs[0], raw_tx); - txs2 = channel_txs(tmpctx, &htlc_map, &wscripts, + txs2 = channel_txs(tmpctx, chainparams, + &htlc_map, &wscripts, rchannel, &local_per_commitment_point, 42, REMOTE); txs_must_be_eq(txs, txs2); @@ -539,10 +542,10 @@ int main(void) assert(lchannel->view[REMOTE].owed[REMOTE].millisatoshis == rchannel->view[LOCAL].owed[LOCAL].millisatoshis); - txs = channel_txs(tmpctx, &htlc_map, &wscripts, + txs = channel_txs(tmpctx, chainparams,&htlc_map, &wscripts, lchannel, &local_per_commitment_point, 42, LOCAL); assert(tal_count(txs) == 1); - txs2 = channel_txs(tmpctx, &htlc_map, &wscripts, + txs2 = channel_txs(tmpctx, chainparams, &htlc_map, &wscripts, rchannel, &local_per_commitment_point, 42, REMOTE); txs_must_be_eq(txs, txs2); @@ -557,10 +560,10 @@ int main(void) assert(lchannel->view[REMOTE].owed[REMOTE].millisatoshis == rchannel->view[LOCAL].owed[LOCAL].millisatoshis); - txs = channel_txs(tmpctx, &htlc_map, &wscripts, + txs = channel_txs(tmpctx, chainparams, &htlc_map, &wscripts, lchannel, &local_per_commitment_point, 42, LOCAL); assert(tal_count(txs) == 6); - txs2 = channel_txs(tmpctx, &htlc_map, &wscripts, + txs2 = channel_txs(tmpctx, chainparams, &htlc_map, &wscripts, rchannel, &local_per_commitment_point, 42, REMOTE); txs_must_be_eq(txs, txs2); @@ -570,6 +573,7 @@ int main(void) * output htlc_success_tx 0: 020000000001018154ecccf11a5fb56c39654c4deb4d2296f83c69268280b94d021370c94e219700000000000000000001e8030000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e050047304402206a6e59f18764a5bf8d4fa45eebc591566689441229c918b480fb2af8cc6a4aeb02205248f273be447684b33e3c8d1d85a8e0ca9fa0bae9ae33f0527ada9c162919a60147304402207cb324fa0de88f452ffa9389678127ebcf4cabe1dd848b8e076c1a1962bf34720220116ed922b12311bd602d67e60d2529917f21c5b82f25ff6506c0f87886b4dfd5012000000000000000000000000000000000000000000000000000000000000000008a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a914b8bcb07f6344b42ab04250c86a6e8b75d3fdbbc688527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f401b175ac686800000000 */ raw_tx = tx_from_hex(tmpctx, "020000000001018154ecccf11a5fb56c39654c4deb4d2296f83c69268280b94d021370c94e219700000000000000000001e8030000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e050047304402206a6e59f18764a5bf8d4fa45eebc591566689441229c918b480fb2af8cc6a4aeb02205248f273be447684b33e3c8d1d85a8e0ca9fa0bae9ae33f0527ada9c162919a60147304402207cb324fa0de88f452ffa9389678127ebcf4cabe1dd848b8e076c1a1962bf34720220116ed922b12311bd602d67e60d2529917f21c5b82f25ff6506c0f87886b4dfd5012000000000000000000000000000000000000000000000000000000000000000008a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a914b8bcb07f6344b42ab04250c86a6e8b75d3fdbbc688527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f401b175ac686800000000"); + raw_tx->chainparams = chainparams_for_network("bitcoin"); bitcoin_tx_input_set_witness(raw_tx, 0, NULL); tx_must_be_eq(raw_tx, txs[1]); @@ -578,6 +582,7 @@ int main(void) * output htlc_timeout_tx 2: 020000000001018154ecccf11a5fb56c39654c4deb4d2296f83c69268280b94d021370c94e219701000000000000000001d0070000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100d5275b3619953cb0c3b5aa577f04bc512380e60fa551762ce3d7a1bb7401cff9022037237ab0dac3fe100cde094e82e2bed9ba0ed1bb40154b48e56aa70f259e608b01483045022100c89172099507ff50f4c925e6c5150e871fb6e83dd73ff9fbb72f6ce829a9633f02203a63821d9162e99f9be712a68f9e589483994feae2661e4546cd5b6cec007be501008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a914b43e1b38138a41b37f7cd9a1d274bc63e3a9b5d188ac6868f6010000 */ raw_tx = tx_from_hex(tmpctx, "020000000001018154ecccf11a5fb56c39654c4deb4d2296f83c69268280b94d021370c94e219701000000000000000001d0070000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100d5275b3619953cb0c3b5aa577f04bc512380e60fa551762ce3d7a1bb7401cff9022037237ab0dac3fe100cde094e82e2bed9ba0ed1bb40154b48e56aa70f259e608b01483045022100c89172099507ff50f4c925e6c5150e871fb6e83dd73ff9fbb72f6ce829a9633f02203a63821d9162e99f9be712a68f9e589483994feae2661e4546cd5b6cec007be501008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a914b43e1b38138a41b37f7cd9a1d274bc63e3a9b5d188ac6868f6010000"); + raw_tx->chainparams = chainparams_for_network("bitcoin"); bitcoin_tx_input_set_witness(raw_tx, 0, NULL); tx_must_be_eq(raw_tx, txs[2]); @@ -586,6 +591,7 @@ int main(void) * output htlc_success_tx 1: 020000000001018154ecccf11a5fb56c39654c4deb4d2296f83c69268280b94d021370c94e219702000000000000000001d0070000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e050047304402201b63ec807771baf4fdff523c644080de17f1da478989308ad13a58b51db91d360220568939d38c9ce295adba15665fa68f51d967e8ed14a007b751540a80b325f20201483045022100def389deab09cee69eaa1ec14d9428770e45bcbe9feb46468ecf481371165c2f022015d2e3c46600b2ebba8dcc899768874cc6851fd1ecb3fffd15db1cc3de7e10da012001010101010101010101010101010101010101010101010101010101010101018a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a9144b6b2e5444c2639cc0fb7bcea5afba3f3cdce23988527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f501b175ac686800000000 */ raw_tx = tx_from_hex(tmpctx, "020000000001018154ecccf11a5fb56c39654c4deb4d2296f83c69268280b94d021370c94e219702000000000000000001d0070000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e050047304402201b63ec807771baf4fdff523c644080de17f1da478989308ad13a58b51db91d360220568939d38c9ce295adba15665fa68f51d967e8ed14a007b751540a80b325f20201483045022100def389deab09cee69eaa1ec14d9428770e45bcbe9feb46468ecf481371165c2f022015d2e3c46600b2ebba8dcc899768874cc6851fd1ecb3fffd15db1cc3de7e10da012001010101010101010101010101010101010101010101010101010101010101018a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a9144b6b2e5444c2639cc0fb7bcea5afba3f3cdce23988527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f501b175ac686800000000"); + raw_tx->chainparams = chainparams_for_network("bitcoin"); bitcoin_tx_input_set_witness(raw_tx, 0, NULL); tx_must_be_eq(raw_tx, txs[3]); @@ -594,6 +600,7 @@ int main(void) * output htlc_timeout_tx 3: 020000000001018154ecccf11a5fb56c39654c4deb4d2296f83c69268280b94d021370c94e219703000000000000000001b80b0000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100daee1808f9861b6c3ecd14f7b707eca02dd6bdfc714ba2f33bc8cdba507bb182022026654bf8863af77d74f51f4e0b62d461a019561bb12acb120d3f7195d148a554014730440220643aacb19bbb72bd2b635bc3f7375481f5981bace78cdd8319b2988ffcc6704202203d27784ec8ad51ed3bd517a05525a5139bb0b755dd719e0054332d186ac0872701008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a9148a486ff2e31d6158bf39e2608864d63fefd09d5b88ac6868f7010000 */ raw_tx = tx_from_hex(tmpctx, "020000000001018154ecccf11a5fb56c39654c4deb4d2296f83c69268280b94d021370c94e219703000000000000000001b80b0000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0500483045022100daee1808f9861b6c3ecd14f7b707eca02dd6bdfc714ba2f33bc8cdba507bb182022026654bf8863af77d74f51f4e0b62d461a019561bb12acb120d3f7195d148a554014730440220643aacb19bbb72bd2b635bc3f7375481f5981bace78cdd8319b2988ffcc6704202203d27784ec8ad51ed3bd517a05525a5139bb0b755dd719e0054332d186ac0872701008576a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c820120876475527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae67a9148a486ff2e31d6158bf39e2608864d63fefd09d5b88ac6868f7010000"); + raw_tx->chainparams = chainparams_for_network("bitcoin"); bitcoin_tx_input_set_witness(raw_tx, 0, NULL); tx_must_be_eq(raw_tx, txs[4]); @@ -602,6 +609,7 @@ int main(void) * output htlc_success_tx 4: 020000000001018154ecccf11a5fb56c39654c4deb4d2296f83c69268280b94d021370c94e219704000000000000000001a00f0000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e050047304402207e0410e45454b0978a623f36a10626ef17b27d9ad44e2760f98cfa3efb37924f0220220bd8acd43ecaa916a80bd4f919c495a2c58982ce7c8625153f8596692a801d014730440220549e80b4496803cbc4a1d09d46df50109f546d43fbbf86cd90b174b1484acd5402205f12a4f995cb9bded597eabfee195a285986aa6d93ae5bb72507ebc6a4e2349e012004040404040404040404040404040404040404040404040404040404040404048a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a91418bc1a114ccf9c052d3d23e28d3b0a9d1227434288527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f801b175ac686800000000 */ raw_tx = tx_from_hex(tmpctx, "020000000001018154ecccf11a5fb56c39654c4deb4d2296f83c69268280b94d021370c94e219704000000000000000001a00f0000000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e050047304402207e0410e45454b0978a623f36a10626ef17b27d9ad44e2760f98cfa3efb37924f0220220bd8acd43ecaa916a80bd4f919c495a2c58982ce7c8625153f8596692a801d014730440220549e80b4496803cbc4a1d09d46df50109f546d43fbbf86cd90b174b1484acd5402205f12a4f995cb9bded597eabfee195a285986aa6d93ae5bb72507ebc6a4e2349e012004040404040404040404040404040404040404040404040404040404040404048a76a91414011f7254d96b819c76986c277d115efce6f7b58763ac67210394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b7c8201208763a91418bc1a114ccf9c052d3d23e28d3b0a9d1227434288527c21030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e752ae677502f801b175ac686800000000"); + raw_tx->chainparams = chainparams_for_network("bitcoin"); bitcoin_tx_input_set_witness(raw_tx, 0, NULL); tx_must_be_eq(raw_tx, txs[5]); @@ -612,23 +620,19 @@ int main(void) lchannel->view[LOCAL].feerate_per_kw = feerate_per_kw[LOCAL]; rchannel->view[REMOTE].feerate_per_kw = feerate_per_kw[REMOTE]; - raw_tx = commit_tx(tmpctx, &funding_txid, funding_output_index, - funding_amount, - LOCAL, remote_config->to_self_delay, - &keyset, - feerate_per_kw[LOCAL], - local_config->dust_limit, - to_local, - to_remote, - htlcs, &htlc_map, - 0x2bb038521914 ^ 42, LOCAL); - - txs = channel_txs(tmpctx, &htlc_map, &wscripts, - lchannel, &local_per_commitment_point, - 42, LOCAL); + raw_tx = commit_tx( + tmpctx, chainparams, &funding_txid, funding_output_index, + funding_amount, LOCAL, remote_config->to_self_delay, + &keyset, feerate_per_kw[LOCAL], local_config->dust_limit, + to_local, to_remote, htlcs, &htlc_map, 0x2bb038521914 ^ 42, + LOCAL); + + txs = channel_txs(tmpctx, chainparams, &htlc_map, &wscripts, + lchannel, &local_per_commitment_point, 42, + LOCAL); tx_must_be_eq(txs[0], raw_tx); - txs2 = channel_txs(tmpctx, &htlc_map, &wscripts, + txs2 = channel_txs(tmpctx, chainparams, &htlc_map, &wscripts, rchannel, &local_per_commitment_point, 42, REMOTE); txs_must_be_eq(txs, txs2); diff --git a/closingd/closingd.c b/closingd/closingd.c index a2b812c5f..99ef7fdf0 100644 --- a/closingd/closingd.c +++ b/closingd/closingd.c @@ -31,6 +31,7 @@ #define HSM_FD 6 static struct bitcoin_tx *close_tx(const tal_t *ctx, + const struct chainparams *chainparams, struct per_peer_state *pps, const struct channel_id *channel_id, u8 *scriptpubkey[NUM_SIDES], @@ -63,6 +64,7 @@ static struct bitcoin_tx *close_tx(const tal_t *ctx, /* FIXME: We need to allow this! */ tx = create_close_tx(ctx, + chainparams, scriptpubkey[LOCAL], scriptpubkey[REMOTE], funding_txid, funding_txout, @@ -210,6 +212,7 @@ static void do_reconnect(struct per_peer_state *pps, } static void send_offer(struct per_peer_state *pps, + const struct chainparams *chainparams, const struct channel_id *channel_id, const struct pubkey funding_pubkey[NUM_SIDES], u8 *scriptpubkey[NUM_SIDES], @@ -231,7 +234,7 @@ static void send_offer(struct per_peer_state *pps, * transaction, as specified in [BOLT * #3](03-transactions.md#closing-transaction). */ - tx = close_tx(tmpctx, pps, channel_id, + tx = close_tx(tmpctx, chainparams, pps, channel_id, scriptpubkey, funding_txid, funding_txout, @@ -286,6 +289,7 @@ static void tell_master_their_offer(const struct bitcoin_signature *their_sig, /* Returns fee they offered. */ static struct amount_sat receive_offer(struct per_peer_state *pps, + const struct chainparams *chainparams, const struct channel_id *channel_id, const struct pubkey funding_pubkey[NUM_SIDES], const u8 *funding_wscript, @@ -340,7 +344,7 @@ receive_offer(struct per_peer_state *pps, * specified in [BOLT #3](03-transactions.md#closing-transaction): * - MUST fail the connection. */ - tx = close_tx(tmpctx, pps, channel_id, + tx = close_tx(tmpctx, chainparams, pps, channel_id, scriptpubkey, funding_txid, funding_txout, @@ -369,7 +373,7 @@ receive_offer(struct per_peer_state *pps, * `dust_limit_satoshis`. * - MAY eliminate its own output. */ - trimmed = close_tx(tmpctx, pps, channel_id, + trimmed = close_tx(tmpctx, chainparams, pps, channel_id, scriptpubkey, funding_txid, funding_txout, @@ -551,6 +555,7 @@ int main(int argc, char *argv[]) u8 *channel_reestablish; struct secret last_remote_per_commit_secret; struct bitcoin_blkid chain_hash; + const struct chainparams *chainparams; subdaemon_setup(argc, argv); @@ -583,6 +588,7 @@ int main(int argc, char *argv[]) /* stdin == requests, 3 == peer, 4 = gossip, 5 = gossip_store, 6 = hsmd */ per_peer_state_set_fds(pps, 3, 4, 5); + chainparams = chainparams_by_chainhash(&chain_hash); status_trace("out = %s/%s", type_to_string(tmpctx, struct amount_sat, &out[LOCAL]), @@ -624,7 +630,7 @@ int main(int argc, char *argv[]) whose_turn = funder; for (size_t i = 0; i < 2; i++, whose_turn = !whose_turn) { if (whose_turn == LOCAL) { - send_offer(pps, + send_offer(pps, chainparams, &channel_id, funding_pubkey, scriptpubkey, &funding_txid, funding_txout, funding, out, funder, @@ -642,7 +648,7 @@ int main(int argc, char *argv[]) struct amount_sat, &offer[LOCAL])); offer[REMOTE] - = receive_offer(pps, + = receive_offer(pps, chainparams, &channel_id, funding_pubkey, funding_wscript, scriptpubkey, &funding_txid, @@ -671,7 +677,7 @@ int main(int argc, char *argv[]) &channel_id, &feerange, offer[REMOTE], min_fee_to_accept); - send_offer(pps, &channel_id, + send_offer(pps, chainparams, &channel_id, funding_pubkey, scriptpubkey, &funding_txid, funding_txout, funding, out, funder, @@ -684,7 +690,7 @@ int main(int argc, char *argv[]) " theirs was %"PRIu64" satoshi,", offer[LOCAL], offer[REMOTE]); offer[REMOTE] - = receive_offer(pps, &channel_id, + = receive_offer(pps, chainparams, &channel_id, funding_pubkey, funding_wscript, scriptpubkey, &funding_txid, diff --git a/common/close_tx.c b/common/close_tx.c index a11072c5f..9bcd3a153 100644 --- a/common/close_tx.c +++ b/common/close_tx.c @@ -5,6 +5,7 @@ #include struct bitcoin_tx *create_close_tx(const tal_t *ctx, + const struct chainparams *chainparams, const u8 *our_script, const u8 *their_script, const struct bitcoin_txid *anchor_txid, @@ -33,7 +34,7 @@ struct bitcoin_tx *create_close_tx(const tal_t *ctx, * * txin count: 1 */ /* Now create close tx: one input, two outputs. */ - tx = bitcoin_tx(ctx, 1, 2); + tx = bitcoin_tx(ctx, chainparams, 1, 2); /* Our input spends the anchor tx output. */ bitcoin_tx_add_input(tx, anchor_txid, anchor_index, diff --git a/common/close_tx.h b/common/close_tx.h index 9db1eed19..57bd1d3c0 100644 --- a/common/close_tx.h +++ b/common/close_tx.h @@ -1,6 +1,8 @@ #ifndef LIGHTNING_COMMON_CLOSE_TX_H #define LIGHTNING_COMMON_CLOSE_TX_H #include "config.h" +#include +#include #include #include #include @@ -10,6 +12,7 @@ struct pubkey; /* Create close tx to spend the anchor tx output; doesn't fill in * input scriptsig. */ struct bitcoin_tx *create_close_tx(const tal_t *ctx, + const struct chainparams *chainparams, const u8 *our_script, const u8 *their_script, const struct bitcoin_txid *anchor_txid, diff --git a/common/funding_tx.c b/common/funding_tx.c index d00de1216..4bd655e5d 100644 --- a/common/funding_tx.c +++ b/common/funding_tx.c @@ -12,6 +12,7 @@ #endif struct bitcoin_tx *funding_tx(const tal_t *ctx, + const struct chainparams *chainparams, u16 *outnum, const struct utxo **utxomap, struct amount_sat funding, @@ -25,7 +26,7 @@ struct bitcoin_tx *funding_tx(const tal_t *ctx, struct bitcoin_tx *tx; bool has_change = !amount_sat_eq(change, AMOUNT_SAT(0)); - tx = tx_spending_utxos(ctx, utxomap, bip32_base, has_change); + tx = tx_spending_utxos(ctx, chainparams, utxomap, bip32_base, has_change); wscript = bitcoin_redeem_2of2(tx, local_fundingkey, remote_fundingkey); diff --git a/common/funding_tx.h b/common/funding_tx.h index 22e48cecb..577a4cafb 100644 --- a/common/funding_tx.h +++ b/common/funding_tx.h @@ -1,6 +1,7 @@ #ifndef LIGHTNING_COMMON_FUNDING_TX_H #define LIGHTNING_COMMON_FUNDING_TX_H #include "config.h" +#include #include #include #include @@ -14,6 +15,7 @@ struct utxo; /** * funding_tx: create a P2WSH funding transaction for a channel. * @ctx: context to tal from. + * @chainparams: (in) the params for the resulting transaction. * @outnum: (out) txout (0 or 1) which is the funding output. * @utxomap: (in/out) tal_arr of UTXO pointers to spend (permuted to match) * @funding: (in) satoshis to output. @@ -33,6 +35,7 @@ struct utxo; * a special case because of the P2SH inputs. */ struct bitcoin_tx *funding_tx(const tal_t *ctx, + const struct chainparams *chainparams, u16 *outnum, const struct utxo **utxomap, struct amount_sat funding, diff --git a/common/htlc_tx.c b/common/htlc_tx.c index cdbc791be..819b1e392 100644 --- a/common/htlc_tx.c +++ b/common/htlc_tx.c @@ -5,6 +5,7 @@ #include static struct bitcoin_tx *htlc_tx(const tal_t *ctx, + const struct chainparams *chainparams, const struct bitcoin_txid *commit_txid, unsigned int commit_output_number, struct amount_msat msat, @@ -14,7 +15,7 @@ static struct bitcoin_tx *htlc_tx(const tal_t *ctx, struct amount_sat htlc_fee, u32 locktime) { - struct bitcoin_tx *tx = bitcoin_tx(ctx, 1, 1); + struct bitcoin_tx *tx = bitcoin_tx(ctx, chainparams, 1, 1); u8 *wscript; struct amount_sat amount; @@ -68,6 +69,7 @@ static struct bitcoin_tx *htlc_tx(const tal_t *ctx, } struct bitcoin_tx *htlc_success_tx(const tal_t *ctx, + const struct chainparams *chainparams, const struct bitcoin_txid *commit_txid, unsigned int commit_output_number, struct amount_msat htlc_msatoshi, @@ -78,7 +80,7 @@ struct bitcoin_tx *htlc_success_tx(const tal_t *ctx, /* BOLT #3: * * locktime: `0` for HTLC-success, `cltv_expiry` for HTLC-timeout */ - return htlc_tx(ctx, commit_txid, commit_output_number, htlc_msatoshi, + return htlc_tx(ctx, chainparams, commit_txid, commit_output_number, htlc_msatoshi, to_self_delay, &keyset->self_revocation_key, &keyset->self_delayed_payment_key, @@ -112,6 +114,7 @@ void htlc_success_tx_add_witness(struct bitcoin_tx *htlc_success, } struct bitcoin_tx *htlc_timeout_tx(const tal_t *ctx, + const struct chainparams *chainparams, const struct bitcoin_txid *commit_txid, unsigned int commit_output_number, struct amount_msat htlc_msatoshi, @@ -123,12 +126,11 @@ struct bitcoin_tx *htlc_timeout_tx(const tal_t *ctx, /* BOLT #3: * * locktime: `0` for HTLC-success, `cltv_expiry` for HTLC-timeout */ - return htlc_tx(ctx, commit_txid, commit_output_number, htlc_msatoshi, - to_self_delay, + return htlc_tx(ctx, chainparams, commit_txid, commit_output_number, + htlc_msatoshi, to_self_delay, &keyset->self_revocation_key, &keyset->self_delayed_payment_key, - htlc_timeout_fee(feerate_per_kw), - cltv_expiry); + htlc_timeout_fee(feerate_per_kw), cltv_expiry); } /* Fill in the witness for HTLC-timeout tx produced above. */ diff --git a/common/htlc_tx.h b/common/htlc_tx.h index 93e7b771c..13aa1a0c7 100644 --- a/common/htlc_tx.h +++ b/common/htlc_tx.h @@ -1,6 +1,7 @@ #ifndef LIGHTNING_COMMON_HTLC_TX_H #define LIGHTNING_COMMON_HTLC_TX_H #include "config.h" +#include #include #include @@ -38,6 +39,7 @@ static inline struct amount_sat 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 chainparams *chainparams, const struct bitcoin_txid *commit_txid, unsigned int commit_output_number, struct amount_msat htlc_msatoshi, @@ -58,6 +60,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 chainparams *chainparams, const struct bitcoin_txid *commit_txid, unsigned int commit_output_number, struct amount_msat htlc_msatoshi, diff --git a/common/initial_channel.c b/common/initial_channel.c index 228587387..c4a5be5c8 100644 --- a/common/initial_channel.c +++ b/common/initial_channel.c @@ -93,7 +93,8 @@ struct bitcoin_tx *initial_channel_tx(const tal_t *ctx, &channel->funding_pubkey[side], &channel->funding_pubkey[!side]); - return initial_commit_tx(ctx, &channel->funding_txid, + return initial_commit_tx(ctx, channel->chainparams, + &channel->funding_txid, channel->funding_txout, channel->funding, channel->funder, diff --git a/common/initial_commit_tx.c b/common/initial_commit_tx.c index 3f520145d..7908b79c2 100644 --- a/common/initial_commit_tx.c +++ b/common/initial_commit_tx.c @@ -60,6 +60,7 @@ u8 *to_self_wscript(const tal_t *ctx, } struct bitcoin_tx *initial_commit_tx(const tal_t *ctx, + const struct chainparams *chainparams, const struct bitcoin_txid *funding_txid, unsigned int funding_txout, struct amount_sat funding, @@ -146,7 +147,7 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx, /* Worst-case sizing: both to-local and to-remote outputs. */ - tx = bitcoin_tx(ctx, 1, untrimmed + 2); + tx = bitcoin_tx(ctx, chainparams, 1, untrimmed + 2); /* This could be done in a single loop, but we follow the BOLT * literally to make comments in test vectors clearer. */ diff --git a/common/initial_commit_tx.h b/common/initial_commit_tx.h index 12a5e1b44..92dccd099 100644 --- a/common/initial_commit_tx.h +++ b/common/initial_commit_tx.h @@ -2,6 +2,7 @@ #ifndef LIGHTNING_COMMON_INITIAL_COMMIT_TX_H #define LIGHTNING_COMMON_INITIAL_COMMIT_TX_H #include "config.h" +#include #include #include #include @@ -52,6 +53,7 @@ static inline struct amount_sat commit_tx_base_fee(u32 feerate_per_kw, /** * initial_commit_tx: create (unsigned) commitment tx to spend the funding tx output * @ctx: context to allocate transaction and @htlc_map from. + * @chainparams: Params for the resulting transactions * @funding_txid, @funding_out, @funding: funding outpoint. * @funder: is the LOCAL or REMOTE paying the fee? * @keyset: keys derived for this commit tx. @@ -69,6 +71,7 @@ static inline struct amount_sat 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 chainparams *chainparams, const struct bitcoin_txid *funding_txid, unsigned int funding_txout, struct amount_sat funding, diff --git a/common/test/run-funding_tx.c b/common/test/run-funding_tx.c index 044505471..af91a2922 100644 --- a/common/test/run-funding_tx.c +++ b/common/test/run-funding_tx.c @@ -110,6 +110,7 @@ int main(void) struct bitcoin_signature sig; struct bitcoin_address addr; struct amount_sat tmpamt; + const struct chainparams *chainparams = chainparams_for_network("bitcoin"); secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN); @@ -122,6 +123,7 @@ int main(void) input = bitcoin_tx_from_hex(tmpctx, "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff03510101ffffffff0100f2052a010000001976a9143ca33c2e4446f4a305f23c80df8ad1afdcf652f988ac00000000", strlen("01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff03510101ffffffff0100f2052a010000001976a9143ca33c2e4446f4a305f23c80df8ad1afdcf652f988ac00000000")); + input->chainparams = chainparams_for_network("bitcoin"); assert(input); /* BOLT #3: @@ -172,7 +174,8 @@ int main(void) if (!amount_sat_sub(&change, utxo.amount, funding_sat) || !amount_sat_sub(&change, change, fee)) abort(); - funding = funding_tx(tmpctx, &funding_outnum, utxomap, + funding = funding_tx(tmpctx, chainparams, + &funding_outnum, utxomap, funding_sat, &local_funding_pubkey, &remote_funding_pubkey, diff --git a/common/utxo.c b/common/utxo.c index b80224f4f..16228a091 100644 --- a/common/utxo.c +++ b/common/utxo.c @@ -49,6 +49,7 @@ struct utxo *fromwire_utxo(const tal_t *ctx, const u8 **ptr, size_t *max) } struct bitcoin_tx *tx_spending_utxos(const tal_t *ctx, + const struct chainparams *chainparams, const struct utxo **utxos, const struct ext_key *bip32_base, bool add_change_output) @@ -56,7 +57,7 @@ struct bitcoin_tx *tx_spending_utxos(const tal_t *ctx, struct pubkey key; u8 *script; size_t outcount = add_change_output ? 2 : 1; - struct bitcoin_tx *tx = bitcoin_tx(ctx, tal_count(utxos), outcount); + struct bitcoin_tx *tx = bitcoin_tx(ctx, chainparams, tal_count(utxos), outcount); for (size_t i = 0; i < tal_count(utxos); i++) { if (utxos[i]->is_p2sh && bip32_base) { diff --git a/common/utxo.h b/common/utxo.h index ad9e1cac0..67dad1ba1 100644 --- a/common/utxo.h +++ b/common/utxo.h @@ -1,6 +1,7 @@ #ifndef LIGHTNING_COMMON_UTXO_H #define LIGHTNING_COMMON_UTXO_H #include "config.h" +#include #include #include #include @@ -46,6 +47,7 @@ struct utxo *fromwire_utxo(const tal_t *ctx, const u8 **ptr, size_t *max); /* Create a tx, and populate inputs from utxos */ struct bitcoin_tx *tx_spending_utxos(const tal_t *ctx, + const struct chainparams *chainparams, const struct utxo **utxos, const struct ext_key *bip32_base, bool add_change_output); diff --git a/common/withdraw_tx.c b/common/withdraw_tx.c index 1616c3155..f9a1203d2 100644 --- a/common/withdraw_tx.c +++ b/common/withdraw_tx.c @@ -9,6 +9,7 @@ #include struct bitcoin_tx *withdraw_tx(const tal_t *ctx, + const struct chainparams *chainparams, const struct utxo **utxos, const u8 *destination, struct amount_sat withdraw_amount, @@ -19,7 +20,7 @@ struct bitcoin_tx *withdraw_tx(const tal_t *ctx, { struct bitcoin_tx *tx; - tx = tx_spending_utxos(ctx, utxos, bip32_base, + tx = tx_spending_utxos(ctx, chainparams, utxos, bip32_base, !amount_sat_eq(change, AMOUNT_SAT(0))); bitcoin_tx_add_output(tx, destination, &withdraw_amount); diff --git a/common/withdraw_tx.h b/common/withdraw_tx.h index f56b649f5..5121ae82b 100644 --- a/common/withdraw_tx.h +++ b/common/withdraw_tx.h @@ -1,6 +1,7 @@ #ifndef LIGHTNING_COMMON_WITHDRAW_TX_H #define LIGHTNING_COMMON_WITHDRAW_TX_H #include "config.h" +#include #include #include #include @@ -16,6 +17,7 @@ struct utxo; * withdraw_tx - Create a p2pkh withdrawal transaction * * @ctx: context to tal from. + * @chainparams: (in) the params for the created transaction. * @utxos: (in/out) tal_arr of UTXO pointers to spend (permuted to match) * @destination: (in) tal_arr of u8, scriptPubKey to send to. * @amount: (in) satoshis to send to the destination @@ -25,6 +27,7 @@ struct utxo; * @change_outnum: (out) set to output index of change output or -1 if none, unless NULL. */ struct bitcoin_tx *withdraw_tx(const tal_t *ctx, + const struct chainparams *chainparams, const struct utxo **utxos, const u8 *destination, struct amount_sat withdraw_amount, diff --git a/devtools/mkcommit.c b/devtools/mkcommit.c index 2141253ff..fd71eab43 100644 --- a/devtools/mkcommit.c +++ b/devtools/mkcommit.c @@ -247,6 +247,7 @@ int main(int argc, char *argv[]) const struct htlc **htlcmap; struct privkey local_htlc_privkey, remote_htlc_privkey; struct pubkey local_htlc_pubkey, remote_htlc_pubkey; + const struct chainparams *chainparams = chainparams_for_network("bitcoin"); setup_locale(); @@ -367,7 +368,7 @@ int main(int argc, char *argv[]) if (!per_commit_point(&localseed, &local_per_commit_point, commitnum)) errx(1, "Bad deriving local per-commitment-point"); - local_txs = channel_txs(NULL, &htlcmap, &wscripts, channel, + local_txs = channel_txs(chainparams, NULL, &htlcmap, &wscripts, channel, &local_per_commit_point, commitnum, LOCAL); printf("## local_commitment\n" @@ -468,7 +469,7 @@ int main(int argc, char *argv[]) /* Create the remote commitment tx */ if (!per_commit_point(&remoteseed, &remote_per_commit_point, commitnum)) errx(1, "Bad deriving remote per-commitment-point"); - remote_txs = channel_txs(NULL, &htlcmap, &wscripts, channel, + remote_txs = channel_txs(chainparams, NULL, &htlcmap, &wscripts, channel, &remote_per_commit_point, commitnum, REMOTE); remote_txs[0]->input_amounts[0] = tal_dup(remote_txs[0], struct amount_sat, &funding_amount); diff --git a/devtools/mkfunding.c b/devtools/mkfunding.c index 7180acc45..09e9b999c 100644 --- a/devtools/mkfunding.c +++ b/devtools/mkfunding.c @@ -46,6 +46,7 @@ int main(int argc, char *argv[]) const struct utxo **utxomap; struct bitcoin_signature sig; struct bitcoin_txid txid; + const struct chainparams *chainparams = chainparams_for_network("bitcoin"); setup_locale(); @@ -106,7 +107,7 @@ int main(int argc, char *argv[]) utxomap[0] = &input; /* No change output, so we don't need a bip32 base. */ - tx = funding_tx(NULL, &outnum, utxomap, funding_amount, + tx = funding_tx(NULL, chainparams, &outnum, utxomap, funding_amount, &funding_localkey, &funding_remotekey, AMOUNT_SAT(0), NULL, NULL); diff --git a/hsmd/hsmd.c b/hsmd/hsmd.c index ac376f6a2..d9f8891d8 100644 --- a/hsmd/hsmd.c +++ b/hsmd/hsmd.c @@ -102,6 +102,9 @@ struct client { /* What is this client allowed to ask for? */ u64 capabilities; + + /* Params to apply to all transactions for this client */ + const struct chainparams *chainparams; }; /*~ We keep a map of nonzero dbid -> clients, mainly for leak detection. @@ -206,6 +209,7 @@ static void destroy_client(struct client *c) } static struct client *new_client(const tal_t *ctx, + const struct chainparams *chainparams, const struct node_id *id, u64 dbid, const u64 capabilities, @@ -227,6 +231,8 @@ static struct client *new_client(const tal_t *ctx, c->dbid = dbid; c->capabilities = capabilities; + c->chainparams = chainparams; + /*~ This is the core of ccan/io: the connection creation calls a * callback which returns the initial plan to execute: in our case, * read a message.*/ @@ -593,6 +599,10 @@ static struct io_plan *init_hsm(struct io_conn *conn, dev_force_channel_secrets = secrets; dev_force_channel_secrets_shaseed = shaseed; #endif + + /* Once we have read the init message we know which params the master + * will use */ + c->chainparams = chainparams_by_chainhash(&chain_hash); maybe_create_new_hsm(); load_hsm(); @@ -1339,7 +1349,7 @@ static struct io_plan *pass_client_hsmfd(struct io_conn *conn, strerror(errno)); status_trace("new_client: %"PRIu64, dbid); - new_client(c, &id, dbid, capabilities, fds[0]); + new_client(c, c->chainparams, &id, dbid, capabilities, fds[0]); /*~ We stash this in a global, because we need to get both the fd and * the client pointer to the callback. The other way would be to @@ -1471,7 +1481,7 @@ static struct io_plan *handle_sign_funding_tx(struct io_conn *conn, } else changekey = NULL; - tx = funding_tx(tmpctx, &outnum, + tx = funding_tx(tmpctx, c->chainparams, &outnum, /*~ For simplicity, our generated code is not const * correct. The C rules around const and * pointer-to-pointer are a bit weird, so we use @@ -1508,9 +1518,9 @@ static struct io_plan *handle_sign_withdrawal_tx(struct io_conn *conn, return bad_req_fmt(conn, c, msg_in, "Failed to get key %u", change_keyindex); - tx = withdraw_tx(tmpctx, cast_const2(const struct utxo **, utxos), - scriptpubkey, satoshi_out, - &changekey, change_out, NULL, NULL); + tx = withdraw_tx(tmpctx, c->chainparams, + cast_const2(const struct utxo **, utxos), scriptpubkey, + satoshi_out, &changekey, change_out, NULL, NULL); sign_all_inputs(tx, utxos); @@ -1854,7 +1864,7 @@ int main(int argc, char *argv[]) status_setup_async(status_conn); uintmap_init(&clients); - master = new_client(NULL, NULL, 0, HSM_CAP_MASTER | HSM_CAP_SIGN_GOSSIP, + master = new_client(NULL, NULL, NULL, 0, HSM_CAP_MASTER | HSM_CAP_SIGN_GOSSIP, REQ_FD); /* First client == lightningd. */ diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index fda14c9f7..dbddfb6b5 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -370,6 +370,7 @@ static bool compose_and_broadcast_tx(struct lightningd *ld, struct bitcoin_tx *fundingtx; struct amount_sat change; struct bitcoin_txid funding_txid; + const struct chainparams *chainparams = get_chainparams(ld); /* Generate the funding tx. */ if (!amount_sat_eq(fc->wtx->change, AMOUNT_SAT(0)) @@ -377,7 +378,7 @@ static bool compose_and_broadcast_tx(struct lightningd *ld, &changekey, fc->wtx->change_key_index)) fatal("Error deriving change key %u", fc->wtx->change_key_index); - fundingtx = funding_tx(tmpctx, &funding_outnum, + fundingtx = funding_tx(tmpctx, chainparams, &funding_outnum, fc->wtx->utxos, fc->wtx->amount, &fc->uc->local_funding_pubkey, &channel_info->remote_fundingkey, @@ -453,6 +454,7 @@ static bool compose_and_broadcast_tx(struct lightningd *ld, if (!fromwire_hsm_sign_funding_reply(tmpctx, msg, &fundingtx)) fatal("HSM gave bad sign_funding_reply %s", tal_hex(msg, resp)); + fundingtx->chainparams = chainparams; /* Extract the change output and add it to the DB */ wallet_extract_owned_outputs(ld->wallet, fundingtx, NULL, &change); diff --git a/onchaind/onchaind.c b/onchaind/onchaind.c index 8e4344314..a2752d129 100644 --- a/onchaind/onchaind.c +++ b/onchaind/onchaind.c @@ -105,6 +105,8 @@ struct tracked_output { /* If it is resolved. */ struct resolution *resolved; + + const struct chainparams *chainparams; }; /* We vary feerate until signature they offered matches. */ @@ -305,7 +307,7 @@ static struct bitcoin_tx *tx_to_us(const tal_t *ctx, u8 *msg; u8 **witness; - tx = bitcoin_tx(ctx, 1, 1); + tx = bitcoin_tx(ctx, out->chainparams, 1, 1); tx->wtx->locktime = locktime; bitcoin_tx_add_input(tx, &out->txid, out->outnum, to_self_delay, &out->sat, NULL); @@ -399,16 +401,17 @@ static void hsm_get_per_commitment_point(struct pubkey *per_commitment_point) } static struct tracked_output * - new_tracked_output(struct tracked_output ***outs, - const struct bitcoin_txid *txid, - u32 tx_blockheight, - enum tx_type tx_type, - u32 outnum, - struct amount_sat sat, - enum output_type output_type, - const struct htlc_stub *htlc, - const u8 *wscript, - const secp256k1_ecdsa_signature *remote_htlc_sig) +new_tracked_output(const struct chainparams *chainparams, + struct tracked_output ***outs, + const struct bitcoin_txid *txid, + u32 tx_blockheight, + enum tx_type tx_type, + u32 outnum, + struct amount_sat sat, + enum output_type output_type, + const struct htlc_stub *htlc, + const u8 *wscript, + const secp256k1_ecdsa_signature *remote_htlc_sig) { struct tracked_output *out = tal(*outs, struct tracked_output); @@ -427,6 +430,7 @@ static struct tracked_output * out->output_type = output_type; out->proposal = NULL; out->resolved = NULL; + out->chainparams = chainparams; if (htlc) out->htlc = *htlc; out->wscript = tal_steal(out, wscript); @@ -900,7 +904,8 @@ static void handle_htlc_onchain_fulfill(struct tracked_output *out, &preimage))); } -static void resolve_htlc_tx(struct tracked_output ***outs, +static void resolve_htlc_tx(const struct chainparams *chainparams, + struct tracked_output ***outs, size_t out_index, const struct bitcoin_tx *htlc_tx, const struct bitcoin_txid *htlc_txid, @@ -925,7 +930,7 @@ static void resolve_htlc_tx(struct tracked_output ***outs, * output. */ amt = bitcoin_tx_output_get_amount(htlc_tx, 0); - out = new_tracked_output(outs, htlc_txid, tx_blockheight, + out = new_tracked_output(chainparams, outs, htlc_txid, tx_blockheight, (*outs)[out_index]->resolved->tx_type, 0, amt, DELAYED_OUTPUT_TO_US, @@ -981,7 +986,8 @@ static void onchain_transaction_annotate(const struct bitcoin_txid *txid, wire_sync_write(REQ_FD, take(msg)); } /* An output has been spent: see if it resolves something we care about. */ -static void output_spent(struct tracked_output ***outs, +static void output_spent(const struct chainparams *chainparams, + struct tracked_output ***outs, const struct bitcoin_tx *tx, u32 input_num, u32 tx_blockheight) @@ -1008,7 +1014,7 @@ static void output_spent(struct tracked_output ***outs, /* If it's our htlc tx, we need to resolve that, too. */ if (out->resolved->tx_type == OUR_HTLC_SUCCESS_TX || out->resolved->tx_type == OUR_HTLC_TIMEOUT_TX) - resolve_htlc_tx(outs, i, tx, &txid, + resolve_htlc_tx(chainparams, outs, i, tx, &txid, tx_blockheight); return; } @@ -1194,7 +1200,8 @@ static void tx_new_depth(struct tracked_output **outs, * - MUST NOT *resolve* the output by spending it. */ /* Master makes sure we only get told preimages once other node is committed. */ -static void handle_preimage(struct tracked_output **outs, +static void handle_preimage(const struct chainparams *chainparams, + struct tracked_output **outs, const struct preimage *preimage) { size_t i; @@ -1249,7 +1256,9 @@ static void handle_preimage(struct tracked_output **outs, type_to_string(tmpctx, struct amount_sat, &outs[i]->sat)); - tx = htlc_success_tx(outs[i], &outs[i]->txid, + tx = htlc_success_tx(outs[i], + chainparams, + &outs[i]->txid, outs[i]->outnum, htlc_amount, to_self_delay[LOCAL], @@ -1347,7 +1356,8 @@ static bool handle_dev_memleak(struct tracked_output **outs, const u8 *msg) * - MUST monitor the blockchain for transactions that spend any output that * is NOT *irrevocably resolved*. */ -static void wait_for_resolved(struct tracked_output **outs) +static void wait_for_resolved(const struct chainparams *chainparams, + struct tracked_output **outs) { billboard_update(outs); @@ -1365,9 +1375,9 @@ static void wait_for_resolved(struct tracked_output **outs) tx_new_depth(outs, &txid, depth); else if (fromwire_onchain_spent(msg, msg, &tx, &input_num, &tx_blockheight)) - output_spent(&outs, tx, input_num, tx_blockheight); + output_spent(chainparams, &outs, tx, input_num, tx_blockheight); else if (fromwire_onchain_known_preimage(msg, &preimage)) - handle_preimage(outs, &preimage); + handle_preimage(chainparams, outs, &preimage); else if (!handle_dev_memleak(outs, msg)) master_badmsg(-1, msg); @@ -1387,7 +1397,8 @@ static void init_reply(const char *what) peer_billboard(true, what); } -static void handle_mutual_close(const struct bitcoin_txid *txid, +static void handle_mutual_close(const struct chainparams *chainparams, + const struct bitcoin_txid *txid, struct tracked_output **outs) { init_reply("Tracking mutual close transaction"); @@ -1402,7 +1413,7 @@ static void handle_mutual_close(const struct bitcoin_txid *txid, */ resolved_by_other(outs[0], txid, MUTUAL_CLOSE); - wait_for_resolved(outs); + wait_for_resolved(chainparams, outs); } static u8 **derive_htlc_scripts(const struct htlc_stub *htlcs, enum side side) @@ -1432,7 +1443,8 @@ static u8 **derive_htlc_scripts(const struct htlc_stub *htlcs, enum side side) return htlc_scripts; } -static size_t resolve_our_htlc_ourcommit(struct tracked_output *out, +static size_t resolve_our_htlc_ourcommit(const struct chainparams *chainparams, + struct tracked_output *out, const size_t *matches, const struct htlc_stub *htlcs, u8 **htlc_scripts) @@ -1463,7 +1475,8 @@ static size_t resolve_our_htlc_ourcommit(struct tracked_output *out, * - MUST *resolve* the output by spending it using the * HTLC-timeout transaction. */ - tx = htlc_timeout_tx(tmpctx, &out->txid, out->outnum, + tx = htlc_timeout_tx(tmpctx, chainparams, + &out->txid, out->outnum, htlc_amount, htlcs[matches[i]].cltv_expiry, to_self_delay[LOCAL], 0, keyset); @@ -1778,7 +1791,8 @@ static void handle_our_unilateral(const struct bitcoin_tx *tx, * node's `to_self_delay` field) before spending * the output. */ - out = new_tracked_output(&outs, txid, tx_blockheight, + out = new_tracked_output(tx->chainparams, + &outs, txid, tx_blockheight, OUR_UNILATERAL, i, amt, DELAYED_OUTPUT_TO_US, @@ -1818,7 +1832,8 @@ static void handle_our_unilateral(const struct bitcoin_tx *tx, * node, as `to_remote` is considered *resolved* * by the commitment transaction itself. */ - out = new_tracked_output(&outs, txid, tx_blockheight, + out = new_tracked_output(tx->chainparams, + &outs, txid, tx_blockheight, OUR_UNILATERAL, i, amt, OUTPUT_TO_THEM, @@ -1844,7 +1859,8 @@ static void handle_our_unilateral(const struct bitcoin_tx *tx, * in [HTLC Output Handling: Local Commitment, * Local Offers] */ - out = new_tracked_output(&outs, txid, + out = new_tracked_output(tx->chainparams, + &outs, txid, tx_blockheight, OUR_UNILATERAL, i, amt, @@ -1852,11 +1868,13 @@ static void handle_our_unilateral(const struct bitcoin_tx *tx, NULL, NULL, remote_htlc_sigs); /* Tells us which htlc to use */ - which_htlc = resolve_our_htlc_ourcommit(out, matches, + which_htlc = resolve_our_htlc_ourcommit(tx->chainparams, + out, matches, htlcs, htlc_scripts); } else { - out = new_tracked_output(&outs, txid, + out = new_tracked_output(tx->chainparams, + &outs, txid, tx_blockheight, OUR_UNILATERAL, i, amt, @@ -1885,7 +1903,7 @@ static void handle_our_unilateral(const struct bitcoin_tx *tx, note_missing_htlcs(htlc_scripts, htlcs, tell_if_missing, tell_immediately); - wait_for_resolved(outs); + wait_for_resolved(tx->chainparams, outs); } /* We produce individual penalty txs. It's less efficient, but avoids them @@ -2085,7 +2103,8 @@ static void handle_their_cheat(const struct bitcoin_tx *tx, * - Note: this output is considered *resolved* by * the commitment transaction itself. */ - out = new_tracked_output(&outs, txid, tx_blockheight, + out = new_tracked_output(tx->chainparams, + &outs, txid, tx_blockheight, THEIR_REVOKED_UNILATERAL, i, amt, OUTPUT_TO_US, NULL, NULL, NULL); @@ -2109,7 +2128,8 @@ static void handle_their_cheat(const struct bitcoin_tx *tx, * - MUST *resolve* the _remote node's main output_ by * spending it using the revocation private key. */ - out = new_tracked_output(&outs, txid, tx_blockheight, + out = new_tracked_output(tx->chainparams, + &outs, txid, tx_blockheight, THEIR_REVOKED_UNILATERAL, i, amt, DELAYED_OUTPUT_TO_THEM, @@ -2136,7 +2156,8 @@ static void handle_their_cheat(const struct bitcoin_tx *tx, * * spend the *commitment tx* once the HTLC timeout has passed. * * spend the *HTLC-success tx*, if the remote node has published it. */ - out = new_tracked_output(&outs, txid, + out = new_tracked_output(tx->chainparams, + &outs, txid, tx_blockheight, THEIR_REVOKED_UNILATERAL, i, amt, @@ -2146,7 +2167,8 @@ static void handle_their_cheat(const struct bitcoin_tx *tx, NULL); steal_htlc(out); } else { - out = new_tracked_output(&outs, txid, + out = new_tracked_output(tx->chainparams, + &outs, txid, tx_blockheight, THEIR_REVOKED_UNILATERAL, i, amt, @@ -2168,7 +2190,7 @@ static void handle_their_cheat(const struct bitcoin_tx *tx, note_missing_htlcs(htlc_scripts, htlcs, tell_if_missing, tell_immediately); - wait_for_resolved(outs); + wait_for_resolved(tx->chainparams, outs); } static void handle_their_unilateral(const struct bitcoin_tx *tx, @@ -2301,7 +2323,8 @@ static void handle_their_unilateral(const struct bitcoin_tx *tx, * - Note: `to_remote` is considered *resolved* by the * commitment transaction itself. */ - out = new_tracked_output(&outs, txid, tx_blockheight, + out = new_tracked_output(tx->chainparams, + &outs, txid, tx_blockheight, THEIR_UNILATERAL, i, amt, OUTPUT_TO_US, NULL, NULL, NULL); @@ -2328,7 +2351,8 @@ static void handle_their_unilateral(const struct bitcoin_tx *tx, * - Note: `to_local` is considered *resolved* by the * commitment transaction itself. */ - out = new_tracked_output(&outs, txid, tx_blockheight, + out = new_tracked_output(tx->chainparams, + &outs, txid, tx_blockheight, THEIR_UNILATERAL, i, amt, DELAYED_OUTPUT_TO_THEM, @@ -2350,7 +2374,8 @@ static void handle_their_unilateral(const struct bitcoin_tx *tx, * [HTLC Output Handling: Remote Commitment, * Local Offers] */ - out = new_tracked_output(&outs, txid, + out = new_tracked_output(tx->chainparams, + &outs, txid, tx_blockheight, THEIR_UNILATERAL, i, amt, @@ -2362,7 +2387,8 @@ static void handle_their_unilateral(const struct bitcoin_tx *tx, htlcs, htlc_scripts); } else { - out = new_tracked_output(&outs, txid, + out = new_tracked_output(tx->chainparams, + &outs, txid, tx_blockheight, THEIR_UNILATERAL, i, amt, @@ -2385,7 +2411,7 @@ static void handle_their_unilateral(const struct bitcoin_tx *tx, note_missing_htlcs(htlc_scripts, htlcs, tell_if_missing, tell_immediately); - wait_for_resolved(outs); + wait_for_resolved(tx->chainparams, outs); } static void handle_unknown_commitment(const struct bitcoin_tx *tx, @@ -2435,7 +2461,8 @@ static void handle_unknown_commitment(const struct bitcoin_tx *tx, * - Note: `to_remote` is considered *resolved* by the * commitment transaction itself. */ - out = new_tracked_output(&outs, txid, tx_blockheight, + out = new_tracked_output(tx->chainparams, + &outs, txid, tx_blockheight, UNKNOWN_UNILATERAL, i, amt, OUTPUT_TO_US, NULL, NULL, NULL); @@ -2477,7 +2504,7 @@ search_done: wire_sync_write(REQ_FD, take(msg)); } - wait_for_resolved(outs); + wait_for_resolved(tx->chainparams, outs); } int main(int argc, char *argv[]) @@ -2538,6 +2565,8 @@ int main(int argc, char *argv[]) master_badmsg(WIRE_ONCHAIN_INIT, msg); } + tx->chainparams = chainparams_by_chainhash(&chain_hash); + status_trace("feerate_per_kw = %u", feerate_per_kw); bitcoin_txid(tx, &txid); /* We need to keep tx around, but there's only one: not really a leak */ @@ -2561,7 +2590,7 @@ int main(int argc, char *argv[]) outs = tal_arr(ctx, struct tracked_output *, 0); bitcoin_tx_input_get_txid(tx, 0, &tmptxid); - new_tracked_output(&outs, &tmptxid, + new_tracked_output(tx->chainparams, &outs, &tmptxid, 0, /* We don't care about funding blockheight */ FUNDING_TRANSACTION, tx->wtx->inputs[0].index, @@ -2586,7 +2615,7 @@ int main(int argc, char *argv[]) * [BOLT #2: Channel Close](02-peer-protocol.md#channel-close)). */ if (is_mutual_close(tx, scriptpubkey[LOCAL], scriptpubkey[REMOTE])) - handle_mutual_close(&txid, outs); + handle_mutual_close(tx->chainparams, &txid, outs); else { /* BOLT #5: * diff --git a/onchaind/test/run-grind_feerate.c b/onchaind/test/run-grind_feerate.c index af99dfb17..0f6087895 100644 --- a/onchaind/test/run-grind_feerate.c +++ b/onchaind/test/run-grind_feerate.c @@ -64,6 +64,7 @@ u8 *htlc_received_wscript(const tal_t *ctx UNNEEDED, { fprintf(stderr, "htlc_received_wscript called!\n"); abort(); } /* Generated stub for htlc_success_tx */ struct bitcoin_tx *htlc_success_tx(const tal_t *ctx UNNEEDED, + const struct chainparams *chainparams UNNEEDED, const struct bitcoin_txid *commit_txid UNNEEDED, unsigned int commit_output_number UNNEEDED, struct amount_msat htlc_msatoshi UNNEEDED, @@ -73,6 +74,7 @@ struct bitcoin_tx *htlc_success_tx(const tal_t *ctx UNNEEDED, { fprintf(stderr, "htlc_success_tx called!\n"); abort(); } /* Generated stub for htlc_timeout_tx */ struct bitcoin_tx *htlc_timeout_tx(const tal_t *ctx UNNEEDED, + const struct chainparams *chainparams UNNEEDED, const struct bitcoin_txid *commit_txid UNNEEDED, unsigned int commit_output_number UNNEEDED, struct amount_msat htlc_msatoshi UNNEEDED, @@ -205,6 +207,7 @@ int main(int argc, char *argv[]) strlen("0200000001e1ebca08cf1c301ac563580a1126d5c8fcb0e5e2043230b852c726553caf1e1d0000000000000000000160ae0a000000000022002082e03c5a9cb79c82cd5a0572dc175290bc044609aabe9cc852d61927436041796d000000")); tx->input_amounts[0] = tal(tx, struct amount_sat); *tx->input_amounts[0] = AMOUNT_SAT(700000); + tx->chainparams = chainparams_for_network("bitcoin"); der = tal_hexdata(tmpctx, "30450221009b2e0eef267b94c3899fb0dc7375012e2cee4c10348a068fe78d1b82b4b14036022077c3fad3adac2ddf33f415e45f0daf6658b7a0b09647de4443938ae2dbafe2b9" "01", strlen("30450221009b2e0eef267b94c3899fb0dc7375012e2cee4c10348a068fe78d1b82b4b14036022077c3fad3adac2ddf33f415e45f0daf6658b7a0b09647de4443938ae2dbafe2b9" "01")); if (!signature_from_der(der, tal_count(der), &sig)) diff --git a/openingd/openingd.c b/openingd/openingd.c index a43268d3f..bb32c630a 100644 --- a/openingd/openingd.c +++ b/openingd/openingd.c @@ -1069,7 +1069,7 @@ static u8 *funder_channel(struct state *state, * it; lightningd will recreate it (and have the HSM sign it) when * we've completed opening negotiation. */ - funding = funding_tx(state, &state->funding_txout, + funding = funding_tx(state, state->chainparams, &state->funding_txout, cast_const2(const struct utxo **, utxos), state->funding, &state->our_funding_pubkey, diff --git a/wallet/walletrpc.c b/wallet/walletrpc.c index c1ced7038..c89d28096 100644 --- a/wallet/walletrpc.c +++ b/wallet/walletrpc.c @@ -128,6 +128,7 @@ static struct command_result *broadcast_and_wait(struct command *cmd, if (!fromwire_hsm_sign_withdrawal_reply(utx, msg, &signed_tx)) fatal("HSM gave bad sign_withdrawal_reply %s", tal_hex(tmpctx, msg)); + signed_tx->chainparams = utx->tx->chainparams; /* Sanity check */ bitcoin_txid(signed_tx, &signed_txid); @@ -199,7 +200,7 @@ static struct command_result *json_prepare_tx(struct command *cmd, return command_fail(cmd, LIGHTNINGD, "Keys generation failure"); } - (*utx)->tx = withdraw_tx(*utx, (*utx)->wtx->utxos, + (*utx)->tx = withdraw_tx(*utx, get_chainparams(cmd->ld), (*utx)->wtx->utxos, (*utx)->destination, (*utx)->wtx->amount, changekey, (*utx)->wtx->change, cmd->ld->wallet->bip32_base,