From 48006cb674679163f0e720fc3b22f87abcd64b5b Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Fri, 22 Mar 2019 15:30:49 +0100 Subject: [PATCH] wally: Switch signatures over to using the wally_tx hash First step towards decomissioning the handrolled bitcoin_tx operations. Signed-off-by: Christian Decker --- bitcoin/signature.c | 38 ++++++++++++++------------------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/bitcoin/signature.c b/bitcoin/signature.c index 5ff0c8820..713d004d9 100644 --- a/bitcoin/signature.c +++ b/bitcoin/signature.c @@ -88,19 +88,6 @@ void sign_hash(const struct privkey *privkey, assert(ok); } -static void sha256_tx_one_input(const struct bitcoin_tx *tx, - size_t input_num, - const u8 *script, - const u8 *witness_script, - enum sighash_type sighash_type, - struct sha256_double *hash) -{ - assert(input_num < tal_count(tx->input)); - - sha256_tx_for_sig(hash, tx, input_num, script, witness_script, - sighash_type); -} - void sign_tx_input(const struct bitcoin_tx *tx, unsigned int in, const u8 *subscript, @@ -109,20 +96,18 @@ void sign_tx_input(const struct bitcoin_tx *tx, enum sighash_type sighash_type, struct bitcoin_signature *sig) { - struct sha256_double hash, whash; - int flags = witness_script != NULL ? WALLY_TX_FLAG_USE_WITNESS : 0; - const u8 *script = witness_script ? witness_script : subscript; + struct sha256_double hash; + bool use_segwit = witness_script != NULL; + int flags = use_segwit ? WALLY_TX_FLAG_USE_WITNESS : 0; + const u8 *script = use_segwit ? witness_script : subscript; assert(sighash_type_valid(sighash_type)); + sig->sighash_type = sighash_type; wally_tx_get_btc_signature_hash( tx->wtx, in, script, tal_bytelen(script), - tx->input[in].amount->satoshis /* Raw: low-level helper */, - sighash_type, flags, whash.sha.u.u8, sizeof(whash)); - sha256_tx_one_input(tx, in, subscript, witness_script, - sighash_type, &hash); - - assert(memeq(&hash, sizeof(hash), &whash, sizeof(whash))); + tx->input_amounts[in]->satoshis /* Raw: low-level helper */, + sighash_type, flags, hash.sha.u.u8, sizeof(hash)); dump_tx("Signing", tx, in, subscript, key, &hash); sign_hash(privkey, &hash, &sig->s); @@ -147,6 +132,9 @@ bool check_tx_sig(const struct bitcoin_tx *tx, size_t input_num, const struct bitcoin_signature *sig) { struct sha256_double hash; + bool use_segwit = witness_script != NULL; + int flags = use_segwit ? WALLY_TX_FLAG_USE_WITNESS : 0; + const u8 *script = use_segwit ? witness_script : redeemscript; bool ret; /* We only support a limited subset of sighash types. */ @@ -158,8 +146,10 @@ bool check_tx_sig(const struct bitcoin_tx *tx, size_t input_num, } assert(input_num < tal_count(tx->input)); - sha256_tx_one_input(tx, input_num, redeemscript, witness_script, - sig->sighash_type, &hash); + wally_tx_get_btc_signature_hash( + tx->wtx, input_num, script, tal_bytelen(script), + tx->input_amounts[input_num]->satoshis /* Raw: low-level helper */, + sig->sighash_type, flags, hash.sha.u.u8, sizeof(hash)); ret = check_signed_hash(&hash, &sig->s, key); if (!ret)