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