mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-20 15:19:07 +01:00
sign: Refactor Schnorr sighash computation out of CreateSchnorrSig
There will be other functions within MutableTransactionSignatureCreator that need to compute the same sighash, so make it a separate member function.
This commit is contained in:
@@ -59,17 +59,14 @@ bool MutableTransactionSignatureCreator::CreateSig(const SigningProvider& provid
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MutableTransactionSignatureCreator::CreateSchnorrSig(const SigningProvider& provider, std::vector<unsigned char>& sig, const XOnlyPubKey& pubkey, const uint256* leaf_hash, const uint256* merkle_root, SigVersion sigversion) const
|
||||
std::optional<uint256> MutableTransactionSignatureCreator::ComputeSchnorrSignatureHash(const uint256* leaf_hash, SigVersion sigversion) const
|
||||
{
|
||||
assert(sigversion == SigVersion::TAPROOT || sigversion == SigVersion::TAPSCRIPT);
|
||||
|
||||
CKey key;
|
||||
if (!provider.GetKeyByXOnly(pubkey, key)) return false;
|
||||
|
||||
// BIP341/BIP342 signing needs lots of precomputed transaction data. While some
|
||||
// (non-SIGHASH_DEFAULT) sighash modes exist that can work with just some subset
|
||||
// of data present, for now, only support signing when everything is provided.
|
||||
if (!m_txdata || !m_txdata->m_bip341_taproot_ready || !m_txdata->m_spent_outputs_ready) return false;
|
||||
if (!m_txdata || !m_txdata->m_bip341_taproot_ready || !m_txdata->m_spent_outputs_ready) return std::nullopt;
|
||||
|
||||
ScriptExecutionData execdata;
|
||||
execdata.m_annex_init = true;
|
||||
@@ -77,15 +74,28 @@ bool MutableTransactionSignatureCreator::CreateSchnorrSig(const SigningProvider&
|
||||
if (sigversion == SigVersion::TAPSCRIPT) {
|
||||
execdata.m_codeseparator_pos_init = true;
|
||||
execdata.m_codeseparator_pos = 0xFFFFFFFF; // Only support non-OP_CODESEPARATOR BIP342 signing for now.
|
||||
if (!leaf_hash) return false; // BIP342 signing needs leaf hash.
|
||||
if (!leaf_hash) return std::nullopt; // BIP342 signing needs leaf hash.
|
||||
execdata.m_tapleaf_hash_init = true;
|
||||
execdata.m_tapleaf_hash = *leaf_hash;
|
||||
}
|
||||
uint256 hash;
|
||||
if (!SignatureHashSchnorr(hash, execdata, m_txto, nIn, nHashType, sigversion, *m_txdata, MissingDataBehavior::FAIL)) return false;
|
||||
if (!SignatureHashSchnorr(hash, execdata, m_txto, nIn, nHashType, sigversion, *m_txdata, MissingDataBehavior::FAIL)) return std::nullopt;
|
||||
return hash;
|
||||
}
|
||||
|
||||
bool MutableTransactionSignatureCreator::CreateSchnorrSig(const SigningProvider& provider, std::vector<unsigned char>& sig, const XOnlyPubKey& pubkey, const uint256* leaf_hash, const uint256* merkle_root, SigVersion sigversion) const
|
||||
{
|
||||
assert(sigversion == SigVersion::TAPROOT || sigversion == SigVersion::TAPSCRIPT);
|
||||
|
||||
CKey key;
|
||||
if (!provider.GetKeyByXOnly(pubkey, key)) return false;
|
||||
|
||||
std::optional<uint256> hash = ComputeSchnorrSignatureHash(leaf_hash, sigversion);
|
||||
if (!hash.has_value()) return false;
|
||||
|
||||
sig.resize(64);
|
||||
// Use uint256{} as aux_rnd for now.
|
||||
if (!key.SignSchnorr(hash, sig, merkle_root, {})) return false;
|
||||
if (!key.SignSchnorr(*hash, sig, merkle_root, {})) return false;
|
||||
if (nHashType) sig.push_back(nHashType);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -45,6 +45,8 @@ class MutableTransactionSignatureCreator : public BaseSignatureCreator
|
||||
const MutableTransactionSignatureChecker checker;
|
||||
const PrecomputedTransactionData* m_txdata;
|
||||
|
||||
std::optional<uint256> ComputeSchnorrSignatureHash(const uint256* leaf_hash, SigVersion sigversion) const;
|
||||
|
||||
public:
|
||||
MutableTransactionSignatureCreator(const CMutableTransaction& tx LIFETIMEBOUND, unsigned int input_idx, const CAmount& amount, int hash_type);
|
||||
MutableTransactionSignatureCreator(const CMutableTransaction& tx LIFETIMEBOUND, unsigned int input_idx, const CAmount& amount, const PrecomputedTransactionData* txdata, int hash_type);
|
||||
|
||||
Reference in New Issue
Block a user