From 92da5ef5f43598cdd030426b5f8cac0eb5ab91e9 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 5 Jun 2019 14:58:57 +0930 Subject: [PATCH] common/withdraw_tx: tell caller where we put the change output. Signed-off-by: Rusty Russell --- common/withdraw_tx.c | 10 +++++++--- common/withdraw_tx.h | 6 ++++-- hsmd/hsmd.c | 2 +- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/common/withdraw_tx.c b/common/withdraw_tx.c index 0c2b12c38..1616c3155 100644 --- a/common/withdraw_tx.c +++ b/common/withdraw_tx.c @@ -10,11 +10,12 @@ struct bitcoin_tx *withdraw_tx(const tal_t *ctx, const struct utxo **utxos, - u8 *destination, + const u8 *destination, struct amount_sat withdraw_amount, const struct pubkey *changekey, struct amount_sat change, - const struct ext_key *bip32_base) + const struct ext_key *bip32_base, + int *change_outnum) { struct bitcoin_tx *tx; @@ -30,7 +31,10 @@ struct bitcoin_tx *withdraw_tx(const tal_t *ctx, bitcoin_tx_add_output(tx, scriptpubkey_p2wpkh(tx, changekey), &change); permute_outputs(tx, NULL, map); - } + if (change_outnum) + *change_outnum = ptr2int(map[1]); + } else if (change_outnum) + *change_outnum = -1; permute_inputs(tx, (const void **)utxos); assert(bitcoin_tx_check(tx)); return tx; diff --git a/common/withdraw_tx.h b/common/withdraw_tx.h index 608e68b14..f56b649f5 100644 --- a/common/withdraw_tx.h +++ b/common/withdraw_tx.h @@ -22,13 +22,15 @@ struct utxo; * @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. */ struct bitcoin_tx *withdraw_tx(const tal_t *ctx, const struct utxo **utxos, - u8 *destination, + const u8 *destination, struct amount_sat withdraw_amount, const struct pubkey *changekey, struct amount_sat change, - const struct ext_key *bip32_base); + const struct ext_key *bip32_base, + int *change_outnum); #endif /* LIGHTNING_COMMON_WITHDRAW_TX_H */ diff --git a/hsmd/hsmd.c b/hsmd/hsmd.c index a34a91238..b65bcf162 100644 --- a/hsmd/hsmd.c +++ b/hsmd/hsmd.c @@ -1456,7 +1456,7 @@ static struct io_plan *handle_sign_withdrawal_tx(struct io_conn *conn, pubkey_from_der(ext.pub_key, sizeof(ext.pub_key), &changekey); tx = withdraw_tx(tmpctx, cast_const2(const struct utxo **, utxos), scriptpubkey, satoshi_out, - &changekey, change_out, NULL); + &changekey, change_out, NULL, NULL); sign_all_inputs(tx, utxos);