Create bitcoin_tx helper.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2015-05-29 11:33:18 +09:30
parent f43cdf085a
commit 3a62a9172d
3 changed files with 43 additions and 22 deletions

View File

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

View File

@ -1,5 +1,6 @@
#include "bitcoin_tx.h"
#include <ccan/crypto/sha256/sha256.h>
#include <assert.h>
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;
}

View File

@ -1,6 +1,7 @@
#ifndef LIGHTNING_BITCOIN_TX_H
#define LIGHTNING_BITCOIN_TX_H
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>
#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 */