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 <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2017-12-20 16:26:40 +10:30
committed by Christian Decker
parent abafcae5a6
commit bbac67f108
2 changed files with 15 additions and 9 deletions

View File

@@ -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;

View File

@@ -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);