From 3a62a9172d5b27fd18cfeddedfea8bddb45a8c7a Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 29 May 2015 11:33:18 +0930 Subject: [PATCH] Create bitcoin_tx helper. Signed-off-by: Rusty Russell --- anchor.c | 37 +++++++++++++++---------------------- bitcoin_tx.c | 22 ++++++++++++++++++++++ bitcoin_tx.h | 6 ++++++ 3 files changed, 43 insertions(+), 22 deletions(-) diff --git a/anchor.c b/anchor.c index 015d5d371..04ba2fa50 100644 --- a/anchor.c +++ b/anchor.c @@ -10,32 +10,30 @@ struct bitcoin_tx *anchor_tx_create(const tal_t *ctx, const OpenChannel *o2, size_t **inmapp, size_t **outmapp) { - uint64_t i; - struct bitcoin_tx *tx = tal(ctx, struct bitcoin_tx); + uint64_t i, n_out; + struct bitcoin_tx *tx; u8 *redeemscript; size_t *inmap, *outmap; - /* Use lesser of two versions. */ + if (add_overflows_size_t(o1->anchor->n_inputs, o2->anchor->n_inputs)) + return tal_free(tx); + + n_out = 1 + !!o1->anchor->change + !!o2->anchor->change; + tx = bitcoin_tx(ctx, o1->anchor->n_inputs+o2->anchor->n_inputs, n_out); + + /* Override version to use lesser of two versions. */ if (o1->tx_version < o2->tx_version) tx->version = o1->tx_version; else tx->version = o2->tx_version; - - if (add_overflows_size_t(o1->anchor->n_inputs, o2->anchor->n_inputs)) - return tal_free(tx); - tx->input_count = o1->anchor->n_inputs + o2->anchor->n_inputs; - - tx->input = tal_arr(tx, struct bitcoin_tx_input, tx->input_count); + /* Populate inputs. */ for (i = 0; i < o1->anchor->n_inputs; i++) { BitcoinInput *pb = o1->anchor->inputs[i]; struct bitcoin_tx_input *in = &tx->input[i]; proto_to_sha256(pb->txid, &in->txid.sha); in->index = pb->output; - in->sequence_number = 0xFFFFFFFF; /* Leave inputs as stubs for now, for signing. */ - in->script_length = 0; - in->script = NULL; } for (i = 0; i < o2->anchor->n_inputs; i++) { BitcoinInput *pb = o2->anchor->inputs[i]; @@ -43,17 +41,10 @@ struct bitcoin_tx *anchor_tx_create(const tal_t *ctx, = &tx->input[o1->anchor->n_inputs + i]; proto_to_sha256(pb->txid, &in->txid.sha); in->index = pb->output; - in->sequence_number = 0xFFFFFFFF; /* Leave inputs as stubs for now, for signing. */ - in->script_length = 0; - in->script = NULL; } /* Populate outputs. */ - tx->output_count = 1; - /* Allocate for worst case. */ - tx->output = tal_arr(tx, struct bitcoin_tx_output, 3); - if (add_overflows_u64(o1->anchor->total, o2->anchor->total)) return tal_free(tx); @@ -65,19 +56,21 @@ struct bitcoin_tx *anchor_tx_create(const tal_t *ctx, tx->output[0].script_length = tal_count(tx->output[0].script); /* Add change transactions (if any) */ + n_out = 1; if (o1->anchor->change) { - struct bitcoin_tx_output *out = &tx->output[tx->output_count++]; + struct bitcoin_tx_output *out = &tx->output[n_out++]; out->amount = o1->anchor->change->amount; out->script_length = o1->anchor->change->script.len; out->script = o1->anchor->change->script.data; } if (o2->anchor->change) { - struct bitcoin_tx_output *out = &tx->output[tx->output_count++]; + struct bitcoin_tx_output *out = &tx->output[n_out++]; out->amount = o2->anchor->change->amount; out->script_length = o2->anchor->change->script.len; out->script = o2->anchor->change->script.data; } - + assert(n_out == tx->output_count); + if (inmapp) inmap = *inmapp = tal_arr(ctx, size_t, tx->input_count); else diff --git a/bitcoin_tx.c b/bitcoin_tx.c index e287c1e30..f2d3a3d2c 100644 --- a/bitcoin_tx.c +++ b/bitcoin_tx.c @@ -1,5 +1,6 @@ #include "bitcoin_tx.h" #include +#include static void sha256_varint(struct sha256_ctx *ctx, varint_t v) { @@ -62,3 +63,24 @@ void sha256_tx(struct sha256_ctx *ctx, const struct bitcoin_tx *tx) sha256_tx_output(ctx, &tx->output[i]); sha256_le32(ctx, tx->lock_time); } + +struct bitcoin_tx *bitcoin_tx(const tal_t *ctx, varint_t input_count, + varint_t output_count) +{ + struct bitcoin_tx *tx = tal(ctx, struct bitcoin_tx); + size_t i; + + tx->version = BITCOIN_TX_VERSION; + tx->output_count = output_count; + tx->output = tal_arrz(tx, struct bitcoin_tx_output, output_count); + tx->input_count = input_count; + tx->input = tal_arrz(tx, struct bitcoin_tx_input, input_count); + for (i = 0; i < tx->input_count; i++) { + /* We assume NULL is a zero bitmap */ + assert(tx->input[i].script == NULL); + tx->input[i].sequence_number = 0xFFFFFFFF; + } + tx->lock_time = 0xFFFFFFFF; + + return tx; +} diff --git a/bitcoin_tx.h b/bitcoin_tx.h index 662b16f2f..84776c45c 100644 --- a/bitcoin_tx.h +++ b/bitcoin_tx.h @@ -1,6 +1,7 @@ #ifndef LIGHTNING_BITCOIN_TX_H #define LIGHTNING_BITCOIN_TX_H #include +#include #include "shadouble.h" #define BITCOIN_TX_VERSION 1 @@ -35,4 +36,9 @@ struct bitcoin_tx_input { * signature. */ void sha256_tx(struct sha256_ctx *shactx, const struct bitcoin_tx *tx); +/* Allocate a tx: you just need to fill in inputs and outputs (they're + * zeroed with inputs' sequence_number set to FFFFFFFF) */ +struct bitcoin_tx *bitcoin_tx(const tal_t *ctx, varint_t input_count, + varint_t output_count); + #endif /* LIGHTNING_BITCOIN_TX_H */