2017-06-20 16:46:31 +02:00
|
|
|
#include "withdraw_tx.h"
|
2019-03-15 14:20:07 +01:00
|
|
|
#include <assert.h>
|
2017-06-20 16:46:31 +02:00
|
|
|
#include <bitcoin/pubkey.h>
|
|
|
|
#include <bitcoin/script.h>
|
|
|
|
#include <ccan/ptrint/ptrint.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/permute_tx.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/utxo.h>
|
2017-12-10 06:48:18 +01:00
|
|
|
#include <string.h>
|
2017-12-21 12:10:24 +01:00
|
|
|
#include <wally_bip32.h>
|
2017-06-20 16:46:31 +02:00
|
|
|
|
|
|
|
struct bitcoin_tx *withdraw_tx(const tal_t *ctx,
|
2019-07-30 16:14:43 +02:00
|
|
|
const struct chainparams *chainparams,
|
2017-06-20 16:46:31 +02:00
|
|
|
const struct utxo **utxos,
|
2019-06-05 07:28:57 +02:00
|
|
|
const u8 *destination,
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat withdraw_amount,
|
2017-06-20 16:46:31 +02:00
|
|
|
const struct pubkey *changekey,
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat change,
|
2019-06-05 07:28:57 +02:00
|
|
|
const struct ext_key *bip32_base,
|
|
|
|
int *change_outnum)
|
2017-06-20 16:46:31 +02:00
|
|
|
{
|
2018-12-03 00:02:11 +01:00
|
|
|
struct bitcoin_tx *tx;
|
|
|
|
|
2019-07-30 16:14:43 +02:00
|
|
|
tx = tx_spending_utxos(ctx, chainparams, utxos, bip32_base,
|
2019-02-21 04:45:55 +01:00
|
|
|
!amount_sat_eq(change, AMOUNT_SAT(0)));
|
2018-12-03 00:02:11 +01:00
|
|
|
|
2019-08-21 05:52:44 +02:00
|
|
|
bitcoin_tx_add_output(tx, destination, withdraw_amount);
|
2017-06-20 16:46:31 +02:00
|
|
|
|
2019-02-21 04:45:55 +01:00
|
|
|
if (!amount_sat_eq(change, AMOUNT_SAT(0))) {
|
2017-06-20 16:46:31 +02:00
|
|
|
const void *map[2];
|
|
|
|
map[0] = int2ptr(0);
|
|
|
|
map[1] = int2ptr(1);
|
2019-03-15 14:20:07 +01:00
|
|
|
bitcoin_tx_add_output(tx, scriptpubkey_p2wpkh(tx, changekey),
|
2019-08-21 05:52:44 +02:00
|
|
|
change);
|
2019-03-14 17:47:13 +01:00
|
|
|
permute_outputs(tx, NULL, map);
|
2019-06-05 07:28:57 +02:00
|
|
|
if (change_outnum)
|
|
|
|
*change_outnum = ptr2int(map[1]);
|
|
|
|
} else if (change_outnum)
|
|
|
|
*change_outnum = -1;
|
2019-03-14 17:47:13 +01:00
|
|
|
permute_inputs(tx, (const void **)utxos);
|
2019-03-15 14:20:07 +01:00
|
|
|
assert(bitcoin_tx_check(tx));
|
2017-06-20 16:46:31 +02:00
|
|
|
return tx;
|
|
|
|
}
|
|
|
|
|