Branch data Line data Source code
# 1 : : // Copyright (c) 2011-2021 The Bitcoin Core developers
# 2 : : // Distributed under the MIT software license, see the accompanying
# 3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
# 4 : :
# 5 : : #include <test/data/script_tests.json.h>
# 6 : : #include <test/data/bip341_wallet_vectors.json.h>
# 7 : :
# 8 : : #include <core_io.h>
# 9 : : #include <fs.h>
# 10 : : #include <key.h>
# 11 : : #include <rpc/util.h>
# 12 : : #include <script/script.h>
# 13 : : #include <script/script_error.h>
# 14 : : #include <script/sigcache.h>
# 15 : : #include <script/sign.h>
# 16 : : #include <script/signingprovider.h>
# 17 : : #include <streams.h>
# 18 : : #include <test/util/setup_common.h>
# 19 : : #include <test/util/transaction_utils.h>
# 20 : : #include <util/strencodings.h>
# 21 : : #include <util/system.h>
# 22 : :
# 23 : : #if defined(HAVE_CONSENSUS_LIB)
# 24 : : #include <script/bitcoinconsensus.h>
# 25 : : #endif
# 26 : :
# 27 : : #include <cstdint>
# 28 : : #include <fstream>
# 29 : : #include <string>
# 30 : : #include <vector>
# 31 : :
# 32 : : #include <boost/test/unit_test.hpp>
# 33 : :
# 34 : : #include <univalue.h>
# 35 : :
# 36 : : // Uncomment if you want to output updated JSON tests.
# 37 : : // #define UPDATE_JSON_TESTS
# 38 : :
# 39 : : static const unsigned int gFlags = SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_STRICTENC;
# 40 : :
# 41 : : unsigned int ParseScriptFlags(std::string strFlags);
# 42 : : std::string FormatScriptFlags(unsigned int flags);
# 43 : :
# 44 : : UniValue read_json(const std::string& jsondata)
# 45 : 20 : {
# 46 : 20 : UniValue v;
# 47 : :
# 48 [ - + ][ - + ]: 20 : if (!v.read(jsondata) || !v.isArray())
# 49 : 0 : {
# 50 : 0 : BOOST_ERROR("Parse error.");
# 51 : 0 : return UniValue(UniValue::VARR);
# 52 : 0 : }
# 53 : 20 : return v.get_array();
# 54 : 20 : }
# 55 : :
# 56 : : struct ScriptErrorDesc
# 57 : : {
# 58 : : ScriptError_t err;
# 59 : : const char *name;
# 60 : : };
# 61 : :
# 62 : : static ScriptErrorDesc script_errors[]={
# 63 : : {SCRIPT_ERR_OK, "OK"},
# 64 : : {SCRIPT_ERR_UNKNOWN_ERROR, "UNKNOWN_ERROR"},
# 65 : : {SCRIPT_ERR_EVAL_FALSE, "EVAL_FALSE"},
# 66 : : {SCRIPT_ERR_OP_RETURN, "OP_RETURN"},
# 67 : : {SCRIPT_ERR_SCRIPT_SIZE, "SCRIPT_SIZE"},
# 68 : : {SCRIPT_ERR_PUSH_SIZE, "PUSH_SIZE"},
# 69 : : {SCRIPT_ERR_OP_COUNT, "OP_COUNT"},
# 70 : : {SCRIPT_ERR_STACK_SIZE, "STACK_SIZE"},
# 71 : : {SCRIPT_ERR_SIG_COUNT, "SIG_COUNT"},
# 72 : : {SCRIPT_ERR_PUBKEY_COUNT, "PUBKEY_COUNT"},
# 73 : : {SCRIPT_ERR_VERIFY, "VERIFY"},
# 74 : : {SCRIPT_ERR_EQUALVERIFY, "EQUALVERIFY"},
# 75 : : {SCRIPT_ERR_CHECKMULTISIGVERIFY, "CHECKMULTISIGVERIFY"},
# 76 : : {SCRIPT_ERR_CHECKSIGVERIFY, "CHECKSIGVERIFY"},
# 77 : : {SCRIPT_ERR_NUMEQUALVERIFY, "NUMEQUALVERIFY"},
# 78 : : {SCRIPT_ERR_BAD_OPCODE, "BAD_OPCODE"},
# 79 : : {SCRIPT_ERR_DISABLED_OPCODE, "DISABLED_OPCODE"},
# 80 : : {SCRIPT_ERR_INVALID_STACK_OPERATION, "INVALID_STACK_OPERATION"},
# 81 : : {SCRIPT_ERR_INVALID_ALTSTACK_OPERATION, "INVALID_ALTSTACK_OPERATION"},
# 82 : : {SCRIPT_ERR_UNBALANCED_CONDITIONAL, "UNBALANCED_CONDITIONAL"},
# 83 : : {SCRIPT_ERR_NEGATIVE_LOCKTIME, "NEGATIVE_LOCKTIME"},
# 84 : : {SCRIPT_ERR_UNSATISFIED_LOCKTIME, "UNSATISFIED_LOCKTIME"},
# 85 : : {SCRIPT_ERR_SIG_HASHTYPE, "SIG_HASHTYPE"},
# 86 : : {SCRIPT_ERR_SIG_DER, "SIG_DER"},
# 87 : : {SCRIPT_ERR_MINIMALDATA, "MINIMALDATA"},
# 88 : : {SCRIPT_ERR_SIG_PUSHONLY, "SIG_PUSHONLY"},
# 89 : : {SCRIPT_ERR_SIG_HIGH_S, "SIG_HIGH_S"},
# 90 : : {SCRIPT_ERR_SIG_NULLDUMMY, "SIG_NULLDUMMY"},
# 91 : : {SCRIPT_ERR_PUBKEYTYPE, "PUBKEYTYPE"},
# 92 : : {SCRIPT_ERR_CLEANSTACK, "CLEANSTACK"},
# 93 : : {SCRIPT_ERR_MINIMALIF, "MINIMALIF"},
# 94 : : {SCRIPT_ERR_SIG_NULLFAIL, "NULLFAIL"},
# 95 : : {SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS, "DISCOURAGE_UPGRADABLE_NOPS"},
# 96 : : {SCRIPT_ERR_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM, "DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM"},
# 97 : : {SCRIPT_ERR_WITNESS_PROGRAM_WRONG_LENGTH, "WITNESS_PROGRAM_WRONG_LENGTH"},
# 98 : : {SCRIPT_ERR_WITNESS_PROGRAM_WITNESS_EMPTY, "WITNESS_PROGRAM_WITNESS_EMPTY"},
# 99 : : {SCRIPT_ERR_WITNESS_PROGRAM_MISMATCH, "WITNESS_PROGRAM_MISMATCH"},
# 100 : : {SCRIPT_ERR_WITNESS_MALLEATED, "WITNESS_MALLEATED"},
# 101 : : {SCRIPT_ERR_WITNESS_MALLEATED_P2SH, "WITNESS_MALLEATED_P2SH"},
# 102 : : {SCRIPT_ERR_WITNESS_UNEXPECTED, "WITNESS_UNEXPECTED"},
# 103 : : {SCRIPT_ERR_WITNESS_PUBKEYTYPE, "WITNESS_PUBKEYTYPE"},
# 104 : : {SCRIPT_ERR_OP_CODESEPARATOR, "OP_CODESEPARATOR"},
# 105 : : {SCRIPT_ERR_SIG_FINDANDDELETE, "SIG_FINDANDDELETE"},
# 106 : : };
# 107 : :
# 108 : : static std::string FormatScriptError(ScriptError_t err)
# 109 : 5632 : {
# 110 [ + - ]: 5632 : for (const auto& se : script_errors)
# 111 [ + + ]: 46090 : if (se.err == err)
# 112 : 5632 : return se.name;
# 113 : 0 : BOOST_ERROR("Unknown scripterror enumeration value, update script_errors in script_tests.cpp.");
# 114 : 0 : return "";
# 115 : 5632 : }
# 116 : :
# 117 : : static ScriptError_t ParseScriptError(const std::string& name)
# 118 : 2414 : {
# 119 [ + - ]: 2414 : for (const auto& se : script_errors)
# 120 [ + + ]: 18302 : if (se.name == name)
# 121 : 2414 : return se.err;
# 122 : 0 : BOOST_ERROR("Unknown scripterror \"" << name << "\" in test description");
# 123 : 0 : return SCRIPT_ERR_UNKNOWN_ERROR;
# 124 : 2414 : }
# 125 : :
# 126 : : BOOST_FIXTURE_TEST_SUITE(script_tests, BasicTestingSetup)
# 127 : :
# 128 : : void DoTest(const CScript& scriptPubKey, const CScript& scriptSig, const CScriptWitness& scriptWitness, uint32_t flags, const std::string& message, int scriptError, CAmount nValue = 0)
# 129 : 2682 : {
# 130 : 2682 : bool expect = (scriptError == SCRIPT_ERR_OK);
# 131 [ + + ]: 2682 : if (flags & SCRIPT_VERIFY_CLEANSTACK) {
# 132 : 12 : flags |= SCRIPT_VERIFY_P2SH;
# 133 : 12 : flags |= SCRIPT_VERIFY_WITNESS;
# 134 : 12 : }
# 135 : 2682 : ScriptError err;
# 136 : 2682 : const CTransaction txCredit{BuildCreditingTransaction(scriptPubKey, nValue)};
# 137 : 2682 : CMutableTransaction tx = BuildSpendingTransaction(scriptSig, scriptWitness, txCredit);
# 138 : 2682 : CMutableTransaction tx2 = tx;
# 139 : 2682 : BOOST_CHECK_MESSAGE(VerifyScript(scriptSig, scriptPubKey, &scriptWitness, flags, MutableTransactionSignatureChecker(&tx, 0, txCredit.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err) == expect, message);
# 140 : 2682 : BOOST_CHECK_MESSAGE(err == scriptError, FormatScriptError(err) + " where " + FormatScriptError((ScriptError_t)scriptError) + " expected: " + message);
# 141 : :
# 142 : : // Verify that removing flags from a passing test or adding flags to a failing test does not change the result.
# 143 [ + + ]: 45594 : for (int i = 0; i < 16; ++i) {
# 144 : 42912 : uint32_t extra_flags(InsecureRandBits(16));
# 145 [ + + ]: 42912 : uint32_t combined_flags{expect ? (flags & ~extra_flags) : (flags | extra_flags)};
# 146 : : // Weed out some invalid flag combinations.
# 147 [ + + ][ + + ]: 42912 : if (combined_flags & SCRIPT_VERIFY_CLEANSTACK && ~combined_flags & (SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS)) continue;
# 148 [ + + ][ + + ]: 38100 : if (combined_flags & SCRIPT_VERIFY_WITNESS && ~combined_flags & SCRIPT_VERIFY_P2SH) continue;
# 149 : 36838 : BOOST_CHECK_MESSAGE(VerifyScript(scriptSig, scriptPubKey, &scriptWitness, combined_flags, MutableTransactionSignatureChecker(&tx, 0, txCredit.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err) == expect, message + strprintf(" (with flags %x)", combined_flags));
# 150 : 36838 : }
# 151 : :
# 152 : 2682 : #if defined(HAVE_CONSENSUS_LIB)
# 153 : 2682 : CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
# 154 : 2682 : stream << tx2;
# 155 : 2682 : uint32_t libconsensus_flags{flags & bitcoinconsensus_SCRIPT_FLAGS_VERIFY_ALL};
# 156 [ + + ]: 2682 : if (libconsensus_flags == flags) {
# 157 [ + + ]: 658 : int expectedSuccessCode = expect ? 1 : 0;
# 158 [ + + ]: 658 : if (flags & bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS) {
# 159 : 168 : BOOST_CHECK_MESSAGE(bitcoinconsensus_verify_script_with_amount(scriptPubKey.data(), scriptPubKey.size(), txCredit.vout[0].nValue, UCharCast(stream.data()), stream.size(), 0, libconsensus_flags, nullptr) == expectedSuccessCode, message);
# 160 : 490 : } else {
# 161 : 490 : BOOST_CHECK_MESSAGE(bitcoinconsensus_verify_script_with_amount(scriptPubKey.data(), scriptPubKey.size(), 0, UCharCast(stream.data()), stream.size(), 0, libconsensus_flags, nullptr) == expectedSuccessCode, message);
# 162 : 490 : BOOST_CHECK_MESSAGE(bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), UCharCast(stream.data()), stream.size(), 0, libconsensus_flags, nullptr) == expectedSuccessCode, message);
# 163 : 490 : }
# 164 : 658 : }
# 165 : 2682 : #endif
# 166 : 2682 : }
# 167 : :
# 168 : 4 : void static NegateSignatureS(std::vector<unsigned char>& vchSig) {
# 169 : : // Parse the signature.
# 170 : 4 : std::vector<unsigned char> r, s;
# 171 : 4 : r = std::vector<unsigned char>(vchSig.begin() + 4, vchSig.begin() + 4 + vchSig[3]);
# 172 : 4 : s = std::vector<unsigned char>(vchSig.begin() + 6 + vchSig[3], vchSig.begin() + 6 + vchSig[3] + vchSig[5 + vchSig[3]]);
# 173 : :
# 174 : : // Really ugly to implement mod-n negation here, but it would be feature creep to expose such functionality from libsecp256k1.
# 175 : 4 : static const unsigned char order[33] = {
# 176 : 4 : 0x00,
# 177 : 4 : 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
# 178 : 4 : 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
# 179 : 4 : 0xBA, 0xAE, 0xDC, 0xE6, 0xAF, 0x48, 0xA0, 0x3B,
# 180 : 4 : 0xBF, 0xD2, 0x5E, 0x8C, 0xD0, 0x36, 0x41, 0x41
# 181 : 4 : };
# 182 [ + + ]: 8 : while (s.size() < 33) {
# 183 : 4 : s.insert(s.begin(), 0x00);
# 184 : 4 : }
# 185 : 4 : int carry = 0;
# 186 [ + + ]: 132 : for (int p = 32; p >= 1; p--) {
# 187 : 128 : int n = (int)order[p] - s[p] - carry;
# 188 : 128 : s[p] = (n + 256) & 0xFF;
# 189 : 128 : carry = (n < 0);
# 190 : 128 : }
# 191 : 4 : assert(carry == 0);
# 192 [ + - ][ + - ]: 4 : if (s.size() > 1 && s[0] == 0 && s[1] < 0x80) {
# [ - + ]
# 193 : 0 : s.erase(s.begin());
# 194 : 0 : }
# 195 : :
# 196 : : // Reconstruct the signature.
# 197 : 4 : vchSig.clear();
# 198 : 4 : vchSig.push_back(0x30);
# 199 : 4 : vchSig.push_back(4 + r.size() + s.size());
# 200 : 4 : vchSig.push_back(0x02);
# 201 : 4 : vchSig.push_back(r.size());
# 202 : 4 : vchSig.insert(vchSig.end(), r.begin(), r.end());
# 203 : 4 : vchSig.push_back(0x02);
# 204 : 4 : vchSig.push_back(s.size());
# 205 : 4 : vchSig.insert(vchSig.end(), s.begin(), s.end());
# 206 : 4 : }
# 207 : :
# 208 : : namespace
# 209 : : {
# 210 : : const unsigned char vchKey0[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
# 211 : : const unsigned char vchKey1[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0};
# 212 : : const unsigned char vchKey2[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0};
# 213 : :
# 214 : : struct KeyData
# 215 : : {
# 216 : : CKey key0, key0C, key1, key1C, key2, key2C;
# 217 : : CPubKey pubkey0, pubkey0C, pubkey0H;
# 218 : : CPubKey pubkey1, pubkey1C;
# 219 : : CPubKey pubkey2, pubkey2C;
# 220 : :
# 221 : : KeyData()
# 222 : 2 : {
# 223 : 2 : key0.Set(vchKey0, vchKey0 + 32, false);
# 224 : 2 : key0C.Set(vchKey0, vchKey0 + 32, true);
# 225 : 2 : pubkey0 = key0.GetPubKey();
# 226 : 2 : pubkey0H = key0.GetPubKey();
# 227 : 2 : pubkey0C = key0C.GetPubKey();
# 228 : 2 : *const_cast<unsigned char*>(pubkey0H.data()) = 0x06 | (pubkey0H[64] & 1);
# 229 : :
# 230 : 2 : key1.Set(vchKey1, vchKey1 + 32, false);
# 231 : 2 : key1C.Set(vchKey1, vchKey1 + 32, true);
# 232 : 2 : pubkey1 = key1.GetPubKey();
# 233 : 2 : pubkey1C = key1C.GetPubKey();
# 234 : :
# 235 : 2 : key2.Set(vchKey2, vchKey2 + 32, false);
# 236 : 2 : key2C.Set(vchKey2, vchKey2 + 32, true);
# 237 : 2 : pubkey2 = key2.GetPubKey();
# 238 : 2 : pubkey2C = key2C.GetPubKey();
# 239 : 2 : }
# 240 : : };
# 241 : :
# 242 : : enum class WitnessMode {
# 243 : : NONE,
# 244 : : PKH,
# 245 : : SH
# 246 : : };
# 247 : :
# 248 : : class TestBuilder
# 249 : : {
# 250 : : private:
# 251 : : //! Actually executed script
# 252 : : CScript script;
# 253 : : //! The P2SH redeemscript
# 254 : : CScript redeemscript;
# 255 : : //! The Witness embedded script
# 256 : : CScript witscript;
# 257 : : CScriptWitness scriptWitness;
# 258 : : CTransactionRef creditTx;
# 259 : : CMutableTransaction spendTx;
# 260 : : bool havePush;
# 261 : : std::vector<unsigned char> push;
# 262 : : std::string comment;
# 263 : : uint32_t flags;
# 264 : : int scriptError;
# 265 : : CAmount nValue;
# 266 : :
# 267 : : void DoPush()
# 268 : 1154 : {
# 269 [ + + ]: 1154 : if (havePush) {
# 270 : 440 : spendTx.vin[0].scriptSig << push;
# 271 : 440 : havePush = false;
# 272 : 440 : }
# 273 : 1154 : }
# 274 : :
# 275 : : void DoPush(const std::vector<unsigned char>& data)
# 276 : 506 : {
# 277 : 506 : DoPush();
# 278 : 506 : push = data;
# 279 : 506 : havePush = true;
# 280 : 506 : }
# 281 : :
# 282 : : public:
# 283 : : TestBuilder(const CScript& script_, const std::string& comment_, uint32_t flags_, bool P2SH = false, WitnessMode wm = WitnessMode::NONE, int witnessversion = 0, CAmount nValue_ = 0) : script(script_), havePush(false), comment(comment_), flags(flags_), scriptError(SCRIPT_ERR_OK), nValue(nValue_)
# 284 : 268 : {
# 285 : 268 : CScript scriptPubKey = script;
# 286 [ + + ]: 268 : if (wm == WitnessMode::PKH) {
# 287 : 32 : uint160 hash;
# 288 : 32 : CHash160().Write(Span{script}.subspan(1)).Finalize(hash);
# 289 : 32 : script = CScript() << OP_DUP << OP_HASH160 << ToByteVector(hash) << OP_EQUALVERIFY << OP_CHECKSIG;
# 290 : 32 : scriptPubKey = CScript() << witnessversion << ToByteVector(hash);
# 291 [ + + ]: 236 : } else if (wm == WitnessMode::SH) {
# 292 : 68 : witscript = scriptPubKey;
# 293 : 68 : uint256 hash;
# 294 : 68 : CSHA256().Write(witscript.data(), witscript.size()).Finalize(hash.begin());
# 295 : 68 : scriptPubKey = CScript() << witnessversion << ToByteVector(hash);
# 296 : 68 : }
# 297 [ + + ]: 268 : if (P2SH) {
# 298 : 72 : redeemscript = scriptPubKey;
# 299 : 72 : scriptPubKey = CScript() << OP_HASH160 << ToByteVector(CScriptID(redeemscript)) << OP_EQUAL;
# 300 : 72 : }
# 301 : 268 : creditTx = MakeTransactionRef(BuildCreditingTransaction(scriptPubKey, nValue));
# 302 : 268 : spendTx = BuildSpendingTransaction(CScript(), CScriptWitness(), *creditTx);
# 303 : 268 : }
# 304 : :
# 305 : : TestBuilder& ScriptError(ScriptError_t err)
# 306 : 142 : {
# 307 : 142 : scriptError = err;
# 308 : 142 : return *this;
# 309 : 142 : }
# 310 : :
# 311 : : TestBuilder& Opcode(const opcodetype& _op)
# 312 : 12 : {
# 313 : 12 : DoPush();
# 314 : 12 : spendTx.vin[0].scriptSig << _op;
# 315 : 12 : return *this;
# 316 : 12 : }
# 317 : :
# 318 : : TestBuilder& Num(int num)
# 319 : 100 : {
# 320 : 100 : DoPush();
# 321 : 100 : spendTx.vin[0].scriptSig << num;
# 322 : 100 : return *this;
# 323 : 100 : }
# 324 : :
# 325 : : TestBuilder& Push(const std::string& hex)
# 326 : 4 : {
# 327 : 4 : DoPush(ParseHex(hex));
# 328 : 4 : return *this;
# 329 : 4 : }
# 330 : :
# 331 : : TestBuilder& Push(const CScript& _script)
# 332 : 42 : {
# 333 : 42 : DoPush(std::vector<unsigned char>(_script.begin(), _script.end()));
# 334 : 42 : return *this;
# 335 : 42 : }
# 336 : :
# 337 : : TestBuilder& PushSig(const CKey& key, int nHashType = SIGHASH_ALL, unsigned int lenR = 32, unsigned int lenS = 32, SigVersion sigversion = SigVersion::BASE, CAmount amount = 0)
# 338 : 284 : {
# 339 : 284 : uint256 hash = SignatureHash(script, spendTx, 0, nHashType, amount, sigversion);
# 340 : 284 : std::vector<unsigned char> vchSig, r, s;
# 341 : 284 : uint32_t iter = 0;
# 342 : 2680 : do {
# 343 : 2680 : key.Sign(hash, vchSig, false, iter++);
# 344 [ + + ]: 2680 : if ((lenS == 33) != (vchSig[5 + vchSig[3]] == 33)) {
# 345 : 4 : NegateSignatureS(vchSig);
# 346 : 4 : }
# 347 : 2680 : r = std::vector<unsigned char>(vchSig.begin() + 4, vchSig.begin() + 4 + vchSig[3]);
# 348 : 2680 : s = std::vector<unsigned char>(vchSig.begin() + 6 + vchSig[3], vchSig.begin() + 6 + vchSig[3] + vchSig[5 + vchSig[3]]);
# 349 [ + + ][ - + ]: 2680 : } while (lenR != r.size() || lenS != s.size());
# 350 : 284 : vchSig.push_back(static_cast<unsigned char>(nHashType));
# 351 : 284 : DoPush(vchSig);
# 352 : 284 : return *this;
# 353 : 284 : }
# 354 : :
# 355 : : TestBuilder& PushWitSig(const CKey& key, CAmount amount = -1, int nHashType = SIGHASH_ALL, unsigned int lenR = 32, unsigned int lenS = 32, SigVersion sigversion = SigVersion::WITNESS_V0)
# 356 : 100 : {
# 357 [ + + ]: 100 : if (amount == -1)
# 358 : 92 : amount = nValue;
# 359 : 100 : return PushSig(key, nHashType, lenR, lenS, sigversion, amount).AsWit();
# 360 : 100 : }
# 361 : :
# 362 : : TestBuilder& Push(const CPubKey& pubkey)
# 363 : 40 : {
# 364 : 40 : DoPush(std::vector<unsigned char>(pubkey.begin(), pubkey.end()));
# 365 : 40 : return *this;
# 366 : 40 : }
# 367 : :
# 368 : : TestBuilder& PushRedeem()
# 369 : 72 : {
# 370 : 72 : DoPush(std::vector<unsigned char>(redeemscript.begin(), redeemscript.end()));
# 371 : 72 : return *this;
# 372 : 72 : }
# 373 : :
# 374 : : TestBuilder& PushWitRedeem()
# 375 : 64 : {
# 376 : 64 : DoPush(std::vector<unsigned char>(witscript.begin(), witscript.end()));
# 377 : 64 : return AsWit();
# 378 : 64 : }
# 379 : :
# 380 : : TestBuilder& EditPush(unsigned int pos, const std::string& hexin, const std::string& hexout)
# 381 : 62 : {
# 382 : 62 : assert(havePush);
# 383 : 0 : std::vector<unsigned char> datain = ParseHex(hexin);
# 384 : 62 : std::vector<unsigned char> dataout = ParseHex(hexout);
# 385 : 62 : assert(pos + datain.size() <= push.size());
# 386 : 0 : BOOST_CHECK_MESSAGE(std::vector<unsigned char>(push.begin() + pos, push.begin() + pos + datain.size()) == datain, comment);
# 387 : 62 : push.erase(push.begin() + pos, push.begin() + pos + datain.size());
# 388 : 62 : push.insert(push.begin() + pos, dataout.begin(), dataout.end());
# 389 : 62 : return *this;
# 390 : 62 : }
# 391 : :
# 392 : : TestBuilder& DamagePush(unsigned int pos)
# 393 : 28 : {
# 394 : 28 : assert(havePush);
# 395 : 0 : assert(pos < push.size());
# 396 : 0 : push[pos] ^= 1;
# 397 : 28 : return *this;
# 398 : 28 : }
# 399 : :
# 400 : : TestBuilder& Test()
# 401 : 268 : {
# 402 : 268 : TestBuilder copy = *this; // Make a copy so we can rollback the push.
# 403 : 268 : DoPush();
# 404 : 268 : DoTest(creditTx->vout[0].scriptPubKey, spendTx.vin[0].scriptSig, scriptWitness, flags, comment, scriptError, nValue);
# 405 : 268 : *this = copy;
# 406 : 268 : return *this;
# 407 : 268 : }
# 408 : :
# 409 : : TestBuilder& AsWit()
# 410 : 244 : {
# 411 : 244 : assert(havePush);
# 412 : 0 : scriptWitness.stack.push_back(push);
# 413 : 244 : havePush = false;
# 414 : 244 : return *this;
# 415 : 244 : }
# 416 : :
# 417 : : UniValue GetJSON()
# 418 : 268 : {
# 419 : 268 : DoPush();
# 420 : 268 : UniValue array(UniValue::VARR);
# 421 [ + + ]: 268 : if (!scriptWitness.stack.empty()) {
# 422 : 102 : UniValue wit(UniValue::VARR);
# 423 [ + + ]: 346 : for (unsigned i = 0; i < scriptWitness.stack.size(); i++) {
# 424 : 244 : wit.push_back(HexStr(scriptWitness.stack[i]));
# 425 : 244 : }
# 426 : 102 : wit.push_back(ValueFromAmount(nValue));
# 427 : 102 : array.push_back(wit);
# 428 : 102 : }
# 429 : 268 : array.push_back(FormatScript(spendTx.vin[0].scriptSig));
# 430 : 268 : array.push_back(FormatScript(creditTx->vout[0].scriptPubKey));
# 431 : 268 : array.push_back(FormatScriptFlags(flags));
# 432 : 268 : array.push_back(FormatScriptError((ScriptError_t)scriptError));
# 433 : 268 : array.push_back(comment);
# 434 : 268 : return array;
# 435 : 268 : }
# 436 : :
# 437 : : std::string GetComment() const
# 438 : 0 : {
# 439 : 0 : return comment;
# 440 : 0 : }
# 441 : : };
# 442 : :
# 443 : : std::string JSONPrettyPrint(const UniValue& univalue)
# 444 : 2784 : {
# 445 : 2784 : std::string ret = univalue.write(4);
# 446 : : // Workaround for libunivalue pretty printer, which puts a space between commas and newlines
# 447 : 2784 : size_t pos = 0;
# 448 [ - + ]: 2784 : while ((pos = ret.find(" \n", pos)) != std::string::npos) {
# 449 : 0 : ret.replace(pos, 2, "\n");
# 450 : 0 : pos++;
# 451 : 0 : }
# 452 : 2784 : return ret;
# 453 : 2784 : }
# 454 : : } // namespace
# 455 : :
# 456 : : BOOST_AUTO_TEST_CASE(script_build)
# 457 : 2 : {
# 458 : 2 : const KeyData keys;
# 459 : :
# 460 : 2 : std::vector<TestBuilder> tests;
# 461 : :
# 462 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG,
# 463 : 2 : "P2PK", 0
# 464 : 2 : ).PushSig(keys.key0));
# 465 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG,
# 466 : 2 : "P2PK, bad sig", 0
# 467 : 2 : ).PushSig(keys.key0).DamagePush(10).ScriptError(SCRIPT_ERR_EVAL_FALSE));
# 468 : :
# 469 : 2 : tests.push_back(TestBuilder(CScript() << OP_DUP << OP_HASH160 << ToByteVector(keys.pubkey1C.GetID()) << OP_EQUALVERIFY << OP_CHECKSIG,
# 470 : 2 : "P2PKH", 0
# 471 : 2 : ).PushSig(keys.key1).Push(keys.pubkey1C));
# 472 : 2 : tests.push_back(TestBuilder(CScript() << OP_DUP << OP_HASH160 << ToByteVector(keys.pubkey2C.GetID()) << OP_EQUALVERIFY << OP_CHECKSIG,
# 473 : 2 : "P2PKH, bad pubkey", 0
# 474 : 2 : ).PushSig(keys.key2).Push(keys.pubkey2C).DamagePush(5).ScriptError(SCRIPT_ERR_EQUALVERIFY));
# 475 : :
# 476 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1) << OP_CHECKSIG,
# 477 : 2 : "P2PK anyonecanpay", 0
# 478 : 2 : ).PushSig(keys.key1, SIGHASH_ALL | SIGHASH_ANYONECANPAY));
# 479 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1) << OP_CHECKSIG,
# 480 : 2 : "P2PK anyonecanpay marked with normal hashtype", 0
# 481 : 2 : ).PushSig(keys.key1, SIGHASH_ALL | SIGHASH_ANYONECANPAY).EditPush(70, "81", "01").ScriptError(SCRIPT_ERR_EVAL_FALSE));
# 482 : :
# 483 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0C) << OP_CHECKSIG,
# 484 : 2 : "P2SH(P2PK)", SCRIPT_VERIFY_P2SH, true
# 485 : 2 : ).PushSig(keys.key0).PushRedeem());
# 486 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0C) << OP_CHECKSIG,
# 487 : 2 : "P2SH(P2PK), bad redeemscript", SCRIPT_VERIFY_P2SH, true
# 488 : 2 : ).PushSig(keys.key0).PushRedeem().DamagePush(10).ScriptError(SCRIPT_ERR_EVAL_FALSE));
# 489 : :
# 490 : 2 : tests.push_back(TestBuilder(CScript() << OP_DUP << OP_HASH160 << ToByteVector(keys.pubkey0.GetID()) << OP_EQUALVERIFY << OP_CHECKSIG,
# 491 : 2 : "P2SH(P2PKH)", SCRIPT_VERIFY_P2SH, true
# 492 : 2 : ).PushSig(keys.key0).Push(keys.pubkey0).PushRedeem());
# 493 : 2 : tests.push_back(TestBuilder(CScript() << OP_DUP << OP_HASH160 << ToByteVector(keys.pubkey1.GetID()) << OP_EQUALVERIFY << OP_CHECKSIG,
# 494 : 2 : "P2SH(P2PKH), bad sig but no VERIFY_P2SH", 0, true
# 495 : 2 : ).PushSig(keys.key0).DamagePush(10).PushRedeem());
# 496 : 2 : tests.push_back(TestBuilder(CScript() << OP_DUP << OP_HASH160 << ToByteVector(keys.pubkey1.GetID()) << OP_EQUALVERIFY << OP_CHECKSIG,
# 497 : 2 : "P2SH(P2PKH), bad sig", SCRIPT_VERIFY_P2SH, true
# 498 : 2 : ).PushSig(keys.key0).DamagePush(10).PushRedeem().ScriptError(SCRIPT_ERR_EQUALVERIFY));
# 499 : :
# 500 : 2 : tests.push_back(TestBuilder(CScript() << OP_3 << ToByteVector(keys.pubkey0C) << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_3 << OP_CHECKMULTISIG,
# 501 : 2 : "3-of-3", 0
# 502 : 2 : ).Num(0).PushSig(keys.key0).PushSig(keys.key1).PushSig(keys.key2));
# 503 : 2 : tests.push_back(TestBuilder(CScript() << OP_3 << ToByteVector(keys.pubkey0C) << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_3 << OP_CHECKMULTISIG,
# 504 : 2 : "3-of-3, 2 sigs", 0
# 505 : 2 : ).Num(0).PushSig(keys.key0).PushSig(keys.key1).Num(0).ScriptError(SCRIPT_ERR_EVAL_FALSE));
# 506 : :
# 507 : 2 : tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey0C) << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_3 << OP_CHECKMULTISIG,
# 508 : 2 : "P2SH(2-of-3)", SCRIPT_VERIFY_P2SH, true
# 509 : 2 : ).Num(0).PushSig(keys.key1).PushSig(keys.key2).PushRedeem());
# 510 : 2 : tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey0C) << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_3 << OP_CHECKMULTISIG,
# 511 : 2 : "P2SH(2-of-3), 1 sig", SCRIPT_VERIFY_P2SH, true
# 512 : 2 : ).Num(0).PushSig(keys.key1).Num(0).PushRedeem().ScriptError(SCRIPT_ERR_EVAL_FALSE));
# 513 : :
# 514 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG,
# 515 : 2 : "P2PK with too much R padding but no DERSIG", 0
# 516 : 2 : ).PushSig(keys.key1, SIGHASH_ALL, 31, 32).EditPush(1, "43021F", "44022000"));
# 517 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG,
# 518 : 2 : "P2PK with too much R padding", SCRIPT_VERIFY_DERSIG
# 519 : 2 : ).PushSig(keys.key1, SIGHASH_ALL, 31, 32).EditPush(1, "43021F", "44022000").ScriptError(SCRIPT_ERR_SIG_DER));
# 520 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG,
# 521 : 2 : "P2PK with too much S padding but no DERSIG", 0
# 522 : 2 : ).PushSig(keys.key1, SIGHASH_ALL).EditPush(1, "44", "45").EditPush(37, "20", "2100"));
# 523 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG,
# 524 : 2 : "P2PK with too much S padding", SCRIPT_VERIFY_DERSIG
# 525 : 2 : ).PushSig(keys.key1, SIGHASH_ALL).EditPush(1, "44", "45").EditPush(37, "20", "2100").ScriptError(SCRIPT_ERR_SIG_DER));
# 526 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG,
# 527 : 2 : "P2PK with too little R padding but no DERSIG", 0
# 528 : 2 : ).PushSig(keys.key1, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220"));
# 529 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG,
# 530 : 2 : "P2PK with too little R padding", SCRIPT_VERIFY_DERSIG
# 531 : 2 : ).PushSig(keys.key1, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220").ScriptError(SCRIPT_ERR_SIG_DER));
# 532 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG << OP_NOT,
# 533 : 2 : "P2PK NOT with bad sig with too much R padding but no DERSIG", 0
# 534 : 2 : ).PushSig(keys.key2, SIGHASH_ALL, 31, 32).EditPush(1, "43021F", "44022000").DamagePush(10));
# 535 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG << OP_NOT,
# 536 : 2 : "P2PK NOT with bad sig with too much R padding", SCRIPT_VERIFY_DERSIG
# 537 : 2 : ).PushSig(keys.key2, SIGHASH_ALL, 31, 32).EditPush(1, "43021F", "44022000").DamagePush(10).ScriptError(SCRIPT_ERR_SIG_DER));
# 538 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG << OP_NOT,
# 539 : 2 : "P2PK NOT with too much R padding but no DERSIG", 0
# 540 : 2 : ).PushSig(keys.key2, SIGHASH_ALL, 31, 32).EditPush(1, "43021F", "44022000").ScriptError(SCRIPT_ERR_EVAL_FALSE));
# 541 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG << OP_NOT,
# 542 : 2 : "P2PK NOT with too much R padding", SCRIPT_VERIFY_DERSIG
# 543 : 2 : ).PushSig(keys.key2, SIGHASH_ALL, 31, 32).EditPush(1, "43021F", "44022000").ScriptError(SCRIPT_ERR_SIG_DER));
# 544 : :
# 545 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG,
# 546 : 2 : "BIP66 example 1, without DERSIG", 0
# 547 : 2 : ).PushSig(keys.key1, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220"));
# 548 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG,
# 549 : 2 : "BIP66 example 1, with DERSIG", SCRIPT_VERIFY_DERSIG
# 550 : 2 : ).PushSig(keys.key1, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220").ScriptError(SCRIPT_ERR_SIG_DER));
# 551 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG << OP_NOT,
# 552 : 2 : "BIP66 example 2, without DERSIG", 0
# 553 : 2 : ).PushSig(keys.key1, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220").ScriptError(SCRIPT_ERR_EVAL_FALSE));
# 554 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG << OP_NOT,
# 555 : 2 : "BIP66 example 2, with DERSIG", SCRIPT_VERIFY_DERSIG
# 556 : 2 : ).PushSig(keys.key1, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220").ScriptError(SCRIPT_ERR_SIG_DER));
# 557 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG,
# 558 : 2 : "BIP66 example 3, without DERSIG", 0
# 559 : 2 : ).Num(0).ScriptError(SCRIPT_ERR_EVAL_FALSE));
# 560 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG,
# 561 : 2 : "BIP66 example 3, with DERSIG", SCRIPT_VERIFY_DERSIG
# 562 : 2 : ).Num(0).ScriptError(SCRIPT_ERR_EVAL_FALSE));
# 563 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG << OP_NOT,
# 564 : 2 : "BIP66 example 4, without DERSIG", 0
# 565 : 2 : ).Num(0));
# 566 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG << OP_NOT,
# 567 : 2 : "BIP66 example 4, with DERSIG", SCRIPT_VERIFY_DERSIG
# 568 : 2 : ).Num(0));
# 569 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG,
# 570 : 2 : "BIP66 example 5, without DERSIG", 0
# 571 : 2 : ).Num(1).ScriptError(SCRIPT_ERR_EVAL_FALSE));
# 572 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG,
# 573 : 2 : "BIP66 example 5, with DERSIG", SCRIPT_VERIFY_DERSIG
# 574 : 2 : ).Num(1).ScriptError(SCRIPT_ERR_SIG_DER));
# 575 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG << OP_NOT,
# 576 : 2 : "BIP66 example 6, without DERSIG", 0
# 577 : 2 : ).Num(1));
# 578 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1C) << OP_CHECKSIG << OP_NOT,
# 579 : 2 : "BIP66 example 6, with DERSIG", SCRIPT_VERIFY_DERSIG
# 580 : 2 : ).Num(1).ScriptError(SCRIPT_ERR_SIG_DER));
# 581 : 2 : tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_2 << OP_CHECKMULTISIG,
# 582 : 2 : "BIP66 example 7, without DERSIG", 0
# 583 : 2 : ).Num(0).PushSig(keys.key1, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220").PushSig(keys.key2));
# 584 : 2 : tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_2 << OP_CHECKMULTISIG,
# 585 : 2 : "BIP66 example 7, with DERSIG", SCRIPT_VERIFY_DERSIG
# 586 : 2 : ).Num(0).PushSig(keys.key1, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220").PushSig(keys.key2).ScriptError(SCRIPT_ERR_SIG_DER));
# 587 : 2 : tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_2 << OP_CHECKMULTISIG << OP_NOT,
# 588 : 2 : "BIP66 example 8, without DERSIG", 0
# 589 : 2 : ).Num(0).PushSig(keys.key1, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220").PushSig(keys.key2).ScriptError(SCRIPT_ERR_EVAL_FALSE));
# 590 : 2 : tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_2 << OP_CHECKMULTISIG << OP_NOT,
# 591 : 2 : "BIP66 example 8, with DERSIG", SCRIPT_VERIFY_DERSIG
# 592 : 2 : ).Num(0).PushSig(keys.key1, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220").PushSig(keys.key2).ScriptError(SCRIPT_ERR_SIG_DER));
# 593 : 2 : tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_2 << OP_CHECKMULTISIG,
# 594 : 2 : "BIP66 example 9, without DERSIG", 0
# 595 : 2 : ).Num(0).Num(0).PushSig(keys.key2, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220").ScriptError(SCRIPT_ERR_EVAL_FALSE));
# 596 : 2 : tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_2 << OP_CHECKMULTISIG,
# 597 : 2 : "BIP66 example 9, with DERSIG", SCRIPT_VERIFY_DERSIG
# 598 : 2 : ).Num(0).Num(0).PushSig(keys.key2, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220").ScriptError(SCRIPT_ERR_SIG_DER));
# 599 : 2 : tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_2 << OP_CHECKMULTISIG << OP_NOT,
# 600 : 2 : "BIP66 example 10, without DERSIG", 0
# 601 : 2 : ).Num(0).Num(0).PushSig(keys.key2, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220"));
# 602 : 2 : tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_2 << OP_CHECKMULTISIG << OP_NOT,
# 603 : 2 : "BIP66 example 10, with DERSIG", SCRIPT_VERIFY_DERSIG
# 604 : 2 : ).Num(0).Num(0).PushSig(keys.key2, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220").ScriptError(SCRIPT_ERR_SIG_DER));
# 605 : 2 : tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_2 << OP_CHECKMULTISIG,
# 606 : 2 : "BIP66 example 11, without DERSIG", 0
# 607 : 2 : ).Num(0).PushSig(keys.key1, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220").Num(0).ScriptError(SCRIPT_ERR_EVAL_FALSE));
# 608 : 2 : tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_2 << OP_CHECKMULTISIG,
# 609 : 2 : "BIP66 example 11, with DERSIG", SCRIPT_VERIFY_DERSIG
# 610 : 2 : ).Num(0).PushSig(keys.key1, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220").Num(0).ScriptError(SCRIPT_ERR_EVAL_FALSE));
# 611 : 2 : tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_2 << OP_CHECKMULTISIG << OP_NOT,
# 612 : 2 : "BIP66 example 12, without DERSIG", 0
# 613 : 2 : ).Num(0).PushSig(keys.key1, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220").Num(0));
# 614 : 2 : tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_2 << OP_CHECKMULTISIG << OP_NOT,
# 615 : 2 : "BIP66 example 12, with DERSIG", SCRIPT_VERIFY_DERSIG
# 616 : 2 : ).Num(0).PushSig(keys.key1, SIGHASH_ALL, 33, 32).EditPush(1, "45022100", "440220").Num(0));
# 617 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG,
# 618 : 2 : "P2PK with multi-byte hashtype, without DERSIG", 0
# 619 : 2 : ).PushSig(keys.key2, SIGHASH_ALL).EditPush(70, "01", "0101"));
# 620 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG,
# 621 : 2 : "P2PK with multi-byte hashtype, with DERSIG", SCRIPT_VERIFY_DERSIG
# 622 : 2 : ).PushSig(keys.key2, SIGHASH_ALL).EditPush(70, "01", "0101").ScriptError(SCRIPT_ERR_SIG_DER));
# 623 : :
# 624 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG,
# 625 : 2 : "P2PK with high S but no LOW_S", 0
# 626 : 2 : ).PushSig(keys.key2, SIGHASH_ALL, 32, 33));
# 627 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG,
# 628 : 2 : "P2PK with high S", SCRIPT_VERIFY_LOW_S
# 629 : 2 : ).PushSig(keys.key2, SIGHASH_ALL, 32, 33).ScriptError(SCRIPT_ERR_SIG_HIGH_S));
# 630 : :
# 631 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0H) << OP_CHECKSIG,
# 632 : 2 : "P2PK with hybrid pubkey but no STRICTENC", 0
# 633 : 2 : ).PushSig(keys.key0, SIGHASH_ALL));
# 634 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0H) << OP_CHECKSIG,
# 635 : 2 : "P2PK with hybrid pubkey", SCRIPT_VERIFY_STRICTENC
# 636 : 2 : ).PushSig(keys.key0, SIGHASH_ALL).ScriptError(SCRIPT_ERR_PUBKEYTYPE));
# 637 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0H) << OP_CHECKSIG << OP_NOT,
# 638 : 2 : "P2PK NOT with hybrid pubkey but no STRICTENC", 0
# 639 : 2 : ).PushSig(keys.key0, SIGHASH_ALL).ScriptError(SCRIPT_ERR_EVAL_FALSE));
# 640 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0H) << OP_CHECKSIG << OP_NOT,
# 641 : 2 : "P2PK NOT with hybrid pubkey", SCRIPT_VERIFY_STRICTENC
# 642 : 2 : ).PushSig(keys.key0, SIGHASH_ALL).ScriptError(SCRIPT_ERR_PUBKEYTYPE));
# 643 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0H) << OP_CHECKSIG << OP_NOT,
# 644 : 2 : "P2PK NOT with invalid hybrid pubkey but no STRICTENC", 0
# 645 : 2 : ).PushSig(keys.key0, SIGHASH_ALL).DamagePush(10));
# 646 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0H) << OP_CHECKSIG << OP_NOT,
# 647 : 2 : "P2PK NOT with invalid hybrid pubkey", SCRIPT_VERIFY_STRICTENC
# 648 : 2 : ).PushSig(keys.key0, SIGHASH_ALL).DamagePush(10).ScriptError(SCRIPT_ERR_PUBKEYTYPE));
# 649 : 2 : tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey0H) << ToByteVector(keys.pubkey1C) << OP_2 << OP_CHECKMULTISIG,
# 650 : 2 : "1-of-2 with the second 1 hybrid pubkey and no STRICTENC", 0
# 651 : 2 : ).Num(0).PushSig(keys.key1, SIGHASH_ALL));
# 652 : 2 : tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey0H) << ToByteVector(keys.pubkey1C) << OP_2 << OP_CHECKMULTISIG,
# 653 : 2 : "1-of-2 with the second 1 hybrid pubkey", SCRIPT_VERIFY_STRICTENC
# 654 : 2 : ).Num(0).PushSig(keys.key1, SIGHASH_ALL));
# 655 : 2 : tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey0H) << OP_2 << OP_CHECKMULTISIG,
# 656 : 2 : "1-of-2 with the first 1 hybrid pubkey", SCRIPT_VERIFY_STRICTENC
# 657 : 2 : ).Num(0).PushSig(keys.key1, SIGHASH_ALL).ScriptError(SCRIPT_ERR_PUBKEYTYPE));
# 658 : :
# 659 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1) << OP_CHECKSIG,
# 660 : 2 : "P2PK with undefined hashtype but no STRICTENC", 0
# 661 : 2 : ).PushSig(keys.key1, 5));
# 662 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1) << OP_CHECKSIG,
# 663 : 2 : "P2PK with undefined hashtype", SCRIPT_VERIFY_STRICTENC
# 664 : 2 : ).PushSig(keys.key1, 5).ScriptError(SCRIPT_ERR_SIG_HASHTYPE));
# 665 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1) << OP_CHECKSIG << OP_NOT,
# 666 : 2 : "P2PK NOT with invalid sig and undefined hashtype but no STRICTENC", 0
# 667 : 2 : ).PushSig(keys.key1, 5).DamagePush(10));
# 668 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1) << OP_CHECKSIG << OP_NOT,
# 669 : 2 : "P2PK NOT with invalid sig and undefined hashtype", SCRIPT_VERIFY_STRICTENC
# 670 : 2 : ).PushSig(keys.key1, 5).DamagePush(10).ScriptError(SCRIPT_ERR_SIG_HASHTYPE));
# 671 : :
# 672 : 2 : tests.push_back(TestBuilder(CScript() << OP_3 << ToByteVector(keys.pubkey0C) << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_3 << OP_CHECKMULTISIG,
# 673 : 2 : "3-of-3 with nonzero dummy but no NULLDUMMY", 0
# 674 : 2 : ).Num(1).PushSig(keys.key0).PushSig(keys.key1).PushSig(keys.key2));
# 675 : 2 : tests.push_back(TestBuilder(CScript() << OP_3 << ToByteVector(keys.pubkey0C) << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_3 << OP_CHECKMULTISIG,
# 676 : 2 : "3-of-3 with nonzero dummy", SCRIPT_VERIFY_NULLDUMMY
# 677 : 2 : ).Num(1).PushSig(keys.key0).PushSig(keys.key1).PushSig(keys.key2).ScriptError(SCRIPT_ERR_SIG_NULLDUMMY));
# 678 : 2 : tests.push_back(TestBuilder(CScript() << OP_3 << ToByteVector(keys.pubkey0C) << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_3 << OP_CHECKMULTISIG << OP_NOT,
# 679 : 2 : "3-of-3 NOT with invalid sig and nonzero dummy but no NULLDUMMY", 0
# 680 : 2 : ).Num(1).PushSig(keys.key0).PushSig(keys.key1).PushSig(keys.key2).DamagePush(10));
# 681 : 2 : tests.push_back(TestBuilder(CScript() << OP_3 << ToByteVector(keys.pubkey0C) << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey2C) << OP_3 << OP_CHECKMULTISIG << OP_NOT,
# 682 : 2 : "3-of-3 NOT with invalid sig with nonzero dummy", SCRIPT_VERIFY_NULLDUMMY
# 683 : 2 : ).Num(1).PushSig(keys.key0).PushSig(keys.key1).PushSig(keys.key2).DamagePush(10).ScriptError(SCRIPT_ERR_SIG_NULLDUMMY));
# 684 : :
# 685 : 2 : tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey1C) << OP_2 << OP_CHECKMULTISIG,
# 686 : 2 : "2-of-2 with two identical keys and sigs pushed using OP_DUP but no SIGPUSHONLY", 0
# 687 : 2 : ).Num(0).PushSig(keys.key1).Opcode(OP_DUP));
# 688 : 2 : tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey1C) << OP_2 << OP_CHECKMULTISIG,
# 689 : 2 : "2-of-2 with two identical keys and sigs pushed using OP_DUP", SCRIPT_VERIFY_SIGPUSHONLY
# 690 : 2 : ).Num(0).PushSig(keys.key1).Opcode(OP_DUP).ScriptError(SCRIPT_ERR_SIG_PUSHONLY));
# 691 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG,
# 692 : 2 : "P2SH(P2PK) with non-push scriptSig but no P2SH or SIGPUSHONLY", 0, true
# 693 : 2 : ).PushSig(keys.key2).Opcode(OP_NOP8).PushRedeem());
# 694 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG,
# 695 : 2 : "P2PK with non-push scriptSig but with P2SH validation", 0
# 696 : 2 : ).PushSig(keys.key2).Opcode(OP_NOP8));
# 697 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG,
# 698 : 2 : "P2SH(P2PK) with non-push scriptSig but no SIGPUSHONLY", SCRIPT_VERIFY_P2SH, true
# 699 : 2 : ).PushSig(keys.key2).Opcode(OP_NOP8).PushRedeem().ScriptError(SCRIPT_ERR_SIG_PUSHONLY));
# 700 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey2C) << OP_CHECKSIG,
# 701 : 2 : "P2SH(P2PK) with non-push scriptSig but not P2SH", SCRIPT_VERIFY_SIGPUSHONLY, true
# 702 : 2 : ).PushSig(keys.key2).Opcode(OP_NOP8).PushRedeem().ScriptError(SCRIPT_ERR_SIG_PUSHONLY));
# 703 : 2 : tests.push_back(TestBuilder(CScript() << OP_2 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey1C) << OP_2 << OP_CHECKMULTISIG,
# 704 : 2 : "2-of-2 with two identical keys and sigs pushed", SCRIPT_VERIFY_SIGPUSHONLY
# 705 : 2 : ).Num(0).PushSig(keys.key1).PushSig(keys.key1));
# 706 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG,
# 707 : 2 : "P2PK with unnecessary input but no CLEANSTACK", SCRIPT_VERIFY_P2SH
# 708 : 2 : ).Num(11).PushSig(keys.key0));
# 709 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG,
# 710 : 2 : "P2PK with unnecessary input", SCRIPT_VERIFY_CLEANSTACK | SCRIPT_VERIFY_P2SH
# 711 : 2 : ).Num(11).PushSig(keys.key0).ScriptError(SCRIPT_ERR_CLEANSTACK));
# 712 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG,
# 713 : 2 : "P2SH with unnecessary input but no CLEANSTACK", SCRIPT_VERIFY_P2SH, true
# 714 : 2 : ).Num(11).PushSig(keys.key0).PushRedeem());
# 715 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG,
# 716 : 2 : "P2SH with unnecessary input", SCRIPT_VERIFY_CLEANSTACK | SCRIPT_VERIFY_P2SH, true
# 717 : 2 : ).Num(11).PushSig(keys.key0).PushRedeem().ScriptError(SCRIPT_ERR_CLEANSTACK));
# 718 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG,
# 719 : 2 : "P2SH with CLEANSTACK", SCRIPT_VERIFY_CLEANSTACK | SCRIPT_VERIFY_P2SH, true
# 720 : 2 : ).PushSig(keys.key0).PushRedeem());
# 721 : :
# 722 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG,
# 723 : 2 : "Basic P2WSH", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false, WitnessMode::SH,
# 724 : 2 : 0, 1).PushWitSig(keys.key0).PushWitRedeem());
# 725 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0),
# 726 : 2 : "Basic P2WPKH", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false, WitnessMode::PKH,
# 727 : 2 : 0, 1).PushWitSig(keys.key0).Push(keys.pubkey0).AsWit());
# 728 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG,
# 729 : 2 : "Basic P2SH(P2WSH)", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true, WitnessMode::SH,
# 730 : 2 : 0, 1).PushWitSig(keys.key0).PushWitRedeem().PushRedeem());
# 731 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0),
# 732 : 2 : "Basic P2SH(P2WPKH)", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true, WitnessMode::PKH,
# 733 : 2 : 0, 1).PushWitSig(keys.key0).Push(keys.pubkey0).AsWit().PushRedeem());
# 734 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1) << OP_CHECKSIG,
# 735 : 2 : "Basic P2WSH with the wrong key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false, WitnessMode::SH
# 736 : 2 : ).PushWitSig(keys.key0).PushWitRedeem().ScriptError(SCRIPT_ERR_EVAL_FALSE));
# 737 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1),
# 738 : 2 : "Basic P2WPKH with the wrong key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false, WitnessMode::PKH
# 739 : 2 : ).PushWitSig(keys.key0).Push(keys.pubkey1).AsWit().ScriptError(SCRIPT_ERR_EVAL_FALSE));
# 740 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1) << OP_CHECKSIG,
# 741 : 2 : "Basic P2SH(P2WSH) with the wrong key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true, WitnessMode::SH
# 742 : 2 : ).PushWitSig(keys.key0).PushWitRedeem().PushRedeem().ScriptError(SCRIPT_ERR_EVAL_FALSE));
# 743 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1),
# 744 : 2 : "Basic P2SH(P2WPKH) with the wrong key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true, WitnessMode::PKH
# 745 : 2 : ).PushWitSig(keys.key0).Push(keys.pubkey1).AsWit().PushRedeem().ScriptError(SCRIPT_ERR_EVAL_FALSE));
# 746 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1) << OP_CHECKSIG,
# 747 : 2 : "Basic P2WSH with the wrong key but no WITNESS", SCRIPT_VERIFY_P2SH, false, WitnessMode::SH
# 748 : 2 : ).PushWitSig(keys.key0).PushWitRedeem());
# 749 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1),
# 750 : 2 : "Basic P2WPKH with the wrong key but no WITNESS", SCRIPT_VERIFY_P2SH, false, WitnessMode::PKH
# 751 : 2 : ).PushWitSig(keys.key0).Push(keys.pubkey1).AsWit());
# 752 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1) << OP_CHECKSIG,
# 753 : 2 : "Basic P2SH(P2WSH) with the wrong key but no WITNESS", SCRIPT_VERIFY_P2SH, true, WitnessMode::SH
# 754 : 2 : ).PushWitSig(keys.key0).PushWitRedeem().PushRedeem());
# 755 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1),
# 756 : 2 : "Basic P2SH(P2WPKH) with the wrong key but no WITNESS", SCRIPT_VERIFY_P2SH, true, WitnessMode::PKH
# 757 : 2 : ).PushWitSig(keys.key0).Push(keys.pubkey1).AsWit().PushRedeem());
# 758 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG,
# 759 : 2 : "Basic P2WSH with wrong value", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false, WitnessMode::SH,
# 760 : 2 : 0, 0).PushWitSig(keys.key0, 1).PushWitRedeem().ScriptError(SCRIPT_ERR_EVAL_FALSE));
# 761 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0),
# 762 : 2 : "Basic P2WPKH with wrong value", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false, WitnessMode::PKH,
# 763 : 2 : 0, 0).PushWitSig(keys.key0, 1).Push(keys.pubkey0).AsWit().ScriptError(SCRIPT_ERR_EVAL_FALSE));
# 764 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG,
# 765 : 2 : "Basic P2SH(P2WSH) with wrong value", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true, WitnessMode::SH,
# 766 : 2 : 0, 0).PushWitSig(keys.key0, 1).PushWitRedeem().PushRedeem().ScriptError(SCRIPT_ERR_EVAL_FALSE));
# 767 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0),
# 768 : 2 : "Basic P2SH(P2WPKH) with wrong value", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true, WitnessMode::PKH,
# 769 : 2 : 0, 0).PushWitSig(keys.key0, 1).Push(keys.pubkey0).AsWit().PushRedeem().ScriptError(SCRIPT_ERR_EVAL_FALSE));
# 770 : :
# 771 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0),
# 772 : 2 : "P2WPKH with future witness version", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH |
# 773 : 2 : SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM, false, WitnessMode::PKH, 1
# 774 : 2 : ).PushWitSig(keys.key0).Push(keys.pubkey0).AsWit().ScriptError(SCRIPT_ERR_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM));
# 775 : 2 : {
# 776 : 2 : CScript witscript = CScript() << ToByteVector(keys.pubkey0);
# 777 : 2 : uint256 hash;
# 778 : 2 : CSHA256().Write(witscript.data(), witscript.size()).Finalize(hash.begin());
# 779 : 2 : std::vector<unsigned char> hashBytes = ToByteVector(hash);
# 780 : 2 : hashBytes.pop_back();
# 781 : 2 : tests.push_back(TestBuilder(CScript() << OP_0 << hashBytes,
# 782 : 2 : "P2WPKH with wrong witness program length", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false
# 783 : 2 : ).PushWitSig(keys.key0).Push(keys.pubkey0).AsWit().ScriptError(SCRIPT_ERR_WITNESS_PROGRAM_WRONG_LENGTH));
# 784 : 2 : }
# 785 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG,
# 786 : 2 : "P2WSH with empty witness", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false, WitnessMode::SH
# 787 : 2 : ).ScriptError(SCRIPT_ERR_WITNESS_PROGRAM_WITNESS_EMPTY));
# 788 : 2 : {
# 789 : 2 : CScript witscript = CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG;
# 790 : 2 : tests.push_back(TestBuilder(witscript,
# 791 : 2 : "P2WSH with witness program mismatch", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false, WitnessMode::SH
# 792 : 2 : ).PushWitSig(keys.key0).Push(witscript).DamagePush(0).AsWit().ScriptError(SCRIPT_ERR_WITNESS_PROGRAM_MISMATCH));
# 793 : 2 : }
# 794 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0),
# 795 : 2 : "P2WPKH with witness program mismatch", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false, WitnessMode::PKH
# 796 : 2 : ).PushWitSig(keys.key0).Push(keys.pubkey0).AsWit().Push("0").AsWit().ScriptError(SCRIPT_ERR_WITNESS_PROGRAM_MISMATCH));
# 797 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0),
# 798 : 2 : "P2WPKH with non-empty scriptSig", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false, WitnessMode::PKH
# 799 : 2 : ).PushWitSig(keys.key0).Push(keys.pubkey0).AsWit().Num(11).ScriptError(SCRIPT_ERR_WITNESS_MALLEATED));
# 800 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey1),
# 801 : 2 : "P2SH(P2WPKH) with superfluous push in scriptSig", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true, WitnessMode::PKH
# 802 : 2 : ).PushWitSig(keys.key0).Push(keys.pubkey1).AsWit().Num(11).PushRedeem().ScriptError(SCRIPT_ERR_WITNESS_MALLEATED_P2SH));
# 803 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG,
# 804 : 2 : "P2PK with witness", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH
# 805 : 2 : ).PushSig(keys.key0).Push("0").AsWit().ScriptError(SCRIPT_ERR_WITNESS_UNEXPECTED));
# 806 : :
# 807 : : // Compressed keys should pass SCRIPT_VERIFY_WITNESS_PUBKEYTYPE
# 808 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0C) << OP_CHECKSIG,
# 809 : 2 : "Basic P2WSH with compressed key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, false, WitnessMode::SH,
# 810 : 2 : 0, 1).PushWitSig(keys.key0C).PushWitRedeem());
# 811 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0C),
# 812 : 2 : "Basic P2WPKH with compressed key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, false, WitnessMode::PKH,
# 813 : 2 : 0, 1).PushWitSig(keys.key0C).Push(keys.pubkey0C).AsWit());
# 814 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0C) << OP_CHECKSIG,
# 815 : 2 : "Basic P2SH(P2WSH) with compressed key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, true, WitnessMode::SH,
# 816 : 2 : 0, 1).PushWitSig(keys.key0C).PushWitRedeem().PushRedeem());
# 817 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0C),
# 818 : 2 : "Basic P2SH(P2WPKH) with compressed key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, true, WitnessMode::PKH,
# 819 : 2 : 0, 1).PushWitSig(keys.key0C).Push(keys.pubkey0C).AsWit().PushRedeem());
# 820 : :
# 821 : : // Testing uncompressed key in witness with SCRIPT_VERIFY_WITNESS_PUBKEYTYPE
# 822 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG,
# 823 : 2 : "Basic P2WSH", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, false, WitnessMode::SH,
# 824 : 2 : 0, 1).PushWitSig(keys.key0).PushWitRedeem().ScriptError(SCRIPT_ERR_WITNESS_PUBKEYTYPE));
# 825 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0),
# 826 : 2 : "Basic P2WPKH", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, false, WitnessMode::PKH,
# 827 : 2 : 0, 1).PushWitSig(keys.key0).Push(keys.pubkey0).AsWit().ScriptError(SCRIPT_ERR_WITNESS_PUBKEYTYPE));
# 828 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0) << OP_CHECKSIG,
# 829 : 2 : "Basic P2SH(P2WSH)", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, true, WitnessMode::SH,
# 830 : 2 : 0, 1).PushWitSig(keys.key0).PushWitRedeem().PushRedeem().ScriptError(SCRIPT_ERR_WITNESS_PUBKEYTYPE));
# 831 : 2 : tests.push_back(TestBuilder(CScript() << ToByteVector(keys.pubkey0),
# 832 : 2 : "Basic P2SH(P2WPKH)", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, true, WitnessMode::PKH,
# 833 : 2 : 0, 1).PushWitSig(keys.key0).Push(keys.pubkey0).AsWit().PushRedeem().ScriptError(SCRIPT_ERR_WITNESS_PUBKEYTYPE));
# 834 : :
# 835 : : // P2WSH 1-of-2 multisig with compressed keys
# 836 : 2 : tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey0C) << OP_2 << OP_CHECKMULTISIG,
# 837 : 2 : "P2WSH CHECKMULTISIG with compressed keys", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, false, WitnessMode::SH,
# 838 : 2 : 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key0C).PushWitRedeem());
# 839 : 2 : tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey0C) << OP_2 << OP_CHECKMULTISIG,
# 840 : 2 : "P2SH(P2WSH) CHECKMULTISIG with compressed keys", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, true, WitnessMode::SH,
# 841 : 2 : 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key0C).PushWitRedeem().PushRedeem());
# 842 : 2 : tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey0C) << OP_2 << OP_CHECKMULTISIG,
# 843 : 2 : "P2WSH CHECKMULTISIG with compressed keys", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, false, WitnessMode::SH,
# 844 : 2 : 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key1C).PushWitRedeem());
# 845 : 2 : tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey0C) << OP_2 << OP_CHECKMULTISIG,
# 846 : 2 : "P2SH(P2WSH) CHECKMULTISIG with compressed keys", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, true, WitnessMode::SH,
# 847 : 2 : 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key1C).PushWitRedeem().PushRedeem());
# 848 : :
# 849 : : // P2WSH 1-of-2 multisig with first key uncompressed
# 850 : 2 : tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey0) << OP_2 << OP_CHECKMULTISIG,
# 851 : 2 : "P2WSH CHECKMULTISIG with first key uncompressed and signing with the first key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false, WitnessMode::SH,
# 852 : 2 : 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key0).PushWitRedeem());
# 853 : 2 : tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey0) << OP_2 << OP_CHECKMULTISIG,
# 854 : 2 : "P2SH(P2WSH) CHECKMULTISIG first key uncompressed and signing with the first key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true, WitnessMode::SH,
# 855 : 2 : 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key0).PushWitRedeem().PushRedeem());
# 856 : 2 : tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey0) << OP_2 << OP_CHECKMULTISIG,
# 857 : 2 : "P2WSH CHECKMULTISIG with first key uncompressed and signing with the first key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, false, WitnessMode::SH,
# 858 : 2 : 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key0).PushWitRedeem().ScriptError(SCRIPT_ERR_WITNESS_PUBKEYTYPE));
# 859 : 2 : tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey0) << OP_2 << OP_CHECKMULTISIG,
# 860 : 2 : "P2SH(P2WSH) CHECKMULTISIG with first key uncompressed and signing with the first key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, true, WitnessMode::SH,
# 861 : 2 : 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key0).PushWitRedeem().PushRedeem().ScriptError(SCRIPT_ERR_WITNESS_PUBKEYTYPE));
# 862 : 2 : tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey0) << OP_2 << OP_CHECKMULTISIG,
# 863 : 2 : "P2WSH CHECKMULTISIG with first key uncompressed and signing with the second key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false, WitnessMode::SH,
# 864 : 2 : 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key1C).PushWitRedeem());
# 865 : 2 : tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey0) << OP_2 << OP_CHECKMULTISIG,
# 866 : 2 : "P2SH(P2WSH) CHECKMULTISIG with first key uncompressed and signing with the second key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true, WitnessMode::SH,
# 867 : 2 : 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key1C).PushWitRedeem().PushRedeem());
# 868 : 2 : tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey0) << OP_2 << OP_CHECKMULTISIG,
# 869 : 2 : "P2WSH CHECKMULTISIG with first key uncompressed and signing with the second key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, false, WitnessMode::SH,
# 870 : 2 : 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key1C).PushWitRedeem().ScriptError(SCRIPT_ERR_WITNESS_PUBKEYTYPE));
# 871 : 2 : tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1C) << ToByteVector(keys.pubkey0) << OP_2 << OP_CHECKMULTISIG,
# 872 : 2 : "P2SH(P2WSH) CHECKMULTISIG with first key uncompressed and signing with the second key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, true, WitnessMode::SH,
# 873 : 2 : 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key1C).PushWitRedeem().PushRedeem().ScriptError(SCRIPT_ERR_WITNESS_PUBKEYTYPE));
# 874 : : // P2WSH 1-of-2 multisig with second key uncompressed
# 875 : 2 : tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1) << ToByteVector(keys.pubkey0C) << OP_2 << OP_CHECKMULTISIG,
# 876 : 2 : "P2WSH CHECKMULTISIG with second key uncompressed and signing with the first key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false, WitnessMode::SH,
# 877 : 2 : 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key0C).PushWitRedeem());
# 878 : 2 : tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1) << ToByteVector(keys.pubkey0C) << OP_2 << OP_CHECKMULTISIG,
# 879 : 2 : "P2SH(P2WSH) CHECKMULTISIG second key uncompressed and signing with the first key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true, WitnessMode::SH,
# 880 : 2 : 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key0C).PushWitRedeem().PushRedeem());
# 881 : 2 : tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1) << ToByteVector(keys.pubkey0C) << OP_2 << OP_CHECKMULTISIG,
# 882 : 2 : "P2WSH CHECKMULTISIG with second key uncompressed and signing with the first key should pass as the uncompressed key is not used", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, false, WitnessMode::SH,
# 883 : 2 : 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key0C).PushWitRedeem());
# 884 : 2 : tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1) << ToByteVector(keys.pubkey0C) << OP_2 << OP_CHECKMULTISIG,
# 885 : 2 : "P2SH(P2WSH) CHECKMULTISIG with second key uncompressed and signing with the first key should pass as the uncompressed key is not used", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, true, WitnessMode::SH,
# 886 : 2 : 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key0C).PushWitRedeem().PushRedeem());
# 887 : 2 : tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1) << ToByteVector(keys.pubkey0C) << OP_2 << OP_CHECKMULTISIG,
# 888 : 2 : "P2WSH CHECKMULTISIG with second key uncompressed and signing with the second key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false, WitnessMode::SH,
# 889 : 2 : 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key1).PushWitRedeem());
# 890 : 2 : tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1) << ToByteVector(keys.pubkey0C) << OP_2 << OP_CHECKMULTISIG,
# 891 : 2 : "P2SH(P2WSH) CHECKMULTISIG with second key uncompressed and signing with the second key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true, WitnessMode::SH,
# 892 : 2 : 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key1).PushWitRedeem().PushRedeem());
# 893 : 2 : tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1) << ToByteVector(keys.pubkey0C) << OP_2 << OP_CHECKMULTISIG,
# 894 : 2 : "P2WSH CHECKMULTISIG with second key uncompressed and signing with the second key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, false, WitnessMode::SH,
# 895 : 2 : 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key1).PushWitRedeem().ScriptError(SCRIPT_ERR_WITNESS_PUBKEYTYPE));
# 896 : 2 : tests.push_back(TestBuilder(CScript() << OP_1 << ToByteVector(keys.pubkey1) << ToByteVector(keys.pubkey0C) << OP_2 << OP_CHECKMULTISIG,
# 897 : 2 : "P2SH(P2WSH) CHECKMULTISIG with second key uncompressed and signing with the second key", SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS_PUBKEYTYPE, true, WitnessMode::SH,
# 898 : 2 : 0, 1).Push(CScript()).AsWit().PushWitSig(keys.key1).PushWitRedeem().PushRedeem().ScriptError(SCRIPT_ERR_WITNESS_PUBKEYTYPE));
# 899 : :
# 900 : 2 : std::set<std::string> tests_set;
# 901 : :
# 902 : 2 : {
# 903 : 2 : UniValue json_tests = read_json(std::string(json_tests::script_tests, json_tests::script_tests + sizeof(json_tests::script_tests)));
# 904 : :
# 905 [ + + ]: 2518 : for (unsigned int idx = 0; idx < json_tests.size(); idx++) {
# 906 : 2516 : const UniValue& tv = json_tests[idx];
# 907 : 2516 : tests_set.insert(JSONPrettyPrint(tv.get_array()));
# 908 : 2516 : }
# 909 : 2 : }
# 910 : :
# 911 : : #ifdef UPDATE_JSON_TESTS
# 912 : : std::string strGen;
# 913 : : #endif
# 914 [ + + ]: 268 : for (TestBuilder& test : tests) {
# 915 : 268 : test.Test();
# 916 : 268 : std::string str = JSONPrettyPrint(test.GetJSON());
# 917 : : #ifdef UPDATE_JSON_TESTS
# 918 : : strGen += str + ",\n";
# 919 : : #else
# 920 [ - + ]: 268 : if (tests_set.count(str) == 0) {
# 921 : 0 : BOOST_CHECK_MESSAGE(false, "Missing auto script_valid test: " + test.GetComment());
# 922 : 0 : }
# 923 : 268 : #endif
# 924 : 268 : }
# 925 : :
# 926 : : #ifdef UPDATE_JSON_TESTS
# 927 : : FILE* file = fsbridge::fopen("script_tests.json.gen", "w");
# 928 : : fputs(strGen.c_str(), file);
# 929 : : fclose(file);
# 930 : : #endif
# 931 : 2 : }
# 932 : :
# 933 : : BOOST_AUTO_TEST_CASE(script_json_test)
# 934 : 2 : {
# 935 : : // Read tests from test/data/script_tests.json
# 936 : : // Format is an array of arrays
# 937 : : // Inner arrays are [ ["wit"..., nValue]?, "scriptSig", "scriptPubKey", "flags", "expected_scripterror" ]
# 938 : : // ... where scriptSig and scriptPubKey are stringified
# 939 : : // scripts.
# 940 : : // If a witness is given, then the last value in the array should be the
# 941 : : // amount (nValue) to use in the crediting tx
# 942 : 2 : UniValue tests = read_json(std::string(json_tests::script_tests, json_tests::script_tests + sizeof(json_tests::script_tests)));
# 943 : :
# 944 [ + + ]: 2518 : for (unsigned int idx = 0; idx < tests.size(); idx++) {
# 945 : 2516 : UniValue test = tests[idx];
# 946 : 2516 : std::string strTest = test.write();
# 947 : 2516 : CScriptWitness witness;
# 948 : 2516 : CAmount nValue = 0;
# 949 : 2516 : unsigned int pos = 0;
# 950 [ + - ][ + + ]: 2516 : if (test.size() > 0 && test[pos].isArray()) {
# 951 : 214 : unsigned int i=0;
# 952 [ + + ]: 650 : for (i = 0; i < test[pos].size()-1; i++) {
# 953 : 436 : witness.stack.push_back(ParseHex(test[pos][i].get_str()));
# 954 : 436 : }
# 955 : 214 : nValue = AmountFromValue(test[pos][i]);
# 956 : 214 : pos++;
# 957 : 214 : }
# 958 [ + + ]: 2516 : if (test.size() < 4 + pos) // Allow size > 3; extra stuff ignored (useful for comments)
# 959 : 102 : {
# 960 [ - + ]: 102 : if (test.size() != 1) {
# 961 : 0 : BOOST_ERROR("Bad test: " << strTest);
# 962 : 0 : }
# 963 : 102 : continue;
# 964 : 102 : }
# 965 : 2414 : std::string scriptSigString = test[pos++].get_str();
# 966 : 2414 : CScript scriptSig = ParseScript(scriptSigString);
# 967 : 2414 : std::string scriptPubKeyString = test[pos++].get_str();
# 968 : 2414 : CScript scriptPubKey = ParseScript(scriptPubKeyString);
# 969 : 2414 : unsigned int scriptflags = ParseScriptFlags(test[pos++].get_str());
# 970 : 2414 : int scriptError = ParseScriptError(test[pos++].get_str());
# 971 : :
# 972 : 2414 : DoTest(scriptPubKey, scriptSig, witness, scriptflags, strTest, scriptError, nValue);
# 973 : 2414 : }
# 974 : 2 : }
# 975 : :
# 976 : : BOOST_AUTO_TEST_CASE(script_PushData)
# 977 : 2 : {
# 978 : : // Check that PUSHDATA1, PUSHDATA2, and PUSHDATA4 create the same value on
# 979 : : // the stack as the 1-75 opcodes do.
# 980 : 2 : static const unsigned char direct[] = { 1, 0x5a };
# 981 : 2 : static const unsigned char pushdata1[] = { OP_PUSHDATA1, 1, 0x5a };
# 982 : 2 : static const unsigned char pushdata2[] = { OP_PUSHDATA2, 1, 0, 0x5a };
# 983 : 2 : static const unsigned char pushdata4[] = { OP_PUSHDATA4, 1, 0, 0, 0, 0x5a };
# 984 : :
# 985 : 2 : ScriptError err;
# 986 : 2 : std::vector<std::vector<unsigned char> > directStack;
# 987 : 2 : BOOST_CHECK(EvalScript(directStack, CScript(direct, direct + sizeof(direct)), SCRIPT_VERIFY_P2SH, BaseSignatureChecker(), SigVersion::BASE, &err));
# 988 : 2 : BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
# 989 : :
# 990 : 2 : std::vector<std::vector<unsigned char> > pushdata1Stack;
# 991 : 2 : BOOST_CHECK(EvalScript(pushdata1Stack, CScript(pushdata1, pushdata1 + sizeof(pushdata1)), SCRIPT_VERIFY_P2SH, BaseSignatureChecker(), SigVersion::BASE, &err));
# 992 : 2 : BOOST_CHECK(pushdata1Stack == directStack);
# 993 : 2 : BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
# 994 : :
# 995 : 2 : std::vector<std::vector<unsigned char> > pushdata2Stack;
# 996 : 2 : BOOST_CHECK(EvalScript(pushdata2Stack, CScript(pushdata2, pushdata2 + sizeof(pushdata2)), SCRIPT_VERIFY_P2SH, BaseSignatureChecker(), SigVersion::BASE, &err));
# 997 : 2 : BOOST_CHECK(pushdata2Stack == directStack);
# 998 : 2 : BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
# 999 : :
# 1000 : 2 : std::vector<std::vector<unsigned char> > pushdata4Stack;
# 1001 : 2 : BOOST_CHECK(EvalScript(pushdata4Stack, CScript(pushdata4, pushdata4 + sizeof(pushdata4)), SCRIPT_VERIFY_P2SH, BaseSignatureChecker(), SigVersion::BASE, &err));
# 1002 : 2 : BOOST_CHECK(pushdata4Stack == directStack);
# 1003 : 2 : BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
# 1004 : :
# 1005 : 2 : const std::vector<unsigned char> pushdata1_trunc{OP_PUSHDATA1, 1};
# 1006 : 2 : const std::vector<unsigned char> pushdata2_trunc{OP_PUSHDATA2, 1, 0};
# 1007 : 2 : const std::vector<unsigned char> pushdata4_trunc{OP_PUSHDATA4, 1, 0, 0, 0};
# 1008 : :
# 1009 : 2 : std::vector<std::vector<unsigned char>> stack_ignore;
# 1010 : 2 : BOOST_CHECK(!EvalScript(stack_ignore, CScript(pushdata1_trunc.begin(), pushdata1_trunc.end()), SCRIPT_VERIFY_P2SH, BaseSignatureChecker(), SigVersion::BASE, &err));
# 1011 : 2 : BOOST_CHECK_EQUAL(err, SCRIPT_ERR_BAD_OPCODE);
# 1012 : 2 : BOOST_CHECK(!EvalScript(stack_ignore, CScript(pushdata2_trunc.begin(), pushdata2_trunc.end()), SCRIPT_VERIFY_P2SH, BaseSignatureChecker(), SigVersion::BASE, &err));
# 1013 : 2 : BOOST_CHECK_EQUAL(err, SCRIPT_ERR_BAD_OPCODE);
# 1014 : 2 : BOOST_CHECK(!EvalScript(stack_ignore, CScript(pushdata4_trunc.begin(), pushdata4_trunc.end()), SCRIPT_VERIFY_P2SH, BaseSignatureChecker(), SigVersion::BASE, &err));
# 1015 : 2 : BOOST_CHECK_EQUAL(err, SCRIPT_ERR_BAD_OPCODE);
# 1016 : 2 : }
# 1017 : :
# 1018 : : BOOST_AUTO_TEST_CASE(script_cltv_truncated)
# 1019 : 2 : {
# 1020 : 2 : const auto script_cltv_trunc = CScript() << OP_CHECKLOCKTIMEVERIFY;
# 1021 : :
# 1022 : 2 : std::vector<std::vector<unsigned char>> stack_ignore;
# 1023 : 2 : ScriptError err;
# 1024 : 2 : BOOST_CHECK(!EvalScript(stack_ignore, script_cltv_trunc, SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY, BaseSignatureChecker(), SigVersion::BASE, &err));
# 1025 : 2 : BOOST_CHECK_EQUAL(err, SCRIPT_ERR_INVALID_STACK_OPERATION);
# 1026 : 2 : }
# 1027 : :
# 1028 : : static CScript
# 1029 : : sign_multisig(const CScript& scriptPubKey, const std::vector<CKey>& keys, const CTransaction& transaction)
# 1030 : 24 : {
# 1031 : 24 : uint256 hash = SignatureHash(scriptPubKey, transaction, 0, SIGHASH_ALL, 0, SigVersion::BASE);
# 1032 : :
# 1033 : 24 : CScript result;
# 1034 : : //
# 1035 : : // NOTE: CHECKMULTISIG has an unfortunate bug; it requires
# 1036 : : // one extra item on the stack, before the signatures.
# 1037 : : // Putting OP_0 on the stack is the workaround;
# 1038 : : // fixing the bug would mean splitting the block chain (old
# 1039 : : // clients would not accept new CHECKMULTISIG transactions,
# 1040 : : // and vice-versa)
# 1041 : : //
# 1042 : 24 : result << OP_0;
# 1043 [ + + ]: 24 : for (const CKey &key : keys)
# 1044 : 38 : {
# 1045 : 38 : std::vector<unsigned char> vchSig;
# 1046 : 38 : BOOST_CHECK(key.Sign(hash, vchSig));
# 1047 : 38 : vchSig.push_back((unsigned char)SIGHASH_ALL);
# 1048 : 38 : result << vchSig;
# 1049 : 38 : }
# 1050 : 24 : return result;
# 1051 : 24 : }
# 1052 : : static CScript
# 1053 : : sign_multisig(const CScript& scriptPubKey, const CKey& key, const CTransaction& transaction)
# 1054 : 6 : {
# 1055 : 6 : std::vector<CKey> keys;
# 1056 : 6 : keys.push_back(key);
# 1057 : 6 : return sign_multisig(scriptPubKey, keys, transaction);
# 1058 : 6 : }
# 1059 : :
# 1060 : : BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG12)
# 1061 : 2 : {
# 1062 : 2 : ScriptError err;
# 1063 : 2 : CKey key1, key2, key3;
# 1064 : 2 : key1.MakeNewKey(true);
# 1065 : 2 : key2.MakeNewKey(false);
# 1066 : 2 : key3.MakeNewKey(true);
# 1067 : :
# 1068 : 2 : CScript scriptPubKey12;
# 1069 : 2 : scriptPubKey12 << OP_1 << ToByteVector(key1.GetPubKey()) << ToByteVector(key2.GetPubKey()) << OP_2 << OP_CHECKMULTISIG;
# 1070 : :
# 1071 : 2 : const CTransaction txFrom12{BuildCreditingTransaction(scriptPubKey12)};
# 1072 : 2 : CMutableTransaction txTo12 = BuildSpendingTransaction(CScript(), CScriptWitness(), txFrom12);
# 1073 : :
# 1074 : 2 : CScript goodsig1 = sign_multisig(scriptPubKey12, key1, CTransaction(txTo12));
# 1075 : 2 : BOOST_CHECK(VerifyScript(goodsig1, scriptPubKey12, nullptr, gFlags, MutableTransactionSignatureChecker(&txTo12, 0, txFrom12.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err));
# 1076 : 2 : BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
# 1077 : 2 : txTo12.vout[0].nValue = 2;
# 1078 : 2 : BOOST_CHECK(!VerifyScript(goodsig1, scriptPubKey12, nullptr, gFlags, MutableTransactionSignatureChecker(&txTo12, 0, txFrom12.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err));
# 1079 : 2 : BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_EVAL_FALSE, ScriptErrorString(err));
# 1080 : :
# 1081 : 2 : CScript goodsig2 = sign_multisig(scriptPubKey12, key2, CTransaction(txTo12));
# 1082 : 2 : BOOST_CHECK(VerifyScript(goodsig2, scriptPubKey12, nullptr, gFlags, MutableTransactionSignatureChecker(&txTo12, 0, txFrom12.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err));
# 1083 : 2 : BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
# 1084 : :
# 1085 : 2 : CScript badsig1 = sign_multisig(scriptPubKey12, key3, CTransaction(txTo12));
# 1086 : 2 : BOOST_CHECK(!VerifyScript(badsig1, scriptPubKey12, nullptr, gFlags, MutableTransactionSignatureChecker(&txTo12, 0, txFrom12.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err));
# 1087 : 2 : BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_EVAL_FALSE, ScriptErrorString(err));
# 1088 : 2 : }
# 1089 : :
# 1090 : : BOOST_AUTO_TEST_CASE(script_CHECKMULTISIG23)
# 1091 : 2 : {
# 1092 : 2 : ScriptError err;
# 1093 : 2 : CKey key1, key2, key3, key4;
# 1094 : 2 : key1.MakeNewKey(true);
# 1095 : 2 : key2.MakeNewKey(false);
# 1096 : 2 : key3.MakeNewKey(true);
# 1097 : 2 : key4.MakeNewKey(false);
# 1098 : :
# 1099 : 2 : CScript scriptPubKey23;
# 1100 : 2 : scriptPubKey23 << OP_2 << ToByteVector(key1.GetPubKey()) << ToByteVector(key2.GetPubKey()) << ToByteVector(key3.GetPubKey()) << OP_3 << OP_CHECKMULTISIG;
# 1101 : :
# 1102 : 2 : const CTransaction txFrom23{BuildCreditingTransaction(scriptPubKey23)};
# 1103 : 2 : CMutableTransaction txTo23 = BuildSpendingTransaction(CScript(), CScriptWitness(), txFrom23);
# 1104 : :
# 1105 : 2 : std::vector<CKey> keys;
# 1106 : 2 : keys.push_back(key1); keys.push_back(key2);
# 1107 : 2 : CScript goodsig1 = sign_multisig(scriptPubKey23, keys, CTransaction(txTo23));
# 1108 : 2 : BOOST_CHECK(VerifyScript(goodsig1, scriptPubKey23, nullptr, gFlags, MutableTransactionSignatureChecker(&txTo23, 0, txFrom23.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err));
# 1109 : 2 : BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
# 1110 : :
# 1111 : 2 : keys.clear();
# 1112 : 2 : keys.push_back(key1); keys.push_back(key3);
# 1113 : 2 : CScript goodsig2 = sign_multisig(scriptPubKey23, keys, CTransaction(txTo23));
# 1114 : 2 : BOOST_CHECK(VerifyScript(goodsig2, scriptPubKey23, nullptr, gFlags, MutableTransactionSignatureChecker(&txTo23, 0, txFrom23.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err));
# 1115 : 2 : BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
# 1116 : :
# 1117 : 2 : keys.clear();
# 1118 : 2 : keys.push_back(key2); keys.push_back(key3);
# 1119 : 2 : CScript goodsig3 = sign_multisig(scriptPubKey23, keys, CTransaction(txTo23));
# 1120 : 2 : BOOST_CHECK(VerifyScript(goodsig3, scriptPubKey23, nullptr, gFlags, MutableTransactionSignatureChecker(&txTo23, 0, txFrom23.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err));
# 1121 : 2 : BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
# 1122 : :
# 1123 : 2 : keys.clear();
# 1124 : 2 : keys.push_back(key2); keys.push_back(key2); // Can't re-use sig
# 1125 : 2 : CScript badsig1 = sign_multisig(scriptPubKey23, keys, CTransaction(txTo23));
# 1126 : 2 : BOOST_CHECK(!VerifyScript(badsig1, scriptPubKey23, nullptr, gFlags, MutableTransactionSignatureChecker(&txTo23, 0, txFrom23.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err));
# 1127 : 2 : BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_EVAL_FALSE, ScriptErrorString(err));
# 1128 : :
# 1129 : 2 : keys.clear();
# 1130 : 2 : keys.push_back(key2); keys.push_back(key1); // sigs must be in correct order
# 1131 : 2 : CScript badsig2 = sign_multisig(scriptPubKey23, keys, CTransaction(txTo23));
# 1132 : 2 : BOOST_CHECK(!VerifyScript(badsig2, scriptPubKey23, nullptr, gFlags, MutableTransactionSignatureChecker(&txTo23, 0, txFrom23.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err));
# 1133 : 2 : BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_EVAL_FALSE, ScriptErrorString(err));
# 1134 : :
# 1135 : 2 : keys.clear();
# 1136 : 2 : keys.push_back(key3); keys.push_back(key2); // sigs must be in correct order
# 1137 : 2 : CScript badsig3 = sign_multisig(scriptPubKey23, keys, CTransaction(txTo23));
# 1138 : 2 : BOOST_CHECK(!VerifyScript(badsig3, scriptPubKey23, nullptr, gFlags, MutableTransactionSignatureChecker(&txTo23, 0, txFrom23.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err));
# 1139 : 2 : BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_EVAL_FALSE, ScriptErrorString(err));
# 1140 : :
# 1141 : 2 : keys.clear();
# 1142 : 2 : keys.push_back(key4); keys.push_back(key2); // sigs must match pubkeys
# 1143 : 2 : CScript badsig4 = sign_multisig(scriptPubKey23, keys, CTransaction(txTo23));
# 1144 : 2 : BOOST_CHECK(!VerifyScript(badsig4, scriptPubKey23, nullptr, gFlags, MutableTransactionSignatureChecker(&txTo23, 0, txFrom23.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err));
# 1145 : 2 : BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_EVAL_FALSE, ScriptErrorString(err));
# 1146 : :
# 1147 : 2 : keys.clear();
# 1148 : 2 : keys.push_back(key1); keys.push_back(key4); // sigs must match pubkeys
# 1149 : 2 : CScript badsig5 = sign_multisig(scriptPubKey23, keys, CTransaction(txTo23));
# 1150 : 2 : BOOST_CHECK(!VerifyScript(badsig5, scriptPubKey23, nullptr, gFlags, MutableTransactionSignatureChecker(&txTo23, 0, txFrom23.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err));
# 1151 : 2 : BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_EVAL_FALSE, ScriptErrorString(err));
# 1152 : :
# 1153 : 2 : keys.clear(); // Must have signatures
# 1154 : 2 : CScript badsig6 = sign_multisig(scriptPubKey23, keys, CTransaction(txTo23));
# 1155 : 2 : BOOST_CHECK(!VerifyScript(badsig6, scriptPubKey23, nullptr, gFlags, MutableTransactionSignatureChecker(&txTo23, 0, txFrom23.vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &err));
# 1156 : 2 : BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_INVALID_STACK_OPERATION, ScriptErrorString(err));
# 1157 : 2 : }
# 1158 : :
# 1159 : : /* Wrapper around ProduceSignature to combine two scriptsigs */
# 1160 : : SignatureData CombineSignatures(const CTxOut& txout, const CMutableTransaction& tx, const SignatureData& scriptSig1, const SignatureData& scriptSig2)
# 1161 : 34 : {
# 1162 : 34 : SignatureData data;
# 1163 : 34 : data.MergeSignatureData(scriptSig1);
# 1164 : 34 : data.MergeSignatureData(scriptSig2);
# 1165 : 34 : ProduceSignature(DUMMY_SIGNING_PROVIDER, MutableTransactionSignatureCreator(&tx, 0, txout.nValue, SIGHASH_DEFAULT), txout.scriptPubKey, data);
# 1166 : 34 : return data;
# 1167 : 34 : }
# 1168 : :
# 1169 : : BOOST_AUTO_TEST_CASE(script_combineSigs)
# 1170 : 2 : {
# 1171 : : // Test the ProduceSignature's ability to combine signatures function
# 1172 : 2 : FillableSigningProvider keystore;
# 1173 : 2 : std::vector<CKey> keys;
# 1174 : 2 : std::vector<CPubKey> pubkeys;
# 1175 [ + + ]: 8 : for (int i = 0; i < 3; i++)
# 1176 : 6 : {
# 1177 : 6 : CKey key;
# 1178 : 6 : key.MakeNewKey(i%2 == 1);
# 1179 : 6 : keys.push_back(key);
# 1180 : 6 : pubkeys.push_back(key.GetPubKey());
# 1181 : 6 : BOOST_CHECK(keystore.AddKey(key));
# 1182 : 6 : }
# 1183 : :
# 1184 : 2 : CMutableTransaction txFrom = BuildCreditingTransaction(GetScriptForDestination(PKHash(keys[0].GetPubKey())));
# 1185 : 2 : CMutableTransaction txTo = BuildSpendingTransaction(CScript(), CScriptWitness(), CTransaction(txFrom));
# 1186 : 2 : CScript& scriptPubKey = txFrom.vout[0].scriptPubKey;
# 1187 : 2 : SignatureData scriptSig;
# 1188 : :
# 1189 : 2 : SignatureData empty;
# 1190 : 2 : SignatureData combined = CombineSignatures(txFrom.vout[0], txTo, empty, empty);
# 1191 : 2 : BOOST_CHECK(combined.scriptSig.empty());
# 1192 : :
# 1193 : : // Single signature case:
# 1194 : 2 : BOOST_CHECK(SignSignature(keystore, CTransaction(txFrom), txTo, 0, SIGHASH_ALL)); // changes scriptSig
# 1195 : 2 : scriptSig = DataFromTransaction(txTo, 0, txFrom.vout[0]);
# 1196 : 2 : combined = CombineSignatures(txFrom.vout[0], txTo, scriptSig, empty);
# 1197 : 2 : BOOST_CHECK(combined.scriptSig == scriptSig.scriptSig);
# 1198 : 2 : combined = CombineSignatures(txFrom.vout[0], txTo, empty, scriptSig);
# 1199 : 2 : BOOST_CHECK(combined.scriptSig == scriptSig.scriptSig);
# 1200 : 2 : SignatureData scriptSigCopy = scriptSig;
# 1201 : : // Signing again will give a different, valid signature:
# 1202 : 2 : BOOST_CHECK(SignSignature(keystore, CTransaction(txFrom), txTo, 0, SIGHASH_ALL));
# 1203 : 2 : scriptSig = DataFromTransaction(txTo, 0, txFrom.vout[0]);
# 1204 : 2 : combined = CombineSignatures(txFrom.vout[0], txTo, scriptSigCopy, scriptSig);
# 1205 : 2 : BOOST_CHECK(combined.scriptSig == scriptSigCopy.scriptSig || combined.scriptSig == scriptSig.scriptSig);
# 1206 : :
# 1207 : : // P2SH, single-signature case:
# 1208 : 2 : CScript pkSingle; pkSingle << ToByteVector(keys[0].GetPubKey()) << OP_CHECKSIG;
# 1209 : 2 : BOOST_CHECK(keystore.AddCScript(pkSingle));
# 1210 : 2 : scriptPubKey = GetScriptForDestination(ScriptHash(pkSingle));
# 1211 : 2 : BOOST_CHECK(SignSignature(keystore, CTransaction(txFrom), txTo, 0, SIGHASH_ALL));
# 1212 : 2 : scriptSig = DataFromTransaction(txTo, 0, txFrom.vout[0]);
# 1213 : 2 : combined = CombineSignatures(txFrom.vout[0], txTo, scriptSig, empty);
# 1214 : 2 : BOOST_CHECK(combined.scriptSig == scriptSig.scriptSig);
# 1215 : 2 : combined = CombineSignatures(txFrom.vout[0], txTo, empty, scriptSig);
# 1216 : 2 : BOOST_CHECK(combined.scriptSig == scriptSig.scriptSig);
# 1217 : 2 : scriptSigCopy = scriptSig;
# 1218 : 2 : BOOST_CHECK(SignSignature(keystore, CTransaction(txFrom), txTo, 0, SIGHASH_ALL));
# 1219 : 2 : scriptSig = DataFromTransaction(txTo, 0, txFrom.vout[0]);
# 1220 : 2 : combined = CombineSignatures(txFrom.vout[0], txTo, scriptSigCopy, scriptSig);
# 1221 : 2 : BOOST_CHECK(combined.scriptSig == scriptSigCopy.scriptSig || combined.scriptSig == scriptSig.scriptSig);
# 1222 : :
# 1223 : : // Hardest case: Multisig 2-of-3
# 1224 : 2 : scriptPubKey = GetScriptForMultisig(2, pubkeys);
# 1225 : 2 : BOOST_CHECK(keystore.AddCScript(scriptPubKey));
# 1226 : 2 : BOOST_CHECK(SignSignature(keystore, CTransaction(txFrom), txTo, 0, SIGHASH_ALL));
# 1227 : 2 : scriptSig = DataFromTransaction(txTo, 0, txFrom.vout[0]);
# 1228 : 2 : combined = CombineSignatures(txFrom.vout[0], txTo, scriptSig, empty);
# 1229 : 2 : BOOST_CHECK(combined.scriptSig == scriptSig.scriptSig);
# 1230 : 2 : combined = CombineSignatures(txFrom.vout[0], txTo, empty, scriptSig);
# 1231 : 2 : BOOST_CHECK(combined.scriptSig == scriptSig.scriptSig);
# 1232 : :
# 1233 : : // A couple of partially-signed versions:
# 1234 : 2 : std::vector<unsigned char> sig1;
# 1235 : 2 : uint256 hash1 = SignatureHash(scriptPubKey, txTo, 0, SIGHASH_ALL, 0, SigVersion::BASE);
# 1236 : 2 : BOOST_CHECK(keys[0].Sign(hash1, sig1));
# 1237 : 2 : sig1.push_back(SIGHASH_ALL);
# 1238 : 2 : std::vector<unsigned char> sig2;
# 1239 : 2 : uint256 hash2 = SignatureHash(scriptPubKey, txTo, 0, SIGHASH_NONE, 0, SigVersion::BASE);
# 1240 : 2 : BOOST_CHECK(keys[1].Sign(hash2, sig2));
# 1241 : 2 : sig2.push_back(SIGHASH_NONE);
# 1242 : 2 : std::vector<unsigned char> sig3;
# 1243 : 2 : uint256 hash3 = SignatureHash(scriptPubKey, txTo, 0, SIGHASH_SINGLE, 0, SigVersion::BASE);
# 1244 : 2 : BOOST_CHECK(keys[2].Sign(hash3, sig3));
# 1245 : 2 : sig3.push_back(SIGHASH_SINGLE);
# 1246 : :
# 1247 : : // Not fussy about order (or even existence) of placeholders or signatures:
# 1248 : 2 : CScript partial1a = CScript() << OP_0 << sig1 << OP_0;
# 1249 : 2 : CScript partial1b = CScript() << OP_0 << OP_0 << sig1;
# 1250 : 2 : CScript partial2a = CScript() << OP_0 << sig2;
# 1251 : 2 : CScript partial2b = CScript() << sig2 << OP_0;
# 1252 : 2 : CScript partial3a = CScript() << sig3;
# 1253 : 2 : CScript partial3b = CScript() << OP_0 << OP_0 << sig3;
# 1254 : 2 : CScript partial3c = CScript() << OP_0 << sig3 << OP_0;
# 1255 : 2 : CScript complete12 = CScript() << OP_0 << sig1 << sig2;
# 1256 : 2 : CScript complete13 = CScript() << OP_0 << sig1 << sig3;
# 1257 : 2 : CScript complete23 = CScript() << OP_0 << sig2 << sig3;
# 1258 : 2 : SignatureData partial1_sigs;
# 1259 : 2 : partial1_sigs.signatures.emplace(keys[0].GetPubKey().GetID(), SigPair(keys[0].GetPubKey(), sig1));
# 1260 : 2 : SignatureData partial2_sigs;
# 1261 : 2 : partial2_sigs.signatures.emplace(keys[1].GetPubKey().GetID(), SigPair(keys[1].GetPubKey(), sig2));
# 1262 : 2 : SignatureData partial3_sigs;
# 1263 : 2 : partial3_sigs.signatures.emplace(keys[2].GetPubKey().GetID(), SigPair(keys[2].GetPubKey(), sig3));
# 1264 : :
# 1265 : 2 : combined = CombineSignatures(txFrom.vout[0], txTo, partial1_sigs, partial1_sigs);
# 1266 : 2 : BOOST_CHECK(combined.scriptSig == partial1a);
# 1267 : 2 : combined = CombineSignatures(txFrom.vout[0], txTo, partial1_sigs, partial2_sigs);
# 1268 : 2 : BOOST_CHECK(combined.scriptSig == complete12);
# 1269 : 2 : combined = CombineSignatures(txFrom.vout[0], txTo, partial2_sigs, partial1_sigs);
# 1270 : 2 : BOOST_CHECK(combined.scriptSig == complete12);
# 1271 : 2 : combined = CombineSignatures(txFrom.vout[0], txTo, partial1_sigs, partial2_sigs);
# 1272 : 2 : BOOST_CHECK(combined.scriptSig == complete12);
# 1273 : 2 : combined = CombineSignatures(txFrom.vout[0], txTo, partial3_sigs, partial1_sigs);
# 1274 : 2 : BOOST_CHECK(combined.scriptSig == complete13);
# 1275 : 2 : combined = CombineSignatures(txFrom.vout[0], txTo, partial2_sigs, partial3_sigs);
# 1276 : 2 : BOOST_CHECK(combined.scriptSig == complete23);
# 1277 : 2 : combined = CombineSignatures(txFrom.vout[0], txTo, partial3_sigs, partial2_sigs);
# 1278 : 2 : BOOST_CHECK(combined.scriptSig == complete23);
# 1279 : 2 : combined = CombineSignatures(txFrom.vout[0], txTo, partial3_sigs, partial3_sigs);
# 1280 : 2 : BOOST_CHECK(combined.scriptSig == partial3c);
# 1281 : 2 : }
# 1282 : :
# 1283 : : BOOST_AUTO_TEST_CASE(script_standard_push)
# 1284 : 2 : {
# 1285 : 2 : ScriptError err;
# 1286 [ + + ]: 134002 : for (int i=0; i<67000; i++) {
# 1287 : 134000 : CScript script;
# 1288 : 134000 : script << i;
# 1289 : 134000 : BOOST_CHECK_MESSAGE(script.IsPushOnly(), "Number " << i << " is not pure push.");
# 1290 : 134000 : BOOST_CHECK_MESSAGE(VerifyScript(script, CScript() << OP_1, nullptr, SCRIPT_VERIFY_MINIMALDATA, BaseSignatureChecker(), &err), "Number " << i << " push is not minimal data.");
# 1291 : 134000 : BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
# 1292 : 134000 : }
# 1293 : :
# 1294 [ + + ]: 1044 : for (unsigned int i=0; i<=MAX_SCRIPT_ELEMENT_SIZE; i++) {
# 1295 : 1042 : std::vector<unsigned char> data(i, '\111');
# 1296 : 1042 : CScript script;
# 1297 : 1042 : script << data;
# 1298 : 1042 : BOOST_CHECK_MESSAGE(script.IsPushOnly(), "Length " << i << " is not pure push.");
# 1299 : 1042 : BOOST_CHECK_MESSAGE(VerifyScript(script, CScript() << OP_1, nullptr, SCRIPT_VERIFY_MINIMALDATA, BaseSignatureChecker(), &err), "Length " << i << " push is not minimal data.");
# 1300 : 1042 : BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
# 1301 : 1042 : }
# 1302 : 2 : }
# 1303 : :
# 1304 : : BOOST_AUTO_TEST_CASE(script_IsPushOnly_on_invalid_scripts)
# 1305 : 2 : {
# 1306 : : // IsPushOnly returns false when given a script containing only pushes that
# 1307 : : // are invalid due to truncation. IsPushOnly() is consensus critical
# 1308 : : // because P2SH evaluation uses it, although this specific behavior should
# 1309 : : // not be consensus critical as the P2SH evaluation would fail first due to
# 1310 : : // the invalid push. Still, it doesn't hurt to test it explicitly.
# 1311 : 2 : static const unsigned char direct[] = { 1 };
# 1312 : 2 : BOOST_CHECK(!CScript(direct, direct+sizeof(direct)).IsPushOnly());
# 1313 : 2 : }
# 1314 : :
# 1315 : : BOOST_AUTO_TEST_CASE(script_GetScriptAsm)
# 1316 : 2 : {
# 1317 : 2 : BOOST_CHECK_EQUAL("OP_CHECKLOCKTIMEVERIFY", ScriptToAsmStr(CScript() << OP_NOP2, true));
# 1318 : 2 : BOOST_CHECK_EQUAL("OP_CHECKLOCKTIMEVERIFY", ScriptToAsmStr(CScript() << OP_CHECKLOCKTIMEVERIFY, true));
# 1319 : 2 : BOOST_CHECK_EQUAL("OP_CHECKLOCKTIMEVERIFY", ScriptToAsmStr(CScript() << OP_NOP2));
# 1320 : 2 : BOOST_CHECK_EQUAL("OP_CHECKLOCKTIMEVERIFY", ScriptToAsmStr(CScript() << OP_CHECKLOCKTIMEVERIFY));
# 1321 : :
# 1322 : 2 : std::string derSig("304502207fa7a6d1e0ee81132a269ad84e68d695483745cde8b541e3bf630749894e342a022100c1f7ab20e13e22fb95281a870f3dcf38d782e53023ee313d741ad0cfbc0c5090");
# 1323 : 2 : std::string pubKey("03b0da749730dc9b4b1f4a14d6902877a92541f5368778853d9c4a0cb7802dcfb2");
# 1324 : 2 : std::vector<unsigned char> vchPubKey = ToByteVector(ParseHex(pubKey));
# 1325 : :
# 1326 : 2 : BOOST_CHECK_EQUAL(derSig + "00 " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "00")) << vchPubKey, true));
# 1327 : 2 : BOOST_CHECK_EQUAL(derSig + "80 " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "80")) << vchPubKey, true));
# 1328 : 2 : BOOST_CHECK_EQUAL(derSig + "[ALL] " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "01")) << vchPubKey, true));
# 1329 : 2 : BOOST_CHECK_EQUAL(derSig + "[NONE] " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "02")) << vchPubKey, true));
# 1330 : 2 : BOOST_CHECK_EQUAL(derSig + "[SINGLE] " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "03")) << vchPubKey, true));
# 1331 : 2 : BOOST_CHECK_EQUAL(derSig + "[ALL|ANYONECANPAY] " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "81")) << vchPubKey, true));
# 1332 : 2 : BOOST_CHECK_EQUAL(derSig + "[NONE|ANYONECANPAY] " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "82")) << vchPubKey, true));
# 1333 : 2 : BOOST_CHECK_EQUAL(derSig + "[SINGLE|ANYONECANPAY] " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "83")) << vchPubKey, true));
# 1334 : :
# 1335 : 2 : BOOST_CHECK_EQUAL(derSig + "00 " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "00")) << vchPubKey));
# 1336 : 2 : BOOST_CHECK_EQUAL(derSig + "80 " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "80")) << vchPubKey));
# 1337 : 2 : BOOST_CHECK_EQUAL(derSig + "01 " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "01")) << vchPubKey));
# 1338 : 2 : BOOST_CHECK_EQUAL(derSig + "02 " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "02")) << vchPubKey));
# 1339 : 2 : BOOST_CHECK_EQUAL(derSig + "03 " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "03")) << vchPubKey));
# 1340 : 2 : BOOST_CHECK_EQUAL(derSig + "81 " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "81")) << vchPubKey));
# 1341 : 2 : BOOST_CHECK_EQUAL(derSig + "82 " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "82")) << vchPubKey));
# 1342 : 2 : BOOST_CHECK_EQUAL(derSig + "83 " + pubKey, ScriptToAsmStr(CScript() << ToByteVector(ParseHex(derSig + "83")) << vchPubKey));
# 1343 : 2 : }
# 1344 : :
# 1345 : : static CScript ScriptFromHex(const std::string& str)
# 1346 : 60 : {
# 1347 : 60 : std::vector<unsigned char> data = ParseHex(str);
# 1348 : 60 : return CScript(data.begin(), data.end());
# 1349 : 60 : }
# 1350 : :
# 1351 : : BOOST_AUTO_TEST_CASE(script_FindAndDelete)
# 1352 : 2 : {
# 1353 : : // Exercise the FindAndDelete functionality
# 1354 : 2 : CScript s;
# 1355 : 2 : CScript d;
# 1356 : 2 : CScript expect;
# 1357 : :
# 1358 : 2 : s = CScript() << OP_1 << OP_2;
# 1359 : 2 : d = CScript(); // delete nothing should be a no-op
# 1360 : 2 : expect = s;
# 1361 : 2 : BOOST_CHECK_EQUAL(FindAndDelete(s, d), 0);
# 1362 : 2 : BOOST_CHECK(s == expect);
# 1363 : :
# 1364 : 2 : s = CScript() << OP_1 << OP_2 << OP_3;
# 1365 : 2 : d = CScript() << OP_2;
# 1366 : 2 : expect = CScript() << OP_1 << OP_3;
# 1367 : 2 : BOOST_CHECK_EQUAL(FindAndDelete(s, d), 1);
# 1368 : 2 : BOOST_CHECK(s == expect);
# 1369 : :
# 1370 : 2 : s = CScript() << OP_3 << OP_1 << OP_3 << OP_3 << OP_4 << OP_3;
# 1371 : 2 : d = CScript() << OP_3;
# 1372 : 2 : expect = CScript() << OP_1 << OP_4;
# 1373 : 2 : BOOST_CHECK_EQUAL(FindAndDelete(s, d), 4);
# 1374 : 2 : BOOST_CHECK(s == expect);
# 1375 : :
# 1376 : 2 : s = ScriptFromHex("0302ff03"); // PUSH 0x02ff03 onto stack
# 1377 : 2 : d = ScriptFromHex("0302ff03");
# 1378 : 2 : expect = CScript();
# 1379 : 2 : BOOST_CHECK_EQUAL(FindAndDelete(s, d), 1);
# 1380 : 2 : BOOST_CHECK(s == expect);
# 1381 : :
# 1382 : 2 : s = ScriptFromHex("0302ff030302ff03"); // PUSH 0x2ff03 PUSH 0x2ff03
# 1383 : 2 : d = ScriptFromHex("0302ff03");
# 1384 : 2 : expect = CScript();
# 1385 : 2 : BOOST_CHECK_EQUAL(FindAndDelete(s, d), 2);
# 1386 : 2 : BOOST_CHECK(s == expect);
# 1387 : :
# 1388 : 2 : s = ScriptFromHex("0302ff030302ff03");
# 1389 : 2 : d = ScriptFromHex("02");
# 1390 : 2 : expect = s; // FindAndDelete matches entire opcodes
# 1391 : 2 : BOOST_CHECK_EQUAL(FindAndDelete(s, d), 0);
# 1392 : 2 : BOOST_CHECK(s == expect);
# 1393 : :
# 1394 : 2 : s = ScriptFromHex("0302ff030302ff03");
# 1395 : 2 : d = ScriptFromHex("ff");
# 1396 : 2 : expect = s;
# 1397 : 2 : BOOST_CHECK_EQUAL(FindAndDelete(s, d), 0);
# 1398 : 2 : BOOST_CHECK(s == expect);
# 1399 : :
# 1400 : : // This is an odd edge case: strip of the push-three-bytes
# 1401 : : // prefix, leaving 02ff03 which is push-two-bytes:
# 1402 : 2 : s = ScriptFromHex("0302ff030302ff03");
# 1403 : 2 : d = ScriptFromHex("03");
# 1404 : 2 : expect = CScript() << ParseHex("ff03") << ParseHex("ff03");
# 1405 : 2 : BOOST_CHECK_EQUAL(FindAndDelete(s, d), 2);
# 1406 : 2 : BOOST_CHECK(s == expect);
# 1407 : :
# 1408 : : // Byte sequence that spans multiple opcodes:
# 1409 : 2 : s = ScriptFromHex("02feed5169"); // PUSH(0xfeed) OP_1 OP_VERIFY
# 1410 : 2 : d = ScriptFromHex("feed51");
# 1411 : 2 : expect = s;
# 1412 : 2 : BOOST_CHECK_EQUAL(FindAndDelete(s, d), 0); // doesn't match 'inside' opcodes
# 1413 : 2 : BOOST_CHECK(s == expect);
# 1414 : :
# 1415 : 2 : s = ScriptFromHex("02feed5169"); // PUSH(0xfeed) OP_1 OP_VERIFY
# 1416 : 2 : d = ScriptFromHex("02feed51");
# 1417 : 2 : expect = ScriptFromHex("69");
# 1418 : 2 : BOOST_CHECK_EQUAL(FindAndDelete(s, d), 1);
# 1419 : 2 : BOOST_CHECK(s == expect);
# 1420 : :
# 1421 : 2 : s = ScriptFromHex("516902feed5169");
# 1422 : 2 : d = ScriptFromHex("feed51");
# 1423 : 2 : expect = s;
# 1424 : 2 : BOOST_CHECK_EQUAL(FindAndDelete(s, d), 0);
# 1425 : 2 : BOOST_CHECK(s == expect);
# 1426 : :
# 1427 : 2 : s = ScriptFromHex("516902feed5169");
# 1428 : 2 : d = ScriptFromHex("02feed51");
# 1429 : 2 : expect = ScriptFromHex("516969");
# 1430 : 2 : BOOST_CHECK_EQUAL(FindAndDelete(s, d), 1);
# 1431 : 2 : BOOST_CHECK(s == expect);
# 1432 : :
# 1433 : 2 : s = CScript() << OP_0 << OP_0 << OP_1 << OP_1;
# 1434 : 2 : d = CScript() << OP_0 << OP_1;
# 1435 : 2 : expect = CScript() << OP_0 << OP_1; // FindAndDelete is single-pass
# 1436 : 2 : BOOST_CHECK_EQUAL(FindAndDelete(s, d), 1);
# 1437 : 2 : BOOST_CHECK(s == expect);
# 1438 : :
# 1439 : 2 : s = CScript() << OP_0 << OP_0 << OP_1 << OP_0 << OP_1 << OP_1;
# 1440 : 2 : d = CScript() << OP_0 << OP_1;
# 1441 : 2 : expect = CScript() << OP_0 << OP_1; // FindAndDelete is single-pass
# 1442 : 2 : BOOST_CHECK_EQUAL(FindAndDelete(s, d), 2);
# 1443 : 2 : BOOST_CHECK(s == expect);
# 1444 : :
# 1445 : : // Another weird edge case:
# 1446 : : // End with invalid push (not enough data)...
# 1447 : 2 : s = ScriptFromHex("0003feed");
# 1448 : 2 : d = ScriptFromHex("03feed"); // ... can remove the invalid push
# 1449 : 2 : expect = ScriptFromHex("00");
# 1450 : 2 : BOOST_CHECK_EQUAL(FindAndDelete(s, d), 1);
# 1451 : 2 : BOOST_CHECK(s == expect);
# 1452 : :
# 1453 : 2 : s = ScriptFromHex("0003feed");
# 1454 : 2 : d = ScriptFromHex("00");
# 1455 : 2 : expect = ScriptFromHex("03feed");
# 1456 : 2 : BOOST_CHECK_EQUAL(FindAndDelete(s, d), 1);
# 1457 : 2 : BOOST_CHECK(s == expect);
# 1458 : 2 : }
# 1459 : :
# 1460 : : BOOST_AUTO_TEST_CASE(script_HasValidOps)
# 1461 : 2 : {
# 1462 : : // Exercise the HasValidOps functionality
# 1463 : 2 : CScript script;
# 1464 : 2 : script = ScriptFromHex("76a9141234567890abcdefa1a2a3a4a5a6a7a8a9a0aaab88ac"); // Normal script
# 1465 : 2 : BOOST_CHECK(script.HasValidOps());
# 1466 : 2 : script = ScriptFromHex("76a914ff34567890abcdefa1a2a3a4a5a6a7a8a9a0aaab88ac");
# 1467 : 2 : BOOST_CHECK(script.HasValidOps());
# 1468 : 2 : script = ScriptFromHex("ff88ac"); // Script with OP_INVALIDOPCODE explicit
# 1469 : 2 : BOOST_CHECK(!script.HasValidOps());
# 1470 : 2 : script = ScriptFromHex("88acc0"); // Script with undefined opcode
# 1471 : 2 : BOOST_CHECK(!script.HasValidOps());
# 1472 : 2 : }
# 1473 : :
# 1474 : : static CMutableTransaction TxFromHex(const std::string& str)
# 1475 : 0 : {
# 1476 : 0 : CMutableTransaction tx;
# 1477 : 0 : SpanReader{SER_DISK, SERIALIZE_TRANSACTION_NO_WITNESS, ParseHex(str)} >> tx;
# 1478 : 0 : return tx;
# 1479 : 0 : }
# 1480 : :
# 1481 : : static std::vector<CTxOut> TxOutsFromJSON(const UniValue& univalue)
# 1482 : 0 : {
# 1483 : 0 : assert(univalue.isArray());
# 1484 : 0 : std::vector<CTxOut> prevouts;
# 1485 [ # # ]: 0 : for (size_t i = 0; i < univalue.size(); ++i) {
# 1486 : 0 : CTxOut txout;
# 1487 : 0 : SpanReader{SER_DISK, 0, ParseHex(univalue[i].get_str())} >> txout;
# 1488 : 0 : prevouts.push_back(std::move(txout));
# 1489 : 0 : }
# 1490 : 0 : return prevouts;
# 1491 : 0 : }
# 1492 : :
# 1493 : : static CScriptWitness ScriptWitnessFromJSON(const UniValue& univalue)
# 1494 : 0 : {
# 1495 : 0 : assert(univalue.isArray());
# 1496 : 0 : CScriptWitness scriptwitness;
# 1497 [ # # ]: 0 : for (size_t i = 0; i < univalue.size(); ++i) {
# 1498 : 0 : auto bytes = ParseHex(univalue[i].get_str());
# 1499 : 0 : scriptwitness.stack.push_back(std::move(bytes));
# 1500 : 0 : }
# 1501 : 0 : return scriptwitness;
# 1502 : 0 : }
# 1503 : :
# 1504 : : #if defined(HAVE_CONSENSUS_LIB)
# 1505 : :
# 1506 : : /* Test simple (successful) usage of bitcoinconsensus_verify_script */
# 1507 : : BOOST_AUTO_TEST_CASE(bitcoinconsensus_verify_script_returns_true)
# 1508 : 2 : {
# 1509 : 2 : unsigned int libconsensus_flags = 0;
# 1510 : 2 : int nIn = 0;
# 1511 : :
# 1512 : 2 : CScript scriptPubKey;
# 1513 : 2 : CScript scriptSig;
# 1514 : 2 : CScriptWitness wit;
# 1515 : :
# 1516 : 2 : scriptPubKey << OP_1;
# 1517 : 2 : CTransaction creditTx = BuildCreditingTransaction(scriptPubKey, 1);
# 1518 : 2 : CTransaction spendTx = BuildSpendingTransaction(scriptSig, wit, creditTx);
# 1519 : :
# 1520 : 2 : CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
# 1521 : 2 : stream << spendTx;
# 1522 : :
# 1523 : 2 : bitcoinconsensus_error err;
# 1524 : 2 : int result = bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), UCharCast(stream.data()), stream.size(), nIn, libconsensus_flags, &err);
# 1525 : 2 : BOOST_CHECK_EQUAL(result, 1);
# 1526 : 2 : BOOST_CHECK_EQUAL(err, bitcoinconsensus_ERR_OK);
# 1527 : 2 : }
# 1528 : :
# 1529 : : /* Test bitcoinconsensus_verify_script returns invalid tx index err*/
# 1530 : : BOOST_AUTO_TEST_CASE(bitcoinconsensus_verify_script_tx_index_err)
# 1531 : 2 : {
# 1532 : 2 : unsigned int libconsensus_flags = 0;
# 1533 : 2 : int nIn = 3;
# 1534 : :
# 1535 : 2 : CScript scriptPubKey;
# 1536 : 2 : CScript scriptSig;
# 1537 : 2 : CScriptWitness wit;
# 1538 : :
# 1539 : 2 : scriptPubKey << OP_EQUAL;
# 1540 : 2 : CTransaction creditTx = BuildCreditingTransaction(scriptPubKey, 1);
# 1541 : 2 : CTransaction spendTx = BuildSpendingTransaction(scriptSig, wit, creditTx);
# 1542 : :
# 1543 : 2 : CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
# 1544 : 2 : stream << spendTx;
# 1545 : :
# 1546 : 2 : bitcoinconsensus_error err;
# 1547 : 2 : int result = bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), UCharCast(stream.data()), stream.size(), nIn, libconsensus_flags, &err);
# 1548 : 2 : BOOST_CHECK_EQUAL(result, 0);
# 1549 : 2 : BOOST_CHECK_EQUAL(err, bitcoinconsensus_ERR_TX_INDEX);
# 1550 : 2 : }
# 1551 : :
# 1552 : : /* Test bitcoinconsensus_verify_script returns tx size mismatch err*/
# 1553 : : BOOST_AUTO_TEST_CASE(bitcoinconsensus_verify_script_tx_size)
# 1554 : 2 : {
# 1555 : 2 : unsigned int libconsensus_flags = 0;
# 1556 : 2 : int nIn = 0;
# 1557 : :
# 1558 : 2 : CScript scriptPubKey;
# 1559 : 2 : CScript scriptSig;
# 1560 : 2 : CScriptWitness wit;
# 1561 : :
# 1562 : 2 : scriptPubKey << OP_EQUAL;
# 1563 : 2 : CTransaction creditTx = BuildCreditingTransaction(scriptPubKey, 1);
# 1564 : 2 : CTransaction spendTx = BuildSpendingTransaction(scriptSig, wit, creditTx);
# 1565 : :
# 1566 : 2 : CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
# 1567 : 2 : stream << spendTx;
# 1568 : :
# 1569 : 2 : bitcoinconsensus_error err;
# 1570 : 2 : int result = bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), UCharCast(stream.data()), stream.size() * 2, nIn, libconsensus_flags, &err);
# 1571 : 2 : BOOST_CHECK_EQUAL(result, 0);
# 1572 : 2 : BOOST_CHECK_EQUAL(err, bitcoinconsensus_ERR_TX_SIZE_MISMATCH);
# 1573 : 2 : }
# 1574 : :
# 1575 : : /* Test bitcoinconsensus_verify_script returns invalid tx serialization error */
# 1576 : : BOOST_AUTO_TEST_CASE(bitcoinconsensus_verify_script_tx_serialization)
# 1577 : 2 : {
# 1578 : 2 : unsigned int libconsensus_flags = 0;
# 1579 : 2 : int nIn = 0;
# 1580 : :
# 1581 : 2 : CScript scriptPubKey;
# 1582 : 2 : CScript scriptSig;
# 1583 : 2 : CScriptWitness wit;
# 1584 : :
# 1585 : 2 : scriptPubKey << OP_EQUAL;
# 1586 : 2 : CTransaction creditTx = BuildCreditingTransaction(scriptPubKey, 1);
# 1587 : 2 : CTransaction spendTx = BuildSpendingTransaction(scriptSig, wit, creditTx);
# 1588 : :
# 1589 : 2 : CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
# 1590 : 2 : stream << 0xffffffff;
# 1591 : :
# 1592 : 2 : bitcoinconsensus_error err;
# 1593 : 2 : int result = bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), UCharCast(stream.data()), stream.size(), nIn, libconsensus_flags, &err);
# 1594 : 2 : BOOST_CHECK_EQUAL(result, 0);
# 1595 : 2 : BOOST_CHECK_EQUAL(err, bitcoinconsensus_ERR_TX_DESERIALIZE);
# 1596 : 2 : }
# 1597 : :
# 1598 : : /* Test bitcoinconsensus_verify_script returns amount required error */
# 1599 : : BOOST_AUTO_TEST_CASE(bitcoinconsensus_verify_script_amount_required_err)
# 1600 : 2 : {
# 1601 : 2 : unsigned int libconsensus_flags = bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS;
# 1602 : 2 : int nIn = 0;
# 1603 : :
# 1604 : 2 : CScript scriptPubKey;
# 1605 : 2 : CScript scriptSig;
# 1606 : 2 : CScriptWitness wit;
# 1607 : :
# 1608 : 2 : scriptPubKey << OP_EQUAL;
# 1609 : 2 : CTransaction creditTx = BuildCreditingTransaction(scriptPubKey, 1);
# 1610 : 2 : CTransaction spendTx = BuildSpendingTransaction(scriptSig, wit, creditTx);
# 1611 : :
# 1612 : 2 : CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
# 1613 : 2 : stream << spendTx;
# 1614 : :
# 1615 : 2 : bitcoinconsensus_error err;
# 1616 : 2 : int result = bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), UCharCast(stream.data()), stream.size(), nIn, libconsensus_flags, &err);
# 1617 : 2 : BOOST_CHECK_EQUAL(result, 0);
# 1618 : 2 : BOOST_CHECK_EQUAL(err, bitcoinconsensus_ERR_AMOUNT_REQUIRED);
# 1619 : 2 : }
# 1620 : :
# 1621 : : /* Test bitcoinconsensus_verify_script returns invalid flags err */
# 1622 : : BOOST_AUTO_TEST_CASE(bitcoinconsensus_verify_script_invalid_flags)
# 1623 : 2 : {
# 1624 : 2 : unsigned int libconsensus_flags = 1 << 3;
# 1625 : 2 : int nIn = 0;
# 1626 : :
# 1627 : 2 : CScript scriptPubKey;
# 1628 : 2 : CScript scriptSig;
# 1629 : 2 : CScriptWitness wit;
# 1630 : :
# 1631 : 2 : scriptPubKey << OP_EQUAL;
# 1632 : 2 : CTransaction creditTx = BuildCreditingTransaction(scriptPubKey, 1);
# 1633 : 2 : CTransaction spendTx = BuildSpendingTransaction(scriptSig, wit, creditTx);
# 1634 : :
# 1635 : 2 : CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
# 1636 : 2 : stream << spendTx;
# 1637 : :
# 1638 : 2 : bitcoinconsensus_error err;
# 1639 : 2 : int result = bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), UCharCast(stream.data()), stream.size(), nIn, libconsensus_flags, &err);
# 1640 : 2 : BOOST_CHECK_EQUAL(result, 0);
# 1641 : 2 : BOOST_CHECK_EQUAL(err, bitcoinconsensus_ERR_INVALID_FLAGS);
# 1642 : 2 : }
# 1643 : :
# 1644 : : #endif // defined(HAVE_CONSENSUS_LIB)
# 1645 : :
# 1646 : : static std::vector<unsigned int> AllConsensusFlags()
# 1647 : 2 : {
# 1648 : 2 : std::vector<unsigned int> ret;
# 1649 : :
# 1650 [ + + ]: 258 : for (unsigned int i = 0; i < 128; ++i) {
# 1651 : 256 : unsigned int flag = 0;
# 1652 [ + + ]: 256 : if (i & 1) flag |= SCRIPT_VERIFY_P2SH;
# 1653 [ + + ]: 256 : if (i & 2) flag |= SCRIPT_VERIFY_DERSIG;
# 1654 [ + + ]: 256 : if (i & 4) flag |= SCRIPT_VERIFY_NULLDUMMY;
# 1655 [ + + ]: 256 : if (i & 8) flag |= SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY;
# 1656 [ + + ]: 256 : if (i & 16) flag |= SCRIPT_VERIFY_CHECKSEQUENCEVERIFY;
# 1657 [ + + ]: 256 : if (i & 32) flag |= SCRIPT_VERIFY_WITNESS;
# 1658 [ + + ]: 256 : if (i & 64) flag |= SCRIPT_VERIFY_TAPROOT;
# 1659 : :
# 1660 : : // SCRIPT_VERIFY_WITNESS requires SCRIPT_VERIFY_P2SH
# 1661 [ + + ][ + + ]: 256 : if (flag & SCRIPT_VERIFY_WITNESS && !(flag & SCRIPT_VERIFY_P2SH)) continue;
# 1662 : : // SCRIPT_VERIFY_TAPROOT requires SCRIPT_VERIFY_WITNESS
# 1663 [ + + ][ + + ]: 192 : if (flag & SCRIPT_VERIFY_TAPROOT && !(flag & SCRIPT_VERIFY_WITNESS)) continue;
# 1664 : :
# 1665 : 128 : ret.push_back(flag);
# 1666 : 128 : }
# 1667 : :
# 1668 : 2 : return ret;
# 1669 : 2 : }
# 1670 : :
# 1671 : : /** Precomputed list of all valid combinations of consensus-relevant script validation flags. */
# 1672 : : static const std::vector<unsigned int> ALL_CONSENSUS_FLAGS = AllConsensusFlags();
# 1673 : :
# 1674 : : static void AssetTest(const UniValue& test)
# 1675 : 0 : {
# 1676 : 0 : BOOST_CHECK(test.isObject());
# 1677 : :
# 1678 : 0 : CMutableTransaction mtx = TxFromHex(test["tx"].get_str());
# 1679 : 0 : const std::vector<CTxOut> prevouts = TxOutsFromJSON(test["prevouts"]);
# 1680 : 0 : BOOST_CHECK(prevouts.size() == mtx.vin.size());
# 1681 : 0 : size_t idx = test["index"].get_int64();
# 1682 : 0 : uint32_t test_flags{ParseScriptFlags(test["flags"].get_str())};
# 1683 [ # # ][ # # ]: 0 : bool fin = test.exists("final") && test["final"].get_bool();
# 1684 : :
# 1685 [ # # ]: 0 : if (test.exists("success")) {
# 1686 : 0 : mtx.vin[idx].scriptSig = ScriptFromHex(test["success"]["scriptSig"].get_str());
# 1687 : 0 : mtx.vin[idx].scriptWitness = ScriptWitnessFromJSON(test["success"]["witness"]);
# 1688 : 0 : CTransaction tx(mtx);
# 1689 : 0 : PrecomputedTransactionData txdata;
# 1690 : 0 : txdata.Init(tx, std::vector<CTxOut>(prevouts));
# 1691 : 0 : CachingTransactionSignatureChecker txcheck(&tx, idx, prevouts[idx].nValue, true, txdata);
# 1692 [ # # ]: 0 : for (const auto flags : ALL_CONSENSUS_FLAGS) {
# 1693 : : // "final": true tests are valid for all flags. Others are only valid with flags that are
# 1694 : : // a subset of test_flags.
# 1695 [ # # ][ # # ]: 0 : if (fin || ((flags & test_flags) == flags)) {
# 1696 : 0 : bool ret = VerifyScript(tx.vin[idx].scriptSig, prevouts[idx].scriptPubKey, &tx.vin[idx].scriptWitness, flags, txcheck, nullptr);
# 1697 : 0 : BOOST_CHECK(ret);
# 1698 : 0 : }
# 1699 : 0 : }
# 1700 : 0 : }
# 1701 : :
# 1702 [ # # ]: 0 : if (test.exists("failure")) {
# 1703 : 0 : mtx.vin[idx].scriptSig = ScriptFromHex(test["failure"]["scriptSig"].get_str());
# 1704 : 0 : mtx.vin[idx].scriptWitness = ScriptWitnessFromJSON(test["failure"]["witness"]);
# 1705 : 0 : CTransaction tx(mtx);
# 1706 : 0 : PrecomputedTransactionData txdata;
# 1707 : 0 : txdata.Init(tx, std::vector<CTxOut>(prevouts));
# 1708 : 0 : CachingTransactionSignatureChecker txcheck(&tx, idx, prevouts[idx].nValue, true, txdata);
# 1709 [ # # ]: 0 : for (const auto flags : ALL_CONSENSUS_FLAGS) {
# 1710 : : // If a test is supposed to fail with test_flags, it should also fail with any superset thereof.
# 1711 [ # # ]: 0 : if ((flags & test_flags) == test_flags) {
# 1712 : 0 : bool ret = VerifyScript(tx.vin[idx].scriptSig, prevouts[idx].scriptPubKey, &tx.vin[idx].scriptWitness, flags, txcheck, nullptr);
# 1713 : 0 : BOOST_CHECK(!ret);
# 1714 : 0 : }
# 1715 : 0 : }
# 1716 : 0 : }
# 1717 : 0 : }
# 1718 : :
# 1719 : : BOOST_AUTO_TEST_CASE(script_assets_test)
# 1720 : 2 : {
# 1721 : : // See src/test/fuzz/script_assets_test_minimizer.cpp for information on how to generate
# 1722 : : // the script_assets_test.json file used by this test.
# 1723 : :
# 1724 : 2 : const char* dir = std::getenv("DIR_UNIT_TEST_DATA");
# 1725 : 2 : BOOST_WARN_MESSAGE(dir != nullptr, "Variable DIR_UNIT_TEST_DATA unset, skipping script_assets_test");
# 1726 [ + - ]: 2 : if (dir == nullptr) return;
# 1727 : 0 : auto path = fs::path(dir) / "script_assets_test.json";
# 1728 : 0 : bool exists = fs::exists(path);
# 1729 : 0 : BOOST_WARN_MESSAGE(exists, "File $DIR_UNIT_TEST_DATA/script_assets_test.json not found, skipping script_assets_test");
# 1730 [ # # ]: 0 : if (!exists) return;
# 1731 : 0 : std::ifstream file{path};
# 1732 : 0 : BOOST_CHECK(file.is_open());
# 1733 : 0 : file.seekg(0, std::ios::end);
# 1734 : 0 : size_t length = file.tellg();
# 1735 : 0 : file.seekg(0, std::ios::beg);
# 1736 : 0 : std::string data(length, '\0');
# 1737 : 0 : file.read(data.data(), data.size());
# 1738 : 0 : UniValue tests = read_json(data);
# 1739 : 0 : BOOST_CHECK(tests.isArray());
# 1740 : 0 : BOOST_CHECK(tests.size() > 0);
# 1741 : :
# 1742 [ # # ]: 0 : for (size_t i = 0; i < tests.size(); i++) {
# 1743 : 0 : AssetTest(tests[i]);
# 1744 : 0 : }
# 1745 : 0 : file.close();
# 1746 : 0 : }
# 1747 : :
# 1748 : : BOOST_AUTO_TEST_CASE(bip341_keypath_test_vectors)
# 1749 : 2 : {
# 1750 : 2 : UniValue tests;
# 1751 : 2 : tests.read((const char*)json_tests::bip341_wallet_vectors, sizeof(json_tests::bip341_wallet_vectors));
# 1752 : :
# 1753 : 2 : const auto& vectors = tests["keyPathSpending"];
# 1754 : :
# 1755 [ + + ]: 2 : for (const auto& vec : vectors.getValues()) {
# 1756 : 2 : auto txhex = ParseHex(vec["given"]["rawUnsignedTx"].get_str());
# 1757 : 2 : CMutableTransaction tx;
# 1758 : 2 : SpanReader{SER_NETWORK, PROTOCOL_VERSION, txhex} >> tx;
# 1759 : 2 : std::vector<CTxOut> utxos;
# 1760 [ + + ]: 18 : for (const auto& utxo_spent : vec["given"]["utxosSpent"].getValues()) {
# 1761 : 18 : auto script_bytes = ParseHex(utxo_spent["scriptPubKey"].get_str());
# 1762 : 18 : CScript script{script_bytes.begin(), script_bytes.end()};
# 1763 : 18 : CAmount amount{utxo_spent["amountSats"].get_int()};
# 1764 : 18 : utxos.emplace_back(amount, script);
# 1765 : 18 : }
# 1766 : :
# 1767 : 2 : PrecomputedTransactionData txdata;
# 1768 : 2 : txdata.Init(tx, std::vector<CTxOut>{utxos}, true);
# 1769 : :
# 1770 : 2 : BOOST_CHECK(txdata.m_bip341_taproot_ready);
# 1771 : 2 : BOOST_CHECK_EQUAL(HexStr(txdata.m_spent_amounts_single_hash), vec["intermediary"]["hashAmounts"].get_str());
# 1772 : 2 : BOOST_CHECK_EQUAL(HexStr(txdata.m_outputs_single_hash), vec["intermediary"]["hashOutputs"].get_str());
# 1773 : 2 : BOOST_CHECK_EQUAL(HexStr(txdata.m_prevouts_single_hash), vec["intermediary"]["hashPrevouts"].get_str());
# 1774 : 2 : BOOST_CHECK_EQUAL(HexStr(txdata.m_spent_scripts_single_hash), vec["intermediary"]["hashScriptPubkeys"].get_str());
# 1775 : 2 : BOOST_CHECK_EQUAL(HexStr(txdata.m_sequences_single_hash), vec["intermediary"]["hashSequences"].get_str());
# 1776 : :
# 1777 [ + + ]: 14 : for (const auto& input : vec["inputSpending"].getValues()) {
# 1778 : 14 : int txinpos = input["given"]["txinIndex"].get_int();
# 1779 : 14 : int hashtype = input["given"]["hashType"].get_int();
# 1780 : :
# 1781 : : // Load key.
# 1782 : 14 : auto privkey = ParseHex(input["given"]["internalPrivkey"].get_str());
# 1783 : 14 : CKey key;
# 1784 : 14 : key.Set(privkey.begin(), privkey.end(), true);
# 1785 : :
# 1786 : : // Load Merkle root.
# 1787 : 14 : uint256 merkle_root;
# 1788 [ + + ]: 14 : if (!input["given"]["merkleRoot"].isNull()) {
# 1789 : 12 : merkle_root = uint256{ParseHex(input["given"]["merkleRoot"].get_str())};
# 1790 : 12 : }
# 1791 : :
# 1792 : : // Compute and verify (internal) public key.
# 1793 : 14 : XOnlyPubKey pubkey{key.GetPubKey()};
# 1794 : 14 : BOOST_CHECK_EQUAL(HexStr(pubkey), input["intermediary"]["internalPubkey"].get_str());
# 1795 : :
# 1796 : : // Sign and verify signature.
# 1797 : 14 : FlatSigningProvider provider;
# 1798 : 14 : provider.keys[key.GetPubKey().GetID()] = key;
# 1799 : 14 : MutableTransactionSignatureCreator creator(&tx, txinpos, utxos[txinpos].nValue, &txdata, hashtype);
# 1800 : 14 : std::vector<unsigned char> signature;
# 1801 : 14 : BOOST_CHECK(creator.CreateSchnorrSig(provider, signature, pubkey, nullptr, &merkle_root, SigVersion::TAPROOT));
# 1802 : 14 : BOOST_CHECK_EQUAL(HexStr(signature), input["expected"]["witness"][0].get_str());
# 1803 : :
# 1804 : : // We can't observe the tweak used inside the signing logic, so verify by recomputing it.
# 1805 : 14 : BOOST_CHECK_EQUAL(HexStr(pubkey.ComputeTapTweakHash(merkle_root.IsNull() ? nullptr : &merkle_root)), input["intermediary"]["tweak"].get_str());
# 1806 : :
# 1807 : : // We can't observe the sighash used inside the signing logic, so verify by recomputing it.
# 1808 : 14 : ScriptExecutionData sed;
# 1809 : 14 : sed.m_annex_init = true;
# 1810 : 14 : sed.m_annex_present = false;
# 1811 : 14 : uint256 sighash;
# 1812 : 14 : BOOST_CHECK(SignatureHashSchnorr(sighash, sed, tx, txinpos, hashtype, SigVersion::TAPROOT, txdata, MissingDataBehavior::FAIL));
# 1813 : 14 : BOOST_CHECK_EQUAL(HexStr(sighash), input["intermediary"]["sigHash"].get_str());
# 1814 : :
# 1815 : : // To verify the sigmsg, hash the expected sigmsg, and compare it with the (expected) sighash.
# 1816 : 14 : BOOST_CHECK_EQUAL(HexStr((CHashWriter(HASHER_TAPSIGHASH) << Span{ParseHex(input["intermediary"]["sigMsg"].get_str())}).GetSHA256()), input["intermediary"]["sigHash"].get_str());
# 1817 : 14 : }
# 1818 : :
# 1819 : 2 : }
# 1820 : :
# 1821 : 2 : }
# 1822 : :
# 1823 : : BOOST_AUTO_TEST_SUITE_END()
|