From 393400fa39fa800c4c625c08333c4aee42745bd3 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 2 Jun 2015 12:59:13 +0930 Subject: [PATCH] Make an explicit bitcoin_txid() call. Neater than open-coding it. We still need the raw version for signatures though. Signed-off-by: Rusty Russell --- anchor.c | 5 +---- bitcoin_tx.c | 8 ++++++++ bitcoin_tx.h | 9 ++++++--- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/anchor.c b/anchor.c index 0085b9803..90f87693c 100644 --- a/anchor.c +++ b/anchor.c @@ -97,7 +97,6 @@ void anchor_txid(struct bitcoin_tx *anchor, Pkt *p1, *p2; LeakAnchorSigsAndPretendWeDidnt *leak1, *leak2; size_t i; - struct sha256_ctx shactx; p1 = pkt_from_file(leakfile1, PKT__PKT_OMG_FAIL); p2 = pkt_from_file(leakfile2, PKT__PKT_OMG_FAIL); @@ -121,9 +120,7 @@ void anchor_txid(struct bitcoin_tx *anchor, = leak2->sigs->script[i].len; } - sha256_init(&shactx); - sha256_tx(&shactx, anchor); - sha256_double_done(&shactx, txid); + bitcoin_txid(anchor, txid); pkt__free_unpacked(p1, NULL); pkt__free_unpacked(p2, NULL); diff --git a/bitcoin_tx.c b/bitcoin_tx.c index f2d3a3d2c..02df29a9a 100644 --- a/bitcoin_tx.c +++ b/bitcoin_tx.c @@ -64,6 +64,14 @@ void sha256_tx(struct sha256_ctx *ctx, const struct bitcoin_tx *tx) sha256_le32(ctx, tx->lock_time); } +void bitcoin_txid(const struct bitcoin_tx *tx, struct sha256_double *txid) +{ + struct sha256_ctx ctx = SHA256_INIT; + + sha256_tx(&ctx, tx); + sha256_double_done(&ctx, txid); +} + struct bitcoin_tx *bitcoin_tx(const tal_t *ctx, varint_t input_count, varint_t output_count) { diff --git a/bitcoin_tx.h b/bitcoin_tx.h index 84776c45c..79d3baf09 100644 --- a/bitcoin_tx.h +++ b/bitcoin_tx.h @@ -32,9 +32,12 @@ struct bitcoin_tx_input { u32 sequence_number; }; -/* Linearize the tx. This is good for deriving the txid, as well as the - * signature. */ -void sha256_tx(struct sha256_ctx *shactx, const struct bitcoin_tx *tx); + +/* SHA256^2 the tx: simpler than sha256_tx */ +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); /* Allocate a tx: you just need to fill in inputs and outputs (they're * zeroed with inputs' sequence_number set to FFFFFFFF) */