43std::vector<unsigned char> CheckedParseHex(
const std::string& str)
45 if (str.size() && !
IsHex(str))
throw std::runtime_error(
"Non-hex input '" + str +
"'");
51 std::vector<unsigned char>
data = CheckedParseHex(str);
60 }
catch (
const std::ios_base::failure&) {
61 throw std::runtime_error(
"Tx deserialization failure");
68 if (!univalue.
isArray())
throw std::runtime_error(
"Prevouts must be array");
69 std::vector<CTxOut> prevouts;
70 for (
size_t i = 0; i < univalue.
size(); ++i) {
73 SpanReader{CheckedParseHex(univalue[i].get_str())} >> txout;
74 }
catch (
const std::ios_base::failure&) {
75 throw std::runtime_error(
"Prevout invalid format");
77 prevouts.push_back(std::move(txout));
84 if (!univalue.
isArray())
throw std::runtime_error(
"Script witness is not array");
86 for (
size_t i = 0; i < univalue.
size(); ++i) {
87 auto bytes = CheckedParseHex(univalue[i].get_str());
88 scriptwitness.
stack.push_back(std::move(bytes));
93const std::map<std::string, unsigned int> FLAG_NAMES = {
103std::vector<unsigned int> AllFlags()
105 std::vector<unsigned int>
ret;
107 for (
unsigned int i = 0; i < 128; ++i) {
108 unsigned int flag = 0;
128const std::vector<unsigned int> ALL_FLAGS = AllFlags();
132 if (str.empty())
return 0;
134 unsigned int flags = 0;
135 std::vector<std::string> words =
SplitString(str,
',');
137 for (
const std::string& word : words) {
138 auto it = FLAG_NAMES.find(word);
139 if (it == FLAG_NAMES.end())
throw std::runtime_error(
"Unknown verification flag " + word);
146void Test(
const std::string& str)
149 if (!test.
read(str) || !test.
isObject())
throw std::runtime_error(
"Non-object test input");
152 const std::vector<CTxOut> prevouts =
TxOutsFromJSON(test[
"prevouts"]);
153 if (prevouts.size() != tx.
vin.size())
throw std::runtime_error(
"Incorrect number of prevouts");
154 size_t idx = test[
"index"].
getInt<int64_t>();
155 if (idx >= tx.
vin.size())
throw std::runtime_error(
"Invalid index");
159 if (test.
exists(
"success")) {
160 tx.
vin[idx].scriptSig =
ScriptFromHex(test[
"success"][
"scriptSig"].get_str());
163 txdata.
Init(tx, std::vector<CTxOut>(prevouts));
165 for (
const auto flags : ALL_FLAGS) {
168 if (
final || ((
flags & test_flags) ==
flags)) {
169 (void)
VerifyScript(tx.
vin[idx].scriptSig, prevouts[idx].scriptPubKey, &tx.
vin[idx].scriptWitness,
flags, txcheck,
nullptr);
174 if (test.
exists(
"failure")) {
175 tx.
vin[idx].scriptSig =
ScriptFromHex(test[
"failure"][
"scriptSig"].get_str());
178 txdata.
Init(tx, std::vector<CTxOut>(prevouts));
180 for (
const auto flags : ALL_FLAGS) {
182 if ((
flags & test_flags) == test_flags) {
183 (void)
VerifyScript(tx.
vin[idx].scriptSig, prevouts[idx].scriptPubKey, &tx.
vin[idx].scriptWitness,
flags, txcheck,
nullptr);
191FUZZ_TARGET(script_assets_test_minimizer, .
init = test_init, .hidden =
true)
193 if (buffer.size() < 2 || buffer.back() !=
'\n' || buffer[buffer.size() - 2] !=
',')
return;
194 const std::string str((
const char*)buffer.data(), buffer.size() - 2);
197 }
catch (
const std::runtime_error&) {
Serialized script, used inside transaction inputs and outputs.
An output of a transaction.
Minimal stream for reading from an existing byte array by Span.
bool read(std::string_view raw)
bool exists(const std::string &key) const
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
@ SCRIPT_VERIFY_NULLDUMMY
@ SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY
@ SCRIPT_VERIFY_CHECKSEQUENCEVERIFY
@ ASSERT_FAIL
Abort execution through assertion failure (for consensus code)
std::vector< std::string > SplitString(std::string_view str, char sep)
static constexpr TransactionSerParams TX_NO_WITNESS
static CScript ScriptFromHex(const std::string &str)
static CMutableTransaction TxFromHex(const std::string &str)
unsigned int ParseScriptFlags(std::string strFlags)
static std::vector< CTxOut > TxOutsFromJSON(const UniValue &univalue)
static CScriptWitness ScriptWitnessFromJSON(const UniValue &univalue)
std::vector< Byte > ParseHex(std::string_view hex_str)
Like TryParseHex, but returns an empty vector on invalid input.
A mutable version of CTransaction.
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.
bool IsHex(std::string_view str)