Browse Source

bitcoin/tx: sha256_tx_for_sig() takes sighash flag.

That way it can assert (as we only support SIGHASH_ALL).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 9 years ago
parent
commit
8545db418b
  1. 7
      bitcoin/signature.c
  2. 13
      bitcoin/tx.c
  3. 5
      bitcoin/tx.h

7
bitcoin/signature.c

@ -93,7 +93,6 @@ static void sha256_tx_one_input(struct bitcoin_tx *tx,
const u8 *script, size_t script_len,
struct sha256_double *hash)
{
struct sha256_ctx ctx = SHA256_INIT;
size_t i;
assert(input_num < tx->input_count);
@ -105,11 +104,7 @@ static void sha256_tx_one_input(struct bitcoin_tx *tx,
tx->input[input_num].script_length = script_len;
tx->input[input_num].script = cast_const(u8 *, script);
sha256_init(&ctx);
sha256_tx_for_sig(&ctx, tx, input_num);
sha256_le32(&ctx, SIGHASH_ALL);
sha256_double_done(&ctx, hash);
sha256_tx_for_sig(hash, tx, input_num, SIGHASH_ALL);
/* Reset it for next time. */
tx->input[input_num].script_length = 0;

13
bitcoin/tx.c

@ -171,17 +171,24 @@ static void add_sha(const void *data, size_t len, void *shactx_)
sha256_update(ctx, memcheck(data, len), len);
}
void sha256_tx_for_sig(struct sha256_ctx *ctx, const struct bitcoin_tx *tx,
unsigned int input_num)
void sha256_tx_for_sig(struct sha256_double *h, const struct bitcoin_tx *tx,
unsigned int input_num, enum sighash_type stype)
{
size_t i;
struct sha256_ctx ctx = SHA256_INIT;
/* We only support this. */
assert(stype == SIGHASH_ALL);
/* Caller should zero-out other scripts for signing! */
assert(input_num < tx->input_count);
for (i = 0; i < tx->input_count; i++)
if (i != input_num)
assert(tx->input[i].script_length == 0);
add_tx(tx, add_sha, ctx, false);
add_tx(tx, add_sha, &ctx, false);
sha256_le32(&ctx, stype);
sha256_double_done(&ctx, h);
}
static void add_linearize(const void *data, size_t len, void *pptr_)

5
bitcoin/tx.h

@ -2,6 +2,7 @@
#define LIGHTNING_BITCOIN_TX_H
#include "config.h"
#include "shadouble.h"
#include "signature.h"
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>
@ -43,8 +44,8 @@ struct bitcoin_tx_input {
void bitcoin_txid(const struct bitcoin_tx *tx, struct sha256_double *txid);
/* Useful for signature code. */
void sha256_tx_for_sig(struct sha256_ctx *ctx, const struct bitcoin_tx *tx,
unsigned int input_num);
void sha256_tx_for_sig(struct sha256_double *h, const struct bitcoin_tx *tx,
unsigned int input_num, enum sighash_type stype);
/* Linear bytes of tx. */
u8 *linearize_tx(const tal_t *ctx, const struct bitcoin_tx *tx);

Loading…
Cancel
Save