mirror of
https://github.com/aljazceru/lightning.git
synced 2026-01-21 06:44:21 +01:00
withdraw: refactor change output handling
We're not using the change_outnum for withdraw tx's (and the way we were calculating it was broken as of the addition of 'multiple outputs'). This removes the change output knowhow from withdraw_tx entirely, and pushes the responsibility up to the caller to include the change output in the output set if desired. Consequently, we also remove the change output knowhow from hsmd.
This commit is contained in:
committed by
Christian Decker
parent
09815c7e7f
commit
16656a85cf
@@ -3,6 +3,7 @@
|
||||
#include <bitcoin/pubkey.h>
|
||||
#include <bitcoin/script.h>
|
||||
#include <ccan/ptrint/ptrint.h>
|
||||
#include <common/key_derive.h>
|
||||
#include <common/permute_tx.h>
|
||||
#include <common/utils.h>
|
||||
#include <common/utxo.h>
|
||||
@@ -13,44 +14,20 @@ 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, u32 nlocktime)
|
||||
u32 nlocktime)
|
||||
{
|
||||
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), nlocktime,
|
||||
false, tal_count(outputs), nlocktime,
|
||||
BITCOIN_TX_DEFAULT_SEQUENCE - 1);
|
||||
|
||||
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),
|
||||
NULL, 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_outputs(tx, NULL, (const void **)outputs);
|
||||
permute_inputs(tx, (const void **)utxos);
|
||||
|
||||
bitcoin_tx_finalize(tx);
|
||||
|
||||
@@ -21,19 +21,14 @@ struct utxo;
|
||||
* @chainparams: (in) the params for the created transaction.
|
||||
* @utxos: (in/out) tal_arr of UTXO pointers to spend (permuted to match)
|
||||
* @outputs: (in) tal_arr of bitcoin_tx_output, scriptPubKeys with amount to send to.
|
||||
* @changekey: (in) key to send change to (only used if change_satoshis != 0).
|
||||
* @change: (in) amount to send as change.
|
||||
* @bip32_base: (in) bip32 base for key derivation, or NULL.
|
||||
* @change_outnum: (out) set to output index of change output or -1 if none, unless NULL.
|
||||
* @nlocktime: (in) the value to set as the transaction's nLockTime.
|
||||
*/
|
||||
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, u32 nlocktime);
|
||||
u32 nlocktime);
|
||||
|
||||
#endif /* LIGHTNING_COMMON_WITHDRAW_TX_H */
|
||||
|
||||
Reference in New Issue
Block a user