5 #include <test/data/script_tests.json.h>
6 #include <test/data/bip341_wallet_vectors.json.h>
23 #if defined(HAVE_CONSENSUS_LIB)
32 #include <boost/test/unit_test.hpp>
50 BOOST_ERROR(
"Parse error.");
113 BOOST_ERROR(
"Unknown scripterror enumeration value, update script_errors in script_tests.cpp.");
122 BOOST_ERROR(
"Unknown scripterror \"" <<
name <<
"\" in test description");
143 for (
int i = 0; i < 16; ++i) {
145 uint32_t combined_flags{
expect ? (
flags & ~extra_flags) : (
flags | extra_flags)};
149 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));
152 #if defined(HAVE_CONSENSUS_LIB)
156 if (libconsensus_flags ==
flags) {
157 int expectedSuccessCode =
expect ? 1 : 0;
170 std::vector<unsigned char> r, s;
171 r = std::vector<unsigned char>(vchSig.begin() + 4, vchSig.begin() + 4 + vchSig[3]);
172 s = std::vector<unsigned char>(vchSig.begin() + 6 + vchSig[3], vchSig.begin() + 6 + vchSig[3] + vchSig[5 + vchSig[3]]);
175 static const unsigned char order[33] = {
177 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
178 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
179 0xBA, 0xAE, 0xDC, 0xE6, 0xAF, 0x48, 0xA0, 0x3B,
180 0xBF, 0xD2, 0x5E, 0x8C, 0xD0, 0x36, 0x41, 0x41
182 while (s.size() < 33) {
183 s.insert(s.begin(), 0x00);
186 for (
int p = 32; p >= 1; p--) {
187 int n = (int)order[p] - s[p] - carry;
188 s[p] = (n + 256) & 0xFF;
192 if (s.size() > 1 && s[0] == 0 && s[1] < 0x80) {
198 vchSig.push_back(0x30);
199 vchSig.push_back(4 + r.size() + s.size());
200 vchSig.push_back(0x02);
201 vchSig.push_back(r.size());
202 vchSig.insert(vchSig.end(), r.begin(), r.end());
203 vchSig.push_back(0x02);
204 vchSig.push_back(s.size());
205 vchSig.insert(vchSig.end(), s.begin(), s.end());
210 const unsigned char vchKey0[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
211 const unsigned char vchKey1[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0};
212 const unsigned char vchKey2[32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0};
216 CKey key0, key0C, key1, key1C, key2, key2C;
217 CPubKey pubkey0, pubkey0C, pubkey0H;
223 key0.
Set(vchKey0, vchKey0 + 32,
false);
224 key0C.
Set(vchKey0, vchKey0 + 32,
true);
228 *
const_cast<unsigned char*
>(pubkey0H.
data()) = 0x06 | (pubkey0H[64] & 1);
230 key1.
Set(vchKey1, vchKey1 + 32,
false);
231 key1C.
Set(vchKey1, vchKey1 + 32,
true);
235 key2.
Set(vchKey2, vchKey2 + 32,
false);
236 key2C.
Set(vchKey2, vchKey2 + 32,
true);
242 enum class WitnessMode {
261 std::vector<unsigned char> push;
270 spendTx.
vin[0].scriptSig << push;
275 void DoPush(
const std::vector<unsigned char>& data)
283 TestBuilder(
const CScript& script_,
const std::string& comment_, uint32_t flags_,
bool P2SH =
false, WitnessMode wm =
WitnessMode::NONE,
int witnessversion = 0,
CAmount nValue_ = 0) : script(script_), havePush(false), comment(comment_),
flags(flags_), scriptError(
SCRIPT_ERR_OK), nValue(nValue_)
286 if (wm == WitnessMode::PKH) {
291 }
else if (wm == WitnessMode::SH) {
292 witscript = scriptPubKey;
298 redeemscript = scriptPubKey;
314 spendTx.
vin[0].scriptSig << _op;
318 TestBuilder&
Num(
int num)
321 spendTx.
vin[0].scriptSig << num;
325 TestBuilder& Push(
const std::string& hex)
331 TestBuilder& Push(
const CScript& _script)
333 DoPush(std::vector<unsigned char>(_script.
begin(), _script.
end()));
340 std::vector<unsigned char> vchSig, r, s;
343 key.
Sign(hash, vchSig,
false, iter++);
344 if ((lenS == 33) != (vchSig[5 + vchSig[3]] == 33)) {
347 r = std::vector<unsigned char>(vchSig.begin() + 4, vchSig.begin() + 4 + vchSig[3]);
348 s = std::vector<unsigned char>(vchSig.begin() + 6 + vchSig[3], vchSig.begin() + 6 + vchSig[3] + vchSig[5 + vchSig[3]]);
349 }
while (lenR != r.size() || lenS != s.size());
350 vchSig.push_back(
static_cast<unsigned char>(nHashType));
359 return PushSig(key, nHashType, lenR, lenS, sigversion, amount).AsWit();
362 TestBuilder& Push(
const CPubKey& pubkey)
364 DoPush(std::vector<unsigned char>(pubkey.
begin(), pubkey.
end()));
368 TestBuilder& PushRedeem()
370 DoPush(std::vector<unsigned char>(redeemscript.
begin(), redeemscript.
end()));
374 TestBuilder& PushWitRedeem()
376 DoPush(std::vector<unsigned char>(witscript.
begin(), witscript.
end()));
380 TestBuilder& EditPush(
unsigned int pos,
const std::string& hexin,
const std::string& hexout)
383 std::vector<unsigned char> datain =
ParseHex(hexin);
384 std::vector<unsigned char> dataout =
ParseHex(hexout);
385 assert(pos + datain.size() <= push.size());
386 BOOST_CHECK_MESSAGE(std::vector<unsigned char>(push.begin() + pos, push.begin() + pos + datain.size()) == datain, comment);
387 push.erase(push.begin() + pos, push.begin() + pos + datain.size());
388 push.insert(push.begin() + pos, dataout.begin(), dataout.end());
392 TestBuilder& DamagePush(
unsigned int pos)
395 assert(pos < push.size());
402 TestBuilder copy = *
this;
404 DoTest(creditTx->vout[0].scriptPubKey, spendTx.
vin[0].scriptSig, scriptWitness,
flags, comment, scriptError, nValue);
412 scriptWitness.
stack.push_back(push);
421 if (!scriptWitness.
stack.empty()) {
423 for (
unsigned i = 0; i < scriptWitness.
stack.size(); i++) {
427 array.push_back(wit);
430 array.push_back(
FormatScript(creditTx->vout[0].scriptPubKey));
433 array.push_back(comment);
437 std::string GetComment()
const
443 std::string JSONPrettyPrint(
const UniValue& univalue)
445 std::string ret = univalue.
write(4);
448 while ((pos = ret.find(
" \n", pos)) != std::string::npos) {
449 ret.replace(pos, 2,
"\n");
460 std::vector<TestBuilder> tests;
464 ).PushSig(keys.key0));
471 ).PushSig(keys.key1).Push(keys.pubkey1C));
473 "P2PKH, bad pubkey", 0
477 "P2PK anyonecanpay", 0
480 "P2PK anyonecanpay marked with normal hashtype", 0
485 ).PushSig(keys.key0).PushRedeem());
492 ).PushSig(keys.key0).Push(keys.pubkey0).PushRedeem());
494 "P2SH(P2PKH), bad sig but no VERIFY_P2SH", 0,
true
495 ).PushSig(keys.key0).DamagePush(10).PushRedeem());
502 ).Num(0).PushSig(keys.key0).PushSig(keys.key1).PushSig(keys.key2));
509 ).Num(0).PushSig(keys.key1).PushSig(keys.key2).PushRedeem());
515 "P2PK with too much R padding but no DERSIG", 0
516 ).PushSig(keys.key1,
SIGHASH_ALL, 31, 32).EditPush(1,
"43021F",
"44022000"));
521 "P2PK with too much S padding but no DERSIG", 0
522 ).PushSig(keys.key1,
SIGHASH_ALL).EditPush(1,
"44",
"45").EditPush(37,
"20",
"2100"));
527 "P2PK with too little R padding but no DERSIG", 0
528 ).PushSig(keys.key1,
SIGHASH_ALL, 33, 32).EditPush(1,
"45022100",
"440220"));
533 "P2PK NOT with bad sig with too much R padding but no DERSIG", 0
534 ).PushSig(keys.key2,
SIGHASH_ALL, 31, 32).EditPush(1,
"43021F",
"44022000").DamagePush(10));
539 "P2PK NOT with too much R padding but no DERSIG", 0
546 "BIP66 example 1, without DERSIG", 0
547 ).PushSig(keys.key1,
SIGHASH_ALL, 33, 32).EditPush(1,
"45022100",
"440220"));
552 "BIP66 example 2, without DERSIG", 0
558 "BIP66 example 3, without DERSIG", 0
564 "BIP66 example 4, without DERSIG", 0
570 "BIP66 example 5, without DERSIG", 0
576 "BIP66 example 6, without DERSIG", 0
582 "BIP66 example 7, without DERSIG", 0
583 ).Num(0).PushSig(keys.key1,
SIGHASH_ALL, 33, 32).EditPush(1,
"45022100",
"440220").PushSig(keys.key2));
588 "BIP66 example 8, without DERSIG", 0
594 "BIP66 example 9, without DERSIG", 0
600 "BIP66 example 10, without DERSIG", 0
601 ).Num(0).Num(0).PushSig(keys.key2,
SIGHASH_ALL, 33, 32).EditPush(1,
"45022100",
"440220"));
606 "BIP66 example 11, without DERSIG", 0
612 "BIP66 example 12, without DERSIG", 0
613 ).Num(0).PushSig(keys.key1,
SIGHASH_ALL, 33, 32).EditPush(1,
"45022100",
"440220").Num(0));
616 ).Num(0).PushSig(keys.key1,
SIGHASH_ALL, 33, 32).EditPush(1,
"45022100",
"440220").Num(0));
618 "P2PK with multi-byte hashtype, without DERSIG", 0
619 ).PushSig(keys.key2,
SIGHASH_ALL).EditPush(70,
"01",
"0101"));
625 "P2PK with high S but no LOW_S", 0
632 "P2PK with hybrid pubkey but no STRICTENC", 0
638 "P2PK NOT with hybrid pubkey but no STRICTENC", 0
644 "P2PK NOT with invalid hybrid pubkey but no STRICTENC", 0
650 "1-of-2 with the second 1 hybrid pubkey and no STRICTENC", 0
660 "P2PK with undefined hashtype but no STRICTENC", 0
661 ).PushSig(keys.key1, 5));
666 "P2PK NOT with invalid sig and undefined hashtype but no STRICTENC", 0
667 ).PushSig(keys.key1, 5).DamagePush(10));
673 "3-of-3 with nonzero dummy but no NULLDUMMY", 0
674 ).Num(1).PushSig(keys.key0).PushSig(keys.key1).PushSig(keys.key2));
679 "3-of-3 NOT with invalid sig and nonzero dummy but no NULLDUMMY", 0
680 ).Num(1).PushSig(keys.key0).PushSig(keys.key1).PushSig(keys.key2).DamagePush(10));
683 ).Num(1).PushSig(keys.key0).PushSig(keys.key1).PushSig(keys.key2).DamagePush(10).ScriptError(
SCRIPT_ERR_SIG_NULLDUMMY));
686 "2-of-2 with two identical keys and sigs pushed using OP_DUP but no SIGPUSHONLY", 0
687 ).Num(0).PushSig(keys.key1).Opcode(
OP_DUP));
692 "P2SH(P2PK) with non-push scriptSig but no P2SH or SIGPUSHONLY", 0,
true
693 ).PushSig(keys.key2).Opcode(
OP_NOP8).PushRedeem());
695 "P2PK with non-push scriptSig but with P2SH validation", 0
696 ).PushSig(keys.key2).Opcode(
OP_NOP8));
705 ).Num(0).PushSig(keys.key1).PushSig(keys.key1));
708 ).Num(11).PushSig(keys.key0));
714 ).Num(11).PushSig(keys.key0).PushRedeem());
720 ).PushSig(keys.key0).PushRedeem());
724 0, 1).PushWitSig(keys.key0).PushWitRedeem());
727 0, 1).PushWitSig(keys.key0).Push(keys.pubkey0).AsWit());
730 0, 1).PushWitSig(keys.key0).PushWitRedeem().PushRedeem());
733 0, 1).PushWitSig(keys.key0).Push(keys.pubkey0).AsWit().PushRedeem());
747 "Basic P2WSH with the wrong key but no WITNESS",
SCRIPT_VERIFY_P2SH,
false, WitnessMode::SH
748 ).PushWitSig(keys.key0).PushWitRedeem());
750 "Basic P2WPKH with the wrong key but no WITNESS",
SCRIPT_VERIFY_P2SH,
false, WitnessMode::PKH
751 ).PushWitSig(keys.key0).Push(keys.pubkey1).AsWit());
753 "Basic P2SH(P2WSH) with the wrong key but no WITNESS",
SCRIPT_VERIFY_P2SH,
true, WitnessMode::SH
754 ).PushWitSig(keys.key0).PushWitRedeem().PushRedeem());
756 "Basic P2SH(P2WPKH) with the wrong key but no WITNESS",
SCRIPT_VERIFY_P2SH,
true, WitnessMode::PKH
757 ).PushWitSig(keys.key0).Push(keys.pubkey1).AsWit().PushRedeem());
769 0, 0).PushWitSig(keys.key0, 1).Push(keys.pubkey0).AsWit().PushRedeem().ScriptError(
SCRIPT_ERR_EVAL_FALSE));
779 std::vector<unsigned char> hashBytes =
ToByteVector(hash);
780 hashBytes.pop_back();
781 tests.push_back(TestBuilder(
CScript() <<
OP_0 << hashBytes,
790 tests.push_back(TestBuilder(witscript,
810 0, 1).PushWitSig(keys.key0C).PushWitRedeem());
813 0, 1).PushWitSig(keys.key0C).Push(keys.pubkey0C).AsWit());
816 0, 1).PushWitSig(keys.key0C).PushWitRedeem().PushRedeem());
819 0, 1).PushWitSig(keys.key0C).Push(keys.pubkey0C).AsWit().PushRedeem());
838 0, 1).Push(
CScript()).AsWit().PushWitSig(keys.key0C).PushWitRedeem());
841 0, 1).Push(
CScript()).AsWit().PushWitSig(keys.key0C).PushWitRedeem().PushRedeem());
844 0, 1).Push(
CScript()).AsWit().PushWitSig(keys.key1C).PushWitRedeem());
847 0, 1).Push(
CScript()).AsWit().PushWitSig(keys.key1C).PushWitRedeem().PushRedeem());
852 0, 1).Push(
CScript()).AsWit().PushWitSig(keys.key0).PushWitRedeem());
855 0, 1).Push(
CScript()).AsWit().PushWitSig(keys.key0).PushWitRedeem().PushRedeem());
864 0, 1).Push(
CScript()).AsWit().PushWitSig(keys.key1C).PushWitRedeem());
867 0, 1).Push(
CScript()).AsWit().PushWitSig(keys.key1C).PushWitRedeem().PushRedeem());
877 0, 1).Push(
CScript()).AsWit().PushWitSig(keys.key0C).PushWitRedeem());
880 0, 1).Push(
CScript()).AsWit().PushWitSig(keys.key0C).PushWitRedeem().PushRedeem());
883 0, 1).Push(
CScript()).AsWit().PushWitSig(keys.key0C).PushWitRedeem());
886 0, 1).Push(
CScript()).AsWit().PushWitSig(keys.key0C).PushWitRedeem().PushRedeem());
889 0, 1).Push(
CScript()).AsWit().PushWitSig(keys.key1).PushWitRedeem());
892 0, 1).Push(
CScript()).AsWit().PushWitSig(keys.key1).PushWitRedeem().PushRedeem());
900 std::set<std::string> tests_set;
903 UniValue json_tests =
read_json(std::string(json_tests::script_tests, json_tests::script_tests +
sizeof(json_tests::script_tests)));
905 for (
unsigned int idx = 0; idx < json_tests.
size(); idx++) {
906 const UniValue& tv = json_tests[idx];
907 tests_set.insert(JSONPrettyPrint(tv.
get_array()));
911 #ifdef UPDATE_JSON_TESTS
914 for (TestBuilder& test : tests) {
916 std::string str = JSONPrettyPrint(test.GetJSON());
917 #ifdef UPDATE_JSON_TESTS
918 strGen += str +
",\n";
920 if (tests_set.count(str) == 0) {
921 BOOST_CHECK_MESSAGE(
false,
"Missing auto script_valid test: " + test.GetComment());
926 #ifdef UPDATE_JSON_TESTS
928 fputs(strGen.c_str(), file);
942 UniValue tests =
read_json(std::string(json_tests::script_tests, json_tests::script_tests +
sizeof(json_tests::script_tests)));
944 for (
unsigned int idx = 0; idx < tests.
size(); idx++) {
946 std::string strTest = test.
write();
949 unsigned int pos = 0;
952 for (i = 0; i < test[pos].
size()-1; i++) {
958 if (test.
size() < 4 + pos)
960 if (test.
size() != 1) {
961 BOOST_ERROR(
"Bad test: " << strTest);
965 std::string scriptSigString = test[pos++].
get_str();
967 std::string scriptPubKeyString = test[pos++].
get_str();
972 DoTest(scriptPubKey, scriptSig, witness, scriptflags, strTest, scriptError, nValue);
980 static const unsigned char direct[] = { 1, 0x5a };
981 static const unsigned char pushdata1[] = {
OP_PUSHDATA1, 1, 0x5a };
982 static const unsigned char pushdata2[] = {
OP_PUSHDATA2, 1, 0, 0x5a };
983 static const unsigned char pushdata4[] = {
OP_PUSHDATA4, 1, 0, 0, 0, 0x5a };
986 std::vector<std::vector<unsigned char> > directStack;
990 std::vector<std::vector<unsigned char> > pushdata1Stack;
995 std::vector<std::vector<unsigned char> > pushdata2Stack;
1000 std::vector<std::vector<unsigned char> > pushdata4Stack;
1005 const std::vector<unsigned char> pushdata1_trunc{
OP_PUSHDATA1, 1};
1006 const std::vector<unsigned char> pushdata2_trunc{
OP_PUSHDATA2, 1, 0};
1007 const std::vector<unsigned char> pushdata4_trunc{
OP_PUSHDATA4, 1, 0, 0, 0};
1009 std::vector<std::vector<unsigned char>> stack_ignore;
1022 std::vector<std::vector<unsigned char>> stack_ignore;
1043 for (
const CKey &key : keys)
1045 std::vector<unsigned char> vchSig;
1055 std::vector<CKey> keys;
1063 CKey key1, key2, key3;
1077 txTo12.
vout[0].nValue = 2;
1093 CKey key1, key2, key3, key4;
1105 std::vector<CKey> keys;
1106 keys.push_back(key1); keys.push_back(key2);
1112 keys.push_back(key1); keys.push_back(key3);
1118 keys.push_back(key2); keys.push_back(key3);
1124 keys.push_back(key2); keys.push_back(key2);
1130 keys.push_back(key2); keys.push_back(key1);
1136 keys.push_back(key3); keys.push_back(key2);
1142 keys.push_back(key4); keys.push_back(key2);
1148 keys.push_back(key1); keys.push_back(key4);
1173 std::vector<CKey> keys;
1174 std::vector<CPubKey> pubkeys;
1175 for (
int i = 0; i < 3; i++)
1179 keys.push_back(key);
1186 CScript& scriptPubKey = txFrom.
vout[0].scriptPubKey;
1217 scriptSigCopy = scriptSig;
1234 std::vector<unsigned char> sig1;
1238 std::vector<unsigned char> sig2;
1242 std::vector<unsigned char> sig3;
1286 for (
int i=0; i<67000; i++) {
1289 BOOST_CHECK_MESSAGE(script.
IsPushOnly(),
"Number " << i <<
" is not pure push.");
1295 std::vector<unsigned char> data(i,
'\111');
1298 BOOST_CHECK_MESSAGE(script.
IsPushOnly(),
"Length " << i <<
" is not pure push.");
1311 static const unsigned char direct[] = { 1 };
1322 std::string derSig(
"304502207fa7a6d1e0ee81132a269ad84e68d695483745cde8b541e3bf630749894e342a022100c1f7ab20e13e22fb95281a870f3dcf38d782e53023ee313d741ad0cfbc0c5090");
1323 std::string pubKey(
"03b0da749730dc9b4b1f4a14d6902877a92541f5368778853d9c4a0cb7802dcfb2");
1347 std::vector<unsigned char> data =
ParseHex(str);
1348 return CScript(data.begin(), data.end());
1464 script =
ScriptFromHex(
"76a9141234567890abcdefa1a2a3a4a5a6a7a8a9a0aaab88ac");
1466 script =
ScriptFromHex(
"76a914ff34567890abcdefa1a2a3a4a5a6a7a8a9a0aaab88ac");
1484 std::vector<CTxOut> prevouts;
1485 for (
size_t i = 0; i < univalue.
size(); ++i) {
1488 prevouts.push_back(std::move(txout));
1497 for (
size_t i = 0; i < univalue.
size(); ++i) {
1498 auto bytes =
ParseHex(univalue[i].get_str());
1499 scriptwitness.
stack.push_back(std::move(bytes));
1501 return scriptwitness;
1504 #if defined(HAVE_CONSENSUS_LIB)
1509 unsigned int libconsensus_flags = 0;
1516 scriptPubKey <<
OP_1;
1532 unsigned int libconsensus_flags = 0;
1555 unsigned int libconsensus_flags = 0;
1578 unsigned int libconsensus_flags = 0;
1590 stream << 0xffffffff;
1624 unsigned int libconsensus_flags = 1 << 3;
1644 #endif // defined(HAVE_CONSENSUS_LIB)
1648 std::vector<unsigned int> ret;
1650 for (
unsigned int i = 0; i < 128; ++i) {
1651 unsigned int flag = 0;
1665 ret.push_back(flag);
1679 const std::vector<CTxOut> prevouts =
TxOutsFromJSON(test[
"prevouts"]);
1685 if (test.
exists(
"success")) {
1686 mtx.
vin[idx].scriptSig =
ScriptFromHex(test[
"success"][
"scriptSig"].get_str());
1690 txdata.
Init(tx, std::vector<CTxOut>(prevouts));
1695 if (fin || ((
flags & test_flags) ==
flags)) {
1696 bool ret =
VerifyScript(tx.
vin[idx].scriptSig, prevouts[idx].scriptPubKey, &tx.
vin[idx].scriptWitness,
flags, txcheck,
nullptr);
1702 if (test.
exists(
"failure")) {
1703 mtx.
vin[idx].scriptSig =
ScriptFromHex(test[
"failure"][
"scriptSig"].get_str());
1707 txdata.
Init(tx, std::vector<CTxOut>(prevouts));
1711 if ((
flags & test_flags) == test_flags) {
1712 bool ret =
VerifyScript(tx.
vin[idx].scriptSig, prevouts[idx].scriptPubKey, &tx.
vin[idx].scriptWitness,
flags, txcheck,
nullptr);
1724 const char* dir = std::getenv(
"DIR_UNIT_TEST_DATA");
1725 BOOST_WARN_MESSAGE(dir !=
nullptr,
"Variable DIR_UNIT_TEST_DATA unset, skipping script_assets_test");
1726 if (dir ==
nullptr)
return;
1727 auto path =
fs::path(dir) /
"script_assets_test.json";
1729 BOOST_WARN_MESSAGE(
exists,
"File $DIR_UNIT_TEST_DATA/script_assets_test.json not found, skipping script_assets_test");
1731 std::ifstream file{path};
1733 file.seekg(0, std::ios::end);
1734 size_t length = file.tellg();
1735 file.seekg(0, std::ios::beg);
1736 std::string data(length,
'\0');
1737 file.read(data.data(), data.size());
1742 for (
size_t i = 0; i < tests.
size(); i++) {
1751 tests.
read((
const char*)json_tests::bip341_wallet_vectors,
sizeof(json_tests::bip341_wallet_vectors));
1753 const auto& vectors = tests[
"keyPathSpending"];
1755 for (
const auto& vec : vectors.getValues()) {
1756 auto txhex =
ParseHex(vec[
"given"][
"rawUnsignedTx"].get_str());
1759 std::vector<CTxOut> utxos;
1760 for (
const auto& utxo_spent : vec[
"given"][
"utxosSpent"].getValues()) {
1761 auto script_bytes =
ParseHex(utxo_spent[
"scriptPubKey"].get_str());
1762 CScript script{script_bytes.
begin(), script_bytes.end()};
1763 CAmount amount{utxo_spent[
"amountSats"].get_int()};
1764 utxos.emplace_back(amount, script);
1768 txdata.
Init(tx, std::vector<CTxOut>{utxos},
true);
1777 for (
const auto& input : vec[
"inputSpending"].getValues()) {
1778 int txinpos = input[
"given"][
"txinIndex"].get_int();
1779 int hashtype = input[
"given"][
"hashType"].get_int();
1782 auto privkey =
ParseHex(input[
"given"][
"internalPrivkey"].get_str());
1784 key.
Set(privkey.begin(), privkey.end(),
true);
1788 if (!input[
"given"][
"merkleRoot"].isNull()) {
1789 merkle_root =
uint256{
ParseHex(input[
"given"][
"merkleRoot"].get_str())};
1800 std::vector<unsigned char> signature;
1805 BOOST_CHECK_EQUAL(
HexStr(pubkey.ComputeTapTweakHash(merkle_root.
IsNull() ?
nullptr : &merkle_root)), input[
"intermediary"][
"tweak"].get_str());