Skip to content

Commit

Permalink
common: make funding_tx and withdraw_tx share UTXO code.
Browse files Browse the repository at this point in the history
They both do the same thing: convert utxos into tx inputs.  Share code.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell authored and cdecker committed Dec 6, 2018
1 parent a046af4 commit 23540fe
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 30 deletions.
1 change: 1 addition & 0 deletions closingd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
18 changes: 2 additions & 16 deletions common/funding_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand All @@ -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);
Expand Down
35 changes: 35 additions & 0 deletions common/test/run-funding_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions common/utxo.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <bitcoin/script.h>
#include <common/key_derive.h>
#include <common/utxo.h>
#include <wire/wire.h>

Expand Down Expand Up @@ -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;
}
9 changes: 9 additions & 0 deletions common/utxo.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
18 changes: 4 additions & 14 deletions common/withdraw_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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;

Expand Down
1 change: 1 addition & 0 deletions connectd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
1 change: 1 addition & 0 deletions gossipd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
1 change: 1 addition & 0 deletions lightningd/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 23540fe

Please sign in to comment.