mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 23:24:27 +01:00
Create bitcoin_tx helper.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
37
anchor.c
37
anchor.c
@@ -10,32 +10,30 @@ struct bitcoin_tx *anchor_tx_create(const tal_t *ctx,
|
|||||||
const OpenChannel *o2,
|
const OpenChannel *o2,
|
||||||
size_t **inmapp, size_t **outmapp)
|
size_t **inmapp, size_t **outmapp)
|
||||||
{
|
{
|
||||||
uint64_t i;
|
uint64_t i, n_out;
|
||||||
struct bitcoin_tx *tx = tal(ctx, struct bitcoin_tx);
|
struct bitcoin_tx *tx;
|
||||||
u8 *redeemscript;
|
u8 *redeemscript;
|
||||||
size_t *inmap, *outmap;
|
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)
|
if (o1->tx_version < o2->tx_version)
|
||||||
tx->version = o1->tx_version;
|
tx->version = o1->tx_version;
|
||||||
else
|
else
|
||||||
tx->version = o2->tx_version;
|
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. */
|
/* Populate inputs. */
|
||||||
for (i = 0; i < o1->anchor->n_inputs; i++) {
|
for (i = 0; i < o1->anchor->n_inputs; i++) {
|
||||||
BitcoinInput *pb = o1->anchor->inputs[i];
|
BitcoinInput *pb = o1->anchor->inputs[i];
|
||||||
struct bitcoin_tx_input *in = &tx->input[i];
|
struct bitcoin_tx_input *in = &tx->input[i];
|
||||||
proto_to_sha256(pb->txid, &in->txid.sha);
|
proto_to_sha256(pb->txid, &in->txid.sha);
|
||||||
in->index = pb->output;
|
in->index = pb->output;
|
||||||
in->sequence_number = 0xFFFFFFFF;
|
|
||||||
/* Leave inputs as stubs for now, for signing. */
|
/* Leave inputs as stubs for now, for signing. */
|
||||||
in->script_length = 0;
|
|
||||||
in->script = NULL;
|
|
||||||
}
|
}
|
||||||
for (i = 0; i < o2->anchor->n_inputs; i++) {
|
for (i = 0; i < o2->anchor->n_inputs; i++) {
|
||||||
BitcoinInput *pb = o2->anchor->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];
|
= &tx->input[o1->anchor->n_inputs + i];
|
||||||
proto_to_sha256(pb->txid, &in->txid.sha);
|
proto_to_sha256(pb->txid, &in->txid.sha);
|
||||||
in->index = pb->output;
|
in->index = pb->output;
|
||||||
in->sequence_number = 0xFFFFFFFF;
|
|
||||||
/* Leave inputs as stubs for now, for signing. */
|
/* Leave inputs as stubs for now, for signing. */
|
||||||
in->script_length = 0;
|
|
||||||
in->script = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Populate outputs. */
|
/* 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))
|
if (add_overflows_u64(o1->anchor->total, o2->anchor->total))
|
||||||
return tal_free(tx);
|
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);
|
tx->output[0].script_length = tal_count(tx->output[0].script);
|
||||||
|
|
||||||
/* Add change transactions (if any) */
|
/* Add change transactions (if any) */
|
||||||
|
n_out = 1;
|
||||||
if (o1->anchor->change) {
|
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->amount = o1->anchor->change->amount;
|
||||||
out->script_length = o1->anchor->change->script.len;
|
out->script_length = o1->anchor->change->script.len;
|
||||||
out->script = o1->anchor->change->script.data;
|
out->script = o1->anchor->change->script.data;
|
||||||
}
|
}
|
||||||
if (o2->anchor->change) {
|
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->amount = o2->anchor->change->amount;
|
||||||
out->script_length = o2->anchor->change->script.len;
|
out->script_length = o2->anchor->change->script.len;
|
||||||
out->script = o2->anchor->change->script.data;
|
out->script = o2->anchor->change->script.data;
|
||||||
}
|
}
|
||||||
|
assert(n_out == tx->output_count);
|
||||||
|
|
||||||
if (inmapp)
|
if (inmapp)
|
||||||
inmap = *inmapp = tal_arr(ctx, size_t, tx->input_count);
|
inmap = *inmapp = tal_arr(ctx, size_t, tx->input_count);
|
||||||
else
|
else
|
||||||
|
|||||||
22
bitcoin_tx.c
22
bitcoin_tx.c
@@ -1,5 +1,6 @@
|
|||||||
#include "bitcoin_tx.h"
|
#include "bitcoin_tx.h"
|
||||||
#include <ccan/crypto/sha256/sha256.h>
|
#include <ccan/crypto/sha256/sha256.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
static void sha256_varint(struct sha256_ctx *ctx, varint_t v)
|
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_tx_output(ctx, &tx->output[i]);
|
||||||
sha256_le32(ctx, tx->lock_time);
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#ifndef LIGHTNING_BITCOIN_TX_H
|
#ifndef LIGHTNING_BITCOIN_TX_H
|
||||||
#define LIGHTNING_BITCOIN_TX_H
|
#define LIGHTNING_BITCOIN_TX_H
|
||||||
#include <ccan/short_types/short_types.h>
|
#include <ccan/short_types/short_types.h>
|
||||||
|
#include <ccan/tal/tal.h>
|
||||||
#include "shadouble.h"
|
#include "shadouble.h"
|
||||||
|
|
||||||
#define BITCOIN_TX_VERSION 1
|
#define BITCOIN_TX_VERSION 1
|
||||||
@@ -35,4 +36,9 @@ struct bitcoin_tx_input {
|
|||||||
* signature. */
|
* signature. */
|
||||||
void sha256_tx(struct sha256_ctx *shactx, const struct bitcoin_tx *tx);
|
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 */
|
#endif /* LIGHTNING_BITCOIN_TX_H */
|
||||||
|
|||||||
Reference in New Issue
Block a user