wallet: Implement withdrawal transaction generation

This will be used by both the master daemon and the HSM to
generate (hopefully) identical transactions for signature and
broadcast.
This commit is contained in:
Christian Decker 2017-06-20 16:46:31 +02:00 committed by Rusty Russell
parent 7738bccf42
commit 479036604b
3 changed files with 82 additions and 1 deletions

View File

@ -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)

47
lightningd/withdraw_tx.c Normal file
View File

@ -0,0 +1,47 @@
#include "withdraw_tx.h"
#include <bitcoin/pubkey.h>
#include <bitcoin/script.h>
#include <ccan/ptrint/ptrint.h>
#include <lightningd/key_derive.h>
#include <lightningd/utxo.h>
#include <permute_tx.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;
}

33
lightningd/withdraw_tx.h Normal file
View File

@ -0,0 +1,33 @@
#ifndef LIGHTNING_LIGHTNINGD_WITHDRAW_TX_H
#define LIGHTNING_LIGHTNINGD_WITHDRAW_TX_H
#include "config.h"
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>
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 */