From dc868630a8ac15d7eb21bb4905e776f62c642cc1 Mon Sep 17 00:00:00 2001 From: niftynei Date: Thu, 21 May 2020 14:46:19 -0500 Subject: [PATCH] tx-psbt: pass in the witness script (if known) when adding an input Update the `bitcoin_tx_add_input` interface to accept a witness script and or scriptPubkey. We save the amount + witness script + witness program (if known) to the PSBT object for a transaction when creating an input. --- bitcoin/tx.c | 24 ++++++++++++++--- bitcoin/tx.h | 11 ++++++-- channeld/commit_tx.c | 4 ++- channeld/commit_tx.h | 1 + channeld/full_channel.c | 23 +++++++++++----- channeld/test/run-commit_tx.c | 38 +++++++++++++-------------- channeld/test/run-full_channel.c | 4 +-- channeld/watchtower.c | 2 +- closingd/closingd.c | 10 +++++-- common/close_tx.c | 4 ++- common/close_tx.h | 1 + common/htlc_tx.c | 12 ++++++--- common/htlc_tx.h | 2 ++ common/initial_channel.c | 2 ++ common/initial_commit_tx.c | 4 ++- common/initial_commit_tx.h | 2 ++ common/test/run-funding_tx.c | 2 +- common/utxo.c | 3 ++- devtools/mkclose.c | 9 ++++--- onchaind/onchaind.c | 5 ++-- onchaind/test/run-grind_feerate-bug.c | 4 ++- onchaind/test/run-grind_feerate.c | 2 ++ 22 files changed, 116 insertions(+), 53 deletions(-) diff --git a/bitcoin/tx.c b/bitcoin/tx.c index 471baf4a2..5024845d4 100644 --- a/bitcoin/tx.c +++ b/bitcoin/tx.c @@ -152,8 +152,9 @@ static int elements_tx_add_fee_output(struct bitcoin_tx *tx) } int bitcoin_tx_add_input(struct bitcoin_tx *tx, const struct bitcoin_txid *txid, - u32 outnum, u32 sequence, - struct amount_sat amount, u8 *script) + u32 outnum, u32 sequence, const u8 *scriptSig, + struct amount_sat amount, const u8 *scriptPubkey, + const u8 *input_wscript) { struct wally_tx_input *input; int wally_err; @@ -164,7 +165,7 @@ int bitcoin_tx_add_input(struct bitcoin_tx *tx, const struct bitcoin_txid *txid, wally_err = wally_tx_input_init_alloc(txid->shad.sha.u.u8, sizeof(struct bitcoin_txid), outnum, sequence, - script, tal_bytelen(script), + scriptSig, tal_bytelen(scriptSig), NULL /* Empty witness stack */, &input); assert(wally_err == WALLY_OK); @@ -172,7 +173,22 @@ int bitcoin_tx_add_input(struct bitcoin_tx *tx, const struct bitcoin_txid *txid, wally_tx_add_input(tx->wtx, input); psbt_add_input(tx->psbt, input, i); - + if (input_wscript) { + /* Add the prev output's data into the PSBT struct */ + psbt_input_set_prev_utxo_wscript(tx->psbt, i, input_wscript, amount); + } else if (scriptPubkey) { + if (is_p2wsh(scriptPubkey, NULL) || is_p2wpkh(scriptPubkey, NULL) || + /* FIXME: assert that p2sh inputs are witness/are accompanied by a redeemscript+witnessscript */ + is_p2sh(scriptPubkey, NULL)) { + /* the only way to get here currently with a p2sh script is via a p2sh-p2wpkh script + * that we've created ...*/ + /* Relevant section from bip-0174, emphasis mine: + * ** Value: The entire transaction output in network serialization which the current input spends from. + * This should only be present for inputs which spend segwit outputs, _including P2SH embedded ones._ + */ + psbt_input_set_prev_utxo(tx->psbt, i, scriptPubkey, amount); + } + } wally_tx_input_free(input); diff --git a/bitcoin/tx.h b/bitcoin/tx.h index b7d9bb332..1edecc229 100644 --- a/bitcoin/tx.h +++ b/bitcoin/tx.h @@ -77,9 +77,16 @@ int bitcoin_tx_add_output(struct bitcoin_tx *tx, const u8 *script, int bitcoin_tx_add_multi_outputs(struct bitcoin_tx *tx, struct bitcoin_tx_output **outputs); +/* Add a new input to a bitcoin tx. + * + * For P2WSH inputs, we'll also store the wscript and/or scriptPubkey + * Passing in just the {input_wscript}, we'll generate the scriptPubkey for you. + * In some cases we may not have the wscript, in which case the scriptPubkey + * should be provided. We'll check that it's P2WSH before saving it */ int bitcoin_tx_add_input(struct bitcoin_tx *tx, const struct bitcoin_txid *txid, - u32 outnum, u32 sequence, - struct amount_sat amount, u8 *script); + u32 outnum, u32 sequence, const u8 *scriptSig, + struct amount_sat amount, const u8 *scriptPubkey, + const u8 *input_wscript); /* This helps is useful because wally uses a raw byte array for txids */ bool wally_tx_input_spends(const struct wally_tx_input *input, diff --git a/channeld/commit_tx.c b/channeld/commit_tx.c index b3cdebf25..75030de84 100644 --- a/channeld/commit_tx.c +++ b/channeld/commit_tx.c @@ -79,6 +79,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx, const struct bitcoin_txid *funding_txid, unsigned int funding_txout, struct amount_sat funding, + const u8 *funding_wscript, enum side opener, u16 to_self_delay, const struct keyset *keyset, @@ -289,7 +290,8 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx, * * `txin[0]` sequence: upper 8 bits are 0x80, lower 24 bits are upper 24 bits of the obscured commitment number */ u32 sequence = (0x80000000 | ((obscured_commitment_number>>24) & 0xFFFFFF)); - bitcoin_tx_add_input(tx, funding_txid, funding_txout, sequence, funding, NULL); + bitcoin_tx_add_input(tx, funding_txid, funding_txout, + sequence, NULL, funding, NULL, funding_wscript); /* Identify the direct outputs (to_us, to_them). */ if (direct_outputs != NULL) { diff --git a/channeld/commit_tx.h b/channeld/commit_tx.h index aab2c0ca5..471d82ed5 100644 --- a/channeld/commit_tx.h +++ b/channeld/commit_tx.h @@ -48,6 +48,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx, const struct bitcoin_txid *funding_txid, unsigned int funding_txout, struct amount_sat funding, + const u8 *funding_wscript, enum side opener, u16 to_self_delay, const struct keyset *keyset, diff --git a/channeld/full_channel.c b/channeld/full_channel.c index 3e8e57109..f67ea35b2 100644 --- a/channeld/full_channel.c +++ b/channeld/full_channel.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -237,19 +238,27 @@ static void add_htlcs(struct bitcoin_tx ***txs, for (i = 0; i < tal_count(htlcmap); i++) { const struct htlc *htlc = htlcmap[i]; struct bitcoin_tx *tx; + struct ripemd160 ripemd; + const u8 *wscript; if (!htlc) continue; if (htlc_owner(htlc) == side) { + ripemd160(&ripemd, htlc->rhash.u.u8, sizeof(htlc->rhash.u.u8)); + wscript = htlc_offered_wscript(tmpctx, &ripemd, keyset); tx = htlc_timeout_tx(*txs, chainparams, &txid, i, + wscript, htlc->amount, htlc->expiry.locktime, channel->config[!side].to_self_delay, feerate_per_kw, keyset); } else { + ripemd160(&ripemd, htlc->rhash.u.u8, sizeof(htlc->rhash.u.u8)); + wscript = htlc_received_wscript(tmpctx, &ripemd, &htlc->expiry, keyset); tx = htlc_success_tx(*txs, chainparams, &txid, i, + wscript, htlc->amount, channel->config[!side].to_self_delay, feerate_per_kw, @@ -285,10 +294,16 @@ struct bitcoin_tx **channel_txs(const tal_t *ctx, /* Figure out what @side will already be committed to. */ gather_htlcs(ctx, channel, side, &committed, NULL, NULL); + /* Generating and saving witness script required to spend + * the funding output */ + *funding_wscript = bitcoin_redeem_2of2(ctx, + &channel->funding_pubkey[side], + &channel->funding_pubkey[!side]); + txs = tal_arr(ctx, struct bitcoin_tx *, 1); txs[0] = commit_tx( ctx, &channel->funding_txid, channel->funding_txout, - channel->funding, channel->opener, + channel->funding, cast_const(u8 *, *funding_wscript), channel->opener, channel->config[!side].to_self_delay, &keyset, channel_feerate(channel, side), channel->config[side].dust_limit, channel->view[side].owed[side], @@ -296,12 +311,6 @@ struct bitcoin_tx **channel_txs(const tal_t *ctx, commitment_number ^ channel->commitment_number_obscurer, side); - /* Generating and saving witness script required to spend - * the funding output */ - *funding_wscript = bitcoin_redeem_2of2(ctx, - &channel->funding_pubkey[side], - &channel->funding_pubkey[!side]); - add_htlcs(&txs, *htlcmap, channel, &keyset, side); tal_free(committed); diff --git a/channeld/test/run-commit_tx.c b/channeld/test/run-commit_tx.c index eb97787ea..fe0fa5b68 100644 --- a/channeld/test/run-commit_tx.c +++ b/channeld/test/run-commit_tx.c @@ -245,31 +245,31 @@ static void report_htlcs(const struct bitcoin_tx *tx, continue; if (htlc_owner(htlc) == LOCAL) { - htlc_tx[i] = htlc_timeout_tx(htlc_tx, tx->chainparams, - &txid, i, - htlc->amount, - htlc->expiry.locktime, - to_self_delay, - feerate_per_kw, - &keyset); wscript[i] = bitcoin_wscript_htlc_offer(tmpctx, local_htlckey, remote_htlckey, &htlc->rhash, remote_revocation_key); - } else { - htlc_tx[i] = htlc_success_tx(htlc_tx, tx->chainparams, - &txid, i, + htlc_tx[i] = htlc_timeout_tx(htlc_tx, tx->chainparams, + &txid, i, wscript[i], htlc->amount, + htlc->expiry.locktime, to_self_delay, feerate_per_kw, &keyset); + } else { wscript[i] = bitcoin_wscript_htlc_receive(tmpctx, &htlc->expiry, local_htlckey, remote_htlckey, &htlc->rhash, remote_revocation_key); + htlc_tx[i] = htlc_success_tx(htlc_tx, tx->chainparams, + &txid, i, wscript[i], + htlc->amount, + to_self_delay, + feerate_per_kw, + &keyset); } sign_tx_input(htlc_tx[i], 0, NULL, @@ -737,7 +737,7 @@ int main(int argc, const char *argv[]) print_superverbose = true; tx = commit_tx(tmpctx, &funding_txid, funding_output_index, - funding_amount, + funding_amount, wscript, LOCAL, to_self_delay, &keyset, feerate_per_kw, @@ -749,7 +749,7 @@ int main(int argc, const char *argv[]) print_superverbose = false; tx2 = commit_tx(tmpctx, &funding_txid, funding_output_index, - funding_amount, + funding_amount, wscript, REMOTE, to_self_delay, &keyset, feerate_per_kw, @@ -793,7 +793,7 @@ int main(int argc, const char *argv[]) print_superverbose = true; tx = commit_tx(tmpctx, &funding_txid, funding_output_index, - funding_amount, + funding_amount, wscript, LOCAL, to_self_delay, &keyset, feerate_per_kw, @@ -805,7 +805,7 @@ int main(int argc, const char *argv[]) print_superverbose = false; tx2 = commit_tx(tmpctx, &funding_txid, funding_output_index, - funding_amount, + funding_amount, wscript, REMOTE, to_self_delay, &keyset, feerate_per_kw, @@ -837,7 +837,7 @@ int main(int argc, const char *argv[]) print_superverbose = false; newtx = commit_tx(tmpctx, &funding_txid, funding_output_index, - funding_amount, + funding_amount, wscript, LOCAL, to_self_delay, &keyset, feerate_per_kw, @@ -850,7 +850,7 @@ int main(int argc, const char *argv[]) /* This is what it would look like for peer generating it! */ tx2 = commit_tx(tmpctx, &funding_txid, funding_output_index, - funding_amount, + funding_amount, wscript, REMOTE, to_self_delay, &keyset, feerate_per_kw, @@ -882,7 +882,7 @@ int main(int argc, const char *argv[]) print_superverbose = true; tx = commit_tx(tmpctx, &funding_txid, funding_output_index, - funding_amount, + funding_amount, wscript, LOCAL, to_self_delay, &keyset, feerate_per_kw-1, @@ -919,7 +919,7 @@ int main(int argc, const char *argv[]) print_superverbose = true; newtx = commit_tx(tmpctx, &funding_txid, funding_output_index, - funding_amount, + funding_amount, wscript, LOCAL, to_self_delay, &keyset, feerate_per_kw, @@ -978,7 +978,7 @@ int main(int argc, const char *argv[]) to_local.millisatoshis, to_remote.millisatoshis, feerate_per_kw); tx = commit_tx(tmpctx, &funding_txid, funding_output_index, - funding_amount, + funding_amount, wscript, LOCAL, to_self_delay, &keyset, feerate_per_kw, diff --git a/channeld/test/run-full_channel.c b/channeld/test/run-full_channel.c index 5a96fa09f..21cfa1bc5 100644 --- a/channeld/test/run-full_channel.c +++ b/channeld/test/run-full_channel.c @@ -524,7 +524,7 @@ int main(int argc, const char *argv[]) raw_tx = commit_tx(tmpctx, &funding_txid, funding_output_index, - funding_amount, + funding_amount, funding_wscript, LOCAL, remote_config->to_self_delay, &keyset, feerate_per_kw[LOCAL], @@ -651,7 +651,7 @@ int main(int argc, const char *argv[]) raw_tx = commit_tx( tmpctx, &funding_txid, funding_output_index, - funding_amount, LOCAL, remote_config->to_self_delay, + funding_amount, funding_wscript, LOCAL, remote_config->to_self_delay, &keyset, feerate_per_kw[LOCAL], local_config->dust_limit, to_local, to_remote, htlcs, &htlc_map, NULL, 0x2bb038521914 ^ 42, LOCAL); diff --git a/channeld/watchtower.c b/channeld/watchtower.c index e79c8f867..46a1c11b4 100644 --- a/channeld/watchtower.c +++ b/channeld/watchtower.c @@ -69,7 +69,7 @@ penalty_tx_create(const tal_t *ctx, tx = bitcoin_tx(ctx, chainparams, 1, 1, locktime); bitcoin_tx_add_input(tx, commitment_txid, to_them_outnum, 0xFFFFFFFF, - to_them_sats, NULL); + NULL, to_them_sats, NULL, wscript); bitcoin_tx_add_output(tx, final_scriptpubkey, NULL, to_them_sats); diff --git a/closingd/closingd.c b/closingd/closingd.c index fa360d4e2..1f454d3a3 100644 --- a/closingd/closingd.c +++ b/closingd/closingd.c @@ -39,6 +39,7 @@ static struct bitcoin_tx *close_tx(const tal_t *ctx, const struct bitcoin_txid *funding_txid, unsigned int funding_txout, struct amount_sat funding, + const u8 *funding_wscript, const struct amount_sat out[NUM_SIDES], enum side opener, struct amount_sat fee, @@ -67,6 +68,7 @@ static struct bitcoin_tx *close_tx(const tal_t *ctx, tx = create_close_tx(ctx, chainparams, scriptpubkey[LOCAL], scriptpubkey[REMOTE], + funding_wscript, funding_txid, funding_txout, funding, @@ -238,6 +240,7 @@ 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], + const u8 *funding_wscript, u8 *scriptpubkey[NUM_SIDES], const struct bitcoin_txid *funding_txid, unsigned int funding_txout, @@ -262,6 +265,7 @@ static void send_offer(struct per_peer_state *pps, funding_txid, funding_txout, funding, + funding_wscript, out, opener, fee_to_offer, our_dust_limit); @@ -371,6 +375,7 @@ receive_offer(struct per_peer_state *pps, funding_txid, funding_txout, funding, + funding_wscript, out, opener, received_fee, our_dust_limit); if (!check_tx_sig(tx, 0, NULL, funding_wscript, @@ -400,6 +405,7 @@ receive_offer(struct per_peer_state *pps, funding_txid, funding_txout, funding, + funding_wscript, trimming_out, opener, received_fee, our_dust_limit); if (!trimmed @@ -695,7 +701,7 @@ int main(int argc, char *argv[]) for (size_t i = 0; i < 2; i++, whose_turn = !whose_turn) { if (whose_turn == LOCAL) { send_offer(pps, chainparams, - &channel_id, funding_pubkey, + &channel_id, funding_pubkey, funding_wscript, scriptpubkey, &funding_txid, funding_txout, funding, out, opener, our_dust_limit, @@ -744,7 +750,7 @@ int main(int argc, char *argv[]) fee_negotiation_step, fee_negotiation_step_unit); send_offer(pps, chainparams, &channel_id, - funding_pubkey, + funding_pubkey, funding_wscript, scriptpubkey, &funding_txid, funding_txout, funding, out, opener, our_dust_limit, diff --git a/common/close_tx.c b/common/close_tx.c index 0bb31b2de..6b58986ff 100644 --- a/common/close_tx.c +++ b/common/close_tx.c @@ -9,6 +9,7 @@ struct bitcoin_tx *create_close_tx(const tal_t *ctx, const struct chainparams *chainparams, const u8 *our_script, const u8 *their_script, + const u8 *funding_wscript, const struct bitcoin_txid *anchor_txid, unsigned int anchor_index, struct amount_sat funding, @@ -39,7 +40,8 @@ struct bitcoin_tx *create_close_tx(const tal_t *ctx, /* Our input spends the anchor tx output. */ bitcoin_tx_add_input(tx, anchor_txid, anchor_index, - BITCOIN_TX_DEFAULT_SEQUENCE, funding, NULL); + BITCOIN_TX_DEFAULT_SEQUENCE, NULL, + funding, NULL, funding_wscript); if (amount_sat_greater_eq(to_us, dust_limit)) { script = tal_dup_talarr(tx, u8, our_script); diff --git a/common/close_tx.h b/common/close_tx.h index 57bd1d3c0..8767737a7 100644 --- a/common/close_tx.h +++ b/common/close_tx.h @@ -15,6 +15,7 @@ struct bitcoin_tx *create_close_tx(const tal_t *ctx, const struct chainparams *chainparams, const u8 *our_script, const u8 *their_script, + const u8 *funding_wscript, const struct bitcoin_txid *anchor_txid, unsigned int anchor_index, struct amount_sat funding, diff --git a/common/htlc_tx.c b/common/htlc_tx.c index c59976009..264b0dfbf 100644 --- a/common/htlc_tx.c +++ b/common/htlc_tx.c @@ -8,6 +8,7 @@ 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, + const u8 *commit_wscript, struct amount_msat msat, u16 to_self_delay, const struct pubkey *revocation_pubkey, @@ -45,8 +46,8 @@ static struct bitcoin_tx *htlc_tx(const tal_t *ctx, * * `txin[0]` sequence: `0` */ amount = amount_msat_to_sat_round_down(msat); - bitcoin_tx_add_input(tx, commit_txid, commit_output_number, 0, amount, - NULL); + bitcoin_tx_add_input(tx, commit_txid, commit_output_number, 0, + NULL, amount, NULL, commit_wscript); /* BOLT #3: * * txout count: 1 @@ -75,6 +76,7 @@ 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, + const u8 *commit_wscript, struct amount_msat htlc_msatoshi, u16 to_self_delay, u32 feerate_per_kw, @@ -83,7 +85,8 @@ 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, chainparams, commit_txid, commit_output_number, htlc_msatoshi, + return htlc_tx(ctx, chainparams, commit_txid, commit_output_number, + commit_wscript, htlc_msatoshi, to_self_delay, &keyset->self_revocation_key, &keyset->self_delayed_payment_key, @@ -120,6 +123,7 @@ 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, + const u8 *commit_wscript, struct amount_msat htlc_msatoshi, u32 cltv_expiry, u16 to_self_delay, @@ -130,7 +134,7 @@ struct bitcoin_tx *htlc_timeout_tx(const tal_t *ctx, * * locktime: `0` for HTLC-success, `cltv_expiry` for HTLC-timeout */ return htlc_tx(ctx, chainparams, commit_txid, commit_output_number, - htlc_msatoshi, to_self_delay, + commit_wscript, htlc_msatoshi, to_self_delay, &keyset->self_revocation_key, &keyset->self_delayed_payment_key, htlc_timeout_fee(feerate_per_kw), cltv_expiry); diff --git a/common/htlc_tx.h b/common/htlc_tx.h index 9c643d080..5f0c7c50a 100644 --- a/common/htlc_tx.h +++ b/common/htlc_tx.h @@ -69,6 +69,7 @@ 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, + const u8 *commit_wscript, struct amount_msat htlc_msatoshi, u16 to_self_delay, u32 feerate_per_kw, @@ -90,6 +91,7 @@ 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, + const u8 *commit_wscript, struct amount_msat htlc_msatoshi, u32 cltv_expiry, u16 to_self_delay, diff --git a/common/initial_channel.c b/common/initial_channel.c index 5f12ade0e..be0426670 100644 --- a/common/initial_channel.c +++ b/common/initial_channel.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -96,6 +97,7 @@ struct bitcoin_tx *initial_channel_tx(const tal_t *ctx, &channel->funding_txid, channel->funding_txout, channel->funding, + cast_const(u8 *, *wscript), channel->opener, /* They specify our to_self_delay and v.v. */ channel->config[!side].to_self_delay, diff --git a/common/initial_commit_tx.c b/common/initial_commit_tx.c index 4a020f9d1..bedb87d86 100644 --- a/common/initial_commit_tx.c +++ b/common/initial_commit_tx.c @@ -62,6 +62,7 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx, const struct bitcoin_txid *funding_txid, unsigned int funding_txout, struct amount_sat funding, + u8 *funding_wscript, enum side opener, u16 to_self_delay, const struct keyset *keyset, @@ -239,7 +240,8 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx, * * `txin[0]` script bytes: 0 */ sequence = (0x80000000 | ((obscured_commitment_number>>24) & 0xFFFFFF)); - bitcoin_tx_add_input(tx, funding_txid, funding_txout, sequence, funding, NULL); + bitcoin_tx_add_input(tx, funding_txid, funding_txout, sequence, + NULL, funding, NULL, funding_wscript); if (direct_outputs != NULL) { direct_outputs[LOCAL] = direct_outputs[REMOTE] = NULL; diff --git a/common/initial_commit_tx.h b/common/initial_commit_tx.h index f016fc2b8..c155a09c4 100644 --- a/common/initial_commit_tx.h +++ b/common/initial_commit_tx.h @@ -76,6 +76,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. * @funding_txid, @funding_out, @funding: funding outpoint. + * @funding_wscript: scriptPubkey of the funding output * @opener: is the LOCAL or REMOTE paying the fee? * @keyset: keys derived for this commit tx. * @feerate_per_kw: feerate to use @@ -96,6 +97,7 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx, const struct bitcoin_txid *funding_txid, unsigned int funding_txout, struct amount_sat funding, + u8 *funding_wscript, enum side opener, u16 to_self_delay, const struct keyset *keyset, diff --git a/common/test/run-funding_tx.c b/common/test/run-funding_tx.c index cf1822b37..f57a7149b 100644 --- a/common/test/run-funding_tx.c +++ b/common/test/run-funding_tx.c @@ -177,7 +177,7 @@ int main(int argc, const char *argv[]) utxo.amount = AMOUNT_SAT(5000000000); utxo.is_p2sh = false; utxo.close_info = NULL; - utxo.scriptPubkey = NULL; + utxo.scriptPubkey = tal_hexdata(tmpctx, "a914a5996075e4b468c9fb01d131bf9d4052a6fee19e87", strlen("a914a5996075e4b468c9fb01d131bf9d4052a6fee19e87")); funding_sat = AMOUNT_SAT(10000000); fee = AMOUNT_SAT(13920); diff --git a/common/utxo.c b/common/utxo.c index 456a95149..d2da66702 100644 --- a/common/utxo.c +++ b/common/utxo.c @@ -87,7 +87,8 @@ struct bitcoin_tx *tx_spending_utxos(const tal_t *ctx, } bitcoin_tx_add_input(tx, &utxos[i]->txid, utxos[i]->outnum, - nsequence, utxos[i]->amount, script); + nsequence, script, utxos[i]->amount, + utxos[i]->scriptPubkey, NULL); } return tx; diff --git a/devtools/mkclose.c b/devtools/mkclose.c index 15dd9bdf3..01253c965 100644 --- a/devtools/mkclose.c +++ b/devtools/mkclose.c @@ -130,10 +130,6 @@ int main(int argc, char *argv[]) tx = bitcoin_tx(NULL, chainparams, 1, 2, 0); - /* Our input spends the anchor tx output. */ - bitcoin_tx_add_input(tx, &funding_txid, funding_outnum, - BITCOIN_TX_DEFAULT_SEQUENCE, funding_amount, NULL); - num_outputs = 0; if (amount_msat_greater_eq_sat(local_msat, dust_limit)) { u8 *script = scriptpubkey_p2wpkh(NULL, &outkey[LOCAL]); @@ -165,6 +161,11 @@ int main(int argc, char *argv[]) printf("# funding witness script = %s\n", tal_hex(NULL, funding_wscript)); + /* Our input spends the anchor tx output. */ + bitcoin_tx_add_input(tx, &funding_txid, funding_outnum, + BITCOIN_TX_DEFAULT_SEQUENCE, NULL, + funding_amount, NULL, funding_wscript); + sign_tx_input(tx, 0, NULL, funding_wscript, &funding_privkey[LOCAL], &funding_pubkey[LOCAL], diff --git a/onchaind/onchaind.c b/onchaind/onchaind.c index 9500385c2..8327fdc80 100644 --- a/onchaind/onchaind.c +++ b/onchaind/onchaind.c @@ -613,7 +613,7 @@ static struct bitcoin_tx *tx_to_us(const tal_t *ctx, tx = bitcoin_tx(ctx, chainparams, 1, 1, locktime); bitcoin_tx_add_input(tx, &out->txid, out->outnum, to_self_delay, - out->sat, NULL); + NULL, out->sat, NULL, wscript); bitcoin_tx_add_output( tx, scriptpubkey_p2wpkh(tx, &our_wallet_pubkey), NULL, out->sat); @@ -1679,6 +1679,7 @@ static void handle_preimage(struct tracked_output **outs, tx = htlc_success_tx(outs[i], chainparams, &outs[i]->txid, outs[i]->outnum, + outs[i]->wscript, htlc_amount, to_self_delay[LOCAL], 0, @@ -1923,7 +1924,7 @@ static size_t resolve_our_htlc_ourcommit(struct tracked_output *out, */ tx = htlc_timeout_tx(tmpctx, chainparams, &out->txid, out->outnum, - htlc_amount, + htlc_scripts[matches[i]], htlc_amount, htlcs[matches[i]].cltv_expiry, to_self_delay[LOCAL], 0, keyset); diff --git a/onchaind/test/run-grind_feerate-bug.c b/onchaind/test/run-grind_feerate-bug.c index ca894e42b..25eac3c53 100644 --- a/onchaind/test/run-grind_feerate-bug.c +++ b/onchaind/test/run-grind_feerate-bug.c @@ -100,6 +100,7 @@ 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, + const u8 *commit_wscript UNNEEDED, struct amount_msat htlc_msatoshi UNNEEDED, u16 to_self_delay UNNEEDED, u32 feerate_per_kw UNNEEDED, @@ -338,6 +339,7 @@ struct bitcoin_tx *htlc_timeout_tx(const tal_t *ctx, const struct chainparams *chainparams, const struct bitcoin_txid *commit_txid UNNEEDED, unsigned int commit_output_number UNNEEDED, + const u8* commit_wscript, struct amount_msat htlc_msatoshi, u32 cltv_expiry, u16 to_self_delay UNNEEDED, @@ -352,7 +354,7 @@ struct bitcoin_tx *htlc_timeout_tx(const tal_t *ctx, assert(tx); in_amount = amount_msat_to_sat_round_down(htlc_msatoshi); - psbt_input_set_prev_utxo_wscript(tx->psbt, 0, NULL, in_amount); + psbt_input_set_prev_utxo_wscript(tx->psbt, 0, commit_wscript, in_amount); tx->chainparams = chainparams; tx->wtx->locktime = cltv_expiry; diff --git a/onchaind/test/run-grind_feerate.c b/onchaind/test/run-grind_feerate.c index 65fa1008f..3dbe8ce48 100644 --- a/onchaind/test/run-grind_feerate.c +++ b/onchaind/test/run-grind_feerate.c @@ -101,6 +101,7 @@ 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, + const u8 *commit_wscript UNNEEDED, struct amount_msat htlc_msatoshi UNNEEDED, u16 to_self_delay UNNEEDED, u32 feerate_per_kw UNNEEDED, @@ -111,6 +112,7 @@ 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, + const u8 *commit_wscript UNNEEDED, struct amount_msat htlc_msatoshi UNNEEDED, u32 cltv_expiry UNNEEDED, u16 to_self_delay UNNEEDED,