diff --git a/bitcoin/signature.c b/bitcoin/signature.c index 8067566d4..6b796b930 100644 --- a/bitcoin/signature.c +++ b/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; diff --git a/bitcoin/tx.c b/bitcoin/tx.c index aa5ce2040..4b59c730c 100644 --- a/bitcoin/tx.c +++ b/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_) diff --git a/bitcoin/tx.h b/bitcoin/tx.h index 75afaf9b5..831c824be 100644 --- a/bitcoin/tx.h +++ b/bitcoin/tx.h @@ -2,6 +2,7 @@ #define LIGHTNING_BITCOIN_TX_H #include "config.h" #include "shadouble.h" +#include "signature.h" #include #include @@ -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);