Branch data Line data Source code
# 1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
# 2 : : // Copyright (c) 2009-2021 The Bitcoin Core developers
# 3 : : // Distributed under the MIT software license, see the accompanying
# 4 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
# 5 : :
# 6 : : #include <script/sign.h>
# 7 : :
# 8 : : #include <consensus/amount.h>
# 9 : : #include <key.h>
# 10 : : #include <policy/policy.h>
# 11 : : #include <primitives/transaction.h>
# 12 : : #include <script/keyorigin.h>
# 13 : : #include <script/signingprovider.h>
# 14 : : #include <script/standard.h>
# 15 : : #include <uint256.h>
# 16 : : #include <util/translation.h>
# 17 : : #include <util/vector.h>
# 18 : :
# 19 : : typedef std::vector<unsigned char> valtype;
# 20 : :
# 21 : : MutableTransactionSignatureCreator::MutableTransactionSignatureCreator(const CMutableTransaction* tx, unsigned int input_idx, const CAmount& amount, int hash_type)
# 22 : : : txTo{tx}, nIn{input_idx}, nHashType{hash_type}, amount{amount}, checker{txTo, nIn, amount, MissingDataBehavior::FAIL},
# 23 : : m_txdata(nullptr)
# 24 : 10038 : {
# 25 : 10038 : }
# 26 : :
# 27 : : MutableTransactionSignatureCreator::MutableTransactionSignatureCreator(const CMutableTransaction* tx, unsigned int input_idx, const CAmount& amount, const PrecomputedTransactionData* txdata, int hash_type)
# 28 : : : txTo{tx}, nIn{input_idx}, nHashType{hash_type}, amount{amount},
# 29 : : checker{txdata ? MutableTransactionSignatureChecker{txTo, nIn, amount, *txdata, MissingDataBehavior::FAIL} :
# 30 : : MutableTransactionSignatureChecker{txTo, nIn, amount, MissingDataBehavior::FAIL}},
# 31 : : m_txdata(txdata)
# 32 : 83238 : {
# 33 : 83238 : }
# 34 : :
# 35 : : bool MutableTransactionSignatureCreator::CreateSig(const SigningProvider& provider, std::vector<unsigned char>& vchSig, const CKeyID& address, const CScript& scriptCode, SigVersion sigversion) const
# 36 : 40829 : {
# 37 : 40829 : assert(sigversion == SigVersion::BASE || sigversion == SigVersion::WITNESS_V0);
# 38 : :
# 39 : 0 : CKey key;
# 40 [ + + ]: 40829 : if (!provider.GetKey(address, key))
# 41 : 4636 : return false;
# 42 : :
# 43 : : // Signing with uncompressed keys is disabled in witness scripts
# 44 [ + + ][ + + ]: 36193 : if (sigversion == SigVersion::WITNESS_V0 && !key.IsCompressed())
# 45 : 8 : return false;
# 46 : :
# 47 : : // Signing without known amount does not work in witness scripts.
# 48 [ + + ][ - + ]: 36185 : if (sigversion == SigVersion::WITNESS_V0 && !MoneyRange(amount)) return false;
# 49 : :
# 50 : : // BASE/WITNESS_V0 signatures don't support explicit SIGHASH_DEFAULT, use SIGHASH_ALL instead.
# 51 [ + + ]: 36185 : const int hashtype = nHashType == SIGHASH_DEFAULT ? SIGHASH_ALL : nHashType;
# 52 : :
# 53 : 36185 : uint256 hash = SignatureHash(scriptCode, *txTo, nIn, hashtype, amount, sigversion, m_txdata);
# 54 [ - + ]: 36185 : if (!key.Sign(hash, vchSig))
# 55 : 0 : return false;
# 56 : 36185 : vchSig.push_back((unsigned char)hashtype);
# 57 : 36185 : return true;
# 58 : 36185 : }
# 59 : :
# 60 : : 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
# 61 : 20087 : {
# 62 : 20087 : assert(sigversion == SigVersion::TAPROOT || sigversion == SigVersion::TAPSCRIPT);
# 63 : :
# 64 : 0 : CKey key;
# 65 [ + + ]: 20087 : if (!provider.GetKeyByXOnly(pubkey, key)) return false;
# 66 : :
# 67 : : // BIP341/BIP342 signing needs lots of precomputed transaction data. While some
# 68 : : // (non-SIGHASH_DEFAULT) sighash modes exist that can work with just some subset
# 69 : : // of data present, for now, only support signing when everything is provided.
# 70 [ - + ][ - + ]: 301 : if (!m_txdata || !m_txdata->m_bip341_taproot_ready || !m_txdata->m_spent_outputs_ready) return false;
# [ - + ]
# 71 : :
# 72 : 301 : ScriptExecutionData execdata;
# 73 : 301 : execdata.m_annex_init = true;
# 74 : 301 : execdata.m_annex_present = false; // Only support annex-less signing for now.
# 75 [ + + ]: 301 : if (sigversion == SigVersion::TAPSCRIPT) {
# 76 : 179 : execdata.m_codeseparator_pos_init = true;
# 77 : 179 : execdata.m_codeseparator_pos = 0xFFFFFFFF; // Only support non-OP_CODESEPARATOR BIP342 signing for now.
# 78 [ - + ]: 179 : if (!leaf_hash) return false; // BIP342 signing needs leaf hash.
# 79 : 179 : execdata.m_tapleaf_hash_init = true;
# 80 : 179 : execdata.m_tapleaf_hash = *leaf_hash;
# 81 : 179 : }
# 82 : 301 : uint256 hash;
# 83 [ - + ]: 301 : if (!SignatureHashSchnorr(hash, execdata, *txTo, nIn, nHashType, sigversion, *m_txdata, MissingDataBehavior::FAIL)) return false;
# 84 : 301 : sig.resize(64);
# 85 : : // Use uint256{} as aux_rnd for now.
# 86 [ - + ]: 301 : if (!key.SignSchnorr(hash, sig, merkle_root, {})) return false;
# 87 [ + + ]: 301 : if (nHashType) sig.push_back(nHashType);
# 88 : 301 : return true;
# 89 : 301 : }
# 90 : :
# 91 : : static bool GetCScript(const SigningProvider& provider, const SignatureData& sigdata, const CScriptID& scriptid, CScript& script)
# 92 : 21466 : {
# 93 [ + + ]: 21466 : if (provider.GetCScript(scriptid, script)) {
# 94 : 15821 : return true;
# 95 : 15821 : }
# 96 : : // Look for scripts in SignatureData
# 97 [ + + ]: 5645 : if (CScriptID(sigdata.redeem_script) == scriptid) {
# 98 : 385 : script = sigdata.redeem_script;
# 99 : 385 : return true;
# 100 [ + + ]: 5260 : } else if (CScriptID(sigdata.witness_script) == scriptid) {
# 101 : 786 : script = sigdata.witness_script;
# 102 : 786 : return true;
# 103 : 786 : }
# 104 : 4474 : return false;
# 105 : 5645 : }
# 106 : :
# 107 : : static bool GetPubKey(const SigningProvider& provider, const SignatureData& sigdata, const CKeyID& address, CPubKey& pubkey)
# 108 : 838829 : {
# 109 : : // Look for pubkey in all partial sigs
# 110 : 838829 : const auto it = sigdata.signatures.find(address);
# 111 [ + + ]: 838829 : if (it != sigdata.signatures.end()) {
# 112 : 22 : pubkey = it->second.first;
# 113 : 22 : return true;
# 114 : 22 : }
# 115 : : // Look for pubkey in pubkey list
# 116 : 838807 : const auto& pk_it = sigdata.misc_pubkeys.find(address);
# 117 [ + + ]: 838807 : if (pk_it != sigdata.misc_pubkeys.end()) {
# 118 : 1693 : pubkey = pk_it->second.first;
# 119 : 1693 : return true;
# 120 : 1693 : }
# 121 : : // Query the underlying provider
# 122 : 837114 : return provider.GetPubKey(address, pubkey);
# 123 : 838807 : }
# 124 : :
# 125 : : static bool CreateSig(const BaseSignatureCreator& creator, SignatureData& sigdata, const SigningProvider& provider, std::vector<unsigned char>& sig_out, const CPubKey& pubkey, const CScript& scriptcode, SigVersion sigversion)
# 126 : 783808 : {
# 127 : 783808 : CKeyID keyid = pubkey.GetID();
# 128 : 783808 : const auto it = sigdata.signatures.find(keyid);
# 129 [ + + ]: 783808 : if (it != sigdata.signatures.end()) {
# 130 : 943 : sig_out = it->second.second;
# 131 : 943 : return true;
# 132 : 943 : }
# 133 : 782865 : KeyOriginInfo info;
# 134 [ + + ]: 782865 : if (provider.GetKeyOrigin(keyid, info)) {
# 135 : 769694 : sigdata.misc_pubkeys.emplace(keyid, std::make_pair(pubkey, std::move(info)));
# 136 : 769694 : }
# 137 [ + + ]: 782865 : if (creator.CreateSig(provider, sig_out, keyid, scriptcode, sigversion)) {
# 138 : 778221 : auto i = sigdata.signatures.emplace(keyid, SigPair(pubkey, sig_out));
# 139 : 778221 : assert(i.second);
# 140 : 0 : return true;
# 141 : 778221 : }
# 142 : : // Could not make signature or signature not found, add keyid to missing
# 143 : 4644 : sigdata.missing_sigs.push_back(keyid);
# 144 : 4644 : return false;
# 145 : 782865 : }
# 146 : :
# 147 : : static bool CreateTaprootScriptSig(const BaseSignatureCreator& creator, SignatureData& sigdata, const SigningProvider& provider, std::vector<unsigned char>& sig_out, const XOnlyPubKey& pubkey, const uint256& leaf_hash, SigVersion sigversion)
# 148 : 16402 : {
# 149 : 16402 : auto lookup_key = std::make_pair(pubkey, leaf_hash);
# 150 : 16402 : auto it = sigdata.taproot_script_sigs.find(lookup_key);
# 151 [ - + ]: 16402 : if (it != sigdata.taproot_script_sigs.end()) {
# 152 : 0 : sig_out = it->second;
# 153 : 0 : }
# 154 [ + + ]: 16402 : if (creator.CreateSchnorrSig(provider, sig_out, pubkey, &leaf_hash, nullptr, sigversion)) {
# 155 : 179 : sigdata.taproot_script_sigs[lookup_key] = sig_out;
# 156 : 179 : return true;
# 157 : 179 : }
# 158 : 16223 : return false;
# 159 : 16402 : }
# 160 : :
# 161 : : static bool SignTaprootScript(const SigningProvider& provider, const BaseSignatureCreator& creator, SignatureData& sigdata, int leaf_version, const CScript& script, std::vector<valtype>& result)
# 162 : 307 : {
# 163 : : // Only BIP342 tapscript signing is supported for now.
# 164 [ - + ]: 307 : if (leaf_version != TAPROOT_LEAF_TAPSCRIPT) return false;
# 165 : 307 : SigVersion sigversion = SigVersion::TAPSCRIPT;
# 166 : :
# 167 : 307 : uint256 leaf_hash = (CHashWriter(HASHER_TAPLEAF) << uint8_t(leaf_version) << script).GetSHA256();
# 168 : :
# 169 : : // <xonly pubkey> OP_CHECKSIG
# 170 [ + + ][ + - ]: 307 : if (script.size() == 34 && script[33] == OP_CHECKSIG && script[0] == 0x20) {
# [ + - ]
# 171 : 196 : XOnlyPubKey pubkey{Span{script}.subspan(1, 32)};
# 172 : 196 : std::vector<unsigned char> sig;
# 173 [ + + ]: 196 : if (CreateTaprootScriptSig(creator, sigdata, provider, sig, pubkey, leaf_hash, sigversion)) {
# 174 : 60 : result = Vector(std::move(sig));
# 175 : 60 : return true;
# 176 : 60 : }
# 177 : 136 : return false;
# 178 : 196 : }
# 179 : :
# 180 : : // multi_a scripts (<key> OP_CHECKSIG <key> OP_CHECKSIGADD <key> OP_CHECKSIGADD <k> OP_NUMEQUAL)
# 181 [ + - ]: 111 : if (auto match = MatchMultiA(script)) {
# 182 : 111 : std::vector<std::vector<unsigned char>> sigs;
# 183 : 111 : int good_sigs = 0;
# 184 [ + + ]: 16317 : for (size_t i = 0; i < match->second.size(); ++i) {
# 185 : 16206 : XOnlyPubKey pubkey{*(match->second.rbegin() + i)};
# 186 : 16206 : std::vector<unsigned char> sig;
# 187 : 16206 : bool good_sig = CreateTaprootScriptSig(creator, sigdata, provider, sig, pubkey, leaf_hash, sigversion);
# 188 [ + + ][ + + ]: 16206 : if (good_sig && good_sigs < match->first) {
# 189 : 107 : ++good_sigs;
# 190 : 107 : sigs.push_back(std::move(sig));
# 191 : 16099 : } else {
# 192 : 16099 : sigs.emplace_back();
# 193 : 16099 : }
# 194 : 16206 : }
# 195 [ + + ]: 111 : if (good_sigs == match->first) {
# 196 : 83 : result = std::move(sigs);
# 197 : 83 : return true;
# 198 : 83 : }
# 199 : 28 : return false;
# 200 : 111 : }
# 201 : :
# 202 : 0 : return false;
# 203 : 111 : }
# 204 : :
# 205 : : static bool SignTaproot(const SigningProvider& provider, const BaseSignatureCreator& creator, const WitnessV1Taproot& output, SignatureData& sigdata, std::vector<valtype>& result)
# 206 : 8272 : {
# 207 : 8272 : TaprootSpendData spenddata;
# 208 : :
# 209 : : // Gather information about this output.
# 210 [ + + ]: 8272 : if (provider.GetTaprootSpendData(output, spenddata)) {
# 211 : 2315 : sigdata.tr_spenddata.Merge(spenddata);
# 212 : 2315 : }
# 213 : :
# 214 : : // Try key path spending.
# 215 : 8272 : {
# 216 : 8272 : std::vector<unsigned char> sig;
# 217 [ + - ]: 8272 : if (sigdata.taproot_key_path_sig.size() == 0) {
# 218 [ + + ]: 8272 : if (creator.CreateSchnorrSig(provider, sig, spenddata.internal_key, nullptr, &spenddata.merkle_root, SigVersion::TAPROOT)) {
# 219 : 4709 : sigdata.taproot_key_path_sig = sig;
# 220 : 4709 : }
# 221 : 8272 : }
# 222 [ + + ]: 8272 : if (sigdata.taproot_key_path_sig.size()) {
# 223 : 4709 : result = Vector(sigdata.taproot_key_path_sig);
# 224 : 4709 : return true;
# 225 : 4709 : }
# 226 : 8272 : }
# 227 : :
# 228 : : // Try script path spending.
# 229 : 3563 : std::vector<std::vector<unsigned char>> smallest_result_stack;
# 230 [ + + ]: 3563 : for (const auto& [key, control_blocks] : sigdata.tr_spenddata.scripts) {
# 231 : 307 : const auto& [script, leaf_ver] = key;
# 232 : 307 : std::vector<std::vector<unsigned char>> result_stack;
# 233 [ + + ]: 307 : if (SignTaprootScript(provider, creator, sigdata, leaf_ver, script, result_stack)) {
# 234 : 143 : result_stack.emplace_back(std::begin(script), std::end(script)); // Push the script
# 235 : 143 : result_stack.push_back(*control_blocks.begin()); // Push the smallest control block
# 236 [ + - ]: 143 : if (smallest_result_stack.size() == 0 ||
# 237 [ # # ]: 143 : GetSerializeSize(result_stack, PROTOCOL_VERSION) < GetSerializeSize(smallest_result_stack, PROTOCOL_VERSION)) {
# 238 : 143 : smallest_result_stack = std::move(result_stack);
# 239 : 143 : }
# 240 : 143 : }
# 241 : 307 : }
# 242 [ + + ]: 3563 : if (smallest_result_stack.size() != 0) {
# 243 : 143 : result = std::move(smallest_result_stack);
# 244 : 143 : return true;
# 245 : 143 : }
# 246 : :
# 247 : 3420 : return false;
# 248 : 3563 : }
# 249 : :
# 250 : : /**
# 251 : : * Sign scriptPubKey using signature made with creator.
# 252 : : * Signatures are returned in scriptSigRet (or returns false if scriptPubKey can't be signed),
# 253 : : * unless whichTypeRet is TxoutType::SCRIPTHASH, in which case scriptSigRet is the redemption script.
# 254 : : * Returns false if scriptPubKey could not be completely satisfied.
# 255 : : */
# 256 : : static bool SignStep(const SigningProvider& provider, const BaseSignatureCreator& creator, const CScript& scriptPubKey,
# 257 : : std::vector<valtype>& ret, TxoutType& whichTypeRet, SigVersion sigversion, SignatureData& sigdata)
# 258 : 1441138 : {
# 259 : 1441138 : CScript scriptRet;
# 260 : 1441138 : uint160 h160;
# 261 : 1441138 : ret.clear();
# 262 : 1441138 : std::vector<unsigned char> sig;
# 263 : :
# 264 : 1441138 : std::vector<valtype> vSolutions;
# 265 : 1441138 : whichTypeRet = Solver(scriptPubKey, vSolutions);
# 266 : :
# 267 [ - + ]: 1441138 : switch (whichTypeRet) {
# 268 [ + + ]: 1733 : case TxoutType::NONSTANDARD:
# 269 [ + + ]: 49121 : case TxoutType::NULL_DATA:
# 270 [ + + ]: 49247 : case TxoutType::WITNESS_UNKNOWN:
# 271 : 49247 : return false;
# 272 [ + + ]: 4603 : case TxoutType::PUBKEY:
# 273 [ + + ]: 4603 : if (!CreateSig(creator, sigdata, provider, sig, CPubKey(vSolutions[0]), scriptPubKey, sigversion)) return false;
# 274 : 4507 : ret.push_back(std::move(sig));
# 275 : 4507 : return true;
# 276 [ + + ]: 838829 : case TxoutType::PUBKEYHASH: {
# 277 : 838829 : CKeyID keyID = CKeyID(uint160(vSolutions[0]));
# 278 : 838829 : CPubKey pubkey;
# 279 [ + + ]: 838829 : if (!GetPubKey(provider, sigdata, keyID, pubkey)) {
# 280 : : // Pubkey could not be found, add to missing
# 281 : 75737 : sigdata.missing_pubkeys.push_back(keyID);
# 282 : 75737 : return false;
# 283 : 75737 : }
# 284 [ + + ]: 763092 : if (!CreateSig(creator, sigdata, provider, sig, pubkey, scriptPubKey, sigversion)) return false;
# 285 : 760872 : ret.push_back(std::move(sig));
# 286 : 760872 : ret.push_back(ToByteVector(pubkey));
# 287 : 760872 : return true;
# 288 : 763092 : }
# 289 [ + + ]: 12474 : case TxoutType::SCRIPTHASH:
# 290 : 12474 : h160 = uint160(vSolutions[0]);
# 291 [ + + ]: 12474 : if (GetCScript(provider, sigdata, CScriptID{h160}, scriptRet)) {
# 292 : 10513 : ret.push_back(std::vector<unsigned char>(scriptRet.begin(), scriptRet.end()));
# 293 : 10513 : return true;
# 294 : 10513 : }
# 295 : : // Could not find redeemScript, add to missing
# 296 : 1961 : sigdata.missing_redeem_script = h160;
# 297 : 1961 : return false;
# 298 : :
# 299 [ + + ]: 7106 : case TxoutType::MULTISIG: {
# 300 : 7106 : size_t required = vSolutions.front()[0];
# 301 : 7106 : ret.push_back(valtype()); // workaround CHECKMULTISIG bug
# 302 [ + + ]: 23219 : for (size_t i = 1; i < vSolutions.size() - 1; ++i) {
# 303 : 16113 : CPubKey pubkey = CPubKey(vSolutions[i]);
# 304 : : // We need to always call CreateSig in order to fill sigdata with all
# 305 : : // possible signatures that we can create. This will allow further PSBT
# 306 : : // processing to work as it needs all possible signature and pubkey pairs
# 307 [ + + ]: 16113 : if (CreateSig(creator, sigdata, provider, sig, pubkey, scriptPubKey, sigversion)) {
# 308 [ + + ]: 13785 : if (ret.size() < required + 1) {
# 309 : 10687 : ret.push_back(std::move(sig));
# 310 : 10687 : }
# 311 : 13785 : }
# 312 : 16113 : }
# 313 : 7106 : bool ok = ret.size() == required + 1;
# 314 [ + + ]: 8202 : for (size_t i = 0; i + ret.size() < required + 1; ++i) {
# 315 : 1096 : ret.push_back(valtype());
# 316 : 1096 : }
# 317 : 7106 : return ok;
# 318 : 12474 : }
# 319 [ + + ]: 511615 : case TxoutType::WITNESS_V0_KEYHASH:
# 320 : 511615 : ret.push_back(vSolutions[0]);
# 321 : 511615 : return true;
# 322 : :
# 323 [ + + ]: 8992 : case TxoutType::WITNESS_V0_SCRIPTHASH:
# 324 : 8992 : CRIPEMD160().Write(vSolutions[0].data(), vSolutions[0].size()).Finalize(h160.begin());
# 325 [ + + ]: 8992 : if (GetCScript(provider, sigdata, CScriptID{h160}, scriptRet)) {
# 326 : 6479 : ret.push_back(std::vector<unsigned char>(scriptRet.begin(), scriptRet.end()));
# 327 : 6479 : return true;
# 328 : 6479 : }
# 329 : : // Could not find witnessScript, add to missing
# 330 : 2513 : sigdata.missing_witness_script = uint256(vSolutions[0]);
# 331 : 2513 : return false;
# 332 : :
# 333 [ + + ]: 8272 : case TxoutType::WITNESS_V1_TAPROOT:
# 334 : 8272 : return SignTaproot(provider, creator, WitnessV1Taproot(XOnlyPubKey{vSolutions[0]}), sigdata, ret);
# 335 : 1441138 : } // no default case, so the compiler can warn about missing cases
# 336 : 0 : assert(false);
# 337 : 0 : }
# 338 : :
# 339 : : static CScript PushAll(const std::vector<valtype>& values)
# 340 : 912531 : {
# 341 : 912531 : CScript result;
# 342 [ + + ]: 912531 : for (const valtype& v : values) {
# 343 [ + + ]: 601538 : if (v.size() == 0) {
# 344 : 1694 : result << OP_0;
# 345 [ - + ][ # # ]: 599844 : } else if (v.size() == 1 && v[0] >= 1 && v[0] <= 16) {
# [ # # ]
# 346 : 0 : result << CScript::EncodeOP_N(v[0]);
# 347 [ - + ][ # # ]: 599844 : } else if (v.size() == 1 && v[0] == 0x81) {
# 348 : 0 : result << OP_1NEGATE;
# 349 : 599844 : } else {
# 350 : 599844 : result << v;
# 351 : 599844 : }
# 352 : 601538 : }
# 353 : 912531 : return result;
# 354 : 912531 : }
# 355 : :
# 356 : : bool ProduceSignature(const SigningProvider& provider, const BaseSignatureCreator& creator, const CScript& fromPubKey, SignatureData& sigdata)
# 357 : 916339 : {
# 358 [ + + ]: 916339 : if (sigdata.complete) return true;
# 359 : :
# 360 : 912531 : std::vector<valtype> result;
# 361 : 912531 : TxoutType whichType;
# 362 : 912531 : bool solved = SignStep(provider, creator, fromPubKey, result, whichType, SigVersion::BASE, sigdata);
# 363 : 912531 : bool P2SH = false;
# 364 : 912531 : CScript subscript;
# 365 : :
# 366 [ + + ][ + + ]: 912531 : if (solved && whichType == TxoutType::SCRIPTHASH)
# 367 : 10513 : {
# 368 : : // Solver returns the subscript that needs to be evaluated;
# 369 : : // the final scriptSig is the signatures from that
# 370 : : // and then the serialized subscript:
# 371 : 10513 : subscript = CScript(result[0].begin(), result[0].end());
# 372 : 10513 : sigdata.redeem_script = subscript;
# 373 [ + - ][ + + ]: 10513 : solved = solved && SignStep(provider, creator, subscript, result, whichType, SigVersion::BASE, sigdata) && whichType != TxoutType::SCRIPTHASH;
# [ + - ]
# 374 : 10513 : P2SH = true;
# 375 : 10513 : }
# 376 : :
# 377 [ + + ][ + + ]: 912531 : if (solved && whichType == TxoutType::WITNESS_V0_KEYHASH)
# 378 : 511615 : {
# 379 : 511615 : CScript witnessscript;
# 380 : 511615 : witnessscript << OP_DUP << OP_HASH160 << ToByteVector(result[0]) << OP_EQUALVERIFY << OP_CHECKSIG;
# 381 : 511615 : TxoutType subType;
# 382 [ + - ][ + + ]: 511615 : solved = solved && SignStep(provider, creator, witnessscript, result, subType, SigVersion::WITNESS_V0, sigdata);
# 383 : 511615 : sigdata.scriptWitness.stack = result;
# 384 : 511615 : sigdata.witness = true;
# 385 : 511615 : result.clear();
# 386 : 511615 : }
# 387 [ + + ][ + + ]: 400916 : else if (solved && whichType == TxoutType::WITNESS_V0_SCRIPTHASH)
# 388 : 6479 : {
# 389 : 6479 : CScript witnessscript(result[0].begin(), result[0].end());
# 390 : 6479 : sigdata.witness_script = witnessscript;
# 391 : 6479 : TxoutType subType;
# 392 [ + - ][ + + ]: 6479 : solved = solved && SignStep(provider, creator, witnessscript, result, subType, SigVersion::WITNESS_V0, sigdata) && subType != TxoutType::SCRIPTHASH && subType != TxoutType::WITNESS_V0_SCRIPTHASH && subType != TxoutType::WITNESS_V0_KEYHASH;
# [ + - ][ + - ]
# [ + - ]
# 393 : 6479 : result.push_back(std::vector<unsigned char>(witnessscript.begin(), witnessscript.end()));
# 394 : 6479 : sigdata.scriptWitness.stack = result;
# 395 : 6479 : sigdata.witness = true;
# 396 : 6479 : result.clear();
# 397 [ + + ][ + - ]: 394437 : } else if (whichType == TxoutType::WITNESS_V1_TAPROOT && !P2SH) {
# 398 : 8272 : sigdata.witness = true;
# 399 [ + + ]: 8272 : if (solved) {
# 400 : 4852 : sigdata.scriptWitness.stack = std::move(result);
# 401 : 4852 : }
# 402 : 8272 : result.clear();
# 403 [ + + ][ - + ]: 386165 : } else if (solved && whichType == TxoutType::WITNESS_UNKNOWN) {
# 404 : 0 : sigdata.witness = true;
# 405 : 0 : }
# 406 : :
# 407 [ + + ]: 912531 : if (!sigdata.witness) sigdata.scriptWitness.stack.clear();
# 408 [ + + ]: 912531 : if (P2SH) {
# 409 : 10513 : result.push_back(std::vector<unsigned char>(subscript.begin(), subscript.end()));
# 410 : 10513 : }
# 411 : 912531 : sigdata.scriptSig = PushAll(result);
# 412 : :
# 413 : : // Test solution
# 414 [ + + ][ + + ]: 912531 : sigdata.complete = solved && VerifyScript(sigdata.scriptSig, fromPubKey, &sigdata.scriptWitness, STANDARD_SCRIPT_VERIFY_FLAGS, creator.Checker());
# 415 : 912531 : return sigdata.complete;
# 416 : 916339 : }
# 417 : :
# 418 : : namespace {
# 419 : : class SignatureExtractorChecker final : public DeferringSignatureChecker
# 420 : : {
# 421 : : private:
# 422 : : SignatureData& sigdata;
# 423 : :
# 424 : : public:
# 425 : 76549 : SignatureExtractorChecker(SignatureData& sigdata, BaseSignatureChecker& checker) : DeferringSignatureChecker(checker), sigdata(sigdata) {}
# 426 : :
# 427 : : bool CheckECDSASignature(const std::vector<unsigned char>& scriptSig, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode, SigVersion sigversion) const override
# 428 : 4226 : {
# 429 [ + + ]: 4226 : if (m_checker.CheckECDSASignature(scriptSig, vchPubKey, scriptCode, sigversion)) {
# 430 : 3812 : CPubKey pubkey(vchPubKey);
# 431 : 3812 : sigdata.signatures.emplace(pubkey.GetID(), SigPair(pubkey, scriptSig));
# 432 : 3812 : return true;
# 433 : 3812 : }
# 434 : 414 : return false;
# 435 : 4226 : }
# 436 : : };
# 437 : :
# 438 : : struct Stacks
# 439 : : {
# 440 : : std::vector<valtype> script;
# 441 : : std::vector<valtype> witness;
# 442 : :
# 443 : : Stacks() = delete;
# 444 : : Stacks(const Stacks&) = delete;
# 445 : 76549 : explicit Stacks(const SignatureData& data) : witness(data.scriptWitness.stack) {
# 446 : 76549 : EvalScript(script, data.scriptSig, SCRIPT_VERIFY_STRICTENC, BaseSignatureChecker(), SigVersion::BASE);
# 447 : 76549 : }
# 448 : : };
# 449 : : }
# 450 : :
# 451 : : // Extracts signatures and scripts from incomplete scriptSigs. Please do not extend this, use PSBT instead
# 452 : : SignatureData DataFromTransaction(const CMutableTransaction& tx, unsigned int nIn, const CTxOut& txout)
# 453 : 76549 : {
# 454 : 76549 : SignatureData data;
# 455 : 76549 : assert(tx.vin.size() > nIn);
# 456 : 0 : data.scriptSig = tx.vin[nIn].scriptSig;
# 457 : 76549 : data.scriptWitness = tx.vin[nIn].scriptWitness;
# 458 : 76549 : Stacks stack(data);
# 459 : :
# 460 : : // Get signatures
# 461 : 76549 : MutableTransactionSignatureChecker tx_checker(&tx, nIn, txout.nValue, MissingDataBehavior::FAIL);
# 462 : 76549 : SignatureExtractorChecker extractor_checker(data, tx_checker);
# 463 [ + + ]: 76549 : if (VerifyScript(data.scriptSig, txout.scriptPubKey, &data.scriptWitness, STANDARD_SCRIPT_VERIFY_FLAGS, extractor_checker)) {
# 464 : 3802 : data.complete = true;
# 465 : 3802 : return data;
# 466 : 3802 : }
# 467 : :
# 468 : : // Get scripts
# 469 : 72747 : std::vector<std::vector<unsigned char>> solutions;
# 470 : 72747 : TxoutType script_type = Solver(txout.scriptPubKey, solutions);
# 471 : 72747 : SigVersion sigversion = SigVersion::BASE;
# 472 : 72747 : CScript next_script = txout.scriptPubKey;
# 473 : :
# 474 [ + + ][ + + ]: 72747 : if (script_type == TxoutType::SCRIPTHASH && !stack.script.empty() && !stack.script.back().empty()) {
# [ + - ]
# 475 : : // Get the redeemScript
# 476 : 16 : CScript redeem_script(stack.script.back().begin(), stack.script.back().end());
# 477 : 16 : data.redeem_script = redeem_script;
# 478 : 16 : next_script = std::move(redeem_script);
# 479 : :
# 480 : : // Get redeemScript type
# 481 : 16 : script_type = Solver(next_script, solutions);
# 482 : 16 : stack.script.pop_back();
# 483 : 16 : }
# 484 [ + + ][ + + ]: 72747 : if (script_type == TxoutType::WITNESS_V0_SCRIPTHASH && !stack.witness.empty() && !stack.witness.back().empty()) {
# [ + - ]
# 485 : : // Get the witnessScript
# 486 : 21 : CScript witness_script(stack.witness.back().begin(), stack.witness.back().end());
# 487 : 21 : data.witness_script = witness_script;
# 488 : 21 : next_script = std::move(witness_script);
# 489 : :
# 490 : : // Get witnessScript type
# 491 : 21 : script_type = Solver(next_script, solutions);
# 492 : 21 : stack.witness.pop_back();
# 493 : 21 : stack.script = std::move(stack.witness);
# 494 : 21 : stack.witness.clear();
# 495 : 21 : sigversion = SigVersion::WITNESS_V0;
# 496 : 21 : }
# 497 [ + + ][ + + ]: 72747 : if (script_type == TxoutType::MULTISIG && !stack.script.empty()) {
# 498 : : // Build a map of pubkey -> signature by matching sigs to pubkeys:
# 499 : 33 : assert(solutions.size() > 1);
# 500 : 0 : unsigned int num_pubkeys = solutions.size()-2;
# 501 : 33 : unsigned int last_success_key = 0;
# 502 [ + + ]: 105 : for (const valtype& sig : stack.script) {
# 503 [ + + ]: 252 : for (unsigned int i = last_success_key; i < num_pubkeys; ++i) {
# 504 : 186 : const valtype& pubkey = solutions[i+1];
# 505 : : // We either have a signature for this pubkey, or we have found a signature and it is valid
# 506 [ - + ][ + + ]: 186 : if (data.signatures.count(CPubKey(pubkey).GetID()) || extractor_checker.CheckECDSASignature(sig, pubkey, next_script, sigversion)) {
# [ + + ]
# 507 : 39 : last_success_key = i + 1;
# 508 : 39 : break;
# 509 : 39 : }
# 510 : 186 : }
# 511 : 105 : }
# 512 : 33 : }
# 513 : :
# 514 : 0 : return data;
# 515 : 76549 : }
# 516 : :
# 517 : : void UpdateInput(CTxIn& input, const SignatureData& data)
# 518 : 458573 : {
# 519 : 458573 : input.scriptSig = data.scriptSig;
# 520 : 458573 : input.scriptWitness = data.scriptWitness;
# 521 : 458573 : }
# 522 : :
# 523 : : void SignatureData::MergeSignatureData(SignatureData sigdata)
# 524 : 78 : {
# 525 [ + + ]: 78 : if (complete) return;
# 526 [ + + ]: 68 : if (sigdata.complete) {
# 527 : 16 : *this = std::move(sigdata);
# 528 : 16 : return;
# 529 : 16 : }
# 530 [ + + ][ - + ]: 52 : if (redeem_script.empty() && !sigdata.redeem_script.empty()) {
# 531 : 0 : redeem_script = sigdata.redeem_script;
# 532 : 0 : }
# 533 [ + + ][ + + ]: 52 : if (witness_script.empty() && !sigdata.witness_script.empty()) {
# 534 : 1 : witness_script = sigdata.witness_script;
# 535 : 1 : }
# 536 : 52 : signatures.insert(std::make_move_iterator(sigdata.signatures.begin()), std::make_move_iterator(sigdata.signatures.end()));
# 537 : 52 : }
# 538 : :
# 539 : : bool SignSignature(const SigningProvider &provider, const CScript& fromPubKey, CMutableTransaction& txTo, unsigned int nIn, const CAmount& amount, int nHashType)
# 540 : 9214 : {
# 541 : 9214 : assert(nIn < txTo.vin.size());
# 542 : :
# 543 : 0 : MutableTransactionSignatureCreator creator(&txTo, nIn, amount, nHashType);
# 544 : :
# 545 : 9214 : SignatureData sigdata;
# 546 : 9214 : bool ret = ProduceSignature(provider, creator, fromPubKey, sigdata);
# 547 : 9214 : UpdateInput(txTo.vin.at(nIn), sigdata);
# 548 : 9214 : return ret;
# 549 : 9214 : }
# 550 : :
# 551 : : bool SignSignature(const SigningProvider &provider, const CTransaction& txFrom, CMutableTransaction& txTo, unsigned int nIn, int nHashType)
# 552 : 214 : {
# 553 : 214 : assert(nIn < txTo.vin.size());
# 554 : 0 : const CTxIn& txin = txTo.vin[nIn];
# 555 : 214 : assert(txin.prevout.n < txFrom.vout.size());
# 556 : 0 : const CTxOut& txout = txFrom.vout[txin.prevout.n];
# 557 : :
# 558 : 214 : return SignSignature(provider, txout.scriptPubKey, txTo, nIn, txout.nValue, nHashType);
# 559 : 214 : }
# 560 : :
# 561 : : namespace {
# 562 : : /** Dummy signature checker which accepts all signatures. */
# 563 : : class DummySignatureChecker final : public BaseSignatureChecker
# 564 : : {
# 565 : : public:
# 566 : 893 : DummySignatureChecker() {}
# 567 : 1106226 : bool CheckECDSASignature(const std::vector<unsigned char>& scriptSig, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode, SigVersion sigversion) const override { return true; }
# 568 : 5468 : bool CheckSchnorrSignature(Span<const unsigned char> sig, Span<const unsigned char> pubkey, SigVersion sigversion, ScriptExecutionData& execdata, ScriptError* serror) const override { return true; }
# 569 : : };
# 570 : : const DummySignatureChecker DUMMY_CHECKER;
# 571 : :
# 572 : : class DummySignatureCreator final : public BaseSignatureCreator {
# 573 : : private:
# 574 : : char m_r_len = 32;
# 575 : : char m_s_len = 32;
# 576 : : public:
# 577 : 1786 : DummySignatureCreator(char r_len, char s_len) : m_r_len(r_len), m_s_len(s_len) {}
# 578 : 740625 : const BaseSignatureChecker& Checker() const override { return DUMMY_CHECKER; }
# 579 : : bool CreateSig(const SigningProvider& provider, std::vector<unsigned char>& vchSig, const CKeyID& keyid, const CScript& scriptCode, SigVersion sigversion) const override
# 580 : 742036 : {
# 581 : : // Create a dummy signature that is a valid DER-encoding
# 582 : 742036 : vchSig.assign(m_r_len + m_s_len + 7, '\000');
# 583 : 742036 : vchSig[0] = 0x30;
# 584 : 742036 : vchSig[1] = m_r_len + m_s_len + 4;
# 585 : 742036 : vchSig[2] = 0x02;
# 586 : 742036 : vchSig[3] = m_r_len;
# 587 : 742036 : vchSig[4] = 0x01;
# 588 : 742036 : vchSig[4 + m_r_len] = 0x02;
# 589 : 742036 : vchSig[5 + m_r_len] = m_s_len;
# 590 : 742036 : vchSig[6 + m_r_len] = 0x01;
# 591 : 742036 : vchSig[6 + m_r_len + m_s_len] = SIGHASH_ALL;
# 592 : 742036 : return true;
# 593 : 742036 : }
# 594 : : bool CreateSchnorrSig(const SigningProvider& provider, std::vector<unsigned char>& sig, const XOnlyPubKey& pubkey, const uint256* leaf_hash, const uint256* tweak, SigVersion sigversion) const override
# 595 : 4601 : {
# 596 : 4601 : sig.assign(64, '\000');
# 597 : 4601 : return true;
# 598 : 4601 : }
# 599 : : };
# 600 : :
# 601 : : }
# 602 : :
# 603 : : const BaseSignatureCreator& DUMMY_SIGNATURE_CREATOR = DummySignatureCreator(32, 32);
# 604 : : const BaseSignatureCreator& DUMMY_MAXIMUM_SIGNATURE_CREATOR = DummySignatureCreator(33, 32);
# 605 : :
# 606 : : bool IsSolvable(const SigningProvider& provider, const CScript& script)
# 607 : 365204 : {
# 608 : : // This check is to make sure that the script we created can actually be solved for and signed by us
# 609 : : // if we were to have the private keys. This is just to make sure that the script is valid and that,
# 610 : : // if found in a transaction, we would still accept and relay that transaction. In particular,
# 611 : : // it will reject witness outputs that require signing with an uncompressed public key.
# 612 : 365204 : SignatureData sigs;
# 613 : : // Make sure that STANDARD_SCRIPT_VERIFY_FLAGS includes SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, the most
# 614 : : // important property this function is designed to test for.
# 615 : 365204 : static_assert(STANDARD_SCRIPT_VERIFY_FLAGS & SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, "IsSolvable requires standard script flags to include WITNESS_PUBKEYTYPE");
# 616 [ + + ]: 365204 : if (ProduceSignature(provider, DUMMY_SIGNATURE_CREATOR, script, sigs)) {
# 617 : : // VerifyScript check is just defensive, and should never fail.
# 618 : 365016 : bool verified = VerifyScript(sigs.scriptSig, script, &sigs.scriptWitness, STANDARD_SCRIPT_VERIFY_FLAGS, DUMMY_CHECKER);
# 619 : 365016 : assert(verified);
# 620 : 0 : return true;
# 621 : 365016 : }
# 622 : 188 : return false;
# 623 : 365204 : }
# 624 : :
# 625 : : bool IsSegWitOutput(const SigningProvider& provider, const CScript& script)
# 626 : 12 : {
# 627 : 12 : int version;
# 628 : 12 : valtype program;
# 629 [ + + ]: 12 : if (script.IsWitnessProgram(version, program)) return true;
# 630 [ + + ]: 8 : if (script.IsPayToScriptHash()) {
# 631 : 4 : std::vector<valtype> solutions;
# 632 : 4 : auto whichtype = Solver(script, solutions);
# 633 [ + - ]: 4 : if (whichtype == TxoutType::SCRIPTHASH) {
# 634 : 4 : auto h160 = uint160(solutions[0]);
# 635 : 4 : CScript subscript;
# 636 [ + + ]: 4 : if (provider.GetCScript(CScriptID{h160}, subscript)) {
# 637 [ + - ]: 2 : if (subscript.IsWitnessProgram(version, program)) return true;
# 638 : 2 : }
# 639 : 4 : }
# 640 : 4 : }
# 641 : 6 : return false;
# 642 : 8 : }
# 643 : :
# 644 : : bool SignTransaction(CMutableTransaction& mtx, const SigningProvider* keystore, const std::map<COutPoint, Coin>& coins, int nHashType, std::map<int, bilingual_str>& input_errors)
# 645 : 18178 : {
# 646 : 18178 : bool fHashSingle = ((nHashType & ~SIGHASH_ANYONECANPAY) == SIGHASH_SINGLE);
# 647 : :
# 648 : : // Use CTransaction for the constant parts of the
# 649 : : // transaction to avoid rehashing.
# 650 : 18178 : const CTransaction txConst(mtx);
# 651 : :
# 652 : 18178 : PrecomputedTransactionData txdata;
# 653 : 18178 : std::vector<CTxOut> spent_outputs;
# 654 [ + + ]: 94699 : for (unsigned int i = 0; i < mtx.vin.size(); ++i) {
# 655 : 76543 : CTxIn& txin = mtx.vin[i];
# 656 : 76543 : auto coin = coins.find(txin.prevout);
# 657 [ + + ][ - + ]: 76543 : if (coin == coins.end() || coin->second.IsSpent()) {
# [ + + ]
# 658 : 22 : txdata.Init(txConst, /*spent_outputs=*/{}, /*force=*/true);
# 659 : 22 : break;
# 660 : 76521 : } else {
# 661 : 76521 : spent_outputs.emplace_back(coin->second.out.nValue, coin->second.out.scriptPubKey);
# 662 : 76521 : }
# 663 : 76543 : }
# 664 [ + + ]: 18178 : if (spent_outputs.size() == mtx.vin.size()) {
# 665 : 18156 : txdata.Init(txConst, std::move(spent_outputs), true);
# 666 : 18156 : }
# 667 : :
# 668 : : // Sign what we can:
# 669 [ + + ]: 94731 : for (unsigned int i = 0; i < mtx.vin.size(); ++i) {
# 670 : 76553 : CTxIn& txin = mtx.vin[i];
# 671 : 76553 : auto coin = coins.find(txin.prevout);
# 672 [ - + ][ + + ]: 76553 : if (coin == coins.end() || coin->second.IsSpent()) {
# [ + + ]
# 673 : 32 : input_errors[i] = _("Input not found or already spent");
# 674 : 32 : continue;
# 675 : 32 : }
# 676 : 76521 : const CScript& prevPubKey = coin->second.out.scriptPubKey;
# 677 : 76521 : const CAmount& amount = coin->second.out.nValue;
# 678 : :
# 679 : 76521 : SignatureData sigdata = DataFromTransaction(mtx, i, coin->second.out);
# 680 : : // Only sign SIGHASH_SINGLE if there's a corresponding output:
# 681 [ + - ][ # # ]: 76521 : if (!fHashSingle || (i < mtx.vout.size())) {
# 682 : 76521 : ProduceSignature(*keystore, MutableTransactionSignatureCreator(&mtx, i, amount, &txdata, nHashType), prevPubKey, sigdata);
# 683 : 76521 : }
# 684 : :
# 685 : 76521 : UpdateInput(txin, sigdata);
# 686 : :
# 687 : : // amount must be specified for valid segwit signature
# 688 [ + + ][ + + ]: 76521 : if (amount == MAX_MONEY && !txin.scriptWitness.IsNull()) {
# 689 : 17 : input_errors[i] = _("Missing amount");
# 690 : 17 : continue;
# 691 : 17 : }
# 692 : :
# 693 : 76504 : ScriptError serror = SCRIPT_ERR_OK;
# 694 [ + + ]: 76504 : if (!VerifyScript(txin.scriptSig, prevPubKey, &txin.scriptWitness, STANDARD_SCRIPT_VERIFY_FLAGS, TransactionSignatureChecker(&txConst, i, amount, txdata, MissingDataBehavior::FAIL), &serror)) {
# 695 [ + + ]: 46935 : if (serror == SCRIPT_ERR_INVALID_STACK_OPERATION) {
# 696 : : // Unable to sign input and verification failed (possible attempt to partially sign).
# 697 : 34245 : input_errors[i] = Untranslated("Unable to sign input, invalid stack size (possibly missing key)");
# 698 [ + + ]: 34245 : } else if (serror == SCRIPT_ERR_SIG_NULLFAIL) {
# 699 : : // Verification failed (possibly due to insufficient signatures).
# 700 : 41 : input_errors[i] = Untranslated("CHECK(MULTI)SIG failing with non-zero signature (possibly need more signatures)");
# 701 : 12649 : } else {
# 702 : 12649 : input_errors[i] = Untranslated(ScriptErrorString(serror));
# 703 : 12649 : }
# 704 : 46935 : } else {
# 705 : : // If this input succeeds, make sure there is no error set for it
# 706 : 29569 : input_errors.erase(i);
# 707 : 29569 : }
# 708 : 76504 : }
# 709 : 18178 : return input_errors.empty();
# 710 : 18178 : }
|