diff --git a/common/utxo.c b/common/utxo.c index d2da66702..f5a932e77 100644 --- a/common/utxo.c +++ b/common/utxo.c @@ -1,5 +1,8 @@ #include +#include +#include #include +#include #include #include #include @@ -71,7 +74,7 @@ struct bitcoin_tx *tx_spending_utxos(const tal_t *ctx, u32 nsequence) { struct pubkey key; - u8 *script; + u8 *scriptSig, *scriptPubkey, *redeemscript; assert(num_output); size_t outcount = add_change_output ? 1 + num_output : num_output; @@ -81,14 +84,31 @@ struct bitcoin_tx *tx_spending_utxos(const tal_t *ctx, for (size_t i = 0; i < tal_count(utxos); i++) { if (utxos[i]->is_p2sh && bip32_base) { bip32_pubkey(bip32_base, &key, utxos[i]->keyindex); - script = bitcoin_scriptsig_p2sh_p2wpkh(tmpctx, &key); + scriptSig = bitcoin_scriptsig_p2sh_p2wpkh(tmpctx, &key); + redeemscript = bitcoin_redeem_p2sh_p2wpkh(tmpctx, &key); + scriptPubkey = scriptpubkey_p2sh(tmpctx, redeemscript); + + /* Make sure we've got the right info! */ + if (utxos[i]->scriptPubkey) + assert(memeq(utxos[i]->scriptPubkey, + tal_bytelen(utxos[i]->scriptPubkey), + scriptPubkey, tal_bytelen(scriptPubkey))); } else { - script = NULL; + scriptSig = NULL; + redeemscript = NULL; + /* We can't definitively derive the pubkey without + * hitting the HSM, so we don't */ + scriptPubkey = utxos[i]->scriptPubkey; } bitcoin_tx_add_input(tx, &utxos[i]->txid, utxos[i]->outnum, - nsequence, script, utxos[i]->amount, - utxos[i]->scriptPubkey, NULL); + nsequence, scriptSig, utxos[i]->amount, + scriptPubkey, NULL); + + /* Add redeemscript to the PSBT input */ + if (redeemscript) + psbt_input_set_redeemscript(tx->psbt, i, redeemscript); + } return tx;