diff --git a/lightningd/Makefile b/lightningd/Makefile index dc2740487..27a999fce 100644 --- a/lightningd/Makefile +++ b/lightningd/Makefile @@ -55,7 +55,8 @@ LIGHTNINGD_LIB_SRC := \ lightningd/ping.c \ lightningd/sphinx.c \ lightningd/status.c \ - lightningd/utxo.c + lightningd/utxo.c \ + lightningd/withdraw_tx.c LIGHTNINGD_LIB_OBJS := $(LIGHTNINGD_LIB_SRC:.c=.o) LIGHTNINGD_LIB_HEADERS := $(LIGHTNINGD_LIB_SRC:.c=.h) diff --git a/lightningd/withdraw_tx.c b/lightningd/withdraw_tx.c new file mode 100644 index 000000000..36bf86503 --- /dev/null +++ b/lightningd/withdraw_tx.c @@ -0,0 +1,47 @@ +#include "withdraw_tx.h" +#include +#include +#include +#include +#include +#include +#include + +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; +} + diff --git a/lightningd/withdraw_tx.h b/lightningd/withdraw_tx.h new file mode 100644 index 000000000..fa3b9475b --- /dev/null +++ b/lightningd/withdraw_tx.h @@ -0,0 +1,33 @@ +#ifndef LIGHTNING_LIGHTNINGD_WITHDRAW_TX_H +#define LIGHTNING_LIGHTNINGD_WITHDRAW_TX_H +#include "config.h" +#include +#include + +struct bitcoin_tx; +struct ext_key; +struct privkey; +struct pubkey; +struct bitcoin_address; +struct utxo; + +/** + * withdraw_tx - Create a p2pkh withdrawal transaction + * + * @ctx: context to tal from. + * @utxos: (in/out) tal_arr of UTXO pointers to spend (permuted to match) + * @destination: (in) bitcoin_address to send to. + * @amount: (in) satoshis to send to the destination + * @changekey: (in) key to send change to (only used if change_satoshis != 0). + * @changesa: (in) amount to send as change. + * @bip32_base: (in) bip32 base for key derivation, or NULL. + */ +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); + +#endif /* LIGHTNING_LIGHTNINGD_WITHDRAW_TX_H */