4 #include <boost/algorithm/string/classification.hpp>
5 #include <boost/algorithm/string/predicate.hpp>
6 #include <boost/algorithm/string/replace.hpp>
7 #include <boost/algorithm/string/split.hpp>
8 #include <boost/foreach.hpp>
9 #include <boost/preprocessor/stringize.hpp>
10 #include <boost/test/unit_test.hpp>
31 static map<string, opcodetype> mapOpNames;
33 if (mapOpNames.size() == 0)
38 if (strcmp(name,
"OP_UNKNOWN") == 0)
43 replace_first(strName,
"OP_",
"");
49 split(words, s, is_any_of(
" \t\n"), token_compress_on);
51 BOOST_FOREACH(
string w, words)
53 if (all(w, is_digit()) ||
54 (starts_with(w,
"-") && all(
string(w.begin()+1, w.end()), is_digit())))
60 else if (starts_with(w,
"0x") &&
IsHex(
string(w.begin()+2, w.end())))
63 std::vector<unsigned char> raw =
ParseHex(
string(w.begin()+2, w.end()));
64 result.insert(result.end(), raw.begin(), raw.end());
66 else if (w.size() >= 2 && starts_with(w,
"'") && ends_with(w,
"'"))
70 std::vector<unsigned char>
value(w.begin()+1, w.end()-1);
73 else if (mapOpNames.count(w))
76 result << mapOpNames[w];
80 BOOST_ERROR(
"Parse error: " << s);
91 namespace fs = boost::filesystem;
92 fs::path testFile = fs::current_path() /
"test" /
"data" / filename;
95 if (!fs::exists(testFile))
97 testFile = fs::path(BOOST_PP_STRINGIZE(TEST_DATA_DIR)) / filename;
101 ifstream ifs(testFile.string().c_str(), ifstream::in);
106 BOOST_ERROR(
"Cound not find/open " << filename);
108 BOOST_ERROR(
"JSON syntax error in " << filename);
113 BOOST_ERROR(filename <<
" does not contain a json array");
117 return v.get_array();
120 BOOST_AUTO_TEST_SUITE(script_tests)
131 BOOST_FOREACH(
Value& tv, tests)
137 BOOST_ERROR(
"Bad test: " << strTest);
140 string scriptSigString = test[0].get_str();
142 string scriptPubKeyString = test[1].get_str();
155 BOOST_FOREACH(
Value& tv, tests)
161 BOOST_ERROR(
"Bad test: " << strTest);
164 string scriptSigString = test[0].get_str();
166 string scriptPubKeyString = test[1].get_str();
178 static const unsigned char direct[] = { 1, 0x5a };
179 static const unsigned char pushdata1[] = {
OP_PUSHDATA1, 1, 0x5a };
180 static const unsigned char pushdata2[] = {
OP_PUSHDATA2, 1, 0, 0x5a };
181 static const unsigned char pushdata4[] = {
OP_PUSHDATA4, 1, 0, 0, 0, 0x5a };
183 vector<vector<unsigned char> > directStack;
186 vector<vector<unsigned char> > pushdata1Stack;
188 BOOST_CHECK(pushdata1Stack == directStack);
190 vector<vector<unsigned char> > pushdata2Stack;
192 BOOST_CHECK(pushdata2Stack == directStack);
194 vector<vector<unsigned char> > pushdata4Stack;
196 BOOST_CHECK(pushdata4Stack == directStack);
214 BOOST_FOREACH(
const CKey &key, keys)
216 vector<unsigned char> vchSig;
217 BOOST_CHECK(key.
Sign(hash, vchSig));
226 std::vector<CKey> keys;
233 CKey key1, key2, key3;
242 txFrom12.
vout.resize(1);
243 txFrom12.
vout[0].scriptPubKey = scriptPubKey12;
246 txTo12.
vin.resize(1);
247 txTo12.
vout.resize(1);
248 txTo12.
vin[0].prevout.n = 0;
249 txTo12.
vin[0].prevout.hash = txFrom12.
GetHash();
250 txTo12.
vout[0].nValue = 1;
253 BOOST_CHECK(
VerifyScript(goodsig1, scriptPubKey12, txTo12, 0, flags, 0));
254 txTo12.
vout[0].nValue = 2;
255 BOOST_CHECK(!
VerifyScript(goodsig1, scriptPubKey12, txTo12, 0, flags, 0));
258 BOOST_CHECK(
VerifyScript(goodsig2, scriptPubKey12, txTo12, 0, flags, 0));
261 BOOST_CHECK(!
VerifyScript(badsig1, scriptPubKey12, txTo12, 0, flags, 0));
266 CKey key1, key2, key3, key4;
276 txFrom23.
vout.resize(1);
277 txFrom23.
vout[0].scriptPubKey = scriptPubKey23;
280 txTo23.
vin.resize(1);
281 txTo23.
vout.resize(1);
282 txTo23.
vin[0].prevout.n = 0;
283 txTo23.
vin[0].prevout.hash = txFrom23.
GetHash();
284 txTo23.
vout[0].nValue = 1;
286 std::vector<CKey> keys;
287 keys.push_back(key1); keys.push_back(key2);
289 BOOST_CHECK(
VerifyScript(goodsig1, scriptPubKey23, txTo23, 0, flags, 0));
292 keys.push_back(key1); keys.push_back(key3);
294 BOOST_CHECK(
VerifyScript(goodsig2, scriptPubKey23, txTo23, 0, flags, 0));
297 keys.push_back(key2); keys.push_back(key3);
299 BOOST_CHECK(
VerifyScript(goodsig3, scriptPubKey23, txTo23, 0, flags, 0));
302 keys.push_back(key2); keys.push_back(key2);
304 BOOST_CHECK(!
VerifyScript(badsig1, scriptPubKey23, txTo23, 0, flags, 0));
307 keys.push_back(key2); keys.push_back(key1);
309 BOOST_CHECK(!
VerifyScript(badsig2, scriptPubKey23, txTo23, 0, flags, 0));
312 keys.push_back(key3); keys.push_back(key2);
314 BOOST_CHECK(!
VerifyScript(badsig3, scriptPubKey23, txTo23, 0, flags, 0));
317 keys.push_back(key4); keys.push_back(key2);
319 BOOST_CHECK(!
VerifyScript(badsig4, scriptPubKey23, txTo23, 0, flags, 0));
322 keys.push_back(key1); keys.push_back(key4);
324 BOOST_CHECK(!
VerifyScript(badsig5, scriptPubKey23, txTo23, 0, flags, 0));
328 BOOST_CHECK(!
VerifyScript(badsig6, scriptPubKey23, txTo23, 0, flags, 0));
336 vector<CPubKey> pubkeys;
337 for (
int i = 0; i < 3; i++)
347 txFrom.
vout.resize(1);
348 txFrom.
vout[0].scriptPubKey.SetDestination(keys[0].GetPubKey().GetID());
349 CScript& scriptPubKey = txFrom.
vout[0].scriptPubKey;
353 txTo.
vin[0].prevout.n = 0;
356 txTo.
vout[0].nValue = 1;
360 BOOST_CHECK(combined.empty());
365 BOOST_CHECK(combined == scriptSig);
367 BOOST_CHECK(combined == scriptSig);
368 CScript scriptSigCopy = scriptSig;
372 BOOST_CHECK(combined == scriptSigCopy || combined == scriptSig);
380 BOOST_CHECK(combined == scriptSig);
382 BOOST_CHECK(combined == scriptSig);
383 scriptSigCopy = scriptSig;
386 BOOST_CHECK(combined == scriptSigCopy || combined == scriptSig);
388 scriptSigCopy =
CScript() << OP_0 << static_cast<vector<unsigned char> >(pkSingle);
390 BOOST_CHECK(combined == scriptSig);
392 BOOST_CHECK(combined == scriptSig);
399 BOOST_CHECK(combined == scriptSig);
401 BOOST_CHECK(combined == scriptSig);
404 vector<unsigned char> sig1;
406 BOOST_CHECK(keys[0].Sign(hash1, sig1));
408 vector<unsigned char> sig2;
410 BOOST_CHECK(keys[1].Sign(hash2, sig2));
412 vector<unsigned char> sig3;
414 BOOST_CHECK(keys[2].Sign(hash3, sig3));
430 BOOST_CHECK(combined == partial1a);
432 BOOST_CHECK(combined == complete12);
434 BOOST_CHECK(combined == complete12);
436 BOOST_CHECK(combined == complete12);
438 BOOST_CHECK(combined == complete13);
440 BOOST_CHECK(combined == complete23);
442 BOOST_CHECK(combined == complete23);
444 BOOST_CHECK(combined == partial3c);
447 BOOST_AUTO_TEST_SUITE_END()
uint256 SignatureHash(CScript scriptCode, const CTransaction &txTo, unsigned int nIn, int nHashType)
CScript sign_multisig(CScript scriptPubKey, std::vector< CKey > keys, CTransaction transaction)
CScript CombineSignatures(CScript scriptPubKey, const CTransaction &txTo, unsigned int nIn, const CScript &scriptSig1, const CScript &scriptSig2)
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CTransaction &txTo, unsigned int nIn, unsigned int flags, int nHashType)
bool SignSignature(const CKeyStore &keystore, const CScript &fromPubKey, CTransaction &txTo, unsigned int nIn, int nHashType)
bool Sign(const uint256 &hash, std::vector< unsigned char > &vchSig) const
bool IsHex(const string &str)
bool read_stream(Istream_type &is, Value_type &value)
int64 atoi64(const char *psz)
const Array & get_array() const
void SetDestination(const CTxDestination &address)
virtual bool AddCScript(const CScript &redeemScript)
opcodetype
Script opcodes.
BOOST_AUTO_TEST_CASE(script_valid)
CPubKey GetPubKey() const
std::vector< CTxOut > vout
void MakeNewKey(bool fCompressed)
bool EvalScript(vector< vector< unsigned char > > &stack, const CScript &script, const CTransaction &txTo, unsigned int nIn, unsigned int flags, int nHashType)
virtual bool AddKey(const CKey &key)
const char * GetOpName(opcodetype opcode)
Value_type::String_type write_string(const Value_type &value, bool pretty)
CScript ParseScript(string s)
Serialized script, used inside transaction inputs and outputs.
void SetMultisig(int nRequired, const std::vector< CPubKey > &keys)
An encapsulated private key.
The basic transaction that is broadcasted on the network and contained in blocks. ...
Array read_json(const std::string &filename)
vector< unsigned char > ParseHex(const char *psz)
Basic key store, that keeps keys in an address->secret map.