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