mirror of
https://github.com/ElementsProject/lightning.git
synced 2024-11-20 02:27:51 +01:00
9288a7906b
The way we build transactions, serialize them, and compute fees depends on the chain we are working on, so let's add some context to the transactions. Signed-off-by: Christian Decker <decker.christian@gmail.com>
44 lines
1.2 KiB
C
44 lines
1.2 KiB
C
#include "withdraw_tx.h"
|
|
#include <assert.h>
|
|
#include <bitcoin/pubkey.h>
|
|
#include <bitcoin/script.h>
|
|
#include <ccan/ptrint/ptrint.h>
|
|
#include <common/permute_tx.h>
|
|
#include <common/utxo.h>
|
|
#include <string.h>
|
|
#include <wally_bip32.h>
|
|
|
|
struct bitcoin_tx *withdraw_tx(const tal_t *ctx,
|
|
const struct chainparams *chainparams,
|
|
const struct utxo **utxos,
|
|
const u8 *destination,
|
|
struct amount_sat withdraw_amount,
|
|
const struct pubkey *changekey,
|
|
struct amount_sat change,
|
|
const struct ext_key *bip32_base,
|
|
int *change_outnum)
|
|
{
|
|
struct bitcoin_tx *tx;
|
|
|
|
tx = tx_spending_utxos(ctx, chainparams, utxos, bip32_base,
|
|
!amount_sat_eq(change, AMOUNT_SAT(0)));
|
|
|
|
bitcoin_tx_add_output(tx, destination, &withdraw_amount);
|
|
|
|
if (!amount_sat_eq(change, AMOUNT_SAT(0))) {
|
|
const void *map[2];
|
|
map[0] = int2ptr(0);
|
|
map[1] = int2ptr(1);
|
|
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;
|
|
}
|
|
|