mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-20 23:54:22 +01:00
Add function to linearize tx into bytes, by generalizing hash code.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
90
bitcoin_tx.c
90
bitcoin_tx.c
@@ -1,8 +1,10 @@
|
|||||||
#include "bitcoin_tx.h"
|
#include "bitcoin_tx.h"
|
||||||
#include <ccan/crypto/sha256/sha256.h>
|
#include <ccan/crypto/sha256/sha256.h>
|
||||||
|
#include <ccan/endian/endian.h>
|
||||||
#include <assert.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;
|
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 >> 8;
|
||||||
(*p++) = v;
|
(*p++) = v;
|
||||||
}
|
}
|
||||||
sha256_update(ctx, buf, p - buf);
|
add(buf, p - buf, addp);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void sha256_tx_input(struct sha256_ctx *ctx,
|
static void add_le32(u32 v,
|
||||||
const struct bitcoin_tx_input *input)
|
void (*add)(const void *, size_t, void *), void *addp)
|
||||||
{
|
{
|
||||||
sha256_update(ctx, &input->txid, sizeof(input->txid));
|
le32 l = cpu_to_le32(v);
|
||||||
sha256_le32(ctx, input->index);
|
add(&l, sizeof(l), addp);
|
||||||
sha256_varint(ctx, input->script_length);
|
|
||||||
sha256_update(ctx, input->script, input->script_length);
|
|
||||||
sha256_le32(ctx, input->sequence_number);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void sha256_tx_output(struct sha256_ctx *ctx,
|
static void add_le64(u64 v,
|
||||||
const struct bitcoin_tx_output *output)
|
void (*add)(const void *, size_t, void *), void *addp)
|
||||||
{
|
{
|
||||||
sha256_le64(ctx, output->amount);
|
le64 l = cpu_to_le64(v);
|
||||||
sha256_varint(ctx, output->script_length);
|
add(&l, sizeof(l), addp);
|
||||||
sha256_update(ctx, output->script, output->script_length);
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
add_le32(tx->version, add, addp);
|
||||||
|
add_varint(tx->input_count, add, addp);
|
||||||
|
for (i = 0; i < tx->input_count; i++)
|
||||||
|
add_tx_input(&tx->input[i], add, addp);
|
||||||
|
add_varint(tx->output_count, add, addp);
|
||||||
|
for (i = 0; i < tx->output_count; i++)
|
||||||
|
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)
|
void sha256_tx(struct sha256_ctx *ctx, const struct bitcoin_tx *tx)
|
||||||
{
|
{
|
||||||
varint_t i;
|
add_tx(tx, add_sha, ctx);
|
||||||
|
}
|
||||||
|
|
||||||
sha256_le32(ctx, tx->version);
|
static void add_linearize(const void *data, size_t len, void *pptr_)
|
||||||
sha256_varint(ctx, tx->input_count);
|
{
|
||||||
for (i = 0; i < tx->input_count; i++)
|
u8 **pptr = pptr_;
|
||||||
sha256_tx_input(ctx, &tx->input[i]);
|
size_t oldsize = tal_count(*pptr);
|
||||||
sha256_varint(ctx, tx->output_count);
|
|
||||||
for (i = 0; i < tx->output_count; i++)
|
tal_resize(pptr, oldsize + len);
|
||||||
sha256_tx_output(ctx, &tx->output[i]);
|
memcpy(*pptr + oldsize, data, len);
|
||||||
sha256_le32(ctx, tx->lock_time);
|
}
|
||||||
|
|
||||||
|
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)
|
void bitcoin_txid(const struct bitcoin_tx *tx, struct sha256_double *txid)
|
||||||
|
|||||||
@@ -39,6 +39,9 @@ void bitcoin_txid(const struct bitcoin_tx *tx, struct sha256_double *txid);
|
|||||||
/* Useful for signature code. */
|
/* Useful for signature code. */
|
||||||
void sha256_tx(struct sha256_ctx *ctx, const struct bitcoin_tx *tx);
|
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
|
/* Allocate a tx: you just need to fill in inputs and outputs (they're
|
||||||
* zeroed with inputs' sequence_number set to FFFFFFFF) */
|
* zeroed with inputs' sequence_number set to FFFFFFFF) */
|
||||||
struct bitcoin_tx *bitcoin_tx(const tal_t *ctx, varint_t input_count,
|
struct bitcoin_tx *bitcoin_tx(const tal_t *ctx, varint_t input_count,
|
||||||
|
|||||||
Reference in New Issue
Block a user