Example #1
0
/* Only does SIGHASH_ALL */
static void sha256_tx_one_input(struct bitcoin_tx *tx,
				size_t input_num,
				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);

	/* You must have all inputs zeroed to start. */
	for (i = 0; i < tx->input_count; i++)
		assert(tx->input[i].script_length == 0);

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

	/* Reset it for next time. */
	tx->input[input_num].script_length = 0;
	tx->input[input_num].script = NULL;
}
Example #2
0
void sha256_tx_for_sig(struct sha256_double *h, const struct bitcoin_tx *tx,
		       unsigned int input_num, enum sighash_type stype,
		       const u8 *witness_script)
{
	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);

	if (witness_script) {
		/* BIP143 hashing if OP_CHECKSIG is inside witness. */
		hash_for_segwit(&ctx, tx, input_num, witness_script);
	} else {
		/* Otherwise signature hashing never includes witness. */
		add_tx(tx, add_sha, &ctx, false);
	}

	sha256_le32(&ctx, stype);
	sha256_double_done(&ctx, h);
}