@@ -84,9 +84,6 @@ static struct command_result *broadcast_and_wait(struct command *cmd,
8484 /* FIXME: hsm will sign almost anything, but it should really
8585 * fail cleanly (not abort!) and let us report the error here. */
8686 u8 * msg = towire_hsm_sign_withdrawal (cmd ,
87- utx -> wtx -> amount ,
88- utx -> wtx -> change ,
89- utx -> wtx -> change_key_index ,
9087 cast_const2 (const struct bitcoin_tx_output * * ,
9188 utx -> outputs ),
9289 utx -> wtx -> utxos );
@@ -293,10 +290,8 @@ static struct command_result *json_prepare_tx(struct command *cmd,
293290 * Support only one output. */
294291 if (destination ) {
295292 outputs = tal_arr (tmpctx , struct bitcoin_tx_output * , 1 );
296- outputs [0 ] = tal (outputs , struct bitcoin_tx_output );
297- outputs [0 ]-> script = tal_steal (outputs [0 ],
298- cast_const (u8 * , destination ));
299- outputs [0 ]-> amount = (* utx )-> wtx -> amount ;
293+ outputs [0 ] = new_tx_output (outputs , (* utx )-> wtx -> amount ,
294+ destination );
300295 out_len = tal_count (outputs [0 ]-> script );
301296
302297 goto create_tx ;
@@ -338,11 +333,9 @@ static struct command_result *json_prepare_tx(struct command *cmd,
338333 "'%.*s' is a invalid satoshi amount" ,
339334 t [2 ].end - t [2 ].start , buffer + t [2 ].start );
340335
336+ outputs [i ] = new_tx_output (outputs , * amount ,
337+ cast_const (u8 * , destination ));
341338 out_len += tal_count (destination );
342- outputs [i ] = tal (outputs , struct bitcoin_tx_output );
343- outputs [i ]-> amount = * amount ;
344- outputs [i ]-> script = tal_steal (outputs [i ],
345- cast_const (u8 * , destination ));
346339
347340 /* In fact, the maximum amount of bitcoin satoshi is 2.1e15.
348341 * It can't be equal to/bigger than 2^64.
@@ -368,8 +361,6 @@ static struct command_result *json_prepare_tx(struct command *cmd,
368361 }
369362
370363create_tx :
371- (* utx )-> outputs = tal_steal (* utx , outputs );
372-
373364 if (chosen_utxos )
374365 result = wtx_from_utxos ((* utx )-> wtx , * feerate_per_kw ,
375366 out_len , maxheight ,
@@ -386,18 +377,26 @@ static struct command_result *json_prepare_tx(struct command *cmd,
386377 if ((* utx )-> wtx -> all_funds )
387378 outputs [0 ]-> amount = (* utx )-> wtx -> amount ;
388379
380+ /* Add the change as the last output */
389381 if (!amount_sat_eq ((* utx )-> wtx -> change , AMOUNT_SAT (0 ))) {
382+ struct bitcoin_tx_output * change_output ;
383+
390384 changekey = tal (tmpctx , struct pubkey );
391385 if (!bip32_pubkey (cmd -> ld -> wallet -> bip32_base , changekey ,
392386 (* utx )-> wtx -> change_key_index ))
393387 return command_fail (cmd , LIGHTNINGD , "Keys generation failure" );
394- } else
395- changekey = NULL ;
388+
389+ change_output = new_tx_output (outputs , (* utx )-> wtx -> change ,
390+ scriptpubkey_p2wpkh (tmpctx , changekey ));
391+ tal_arr_expand (outputs , * change_output );
392+ }
393+
394+ (* utx )-> outputs = tal_steal (* utx , outputs );
396395 (* utx )-> tx = withdraw_tx (* utx , get_chainparams (cmd -> ld ),
397- (* utx )-> wtx -> utxos , ( * utx ) -> outputs ,
398- changekey , (* utx )-> wtx -> change ,
399- cmd -> ld -> wallet -> bip32_base ,
400- & ( * utx ) -> change_outnum );
396+ (* utx )-> wtx -> utxos ,
397+ (* utx )-> outputs ,
398+ cmd -> ld -> wallet -> bip32_base );
399+
401400 bitcoin_txid ((* utx )-> tx , & (* utx )-> txid );
402401
403402 return NULL ;
0 commit comments