Browse Source

wally: Switch signatures over to using the wally_tx hash

First step towards decomissioning the handrolled bitcoin_tx operations.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
pr-2587
Christian Decker 6 years ago
committed by Rusty Russell
parent
commit
48006cb674
  1. 38
      bitcoin/signature.c

38
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)

Loading…
Cancel
Save