Browse Source

Add function to linearize tx into bytes, by generalizing hash code.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 10 years ago
parent
commit
bd38003db6
  1. 86
      bitcoin_tx.c
  2. 3
      bitcoin_tx.h

86
bitcoin_tx.c

@ -1,8 +1,10 @@
#include "bitcoin_tx.h"
#include <ccan/crypto/sha256/sha256.h>
#include <ccan/endian/endian.h>
#include <assert.h>
static void sha256_varint(struct sha256_ctx *ctx, varint_t v)
static void add_varint(varint_t v,
void (*add)(const void *, size_t, void *), void *addp)
{
u8 buf[9], *p = buf;
@ -29,39 +31,81 @@ static void sha256_varint(struct sha256_ctx *ctx, varint_t v)
(*p++) = v >> 8;
(*p++) = v;
}
sha256_update(ctx, buf, p - buf);
add(buf, p - buf, addp);
}
static void sha256_tx_input(struct sha256_ctx *ctx,
const struct bitcoin_tx_input *input)
static void add_le32(u32 v,
void (*add)(const void *, size_t, void *), void *addp)
{
sha256_update(ctx, &input->txid, sizeof(input->txid));
sha256_le32(ctx, input->index);
sha256_varint(ctx, input->script_length);
sha256_update(ctx, input->script, input->script_length);
sha256_le32(ctx, input->sequence_number);
le32 l = cpu_to_le32(v);
add(&l, sizeof(l), addp);
}
static void sha256_tx_output(struct sha256_ctx *ctx,
const struct bitcoin_tx_output *output)
static void add_le64(u64 v,
void (*add)(const void *, size_t, void *), void *addp)
{
sha256_le64(ctx, output->amount);
sha256_varint(ctx, output->script_length);
sha256_update(ctx, output->script, output->script_length);
le64 l = cpu_to_le64(v);
add(&l, sizeof(l), addp);
}
void sha256_tx(struct sha256_ctx *ctx, const struct bitcoin_tx *tx)
static void add_tx_input(const struct bitcoin_tx_input *input,
void (*add)(const void *, size_t, void *), void *addp)
{
add(&input->txid, sizeof(input->txid), addp);
add_le32(input->index, add, addp);
add_varint(input->script_length, add, addp);
add(input->script, input->script_length, addp);
add_le32(input->sequence_number, add, addp);
}
static void add_tx_output(const struct bitcoin_tx_output *output,
void (*add)(const void *, size_t, void *), void *addp)
{
add_le64(output->amount, add, addp);
add_varint(output->script_length, add, addp);
add(output->script, output->script_length, addp);
}
static void add_tx(const struct bitcoin_tx *tx,
void (*add)(const void *, size_t, void *), void *addp)
{
varint_t i;
sha256_le32(ctx, tx->version);
sha256_varint(ctx, tx->input_count);
add_le32(tx->version, add, addp);
add_varint(tx->input_count, add, addp);
for (i = 0; i < tx->input_count; i++)
sha256_tx_input(ctx, &tx->input[i]);
sha256_varint(ctx, tx->output_count);
add_tx_input(&tx->input[i], add, addp);
add_varint(tx->output_count, add, addp);
for (i = 0; i < tx->output_count; i++)
sha256_tx_output(ctx, &tx->output[i]);
sha256_le32(ctx, tx->lock_time);
add_tx_output(&tx->output[i], add, addp);
add_le32(tx->lock_time, add, addp);
}
static void add_sha(const void *data, size_t len, void *shactx_)
{
struct sha256_ctx *ctx = shactx_;
sha256_update(ctx, data, len);
}
void sha256_tx(struct sha256_ctx *ctx, const struct bitcoin_tx *tx)
{
add_tx(tx, add_sha, ctx);
}
static void add_linearize(const void *data, size_t len, void *pptr_)
{
u8 **pptr = pptr_;
size_t oldsize = tal_count(*pptr);
tal_resize(pptr, oldsize + len);
memcpy(*pptr + oldsize, data, len);
}
u8 *linearize_tx(const tal_t *ctx, const struct bitcoin_tx *tx)
{
u8 *arr = tal_arr(ctx, u8, 0);
add_tx(tx, add_linearize, &arr);
return arr;
}
void bitcoin_txid(const struct bitcoin_tx *tx, struct sha256_double *txid)

3
bitcoin_tx.h

@ -39,6 +39,9 @@ void bitcoin_txid(const struct bitcoin_tx *tx, struct sha256_double *txid);
/* Useful for signature code. */
void sha256_tx(struct sha256_ctx *ctx, const struct bitcoin_tx *tx);
/* Linear bytes of tx. */
u8 *linearize_tx(const tal_t *ctx, 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,

Loading…
Cancel
Save