mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 07:04:22 +01:00
common: make funding_tx and withdraw_tx share UTXO code.
They both do the same thing: convert utxos into tx inputs. Share code. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
committed by
Christian Decker
parent
a046af4416
commit
23540fe956
@@ -55,6 +55,7 @@ CLOSINGD_COMMON_OBJS := \
|
||||
common/gen_peer_status_wire.o \
|
||||
common/gen_status_wire.o \
|
||||
common/htlc_wire.o \
|
||||
common/key_derive.o \
|
||||
common/memleak.o \
|
||||
common/msg_queue.o \
|
||||
common/peer_billboard.o \
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
#include <bitcoin/script.h>
|
||||
#include <bitcoin/tx.h>
|
||||
#include <ccan/ptrint/ptrint.h>
|
||||
#include <common/key_derive.h>
|
||||
#include <common/permute_tx.h>
|
||||
#include <common/utxo.h>
|
||||
|
||||
@@ -21,23 +20,10 @@ struct bitcoin_tx *funding_tx(const tal_t *ctx,
|
||||
const struct pubkey *changekey,
|
||||
const struct ext_key *bip32_base)
|
||||
{
|
||||
struct bitcoin_tx *tx = bitcoin_tx(ctx, tal_count(utxomap),
|
||||
change_satoshis ? 2 : 1);
|
||||
u8 *wscript;
|
||||
size_t i;
|
||||
struct bitcoin_tx *tx;
|
||||
|
||||
for (i = 0; i < tal_count(utxomap); i++) {
|
||||
tx->input[i].txid = utxomap[i]->txid;
|
||||
tx->input[i].index = utxomap[i]->outnum;
|
||||
tx->input[i].amount = tal_dup(tx, u64, &utxomap[i]->amount);
|
||||
if (utxomap[i]->is_p2sh && bip32_base) {
|
||||
struct pubkey key;
|
||||
|
||||
bip32_pubkey(bip32_base, &key, utxomap[i]->keyindex);
|
||||
tx->input[i].script
|
||||
= bitcoin_scriptsig_p2sh_p2wpkh(tx, &key);
|
||||
}
|
||||
}
|
||||
tx = tx_spending_utxos(ctx, utxomap, bip32_base, change_satoshis != 0);
|
||||
|
||||
tx->output[0].amount = funding_satoshis;
|
||||
wscript = bitcoin_redeem_2of2(tx, local_fundingkey, remote_fundingkey);
|
||||
|
||||
@@ -14,6 +14,41 @@
|
||||
#include "../key_derive.c"
|
||||
#include "../type_to_string.c"
|
||||
#include "../permute_tx.c"
|
||||
#include "../utxo.c"
|
||||
|
||||
/* AUTOGENERATED MOCKS START */
|
||||
/* Generated stub for fromwire_bitcoin_txid */
|
||||
void fromwire_bitcoin_txid(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
|
||||
struct bitcoin_txid *txid UNNEEDED)
|
||||
{ fprintf(stderr, "fromwire_bitcoin_txid called!\n"); abort(); }
|
||||
/* Generated stub for fromwire_bool */
|
||||
bool fromwire_bool(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
|
||||
{ fprintf(stderr, "fromwire_bool called!\n"); abort(); }
|
||||
/* Generated stub for fromwire_pubkey */
|
||||
void fromwire_pubkey(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct pubkey *pubkey UNNEEDED)
|
||||
{ fprintf(stderr, "fromwire_pubkey called!\n"); abort(); }
|
||||
/* Generated stub for fromwire_u32 */
|
||||
u32 fromwire_u32(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
|
||||
{ fprintf(stderr, "fromwire_u32 called!\n"); abort(); }
|
||||
/* Generated stub for fromwire_u64 */
|
||||
u64 fromwire_u64(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
|
||||
{ fprintf(stderr, "fromwire_u64 called!\n"); abort(); }
|
||||
/* Generated stub for towire_bitcoin_txid */
|
||||
void towire_bitcoin_txid(u8 **pptr UNNEEDED, const struct bitcoin_txid *txid UNNEEDED)
|
||||
{ fprintf(stderr, "towire_bitcoin_txid called!\n"); abort(); }
|
||||
/* Generated stub for towire_bool */
|
||||
void towire_bool(u8 **pptr UNNEEDED, bool v UNNEEDED)
|
||||
{ fprintf(stderr, "towire_bool called!\n"); abort(); }
|
||||
/* Generated stub for towire_pubkey */
|
||||
void towire_pubkey(u8 **pptr UNNEEDED, const struct pubkey *pubkey UNNEEDED)
|
||||
{ fprintf(stderr, "towire_pubkey called!\n"); abort(); }
|
||||
/* Generated stub for towire_u32 */
|
||||
void towire_u32(u8 **pptr UNNEEDED, u32 v UNNEEDED)
|
||||
{ fprintf(stderr, "towire_u32 called!\n"); abort(); }
|
||||
/* Generated stub for towire_u64 */
|
||||
void towire_u64(u8 **pptr UNNEEDED, u64 v UNNEEDED)
|
||||
{ fprintf(stderr, "towire_u64 called!\n"); abort(); }
|
||||
/* AUTOGENERATED MOCKS END */
|
||||
|
||||
#if 0
|
||||
static struct sha256 sha256_from_hex(const char *hex)
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#include <bitcoin/script.h>
|
||||
#include <common/key_derive.h>
|
||||
#include <common/utxo.h>
|
||||
#include <wire/wire.h>
|
||||
|
||||
@@ -39,3 +41,26 @@ struct utxo *fromwire_utxo(const tal_t *ctx, const u8 **ptr, size_t *max)
|
||||
}
|
||||
return utxo;
|
||||
}
|
||||
|
||||
struct bitcoin_tx *tx_spending_utxos(const tal_t *ctx,
|
||||
const struct utxo **utxos,
|
||||
const struct ext_key *bip32_base,
|
||||
bool add_change_output)
|
||||
{
|
||||
struct bitcoin_tx *tx =
|
||||
bitcoin_tx(ctx, tal_count(utxos), add_change_output ? 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);
|
||||
}
|
||||
}
|
||||
|
||||
return tx;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
#include <ccan/tal/tal.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
struct ext_key;
|
||||
|
||||
/* Information needed for their_unilateral/to-us outputs */
|
||||
struct unilateral_close_info {
|
||||
u64 channel_id;
|
||||
@@ -36,4 +38,11 @@ struct utxo {
|
||||
|
||||
void towire_utxo(u8 **pptr, const struct utxo *utxo);
|
||||
struct utxo *fromwire_utxo(const tal_t *ctx, const u8 **ptr, size_t *max);
|
||||
|
||||
/* Create a tx, and populate inputs from utxos */
|
||||
struct bitcoin_tx *tx_spending_utxos(const tal_t *ctx,
|
||||
const struct utxo **utxos,
|
||||
const struct ext_key *bip32_base,
|
||||
bool add_change_output);
|
||||
|
||||
#endif /* LIGHTNING_COMMON_UTXO_H */
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
#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 <string.h>
|
||||
@@ -16,19 +15,10 @@ struct bitcoin_tx *withdraw_tx(const tal_t *ctx,
|
||||
const u64 changesat,
|
||||
const struct ext_key *bip32_base)
|
||||
{
|
||||
struct bitcoin_tx *tx =
|
||||
bitcoin_tx(ctx, tal_count(utxos), changesat ? 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);
|
||||
}
|
||||
}
|
||||
struct bitcoin_tx *tx;
|
||||
|
||||
tx = tx_spending_utxos(ctx, utxos, bip32_base, changesat != 0);
|
||||
|
||||
tx->output[0].amount = withdraw_amount;
|
||||
tx->output[0].script = destination;
|
||||
|
||||
|
||||
@@ -49,6 +49,7 @@ CONNECTD_COMMON_OBJS := \
|
||||
common/dev_disconnect.o \
|
||||
common/features.o \
|
||||
common/gen_status_wire.o \
|
||||
common/key_derive.o \
|
||||
common/memleak.o \
|
||||
common/msg_queue.o \
|
||||
common/pseudorand.o \
|
||||
|
||||
@@ -49,6 +49,7 @@ GOSSIPD_COMMON_OBJS := \
|
||||
common/dev_disconnect.o \
|
||||
common/features.o \
|
||||
common/gen_status_wire.o \
|
||||
common/key_derive.o \
|
||||
common/memleak.o \
|
||||
common/msg_queue.o \
|
||||
common/ping.o \
|
||||
|
||||
@@ -18,6 +18,7 @@ LIGHTNINGD_TEST_COMMON_OBJS := \
|
||||
common/memleak.o \
|
||||
common/msg_queue.o \
|
||||
common/utils.o \
|
||||
common/utxo.o \
|
||||
common/type_to_string.o \
|
||||
common/permute_tx.o
|
||||
|
||||
|
||||
Reference in New Issue
Block a user