hsm: fix complex withdrawl transactions.

Firstly, not every output is a P2SH (our change outputs aren't, and in
future we'll have native incoming segwit txs).

Secondly, withdraw_tx() permutes the utxo array, so we can't use a
temporary: we got away with it because we were always using the same
key!

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2017-12-20 06:04:00 +10:30
parent db1b9ffb61
commit bd0b762b2f

View File

@ -599,7 +599,8 @@ static void sign_withdrawal_tx(struct daemon_conn *master, const u8 *msg)
const tal_t *tmpctx = tal_tmpctx(master); const tal_t *tmpctx = tal_tmpctx(master);
u64 satoshi_out, change_out; u64 satoshi_out, change_out;
u32 change_keyindex; u32 change_keyindex;
struct utxo *utxos; struct utxo *inutxos;
const struct utxo **utxos;
secp256k1_ecdsa_signature *sigs; secp256k1_ecdsa_signature *sigs;
u8 *wscript; u8 *wscript;
struct bitcoin_tx *tx; struct bitcoin_tx *tx;
@ -609,7 +610,7 @@ static void sign_withdrawal_tx(struct daemon_conn *master, const u8 *msg)
if (!fromwire_hsm_sign_withdrawal(tmpctx, msg, NULL, &satoshi_out, if (!fromwire_hsm_sign_withdrawal(tmpctx, msg, NULL, &satoshi_out,
&change_out, &change_keyindex, &change_out, &change_keyindex,
&scriptpubkey, &utxos)) { &scriptpubkey, &inutxos)) {
status_trace("Failed to parse sign_withdrawal: %s", status_trace("Failed to parse sign_withdrawal: %s",
tal_hex(trc, msg)); tal_hex(trc, msg));
return; return;
@ -622,9 +623,11 @@ static void sign_withdrawal_tx(struct daemon_conn *master, const u8 *msg)
return; return;
} }
/* We need an array of pointers, since withdraw_tx permutes them */
utxos = to_utxoptr_arr(tmpctx, inutxos);
pubkey_from_der(ext.pub_key, sizeof(ext.pub_key), &changekey); pubkey_from_der(ext.pub_key, sizeof(ext.pub_key), &changekey);
tx = withdraw_tx( tx = withdraw_tx(
tmpctx, to_utxoptr_arr(tmpctx, utxos), scriptpubkey, satoshi_out, tmpctx, utxos, scriptpubkey, satoshi_out,
&changekey, change_out, NULL); &changekey, change_out, NULL);
/* Now generate signatures. */ /* Now generate signatures. */
@ -632,12 +635,14 @@ static void sign_withdrawal_tx(struct daemon_conn *master, const u8 *msg)
for (size_t i = 0; i < tal_count(utxos); i++) { for (size_t i = 0; i < tal_count(utxos); i++) {
struct pubkey inkey; struct pubkey inkey;
struct privkey inprivkey; struct privkey inprivkey;
const struct utxo *in = &utxos[i]; const struct utxo *in = utxos[i];
u8 *subscript; u8 *subscript;
bitcoin_keypair(&inprivkey, &inkey, in->keyindex); bitcoin_keypair(&inprivkey, &inkey, in->keyindex);
/* We know these are p2sh since that's the only kind we handle */ if (utxos[i]->is_p2sh)
subscript = bitcoin_redeem_p2sh_p2wpkh(tmpctx, &inkey); subscript = bitcoin_redeem_p2sh_p2wpkh(tmpctx, &inkey);
else
subscript = NULL;
wscript = p2wpkh_scriptcode(tmpctx, &inkey); wscript = p2wpkh_scriptcode(tmpctx, &inkey);
sign_tx_input(tx, i, subscript, wscript, sign_tx_input(tx, i, subscript, wscript,