22#if defined(HAVE_CONSENSUS_LIB)
30#include <boost/test/unit_test.hpp>
48 BOOST_ERROR(
"Parse error.");
111 BOOST_ERROR(
"Unknown scripterror enumeration value, update script_errors in script_tests.cpp.");
120 BOOST_ERROR(
"Unknown scripterror \"" <<
name <<
"\" in test description");
141 for (
int i = 0; i < 16; ++i) {
143 uint32_t combined_flags{
expect ? (
flags & ~extra_flags) : (
flags | extra_flags)};
147 BOOST_CHECK_MESSAGE(
VerifyScript(scriptSig, scriptPubKey, &scriptWitness, combined_flags,
MutableTransactionSignatureChecker(&tx, 0, txCredit.
vout[0].nValue,
MissingDataBehavior::ASSERT_FAIL), &err) ==
expect, message +
strprintf(
" (with flags %x)", combined_flags));
150#if defined(HAVE_CONSENSUS_LIB)
154 if (libconsensus_flags ==
flags) {
155 int expectedSuccessCode =
expect ? 1 : 0;
160 BOOST_CHECK_MESSAGE(
bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), stream.
data(), stream.
size(), 0, libconsensus_flags,
nullptr) == expectedSuccessCode, message);
168 std::vector<unsigned char> r, s;
169 r = std::vector<unsigned char>(vchSig.begin() + 4, vchSig.begin() + 4 + vchSig[3]);
170 s = std::vector<unsigned char>(vchSig.begin() + 6 + vchSig[3], vchSig.begin() + 6 + vchSig[3] + vchSig[5 + vchSig[3]]);
173 static const unsigned char order[33] = {
175 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
176 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
177 0xBA, 0xAE, 0xDC, 0xE6, 0xAF, 0x48, 0xA0, 0x3B,
178 0xBF, 0xD2, 0x5E, 0x8C, 0xD0, 0x36, 0x41, 0x41
180 while (s.size() < 33) {
181 s.insert(s.begin(), 0x00);
184 for (
int p = 32; p >= 1; p--) {
185 int n = (int)order[p] - s[p] - carry;
186 s[p] = (n + 256) & 0xFF;
190 if (s.size() > 1 && s[0] == 0 && s[1] < 0x80) {
196 vchSig.push_back(0x30);
197 vchSig.push_back(4 + r.size() + s.size());
198 vchSig.push_back(0x02);
199 vchSig.push_back(r.size());
200 vchSig.insert(vchSig.end(), r.begin(), r.end());
201 vchSig.push_back(0x02);
202 vchSig.push_back(s.size());
203 vchSig.insert(vchSig.end(), s.begin(), s.end());
208const 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};
209const 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};
210const 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};
214 CKey key0, key0C, key1, key1C, key2, key2C;
215 CPubKey pubkey0, pubkey0C, pubkey0H;
221 key0.
Set(vchKey0, vchKey0 + 32,
false);
222 key0C.
Set(vchKey0, vchKey0 + 32,
true);
226 *
const_cast<unsigned char*
>(pubkey0H.
data()) = 0x06 | (pubkey0H[64] & 1);
228 key1.
Set(vchKey1, vchKey1 + 32,
false);
229 key1C.
Set(vchKey1, vchKey1 + 32,
true);
233 key2.
Set(vchKey2, vchKey2 + 32,
false);
234 key2C.
Set(vchKey2, vchKey2 + 32,
true);
240enum class WitnessMode {
259 std::vector<unsigned char> push;
268 spendTx.
vin[0].scriptSig << push;
273 void DoPush(
const std::vector<unsigned char>& data)
281 TestBuilder(
const CScript& script_,
const std::string& comment_, uint32_t flags_,
bool P2SH =
false, WitnessMode wm =
WitnessMode::NONE,
int witnessversion = 0,
CAmount nValue_ = 0) : script(script_), havePush(false), comment(comment_),
flags(flags_), scriptError(
SCRIPT_ERR_OK), nValue(nValue_)
284 if (wm == WitnessMode::PKH) {
289 }
else if (wm == WitnessMode::SH) {
290 witscript = scriptPubKey;
296 redeemscript = scriptPubKey;
312 spendTx.
vin[0].scriptSig << _op;
316 TestBuilder& Num(
int num)
319 spendTx.
vin[0].scriptSig << num;
323 TestBuilder& Push(
const std::string& hex)
329 TestBuilder& Push(
const CScript& _script)
331 DoPush(std::vector<unsigned char>(_script.
begin(), _script.
end()));
338 std::vector<unsigned char> vchSig, r, s;
341 key.
Sign(hash, vchSig,
false, iter++);
342 if ((lenS == 33) != (vchSig[5 + vchSig[3]] == 33)) {
345 r = std::vector<unsigned char>(vchSig.begin() + 4, vchSig.begin() + 4 + vchSig[3]);
346 s = std::vector<unsigned char>(vchSig.begin() + 6 + vchSig[3], vchSig.begin() + 6 + vchSig[3] + vchSig[5 + vchSig[3]]);
347 }
while (lenR != r.size() || lenS != s.size());
348 vchSig.push_back(
static_cast<unsigned char>(nHashType));
357 return PushSig(key, nHashType, lenR, lenS, sigversion, amount).AsWit();
360 TestBuilder& Push(
const CPubKey& pubkey)
362 DoPush(std::vector<unsigned char>(pubkey.
begin(), pubkey.
end()));
366 TestBuilder& PushRedeem()
368 DoPush(std::vector<unsigned char>(redeemscript.
begin(), redeemscript.
end()));
372 TestBuilder& PushWitRedeem()
374 DoPush(std::vector<unsigned char>(witscript.
begin(), witscript.
end()));
378 TestBuilder& EditPush(
unsigned int pos,
const std::string& hexin,
const std::string& hexout)
381 std::vector<unsigned char> datain =
ParseHex(hexin);
382 std::vector<unsigned char> dataout =
ParseHex(hexout);
383 assert(pos + datain.size() <= push.size());
384 BOOST_CHECK_MESSAGE(std::vector<unsigned char>(push.begin() + pos, push.begin() + pos + datain.size()) == datain, comment);
385 push.erase(push.begin() + pos, push.begin() + pos + datain.size());
386 push.insert(push.begin() + pos, dataout.begin(), dataout.end());
390 TestBuilder& DamagePush(
unsigned int pos)
393 assert(pos < push.size());
400 TestBuilder copy = *
this;
402 DoTest(creditTx->vout[0].scriptPubKey, spendTx.
vin[0].scriptSig, scriptWitness,
flags, comment, scriptError, nValue);
410 scriptWitness.
stack.push_back(push);
419 if (!scriptWitness.
stack.empty()) {
421 for (
unsigned i = 0; i < scriptWitness.
stack.size(); i++) {
425 array.push_back(wit);
428 array.push_back(
FormatScript(creditTx->vout[0].scriptPubKey));
431 array.push_back(comment);
435 std::string GetComment()
const
441std::string JSONPrettyPrint(
const UniValue& univalue)
443 std::string ret = univalue.
write(4);
446 while ((pos = ret.find(
" \n", pos)) != std::string::npos) {
447 ret.replace(pos, 2,
"\n");
458 std::vector<TestBuilder> tests;
462 ).PushSig(keys.key0));
469 ).PushSig(keys.key1).Push(keys.pubkey1C));
471 "P2PKH, bad pubkey", 0
475 "P2PK anyonecanpay", 0
478 "P2PK anyonecanpay marked with normal hashtype", 0
483 ).PushSig(keys.key0).PushRedeem());
490 ).PushSig(keys.key0).Push(keys.pubkey0).PushRedeem());
492 "P2SH(P2PKH), bad sig but no VERIFY_P2SH", 0,
true
493 ).PushSig(keys.key0).DamagePush(10).PushRedeem());
500 ).Num(0).PushSig(keys.key0).PushSig(keys.key1).PushSig(keys.key2));
507 ).Num(0).PushSig(keys.key1).PushSig(keys.key2).PushRedeem());
513 "P2PK with too much R padding but no DERSIG", 0
514 ).PushSig(keys.key1,
SIGHASH_ALL, 31, 32).EditPush(1,
"43021F",
"44022000"));
519 "P2PK with too much S padding but no DERSIG", 0
520 ).PushSig(keys.key1,
SIGHASH_ALL).EditPush(1,
"44",
"45").EditPush(37,
"20",
"2100"));
525 "P2PK with too little R padding but no DERSIG", 0
526 ).PushSig(keys.key1,
SIGHASH_ALL, 33, 32).EditPush(1,
"45022100",
"440220"));
531 "P2PK NOT with bad sig with too much R padding but no DERSIG", 0
532 ).PushSig(keys.key2,
SIGHASH_ALL, 31, 32).EditPush(1,
"43021F",
"44022000").DamagePush(10));
537 "P2PK NOT with too much R padding but no DERSIG", 0
544 "BIP66 example 1, without DERSIG", 0
545 ).PushSig(keys.key1,
SIGHASH_ALL, 33, 32).EditPush(1,
"45022100",
"440220"));
550 "BIP66 example 2, without DERSIG", 0
556 "BIP66 example 3, without DERSIG", 0
562 "BIP66 example 4, without DERSIG", 0
568 "BIP66 example 5, without DERSIG", 0
574 "BIP66 example 6, without DERSIG", 0
580 "BIP66 example 7, without DERSIG", 0
581 ).Num(0).PushSig(keys.key1,
SIGHASH_ALL, 33, 32).EditPush(1,
"45022100",
"440220").PushSig(keys.key2));
586 "BIP66 example 8, without DERSIG", 0
592 "BIP66 example 9, without DERSIG", 0
598 "BIP66 example 10, without DERSIG", 0
599 ).Num(0).Num(0).PushSig(keys.key2,
SIGHASH_ALL, 33, 32).EditPush(1,
"45022100",
"440220"));
604 "BIP66 example 11, without DERSIG", 0
610 "BIP66 example 12, without DERSIG", 0
611 ).Num(0).PushSig(keys.key1,
SIGHASH_ALL, 33, 32).EditPush(1,
"45022100",
"440220").Num(0));
614 ).Num(0).PushSig(keys.key1,
SIGHASH_ALL, 33, 32).EditPush(1,
"45022100",
"440220").Num(0));
616 "P2PK with multi-byte hashtype, without DERSIG", 0
617 ).PushSig(keys.key2,
SIGHASH_ALL).EditPush(70,
"01",
"0101"));
623 "P2PK with high S but no LOW_S", 0
630 "P2PK with hybrid pubkey but no STRICTENC", 0
636 "P2PK NOT with hybrid pubkey but no STRICTENC", 0
642 "P2PK NOT with invalid hybrid pubkey but no STRICTENC", 0
648 "1-of-2 with the second 1 hybrid pubkey and no STRICTENC", 0
658 "P2PK with undefined hashtype but no STRICTENC", 0
659 ).PushSig(keys.key1, 5));
664 "P2PK NOT with invalid sig and undefined hashtype but no STRICTENC", 0
665 ).PushSig(keys.key1, 5).DamagePush(10));
671 "3-of-3 with nonzero dummy but no NULLDUMMY", 0
672 ).Num(1).PushSig(keys.key0).PushSig(keys.key1).PushSig(keys.key2));
677 "3-of-3 NOT with invalid sig and nonzero dummy but no NULLDUMMY", 0
678 ).Num(1).PushSig(keys.key0).PushSig(keys.key1).PushSig(keys.key2).DamagePush(10));
681 ).Num(1).PushSig(keys.key0).PushSig(keys.key1).PushSig(keys.key2).DamagePush(10).ScriptError(
SCRIPT_ERR_SIG_NULLDUMMY));
684 "2-of-2 with two identical keys and sigs pushed using OP_DUP but no SIGPUSHONLY", 0
685 ).Num(0).PushSig(keys.key1).Opcode(
OP_DUP));
690 "P2SH(P2PK) with non-push scriptSig but no P2SH or SIGPUSHONLY", 0,
true
691 ).PushSig(keys.key2).Opcode(
OP_NOP8).PushRedeem());
693 "P2PK with non-push scriptSig but with P2SH validation", 0
694 ).PushSig(keys.key2).Opcode(
OP_NOP8));
703 ).Num(0).PushSig(keys.key1).PushSig(keys.key1));
706 ).Num(11).PushSig(keys.key0));
712 ).Num(11).PushSig(keys.key0).PushRedeem());
718 ).PushSig(keys.key0).PushRedeem());
722 0, 1).PushWitSig(keys.key0).PushWitRedeem());
725 0, 1).PushWitSig(keys.key0).Push(keys.pubkey0).AsWit());
728 0, 1).PushWitSig(keys.key0).PushWitRedeem().PushRedeem());
731 0, 1).PushWitSig(keys.key0).Push(keys.pubkey0).AsWit().PushRedeem());
745 "Basic P2WSH with the wrong key but no WITNESS",
SCRIPT_VERIFY_P2SH,
false, WitnessMode::SH
746 ).PushWitSig(keys.key0).PushWitRedeem());
748 "Basic P2WPKH with the wrong key but no WITNESS",
SCRIPT_VERIFY_P2SH,
false, WitnessMode::PKH
749 ).PushWitSig(keys.key0).Push(keys.pubkey1).AsWit());
751 "Basic P2SH(P2WSH) with the wrong key but no WITNESS",
SCRIPT_VERIFY_P2SH,
true, WitnessMode::SH
752 ).PushWitSig(keys.key0).PushWitRedeem().PushRedeem());
754 "Basic P2SH(P2WPKH) with the wrong key but no WITNESS",
SCRIPT_VERIFY_P2SH,
true, WitnessMode::PKH
755 ).PushWitSig(keys.key0).Push(keys.pubkey1).AsWit().PushRedeem());
767 0, 0).PushWitSig(keys.key0, 1).Push(keys.pubkey0).AsWit().PushRedeem().ScriptError(
SCRIPT_ERR_EVAL_FALSE));
777 std::vector<unsigned char> hashBytes =
ToByteVector(hash);
778 hashBytes.pop_back();
779 tests.push_back(TestBuilder(
CScript() <<
OP_0 << hashBytes,
788 tests.push_back(TestBuilder(witscript,
808 0, 1).PushWitSig(keys.key0C).PushWitRedeem());
811 0, 1).PushWitSig(keys.key0C).Push(keys.pubkey0C).AsWit());
814 0, 1).PushWitSig(keys.key0C).PushWitRedeem().PushRedeem());
817 0, 1).PushWitSig(keys.key0C).Push(keys.pubkey0C).AsWit().PushRedeem());
836 0, 1).Push(
CScript()).AsWit().PushWitSig(keys.key0C).PushWitRedeem());
839 0, 1).Push(
CScript()).AsWit().PushWitSig(keys.key0C).PushWitRedeem().PushRedeem());
842 0, 1).Push(
CScript()).AsWit().PushWitSig(keys.key1C).PushWitRedeem());
845 0, 1).Push(
CScript()).AsWit().PushWitSig(keys.key1C).PushWitRedeem().PushRedeem());
850 0, 1).Push(
CScript()).AsWit().PushWitSig(keys.key0).PushWitRedeem());
853 0, 1).Push(
CScript()).AsWit().PushWitSig(keys.key0).PushWitRedeem().PushRedeem());
862 0, 1).Push(
CScript()).AsWit().PushWitSig(keys.key1C).PushWitRedeem());
865 0, 1).Push(
CScript()).AsWit().PushWitSig(keys.key1C).PushWitRedeem().PushRedeem());
875 0, 1).Push(
CScript()).AsWit().PushWitSig(keys.key0C).PushWitRedeem());
878 0, 1).Push(
CScript()).AsWit().PushWitSig(keys.key0C).PushWitRedeem().PushRedeem());
881 0, 1).Push(
CScript()).AsWit().PushWitSig(keys.key0C).PushWitRedeem());
884 0, 1).Push(
CScript()).AsWit().PushWitSig(keys.key0C).PushWitRedeem().PushRedeem());
887 0, 1).Push(
CScript()).AsWit().PushWitSig(keys.key1).PushWitRedeem());
890 0, 1).Push(
CScript()).AsWit().PushWitSig(keys.key1).PushWitRedeem().PushRedeem());
898 std::set<std::string> tests_set;
903 for (
unsigned int idx = 0; idx <
json_tests.size(); idx++) {
905 tests_set.insert(JSONPrettyPrint(tv.
get_array()));
909#ifdef UPDATE_JSON_TESTS
912 for (TestBuilder& test : tests) {
914 std::string str = JSONPrettyPrint(test.GetJSON());
915#ifdef UPDATE_JSON_TESTS
916 strGen += str +
",\n";
918 if (tests_set.count(str) == 0) {
919 BOOST_CHECK_MESSAGE(
false,
"Missing auto script_valid test: " + test.GetComment());
924#ifdef UPDATE_JSON_TESTS
925 FILE* file =
fopen(
"script_tests.json.gen",
"w");
926 fputs(strGen.c_str(), file);
942 for (
unsigned int idx = 0; idx < tests.
size(); idx++) {
944 std::string strTest = test.
write();
947 unsigned int pos = 0;
950 for (i = 0; i < test[pos].
size()-1; i++) {
956 if (test.
size() < 4 + pos)
958 if (test.
size() != 1) {
959 BOOST_ERROR(
"Bad test: " << strTest);
963 std::string scriptSigString = test[pos++].
get_str();
965 std::string scriptPubKeyString = test[pos++].
get_str();
970 DoTest(scriptPubKey, scriptSig, witness, scriptflags, strTest, scriptError, nValue);
978 static const unsigned char direct[] = { 1, 0x5a };
979 static const unsigned char pushdata1[] = {
OP_PUSHDATA1, 1, 0x5a };
980 static const unsigned char pushdata2[] = {
OP_PUSHDATA2, 1, 0, 0x5a };
981 static const unsigned char pushdata4[] = {
OP_PUSHDATA4, 1, 0, 0, 0, 0x5a };
984 std::vector<std::vector<unsigned char> > directStack;
988 std::vector<std::vector<unsigned char> > pushdata1Stack;
993 std::vector<std::vector<unsigned char> > pushdata2Stack;
998 std::vector<std::vector<unsigned char> > pushdata4Stack;
1003 const std::vector<unsigned char> pushdata1_trunc{
OP_PUSHDATA1, 1};
1004 const std::vector<unsigned char> pushdata2_trunc{
OP_PUSHDATA2, 1, 0};
1005 const std::vector<unsigned char> pushdata4_trunc{
OP_PUSHDATA4, 1, 0, 0, 0};
1007 std::vector<std::vector<unsigned char>> stack_ignore;
1020 std::vector<std::vector<unsigned char>> stack_ignore;
1041 for (
const CKey &key : keys)
1043 std::vector<unsigned char> vchSig;
1053 std::vector<CKey> keys;
1061 CKey key1, key2, key3;
1075 txTo12.
vout[0].nValue = 2;
1091 CKey key1, key2, key3, key4;
1103 std::vector<CKey> keys;
1104 keys.push_back(key1); keys.push_back(key2);
1110 keys.push_back(key1); keys.push_back(key3);
1116 keys.push_back(key2); keys.push_back(key3);
1122 keys.push_back(key2); keys.push_back(key2);
1128 keys.push_back(key2); keys.push_back(key1);
1134 keys.push_back(key3); keys.push_back(key2);
1140 keys.push_back(key4); keys.push_back(key2);
1146 keys.push_back(key1); keys.push_back(key4);
1171 std::vector<CKey> keys;
1172 std::vector<CPubKey> pubkeys;
1173 for (
int i = 0; i < 3; i++)
1177 keys.push_back(key);
1184 CScript& scriptPubKey = txFrom.
vout[0].scriptPubKey;
1215 scriptSigCopy = scriptSig;
1232 std::vector<unsigned char> sig1;
1236 std::vector<unsigned char> sig2;
1240 std::vector<unsigned char> sig3;
1284 for (
int i=0; i<67000; i++) {
1287 BOOST_CHECK_MESSAGE(script.
IsPushOnly(),
"Number " << i <<
" is not pure push.");
1293 std::vector<unsigned char> data(i,
'\111');
1296 BOOST_CHECK_MESSAGE(script.
IsPushOnly(),
"Length " << i <<
" is not pure push.");
1309 static const unsigned char direct[] = { 1 };
1320 std::string derSig(
"304502207fa7a6d1e0ee81132a269ad84e68d695483745cde8b541e3bf630749894e342a022100c1f7ab20e13e22fb95281a870f3dcf38d782e53023ee313d741ad0cfbc0c5090");
1321 std::string pubKey(
"03b0da749730dc9b4b1f4a14d6902877a92541f5368778853d9c4a0cb7802dcfb2");
1345 std::vector<unsigned char> data =
ParseHex(str);
1346 return CScript(data.begin(), data.end());
1462 script =
ScriptFromHex(
"76a9141234567890abcdefa1a2a3a4a5a6a7a8a9a0aaab88ac");
1464 script =
ScriptFromHex(
"76a914ff34567890abcdefa1a2a3a4a5a6a7a8a9a0aaab88ac");
1482 std::vector<CTxOut> prevouts;
1483 for (
size_t i = 0; i < univalue.
size(); ++i) {
1486 prevouts.push_back(std::move(txout));
1495 for (
size_t i = 0; i < univalue.
size(); ++i) {
1496 auto bytes =
ParseHex(univalue[i].get_str());
1497 scriptwitness.
stack.push_back(std::move(bytes));
1499 return scriptwitness;
1502#if defined(HAVE_CONSENSUS_LIB)
1507 unsigned int libconsensus_flags = 0;
1514 scriptPubKey <<
OP_1;
1530 unsigned int libconsensus_flags = 0;
1553 unsigned int libconsensus_flags = 0;
1576 unsigned int libconsensus_flags = 0;
1588 stream << 0xffffffff;
1622 unsigned int libconsensus_flags = 1 << 3;
1646 std::vector<unsigned int> ret;
1648 for (
unsigned int i = 0; i < 128; ++i) {
1649 unsigned int flag = 0;
1663 ret.push_back(flag);
1677 const std::vector<CTxOut> prevouts =
TxOutsFromJSON(test[
"prevouts"]);
1683 if (test.
exists(
"success")) {
1684 mtx.
vin[idx].scriptSig =
ScriptFromHex(test[
"success"][
"scriptSig"].get_str());
1688 txdata.
Init(tx, std::vector<CTxOut>(prevouts));
1693 if (fin || ((
flags & test_flags) ==
flags)) {
1694 bool ret =
VerifyScript(tx.
vin[idx].scriptSig, prevouts[idx].scriptPubKey, &tx.
vin[idx].scriptWitness,
flags, txcheck,
nullptr);
1700 if (test.
exists(
"failure")) {
1701 mtx.
vin[idx].scriptSig =
ScriptFromHex(test[
"failure"][
"scriptSig"].get_str());
1705 txdata.
Init(tx, std::vector<CTxOut>(prevouts));
1709 if ((
flags & test_flags) == test_flags) {
1710 bool ret =
VerifyScript(tx.
vin[idx].scriptSig, prevouts[idx].scriptPubKey, &tx.
vin[idx].scriptWitness,
flags, txcheck,
nullptr);
1722 const char* dir = std::getenv(
"DIR_UNIT_TEST_DATA");
1723 BOOST_WARN_MESSAGE(dir !=
nullptr,
"Variable DIR_UNIT_TEST_DATA unset, skipping script_assets_test");
1724 if (dir ==
nullptr)
return;
1725 auto path =
fs::path(dir) /
"script_assets_test.json";
1727 BOOST_WARN_MESSAGE(
exists,
"File $DIR_UNIT_TEST_DATA/script_assets_test.json not found, skipping script_assets_test");
1731 file.seekg(0, std::ios::end);
1732 size_t length = file.tellg();
1733 file.seekg(0, std::ios::beg);
1734 std::string data(length,
'\0');
1735 file.read(data.data(), data.size());
1740 for (
size_t i = 0; i < tests.
size(); i++) {
int64_t CAmount
Amount in satoshis (Can be negative)
static CAmount AmountFromValue(const UniValue &value)
int bitcoinconsensus_verify_script(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, const unsigned char *txTo, unsigned int txToLen, unsigned int nIn, unsigned int flags, bitcoinconsensus_error *err)
Returns 1 if the input nIn of the serialized transaction pointed to by txTo correctly spends the scri...
int bitcoinconsensus_verify_script_with_amount(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, int64_t amount, const unsigned char *txTo, unsigned int txToLen, unsigned int nIn, unsigned int flags, bitcoinconsensus_error *err)
enum bitcoinconsensus_error_t bitcoinconsensus_error
@ bitcoinconsensus_ERR_OK
@ bitcoinconsensus_ERR_TX_DESERIALIZE
@ bitcoinconsensus_ERR_AMOUNT_REQUIRED
@ bitcoinconsensus_ERR_TX_INDEX
@ bitcoinconsensus_ERR_INVALID_FLAGS
@ bitcoinconsensus_ERR_TX_SIZE_MISMATCH
@ bitcoinconsensus_SCRIPT_FLAGS_VERIFY_ALL
@ bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS
Double ended buffer combining vector and stream-like interfaces.
A hasher class for Bitcoin's 160-bit hash (SHA-256 + RIPEMD-160).
void Finalize(Span< unsigned char > output)
CHash160 & Write(Span< const unsigned char > input)
An encapsulated private key.
bool Sign(const uint256 &hash, std::vector< unsigned char > &vchSig, bool grind=true, uint32_t test_case=0) const
Create a DER-serialized signature.
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
CPubKey GetPubKey() const
Compute the public key from a private key.
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
An encapsulated public key.
const unsigned char * data() const
const unsigned char * end() const
const unsigned char * begin() const
A hasher class for SHA-256.
void Finalize(unsigned char hash[OUTPUT_SIZE])
CSHA256 & Write(const unsigned char *data, size_t len)
Serialized script, used inside transaction inputs and outputs.
bool IsPushOnly(const_iterator pc) const
Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical).
bool HasValidOps() const
Check if the script contains valid OP_CODES.
A reference to a CScript: the Hash160 of its serialization (see script.h)
The basic transaction that is broadcasted on the network and contained in blocks.
const std::vector< CTxIn > vin
An output of a transaction.
Fillable signing provider that keeps keys in an address->secret map.
virtual bool AddCScript(const CScript &redeemScript)
virtual bool AddKey(const CKey &key)
A signature creator for transactions.
const std::string & get_str() const
int64_t get_int64() const
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
const UniValue & get_array() const
bool exists(const std::string &key) const
bool read(const char *raw, size_t len)
Minimal stream for reading from an existing vector by reference.
Path class wrapper to prepare application code for transition from boost::filesystem library to std::...
void push_back(const T &value)
CScript ParseScript(const std::string &s)
std::string FormatScript(const CScript &script)
std::string ScriptToAsmStr(const CScript &script, const bool fAttemptSighashDecode=false)
Create the assembly string representation of a CScript object.
UniValue ValueFromAmount(const CAmount amount)
BOOST_AUTO_TEST_SUITE_END()
int FindAndDelete(CScript &script, const CScript &b)
uint256 SignatureHash(const CScript &scriptCode, const T &txTo, unsigned int nIn, int nHashType, const CAmount &amount, SigVersion sigversion, const PrecomputedTransactionData *cache)
bool EvalScript(std::vector< std::vector< unsigned char > > &stack, const CScript &script, unsigned int flags, const BaseSignatureChecker &checker, SigVersion sigversion, ScriptExecutionData &execdata, ScriptError *serror)
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
@ BASE
Bare scripts and BIP16 P2SH-wrapped redeemscripts.
@ WITNESS_V0
Witness v0 (P2WPKH and P2WSH); see BIP 141.
@ SCRIPT_VERIFY_NULLDUMMY
@ SCRIPT_VERIFY_SIGPUSHONLY
@ SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY
@ SCRIPT_VERIFY_WITNESS_PUBKEYTYPE
@ SCRIPT_VERIFY_STRICTENC
@ SCRIPT_VERIFY_CLEANSTACK
@ SCRIPT_VERIFY_MINIMALDATA
@ SCRIPT_VERIFY_CHECKSEQUENCEVERIFY
@ SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM
GenericTransactionSignatureChecker< CMutableTransaction > MutableTransactionSignatureChecker
@ SIGHASH_DEFAULT
Taproot only; implied when sighash byte is missing, and equivalent to SIGHASH_ALL.
@ ASSERT_FAIL
Abort execution through assertion failure (for consensus code)
static bool exists(const path &p)
FILE * fopen(const fs::path &p, const char *mode)
static unsigned const char script_tests[]
#define BOOST_FIXTURE_TEST_SUITE(a, b)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
static const int SERIALIZE_TRANSACTION_NO_WITNESS
A flag that is ORed into the protocol version to designate that a transaction should be (un)serialize...
static CTransactionRef MakeTransactionRef(Tx &&txIn)
std::shared_ptr< const CTransaction > CTransactionRef
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
opcodetype
Script opcodes.
std::vector< unsigned char > ToByteVector(const T &in)
std::string ScriptErrorString(const ScriptError serror)
enum ScriptError_t ScriptError
@ SCRIPT_ERR_OP_CODESEPARATOR
@ SCRIPT_ERR_SIG_PUSHONLY
@ SCRIPT_ERR_NUMEQUALVERIFY
@ SCRIPT_ERR_DISABLED_OPCODE
@ SCRIPT_ERR_INVALID_ALTSTACK_OPERATION
@ SCRIPT_ERR_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM
@ SCRIPT_ERR_UNKNOWN_ERROR
@ SCRIPT_ERR_WITNESS_PROGRAM_WRONG_LENGTH
@ SCRIPT_ERR_SIG_HASHTYPE
@ SCRIPT_ERR_CHECKSIGVERIFY
@ SCRIPT_ERR_WITNESS_MALLEATED_P2SH
@ SCRIPT_ERR_WITNESS_MALLEATED
@ SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS
@ SCRIPT_ERR_INVALID_STACK_OPERATION
@ SCRIPT_ERR_WITNESS_UNEXPECTED
@ SCRIPT_ERR_NEGATIVE_LOCKTIME
@ SCRIPT_ERR_WITNESS_PROGRAM_MISMATCH
@ SCRIPT_ERR_SIG_NULLFAIL
@ SCRIPT_ERR_SIG_NULLDUMMY
@ SCRIPT_ERR_CHECKMULTISIGVERIFY
@ SCRIPT_ERR_UNSATISFIED_LOCKTIME
@ SCRIPT_ERR_WITNESS_PUBKEYTYPE
@ SCRIPT_ERR_SIG_FINDANDDELETE
@ SCRIPT_ERR_PUBKEY_COUNT
@ SCRIPT_ERR_WITNESS_PROGRAM_WITNESS_EMPTY
@ SCRIPT_ERR_UNBALANCED_CONDITIONAL
static std::string FormatScriptError(ScriptError_t err)
static void NegateSignatureS(std::vector< unsigned char > &vchSig)
static ScriptError_t ParseScriptError(const std::string &name)
static CScript ScriptFromHex(const std::string &str)
SignatureData CombineSignatures(const CTxOut &txout, const CMutableTransaction &tx, const SignatureData &scriptSig1, const SignatureData &scriptSig2)
static ScriptErrorDesc script_errors[]
static CMutableTransaction TxFromHex(const std::string &str)
void DoTest(const CScript &scriptPubKey, const CScript &scriptSig, const CScriptWitness &scriptWitness, uint32_t flags, const std::string &message, int scriptError, CAmount nValue=0)
UniValue read_json(const std::string &jsondata)
static void AssetTest(const UniValue &test)
static CScript sign_multisig(const CScript &scriptPubKey, const std::vector< CKey > &keys, const CTransaction &transaction)
unsigned int ParseScriptFlags(std::string strFlags)
static std::vector< unsigned int > AllConsensusFlags()
BOOST_AUTO_TEST_CASE(script_build)
std::string FormatScriptFlags(unsigned int flags)
static const unsigned int gFlags
static std::vector< CTxOut > TxOutsFromJSON(const UniValue &univalue)
static CScriptWitness ScriptWitnessFromJSON(const UniValue &univalue)
static const std::vector< unsigned int > ALL_CONSENSUS_FLAGS
Precomputed list of all valid combinations of consensus-relevant script validation flags.
static uint64_t InsecureRandBits(int bits)
bool ProduceSignature(const SigningProvider &provider, const BaseSignatureCreator &creator, const CScript &fromPubKey, SignatureData &sigdata)
Produce a script signature using a generic signature creator.
bool SignSignature(const SigningProvider &provider, const CScript &fromPubKey, CMutableTransaction &txTo, unsigned int nIn, const CAmount &amount, int nHashType)
Produce a script signature for a transaction.
static bool GetPubKey(const SigningProvider &provider, const SignatureData &sigdata, const CKeyID &address, CPubKey &pubkey)
SignatureData DataFromTransaction(const CMutableTransaction &tx, unsigned int nIn, const CTxOut &txout)
Extract signature data from a transaction input, and insert it.
std::pair< CPubKey, std::vector< unsigned char > > SigPair
const SigningProvider & DUMMY_SIGNING_PROVIDER
constexpr Span< A > MakeSpan(A(&a)[N])
MakeSpan for arrays:
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
std::vector< unsigned char > ParseHex(const char *psz)
A mutable version of CTransaction.
std::vector< CTxOut > vout
std::vector< std::vector< unsigned char > > stack
void Init(const T &tx, std::vector< CTxOut > &&spent_outputs, bool force=false)
Initialize this PrecomputedTransactionData with transaction data.
void MergeSignatureData(SignatureData sigdata)
std::map< CKeyID, SigPair > signatures
BIP 174 style partial signatures for the input. May contain all signatures necessary for producing a ...
CScript scriptSig
The scriptSig of an input. Contains complete signatures or the traditional partial signatures format.
CMutableTransaction BuildSpendingTransaction(const CScript &scriptSig, const CScriptWitness &scriptWitness, const CTransaction &txCredit)
CMutableTransaction BuildCreditingTransaction(const CScript &scriptPubKey, int nValue)
static const int PROTOCOL_VERSION
network protocol versioning