From 9e0ef4504807a20b21b4acd899095d51309632a6 Mon Sep 17 00:00:00 2001 From: niftynei Date: Thu, 28 May 2020 22:09:59 -0500 Subject: [PATCH] psbt: handle 'unsetting' final witness stack Prior to this commit, passing a NULL stack to `bitcoin_tx_input_set_witness` unsets the witness stack on the bitcoin_tx's wally_tx but leaves the final witness on the PSBT unchanged. at the moment, libwally's `wally_psbt_input_set_final_witness` will blow up if you attempt to set a NULL witness -- instead we manually remove it if the passed in stack is NULL. previously we would leave the PSBT's witness unchanged. --- bitcoin/tx.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/bitcoin/tx.c b/bitcoin/tx.c index da5fdaa4c..2dc66c6c7 100644 --- a/bitcoin/tx.c +++ b/bitcoin/tx.c @@ -309,7 +309,6 @@ void bitcoin_tx_input_set_witness(struct bitcoin_tx *tx, int innum, { struct wally_tx_witness_stack *stack = NULL; size_t stack_size = tal_count(witness); - struct wally_psbt_input *in; /* Free any lingering witness */ if (witness) { @@ -321,17 +320,21 @@ void bitcoin_tx_input_set_witness(struct bitcoin_tx *tx, int innum, wally_tx_set_input_witness(tx->wtx, innum, stack); /* Also add to the psbt */ - if (stack) { - assert(innum < tx->psbt->num_inputs); - in = &tx->psbt->inputs[innum]; - wally_psbt_input_set_final_witness(in, stack); + if (stack) + wally_psbt_input_set_final_witness(&tx->psbt->inputs[innum], stack); + else { + /* FIXME: libwally-psbt doesn't allow 'unsetting' of witness via + * the set method at the moment, so we do it manually*/ + struct wally_psbt_input *in = &tx->psbt->inputs[innum]; + if (in->final_witness) + wally_tx_witness_stack_free(in->final_witness); + in->final_witness = NULL; } if (stack) wally_tx_witness_stack_free(stack); if (taken(witness)) tal_free(witness); - } void bitcoin_tx_input_set_script(struct bitcoin_tx *tx, int innum, u8 *script)