core-lightning/common/withdraw_tx.c
Rusty Russell a37c165cb9 common: move some files out of lightningd/
Basically all files shared by different daemons.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-08-29 17:54:14 +02:00

48 lines
1.4 KiB
C

#include "withdraw_tx.h"
#include <bitcoin/pubkey.h>
#include <bitcoin/script.h>
#include <ccan/ptrint/ptrint.h>
#include <common/key_derive.h>
#include <common/permute_tx.h>
#include <common/utxo.h>
#include <wally_bip32.h>
struct bitcoin_tx *withdraw_tx(const tal_t *ctx,
const struct utxo **utxos,
const struct bitcoin_address *destination,
const u64 withdraw_amount,
const struct pubkey *changekey,
const u64 changesat,
const struct ext_key *bip32_base)
{
u8 *script;
struct bitcoin_tx *tx =
bitcoin_tx(ctx, tal_count(utxos), changekey ? 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;
tx->input[i].amount = tal_dup(tx, u64, &utxos[i]->amount);
if (utxos[i]->is_p2sh && bip32_base) {
struct pubkey key;
bip32_pubkey(bip32_base, &key, utxos[i]->keyindex);
tx->input[i].script =
bitcoin_scriptsig_p2sh_p2wpkh(tx, &key);
}
}
tx->output[0].amount = withdraw_amount;
script = scriptpubkey_p2pkh(ctx, destination);
tx->output[0].script = script;
if (changesat != 0) {
const void *map[2];
map[0] = int2ptr(0);
map[1] = int2ptr(1);
tx->output[1].script = scriptpubkey_p2wpkh(tx, changekey);
tx->output[1].amount = changesat;
permute_outputs(tx->output, tal_count(tx->output), map);
}
permute_inputs(tx->input, tal_count(tx->input), (const void **)utxos);
return tx;
}