mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-06 05:49:30 +01:00
d36af2c340
The number of outputs got updated, but the map used to calculate the change output's location did not (still assumes only one output). This patch fixes this to make the output map a variable size. Changelog-Fixed: JSON API: `txprepare` no longer crashes when more than two outputs are specified
59 lines
1.6 KiB
C
59 lines
1.6 KiB
C
#include "withdraw_tx.h"
|
|
#include <assert.h>
|
|
#include <bitcoin/pubkey.h>
|
|
#include <bitcoin/script.h>
|
|
#include <ccan/ptrint/ptrint.h>
|
|
#include <common/permute_tx.h>
|
|
#include <common/utils.h>
|
|
#include <common/utxo.h>
|
|
#include <string.h>
|
|
#include <wally_bip32.h>
|
|
|
|
struct bitcoin_tx *withdraw_tx(const tal_t *ctx,
|
|
const struct chainparams *chainparams,
|
|
const struct utxo **utxos,
|
|
struct bitcoin_tx_output **outputs,
|
|
const struct pubkey *changekey,
|
|
struct amount_sat change,
|
|
const struct ext_key *bip32_base,
|
|
int *change_outnum)
|
|
{
|
|
struct bitcoin_tx *tx;
|
|
int output_count;
|
|
|
|
tx = tx_spending_utxos(ctx, chainparams, utxos, bip32_base,
|
|
!amount_sat_eq(change, AMOUNT_SAT(0)),
|
|
tal_count(outputs));
|
|
|
|
output_count = bitcoin_tx_add_multi_outputs(tx, outputs);
|
|
assert(output_count == tal_count(outputs));
|
|
|
|
if (!amount_sat_eq(change, AMOUNT_SAT(0))) {
|
|
/* Add one to the output_count, for the change */
|
|
output_count++;
|
|
|
|
const void *map[output_count];
|
|
for (size_t i = 0; i < output_count; i++)
|
|
map[i] = int2ptr(i);
|
|
|
|
bitcoin_tx_add_output(tx, scriptpubkey_p2wpkh(tmpctx, changekey),
|
|
change);
|
|
|
|
assert(tx->wtx->num_outputs == output_count);
|
|
permute_outputs(tx, NULL, map);
|
|
|
|
/* The change is the last output added, so the last position
|
|
* in the map */
|
|
if (change_outnum)
|
|
*change_outnum = ptr2int(map[output_count - 1]);
|
|
|
|
} else if (change_outnum)
|
|
*change_outnum = -1;
|
|
|
|
permute_inputs(tx, (const void **)utxos);
|
|
elements_tx_add_fee_output(tx);
|
|
assert(bitcoin_tx_check(tx));
|
|
return tx;
|
|
}
|
|
|