From bbac67f10829a7023993f46cef9b18ebf68964ad Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 20 Dec 2017 16:26:40 +1030 Subject: [PATCH] withdraw_tx: don't create empty output if no change. We were using changekey as the flag to produce change, not changesat, but the caller was using changesat as the flag. Also, don't allocate changekey at all if we don't need it; this means valgrind will complain if we use it at all, too. Signed-off-by: Rusty Russell --- common/withdraw_tx.c | 2 +- wallet/walletrpc.c | 22 ++++++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/common/withdraw_tx.c b/common/withdraw_tx.c index 21099cc61..2a1b4cbc2 100644 --- a/common/withdraw_tx.c +++ b/common/withdraw_tx.c @@ -17,7 +17,7 @@ struct bitcoin_tx *withdraw_tx(const tal_t *ctx, const struct ext_key *bip32_base) { struct bitcoin_tx *tx = - bitcoin_tx(ctx, tal_count(utxos), changekey ? 2 : 1); + bitcoin_tx(ctx, tal_count(utxos), changesat ? 2 : 1); for (size_t i = 0; i < tal_count(utxos); i++) { tx->input[i].txid = utxos[i]->txid; tx->input[i].index = utxos[i]->outnum; diff --git a/wallet/walletrpc.c b/wallet/walletrpc.c index 1b86b8d3a..e9731b158 100644 --- a/wallet/walletrpc.c +++ b/wallet/walletrpc.c @@ -271,7 +271,10 @@ static void json_withdraw(struct command *cmd, if (withdraw->changesatoshi <= 546) withdraw->changesatoshi = 0; - withdraw->change_key_index = wallet_get_newindex(cmd->ld); + if (withdraw->changesatoshi) + withdraw->change_key_index = wallet_get_newindex(cmd->ld); + else + withdraw->change_key_index = 0; utxos = from_utxoptr_arr(withdraw, withdraw->utxos); u8 *msg = towire_hsm_sign_withdrawal(cmd, @@ -292,14 +295,17 @@ static void json_withdraw(struct command *cmd, fatal("HSM gave bad sign_withdrawal_reply %s", tal_hex(withdraw, msg)); - if (bip32_key_from_parent(cmd->ld->wallet->bip32_base, - withdraw->change_key_index, - BIP32_FLAG_KEY_PUBLIC, &ext) != WALLY_OK) { - command_fail(cmd, "Changekey generation failure"); - return; - } + if (withdraw->changesatoshi) { + if (bip32_key_from_parent(cmd->ld->wallet->bip32_base, + withdraw->change_key_index, + BIP32_FLAG_KEY_PUBLIC, &ext) + != WALLY_OK) { + command_fail(cmd, "Changekey generation failure"); + return; + } - 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(withdraw, withdraw->utxos, withdraw->destination, withdraw->amount, &changekey, withdraw->changesatoshi, cmd->ld->wallet->bip32_base);