script.cpp 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938
  1. // ECOin - Copyright (c) - 2014/2022 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #include <boost/foreach.hpp>
  3. #include <boost/tuple/tuple.hpp>
  4. using namespace std;
  5. using namespace boost;
  6. #include "script.h"
  7. #include "keystore.h"
  8. #include "bignum.h"
  9. #include "key.h"
  10. #include "main.h"
  11. #include "sync.h"
  12. #include "util.h"
  13. bool CheckSig(vector<unsigned char> vchSig, vector<unsigned char> vchPubKey, CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType);
  14. static const valtype vchFalse(0);
  15. static const valtype vchZero(0);
  16. static const valtype vchTrue(1, 1);
  17. static const CBigNum bnZero(0);
  18. static const CBigNum bnOne(1);
  19. static const CBigNum bnFalse(0);
  20. static const CBigNum bnTrue(1);
  21. static const size_t nMaxNumSize = 4;
  22. CBigNum CastToBigNum(const valtype& vch)
  23. {
  24. if (vch.size() > nMaxNumSize)
  25. throw runtime_error("CastToBigNum() : overflow");
  26. // Get rid of extra leading zeros
  27. return CBigNum(CBigNum(vch).getvch());
  28. }
  29. bool CastToBool(const valtype& vch)
  30. {
  31. for (unsigned int i = 0; i < vch.size(); i++)
  32. {
  33. if (vch[i] != 0)
  34. {
  35. // Can be negative zero
  36. if (i == vch.size()-1 && vch[i] == 0x80)
  37. return false;
  38. return true;
  39. }
  40. }
  41. return false;
  42. }
  43. //
  44. // WARNING: This does not work as expected for signed integers; the sign-bit
  45. // is left in place as the integer is zero-extended. The correct behavior
  46. // would be to move the most significant bit of the last byte during the
  47. // resize process. MakeSameSize() is currently only used by the disabled
  48. // opcodes OP_AND, OP_OR, and OP_XOR.
  49. //
  50. void MakeSameSize(valtype& vch1, valtype& vch2)
  51. {
  52. // Lengthen the shorter one
  53. if (vch1.size() < vch2.size())
  54. // PATCH:
  55. // +unsigned char msb = vch1[vch1.size()-1];
  56. // +vch1[vch1.size()-1] &= 0x7f;
  57. // vch1.resize(vch2.size(), 0);
  58. // +vch1[vch1.size()-1] = msb;
  59. vch1.resize(vch2.size(), 0);
  60. if (vch2.size() < vch1.size())
  61. // PATCH:
  62. // +unsigned char msb = vch2[vch2.size()-1];
  63. // +vch2[vch2.size()-1] &= 0x7f;
  64. // vch2.resize(vch1.size(), 0);
  65. // +vch2[vch2.size()-1] = msb;
  66. vch2.resize(vch1.size(), 0);
  67. }
  68. //
  69. // Script is a stack machine (like Forth) that evaluates a predicate
  70. // returning a bool indicating valid or not. There are no loops.
  71. //
  72. #define stacktop(i) (stack.at(stack.size()+(i)))
  73. #define altstacktop(i) (altstack.at(altstack.size()+(i)))
  74. static inline void popstack(vector<valtype>& stack)
  75. {
  76. if (stack.empty())
  77. throw runtime_error("popstack() : stack empty");
  78. stack.pop_back();
  79. }
  80. const char* GetTxnOutputType(txnouttype t)
  81. {
  82. switch (t)
  83. {
  84. case TX_NONSTANDARD: return "nonstandard";
  85. case TX_PUBKEY: return "pubkey";
  86. case TX_PUBKEYHASH: return "pubkeyhash";
  87. case TX_SCRIPTHASH: return "scripthash";
  88. case TX_MULTISIG: return "multisig";
  89. }
  90. return NULL;
  91. }
  92. const char* GetOpName(opcodetype opcode)
  93. {
  94. switch (opcode)
  95. {
  96. // push value
  97. case OP_0 : return "0";
  98. case OP_PUSHDATA1 : return "OP_PUSHDATA1";
  99. case OP_PUSHDATA2 : return "OP_PUSHDATA2";
  100. case OP_PUSHDATA4 : return "OP_PUSHDATA4";
  101. case OP_1NEGATE : return "-1";
  102. case OP_RESERVED : return "OP_RESERVED";
  103. case OP_1 : return "1";
  104. case OP_2 : return "2";
  105. case OP_3 : return "3";
  106. case OP_4 : return "4";
  107. case OP_5 : return "5";
  108. case OP_6 : return "6";
  109. case OP_7 : return "7";
  110. case OP_8 : return "8";
  111. case OP_9 : return "9";
  112. case OP_10 : return "10";
  113. case OP_11 : return "11";
  114. case OP_12 : return "12";
  115. case OP_13 : return "13";
  116. case OP_14 : return "14";
  117. case OP_15 : return "15";
  118. case OP_16 : return "16";
  119. // control
  120. case OP_NOP : return "OP_NOP";
  121. case OP_VER : return "OP_VER";
  122. case OP_IF : return "OP_IF";
  123. case OP_NOTIF : return "OP_NOTIF";
  124. case OP_VERIF : return "OP_VERIF";
  125. case OP_VERNOTIF : return "OP_VERNOTIF";
  126. case OP_ELSE : return "OP_ELSE";
  127. case OP_ENDIF : return "OP_ENDIF";
  128. case OP_VERIFY : return "OP_VERIFY";
  129. case OP_RETURN : return "OP_RETURN";
  130. // stack ops
  131. case OP_TOALTSTACK : return "OP_TOALTSTACK";
  132. case OP_FROMALTSTACK : return "OP_FROMALTSTACK";
  133. case OP_2DROP : return "OP_2DROP";
  134. case OP_2DUP : return "OP_2DUP";
  135. case OP_3DUP : return "OP_3DUP";
  136. case OP_2OVER : return "OP_2OVER";
  137. case OP_2ROT : return "OP_2ROT";
  138. case OP_2SWAP : return "OP_2SWAP";
  139. case OP_IFDUP : return "OP_IFDUP";
  140. case OP_DEPTH : return "OP_DEPTH";
  141. case OP_DROP : return "OP_DROP";
  142. case OP_DUP : return "OP_DUP";
  143. case OP_NIP : return "OP_NIP";
  144. case OP_OVER : return "OP_OVER";
  145. case OP_PICK : return "OP_PICK";
  146. case OP_ROLL : return "OP_ROLL";
  147. case OP_ROT : return "OP_ROT";
  148. case OP_SWAP : return "OP_SWAP";
  149. case OP_TUCK : return "OP_TUCK";
  150. // splice ops
  151. case OP_CAT : return "OP_CAT";
  152. case OP_SUBSTR : return "OP_SUBSTR";
  153. case OP_LEFT : return "OP_LEFT";
  154. case OP_RIGHT : return "OP_RIGHT";
  155. case OP_SIZE : return "OP_SIZE";
  156. // bit logic
  157. case OP_INVERT : return "OP_INVERT";
  158. case OP_AND : return "OP_AND";
  159. case OP_OR : return "OP_OR";
  160. case OP_XOR : return "OP_XOR";
  161. case OP_EQUAL : return "OP_EQUAL";
  162. case OP_EQUALVERIFY : return "OP_EQUALVERIFY";
  163. case OP_RESERVED1 : return "OP_RESERVED1";
  164. case OP_RESERVED2 : return "OP_RESERVED2";
  165. // numeric
  166. case OP_1ADD : return "OP_1ADD";
  167. case OP_1SUB : return "OP_1SUB";
  168. case OP_2MUL : return "OP_2MUL";
  169. case OP_2DIV : return "OP_2DIV";
  170. case OP_NEGATE : return "OP_NEGATE";
  171. case OP_ABS : return "OP_ABS";
  172. case OP_NOT : return "OP_NOT";
  173. case OP_0NOTEQUAL : return "OP_0NOTEQUAL";
  174. case OP_ADD : return "OP_ADD";
  175. case OP_SUB : return "OP_SUB";
  176. case OP_MUL : return "OP_MUL";
  177. case OP_DIV : return "OP_DIV";
  178. case OP_MOD : return "OP_MOD";
  179. case OP_LSHIFT : return "OP_LSHIFT";
  180. case OP_RSHIFT : return "OP_RSHIFT";
  181. case OP_BOOLAND : return "OP_BOOLAND";
  182. case OP_BOOLOR : return "OP_BOOLOR";
  183. case OP_NUMEQUAL : return "OP_NUMEQUAL";
  184. case OP_NUMEQUALVERIFY : return "OP_NUMEQUALVERIFY";
  185. case OP_NUMNOTEQUAL : return "OP_NUMNOTEQUAL";
  186. case OP_LESSTHAN : return "OP_LESSTHAN";
  187. case OP_GREATERTHAN : return "OP_GREATERTHAN";
  188. case OP_LESSTHANOREQUAL : return "OP_LESSTHANOREQUAL";
  189. case OP_GREATERTHANOREQUAL : return "OP_GREATERTHANOREQUAL";
  190. case OP_MIN : return "OP_MIN";
  191. case OP_MAX : return "OP_MAX";
  192. case OP_WITHIN : return "OP_WITHIN";
  193. // crypto
  194. case OP_RIPEMD160 : return "OP_RIPEMD160";
  195. case OP_SHA1 : return "OP_SHA1";
  196. case OP_SHA256 : return "OP_SHA256";
  197. case OP_HASH160 : return "OP_HASH160";
  198. case OP_HASH256 : return "OP_HASH256";
  199. case OP_CODESEPARATOR : return "OP_CODESEPARATOR";
  200. case OP_CHECKSIG : return "OP_CHECKSIG";
  201. case OP_CHECKSIGVERIFY : return "OP_CHECKSIGVERIFY";
  202. case OP_CHECKMULTISIG : return "OP_CHECKMULTISIG";
  203. case OP_CHECKMULTISIGVERIFY : return "OP_CHECKMULTISIGVERIFY";
  204. // expanson
  205. case OP_NOP1 : return "OP_NOP1";
  206. case OP_NOP2 : return "OP_NOP2";
  207. case OP_NOP3 : return "OP_NOP3";
  208. case OP_NOP4 : return "OP_NOP4";
  209. case OP_NOP5 : return "OP_NOP5";
  210. case OP_NOP6 : return "OP_NOP6";
  211. case OP_NOP7 : return "OP_NOP7";
  212. case OP_NOP8 : return "OP_NOP8";
  213. case OP_NOP9 : return "OP_NOP9";
  214. case OP_NOP10 : return "OP_NOP10";
  215. // template matching params
  216. case OP_PUBKEYHASH : return "OP_PUBKEYHASH";
  217. case OP_PUBKEY : return "OP_PUBKEY";
  218. case OP_INVALIDOPCODE : return "OP_INVALIDOPCODE";
  219. default:
  220. return "OP_UNKNOWN";
  221. }
  222. }
  223. bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, int nHashType)
  224. {
  225. CAutoBN_CTX pctx;
  226. CScript::const_iterator pc = script.begin();
  227. CScript::const_iterator pend = script.end();
  228. CScript::const_iterator pbegincodehash = script.begin();
  229. opcodetype opcode;
  230. valtype vchPushValue;
  231. vector<bool> vfExec;
  232. vector<valtype> altstack;
  233. if (script.size() > 10000)
  234. return false;
  235. int nOpCount = 0;
  236. try
  237. {
  238. while (pc < pend)
  239. {
  240. bool fExec = !count(vfExec.begin(), vfExec.end(), false);
  241. //
  242. // Read instruction
  243. //
  244. if (!script.GetOp(pc, opcode, vchPushValue))
  245. return false;
  246. if (vchPushValue.size() > 520)
  247. return false;
  248. if (opcode > OP_16 && ++nOpCount > 201)
  249. return false;
  250. if (opcode == OP_CAT ||
  251. opcode == OP_SUBSTR ||
  252. opcode == OP_LEFT ||
  253. opcode == OP_RIGHT ||
  254. opcode == OP_INVERT ||
  255. opcode == OP_AND ||
  256. opcode == OP_OR ||
  257. opcode == OP_XOR ||
  258. opcode == OP_2MUL ||
  259. opcode == OP_2DIV ||
  260. opcode == OP_MUL ||
  261. opcode == OP_DIV ||
  262. opcode == OP_MOD ||
  263. opcode == OP_LSHIFT ||
  264. opcode == OP_RSHIFT)
  265. return false;
  266. if (fExec && 0 <= opcode && opcode <= OP_PUSHDATA4)
  267. stack.push_back(vchPushValue);
  268. else if (fExec || (OP_IF <= opcode && opcode <= OP_ENDIF))
  269. switch (opcode)
  270. {
  271. //
  272. // Push value
  273. //
  274. case OP_1NEGATE:
  275. case OP_1:
  276. case OP_2:
  277. case OP_3:
  278. case OP_4:
  279. case OP_5:
  280. case OP_6:
  281. case OP_7:
  282. case OP_8:
  283. case OP_9:
  284. case OP_10:
  285. case OP_11:
  286. case OP_12:
  287. case OP_13:
  288. case OP_14:
  289. case OP_15:
  290. case OP_16:
  291. {
  292. // ( -- value)
  293. CBigNum bn((int)opcode - (int)(OP_1 - 1));
  294. stack.push_back(bn.getvch());
  295. }
  296. break;
  297. //
  298. // Control
  299. //
  300. case OP_NOP:
  301. case OP_NOP1: case OP_NOP2: case OP_NOP3: case OP_NOP4: case OP_NOP5:
  302. case OP_NOP6: case OP_NOP7: case OP_NOP8: case OP_NOP9: case OP_NOP10:
  303. break;
  304. case OP_IF:
  305. case OP_NOTIF:
  306. {
  307. // <expression> if [statements] [else [statements]] endif
  308. bool fValue = false;
  309. if (fExec)
  310. {
  311. if (stack.size() < 1)
  312. return false;
  313. valtype& vch = stacktop(-1);
  314. fValue = CastToBool(vch);
  315. if (opcode == OP_NOTIF)
  316. fValue = !fValue;
  317. popstack(stack);
  318. }
  319. vfExec.push_back(fValue);
  320. }
  321. break;
  322. case OP_ELSE:
  323. {
  324. if (vfExec.empty())
  325. return false;
  326. vfExec.back() = !vfExec.back();
  327. }
  328. break;
  329. case OP_ENDIF:
  330. {
  331. if (vfExec.empty())
  332. return false;
  333. vfExec.pop_back();
  334. }
  335. break;
  336. case OP_VERIFY:
  337. {
  338. // (true -- ) or
  339. // (false -- false) and return
  340. if (stack.size() < 1)
  341. return false;
  342. bool fValue = CastToBool(stacktop(-1));
  343. if (fValue)
  344. popstack(stack);
  345. else
  346. return false;
  347. }
  348. break;
  349. case OP_RETURN:
  350. {
  351. return false;
  352. }
  353. break;
  354. //
  355. // Stack ops
  356. //
  357. case OP_TOALTSTACK:
  358. {
  359. if (stack.size() < 1)
  360. return false;
  361. altstack.push_back(stacktop(-1));
  362. popstack(stack);
  363. }
  364. break;
  365. case OP_FROMALTSTACK:
  366. {
  367. if (altstack.size() < 1)
  368. return false;
  369. stack.push_back(altstacktop(-1));
  370. popstack(altstack);
  371. }
  372. break;
  373. case OP_2DROP:
  374. {
  375. // (x1 x2 -- )
  376. if (stack.size() < 2)
  377. return false;
  378. popstack(stack);
  379. popstack(stack);
  380. }
  381. break;
  382. case OP_2DUP:
  383. {
  384. // (x1 x2 -- x1 x2 x1 x2)
  385. if (stack.size() < 2)
  386. return false;
  387. valtype vch1 = stacktop(-2);
  388. valtype vch2 = stacktop(-1);
  389. stack.push_back(vch1);
  390. stack.push_back(vch2);
  391. }
  392. break;
  393. case OP_3DUP:
  394. {
  395. // (x1 x2 x3 -- x1 x2 x3 x1 x2 x3)
  396. if (stack.size() < 3)
  397. return false;
  398. valtype vch1 = stacktop(-3);
  399. valtype vch2 = stacktop(-2);
  400. valtype vch3 = stacktop(-1);
  401. stack.push_back(vch1);
  402. stack.push_back(vch2);
  403. stack.push_back(vch3);
  404. }
  405. break;
  406. case OP_2OVER:
  407. {
  408. // (x1 x2 x3 x4 -- x1 x2 x3 x4 x1 x2)
  409. if (stack.size() < 4)
  410. return false;
  411. valtype vch1 = stacktop(-4);
  412. valtype vch2 = stacktop(-3);
  413. stack.push_back(vch1);
  414. stack.push_back(vch2);
  415. }
  416. break;
  417. case OP_2ROT:
  418. {
  419. // (x1 x2 x3 x4 x5 x6 -- x3 x4 x5 x6 x1 x2)
  420. if (stack.size() < 6)
  421. return false;
  422. valtype vch1 = stacktop(-6);
  423. valtype vch2 = stacktop(-5);
  424. stack.erase(stack.end()-6, stack.end()-4);
  425. stack.push_back(vch1);
  426. stack.push_back(vch2);
  427. }
  428. break;
  429. case OP_2SWAP:
  430. {
  431. // (x1 x2 x3 x4 -- x3 x4 x1 x2)
  432. if (stack.size() < 4)
  433. return false;
  434. swap(stacktop(-4), stacktop(-2));
  435. swap(stacktop(-3), stacktop(-1));
  436. }
  437. break;
  438. case OP_IFDUP:
  439. {
  440. // (x - 0 | x x)
  441. if (stack.size() < 1)
  442. return false;
  443. valtype vch = stacktop(-1);
  444. if (CastToBool(vch))
  445. stack.push_back(vch);
  446. }
  447. break;
  448. case OP_DEPTH:
  449. {
  450. // -- stacksize
  451. CBigNum bn(stack.size());
  452. stack.push_back(bn.getvch());
  453. }
  454. break;
  455. case OP_DROP:
  456. {
  457. // (x -- )
  458. if (stack.size() < 1)
  459. return false;
  460. popstack(stack);
  461. }
  462. break;
  463. case OP_DUP:
  464. {
  465. // (x -- x x)
  466. if (stack.size() < 1)
  467. return false;
  468. valtype vch = stacktop(-1);
  469. stack.push_back(vch);
  470. }
  471. break;
  472. case OP_NIP:
  473. {
  474. // (x1 x2 -- x2)
  475. if (stack.size() < 2)
  476. return false;
  477. stack.erase(stack.end() - 2);
  478. }
  479. break;
  480. case OP_OVER:
  481. {
  482. // (x1 x2 -- x1 x2 x1)
  483. if (stack.size() < 2)
  484. return false;
  485. valtype vch = stacktop(-2);
  486. stack.push_back(vch);
  487. }
  488. break;
  489. case OP_PICK:
  490. case OP_ROLL:
  491. {
  492. // (xn ... x2 x1 x0 n - xn ... x2 x1 x0 xn)
  493. // (xn ... x2 x1 x0 n - ... x2 x1 x0 xn)
  494. if (stack.size() < 2)
  495. return false;
  496. int n = CastToBigNum(stacktop(-1)).getint();
  497. popstack(stack);
  498. if (n < 0 || n >= (int)stack.size())
  499. return false;
  500. valtype vch = stacktop(-n-1);
  501. if (opcode == OP_ROLL)
  502. stack.erase(stack.end()-n-1);
  503. stack.push_back(vch);
  504. }
  505. break;
  506. case OP_ROT:
  507. {
  508. // (x1 x2 x3 -- x2 x3 x1)
  509. // x2 x1 x3 after first swap
  510. // x2 x3 x1 after second swap
  511. if (stack.size() < 3)
  512. return false;
  513. swap(stacktop(-3), stacktop(-2));
  514. swap(stacktop(-2), stacktop(-1));
  515. }
  516. break;
  517. case OP_SWAP:
  518. {
  519. // (x1 x2 -- x2 x1)
  520. if (stack.size() < 2)
  521. return false;
  522. swap(stacktop(-2), stacktop(-1));
  523. }
  524. break;
  525. case OP_TUCK:
  526. {
  527. // (x1 x2 -- x2 x1 x2)
  528. if (stack.size() < 2)
  529. return false;
  530. valtype vch = stacktop(-1);
  531. stack.insert(stack.end()-2, vch);
  532. }
  533. break;
  534. //
  535. // Splice ops
  536. //
  537. case OP_CAT:
  538. {
  539. // (x1 x2 -- out)
  540. if (stack.size() < 2)
  541. return false;
  542. valtype& vch1 = stacktop(-2);
  543. valtype& vch2 = stacktop(-1);
  544. vch1.insert(vch1.end(), vch2.begin(), vch2.end());
  545. popstack(stack);
  546. if (stacktop(-1).size() > 520)
  547. return false;
  548. }
  549. break;
  550. case OP_SUBSTR:
  551. {
  552. // (in begin size -- out)
  553. if (stack.size() < 3)
  554. return false;
  555. valtype& vch = stacktop(-3);
  556. int nBegin = CastToBigNum(stacktop(-2)).getint();
  557. int nEnd = nBegin + CastToBigNum(stacktop(-1)).getint();
  558. if (nBegin < 0 || nEnd < nBegin)
  559. return false;
  560. if (nBegin > (int)vch.size())
  561. nBegin = vch.size();
  562. if (nEnd > (int)vch.size())
  563. nEnd = vch.size();
  564. vch.erase(vch.begin() + nEnd, vch.end());
  565. vch.erase(vch.begin(), vch.begin() + nBegin);
  566. popstack(stack);
  567. popstack(stack);
  568. }
  569. break;
  570. case OP_LEFT:
  571. case OP_RIGHT:
  572. {
  573. // (in size -- out)
  574. if (stack.size() < 2)
  575. return false;
  576. valtype& vch = stacktop(-2);
  577. int nSize = CastToBigNum(stacktop(-1)).getint();
  578. if (nSize < 0)
  579. return false;
  580. if (nSize > (int)vch.size())
  581. nSize = vch.size();
  582. if (opcode == OP_LEFT)
  583. vch.erase(vch.begin() + nSize, vch.end());
  584. else
  585. vch.erase(vch.begin(), vch.end() - nSize);
  586. popstack(stack);
  587. }
  588. break;
  589. case OP_SIZE:
  590. {
  591. // (in -- in size)
  592. if (stack.size() < 1)
  593. return false;
  594. CBigNum bn(stacktop(-1).size());
  595. stack.push_back(bn.getvch());
  596. }
  597. break;
  598. //
  599. // Bitwise logic
  600. //
  601. case OP_INVERT:
  602. {
  603. // (in - out)
  604. if (stack.size() < 1)
  605. return false;
  606. valtype& vch = stacktop(-1);
  607. for (unsigned int i = 0; i < vch.size(); i++)
  608. vch[i] = ~vch[i];
  609. }
  610. break;
  611. //
  612. // WARNING: These disabled opcodes exhibit unexpected behavior
  613. // when used on signed integers due to a bug in MakeSameSize()
  614. // [see definition of MakeSameSize() above].
  615. //
  616. case OP_AND:
  617. case OP_OR:
  618. case OP_XOR:
  619. {
  620. // (x1 x2 - out)
  621. if (stack.size() < 2)
  622. return false;
  623. valtype& vch1 = stacktop(-2);
  624. valtype& vch2 = stacktop(-1);
  625. MakeSameSize(vch1, vch2); // <-- NOT SAFE FOR SIGNED VALUES
  626. if (opcode == OP_AND)
  627. {
  628. for (unsigned int i = 0; i < vch1.size(); i++)
  629. vch1[i] &= vch2[i];
  630. }
  631. else if (opcode == OP_OR)
  632. {
  633. for (unsigned int i = 0; i < vch1.size(); i++)
  634. vch1[i] |= vch2[i];
  635. }
  636. else if (opcode == OP_XOR)
  637. {
  638. for (unsigned int i = 0; i < vch1.size(); i++)
  639. vch1[i] ^= vch2[i];
  640. }
  641. popstack(stack);
  642. }
  643. break;
  644. case OP_EQUAL:
  645. case OP_EQUALVERIFY:
  646. //case OP_NOTEQUAL: // use OP_NUMNOTEQUAL
  647. {
  648. // (x1 x2 - bool)
  649. if (stack.size() < 2)
  650. return false;
  651. valtype& vch1 = stacktop(-2);
  652. valtype& vch2 = stacktop(-1);
  653. bool fEqual = (vch1 == vch2);
  654. // OP_NOTEQUAL is disabled because it would be too easy to say
  655. // something like n != 1 and have some wiseguy pass in 1 with extra
  656. // zero bytes after it (numerically, 0x01 == 0x0001 == 0x000001)
  657. //if (opcode == OP_NOTEQUAL)
  658. // fEqual = !fEqual;
  659. popstack(stack);
  660. popstack(stack);
  661. stack.push_back(fEqual ? vchTrue : vchFalse);
  662. if (opcode == OP_EQUALVERIFY)
  663. {
  664. if (fEqual)
  665. popstack(stack);
  666. else
  667. return false;
  668. }
  669. }
  670. break;
  671. //
  672. // Numeric
  673. //
  674. case OP_1ADD:
  675. case OP_1SUB:
  676. case OP_2MUL:
  677. case OP_2DIV:
  678. case OP_NEGATE:
  679. case OP_ABS:
  680. case OP_NOT:
  681. case OP_0NOTEQUAL:
  682. {
  683. // (in -- out)
  684. if (stack.size() < 1)
  685. return false;
  686. CBigNum bn = CastToBigNum(stacktop(-1));
  687. switch (opcode)
  688. {
  689. case OP_1ADD: bn += bnOne; break;
  690. case OP_1SUB: bn -= bnOne; break;
  691. case OP_2MUL: bn <<= 1; break;
  692. case OP_2DIV: bn >>= 1; break;
  693. case OP_NEGATE: bn = -bn; break;
  694. case OP_ABS: if (bn < bnZero) bn = -bn; break;
  695. case OP_NOT: bn = (bn == bnZero); break;
  696. case OP_0NOTEQUAL: bn = (bn != bnZero); break;
  697. default: assert(!"invalid opcode"); break;
  698. }
  699. popstack(stack);
  700. stack.push_back(bn.getvch());
  701. }
  702. break;
  703. case OP_ADD:
  704. case OP_SUB:
  705. case OP_MUL:
  706. case OP_DIV:
  707. case OP_MOD:
  708. case OP_LSHIFT:
  709. case OP_RSHIFT:
  710. case OP_BOOLAND:
  711. case OP_BOOLOR:
  712. case OP_NUMEQUAL:
  713. case OP_NUMEQUALVERIFY:
  714. case OP_NUMNOTEQUAL:
  715. case OP_LESSTHAN:
  716. case OP_GREATERTHAN:
  717. case OP_LESSTHANOREQUAL:
  718. case OP_GREATERTHANOREQUAL:
  719. case OP_MIN:
  720. case OP_MAX:
  721. {
  722. // (x1 x2 -- out)
  723. if (stack.size() < 2)
  724. return false;
  725. CBigNum bn1 = CastToBigNum(stacktop(-2));
  726. CBigNum bn2 = CastToBigNum(stacktop(-1));
  727. CBigNum bn;
  728. switch (opcode)
  729. {
  730. case OP_ADD:
  731. bn = bn1 + bn2;
  732. break;
  733. case OP_SUB:
  734. bn = bn1 - bn2;
  735. break;
  736. case OP_MUL:
  737. if (!BN_mul(&bn, &bn1, &bn2, pctx))
  738. return false;
  739. break;
  740. case OP_DIV:
  741. if (!BN_div(&bn, NULL, &bn1, &bn2, pctx))
  742. return false;
  743. break;
  744. case OP_MOD:
  745. if (!BN_mod(&bn, &bn1, &bn2, pctx))
  746. return false;
  747. break;
  748. case OP_LSHIFT:
  749. if (bn2 < bnZero || bn2 > CBigNum(2048))
  750. return false;
  751. bn = bn1 << bn2.getulong();
  752. break;
  753. case OP_RSHIFT:
  754. if (bn2 < bnZero || bn2 > CBigNum(2048))
  755. return false;
  756. bn = bn1 >> bn2.getulong();
  757. break;
  758. case OP_BOOLAND: bn = (bn1 != bnZero && bn2 != bnZero); break;
  759. case OP_BOOLOR: bn = (bn1 != bnZero || bn2 != bnZero); break;
  760. case OP_NUMEQUAL: bn = (bn1 == bn2); break;
  761. case OP_NUMEQUALVERIFY: bn = (bn1 == bn2); break;
  762. case OP_NUMNOTEQUAL: bn = (bn1 != bn2); break;
  763. case OP_LESSTHAN: bn = (bn1 < bn2); break;
  764. case OP_GREATERTHAN: bn = (bn1 > bn2); break;
  765. case OP_LESSTHANOREQUAL: bn = (bn1 <= bn2); break;
  766. case OP_GREATERTHANOREQUAL: bn = (bn1 >= bn2); break;
  767. case OP_MIN: bn = (bn1 < bn2 ? bn1 : bn2); break;
  768. case OP_MAX: bn = (bn1 > bn2 ? bn1 : bn2); break;
  769. default: assert(!"invalid opcode"); break;
  770. }
  771. popstack(stack);
  772. popstack(stack);
  773. stack.push_back(bn.getvch());
  774. if (opcode == OP_NUMEQUALVERIFY)
  775. {
  776. if (CastToBool(stacktop(-1)))
  777. popstack(stack);
  778. else
  779. return false;
  780. }
  781. }
  782. break;
  783. case OP_WITHIN:
  784. {
  785. // (x min max -- out)
  786. if (stack.size() < 3)
  787. return false;
  788. CBigNum bn1 = CastToBigNum(stacktop(-3));
  789. CBigNum bn2 = CastToBigNum(stacktop(-2));
  790. CBigNum bn3 = CastToBigNum(stacktop(-1));
  791. bool fValue = (bn2 <= bn1 && bn1 < bn3);
  792. popstack(stack);
  793. popstack(stack);
  794. popstack(stack);
  795. stack.push_back(fValue ? vchTrue : vchFalse);
  796. }
  797. break;
  798. //
  799. // Crypto
  800. //
  801. case OP_RIPEMD160:
  802. case OP_SHA1:
  803. case OP_SHA256:
  804. case OP_HASH160:
  805. case OP_HASH256:
  806. {
  807. // (in -- hash)
  808. if (stack.size() < 1)
  809. return false;
  810. valtype& vch = stacktop(-1);
  811. valtype vchHash((opcode == OP_RIPEMD160 || opcode == OP_SHA1 || opcode == OP_HASH160) ? 20 : 32);
  812. if (opcode == OP_RIPEMD160)
  813. RIPEMD160(&vch[0], vch.size(), &vchHash[0]);
  814. else if (opcode == OP_SHA1)
  815. SHA1(&vch[0], vch.size(), &vchHash[0]);
  816. else if (opcode == OP_SHA256)
  817. SHA256(&vch[0], vch.size(), &vchHash[0]);
  818. else if (opcode == OP_HASH160)
  819. {
  820. uint160 hash160 = Hash160(vch);
  821. memcpy(&vchHash[0], &hash160, sizeof(hash160));
  822. }
  823. else if (opcode == OP_HASH256)
  824. {
  825. uint256 hash = Hash(vch.begin(), vch.end());
  826. memcpy(&vchHash[0], &hash, sizeof(hash));
  827. }
  828. popstack(stack);
  829. stack.push_back(vchHash);
  830. }
  831. break;
  832. case OP_CODESEPARATOR:
  833. {
  834. // Hash starts after the code separator
  835. pbegincodehash = pc;
  836. }
  837. break;
  838. case OP_CHECKSIG:
  839. case OP_CHECKSIGVERIFY:
  840. {
  841. // (sig pubkey -- bool)
  842. if (stack.size() < 2)
  843. return false;
  844. valtype& vchSig = stacktop(-2);
  845. valtype& vchPubKey = stacktop(-1);
  846. ////// debug print
  847. //PrintHex(vchSig.begin(), vchSig.end(), "sig: %s\n");
  848. //PrintHex(vchPubKey.begin(), vchPubKey.end(), "pubkey: %s\n");
  849. // Subset of script starting at the most recent codeseparator
  850. CScript scriptCode(pbegincodehash, pend);
  851. // Drop the signature, since there's no way for a signature to sign itself
  852. scriptCode.FindAndDelete(CScript(vchSig));
  853. bool fSuccess = CheckSig(vchSig, vchPubKey, scriptCode, txTo, nIn, nHashType);
  854. popstack(stack);
  855. popstack(stack);
  856. stack.push_back(fSuccess ? vchTrue : vchFalse);
  857. if (opcode == OP_CHECKSIGVERIFY)
  858. {
  859. if (fSuccess)
  860. popstack(stack);
  861. else
  862. return false;
  863. }
  864. }
  865. break;
  866. case OP_CHECKMULTISIG:
  867. case OP_CHECKMULTISIGVERIFY:
  868. {
  869. // ([sig ...] num_of_signatures [pubkey ...] num_of_pubkeys -- bool)
  870. int i = 1;
  871. if ((int)stack.size() < i)
  872. return false;
  873. int nKeysCount = CastToBigNum(stacktop(-i)).getint();
  874. if (nKeysCount < 0 || nKeysCount > 20)
  875. return false;
  876. nOpCount += nKeysCount;
  877. if (nOpCount > 201)
  878. return false;
  879. int ikey = ++i;
  880. i += nKeysCount;
  881. if ((int)stack.size() < i)
  882. return false;
  883. int nSigsCount = CastToBigNum(stacktop(-i)).getint();
  884. if (nSigsCount < 0 || nSigsCount > nKeysCount)
  885. return false;
  886. int isig = ++i;
  887. i += nSigsCount;
  888. if ((int)stack.size() < i)
  889. return false;
  890. // Subset of script starting at the most recent codeseparator
  891. CScript scriptCode(pbegincodehash, pend);
  892. // Drop the signatures, since there's no way for a signature to sign itself
  893. for (int k = 0; k < nSigsCount; k++)
  894. {
  895. valtype& vchSig = stacktop(-isig-k);
  896. scriptCode.FindAndDelete(CScript(vchSig));
  897. }
  898. bool fSuccess = true;
  899. while (fSuccess && nSigsCount > 0)
  900. {
  901. valtype& vchSig = stacktop(-isig);
  902. valtype& vchPubKey = stacktop(-ikey);
  903. // Check signature
  904. if (CheckSig(vchSig, vchPubKey, scriptCode, txTo, nIn, nHashType))
  905. {
  906. isig++;
  907. nSigsCount--;
  908. }
  909. ikey++;
  910. nKeysCount--;
  911. // If there are more signatures left than keys left,
  912. // then too many signatures have failed
  913. if (nSigsCount > nKeysCount)
  914. fSuccess = false;
  915. }
  916. while (i-- > 0)
  917. popstack(stack);
  918. stack.push_back(fSuccess ? vchTrue : vchFalse);
  919. if (opcode == OP_CHECKMULTISIGVERIFY)
  920. {
  921. if (fSuccess)
  922. popstack(stack);
  923. else
  924. return false;
  925. }
  926. }
  927. break;
  928. default:
  929. return false;
  930. }
  931. // Size limits
  932. if (stack.size() + altstack.size() > 1000)
  933. return false;
  934. }
  935. }
  936. catch (...)
  937. {
  938. return false;
  939. }
  940. if (!vfExec.empty())
  941. return false;
  942. return true;
  943. }
  944. uint256 SignatureHash(CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType)
  945. {
  946. if (nIn >= txTo.vin.size())
  947. {
  948. printf("ERROR: SignatureHash() : nIn=%d out of range\n", nIn);
  949. return 1;
  950. }
  951. CTransaction txTmp(txTo);
  952. // In case concatenating two scripts ends up with two codeseparators,
  953. // or an extra one at the end, this prevents all those possible incompatibilities.
  954. scriptCode.FindAndDelete(CScript(OP_CODESEPARATOR));
  955. // Blank out other inputs' signatures
  956. for (unsigned int i = 0; i < txTmp.vin.size(); i++)
  957. txTmp.vin[i].scriptSig = CScript();
  958. txTmp.vin[nIn].scriptSig = scriptCode;
  959. // Blank out some of the outputs
  960. if ((nHashType & 0x1f) == SIGHASH_NONE)
  961. {
  962. // Wildcard payee
  963. txTmp.vout.clear();
  964. // Let the others update at will
  965. for (unsigned int i = 0; i < txTmp.vin.size(); i++)
  966. if (i != nIn)
  967. txTmp.vin[i].nSequence = 0;
  968. }
  969. else if ((nHashType & 0x1f) == SIGHASH_SINGLE)
  970. {
  971. // Only lock-in the txout payee at same index as txin
  972. unsigned int nOut = nIn;
  973. if (nOut >= txTmp.vout.size())
  974. {
  975. printf("ERROR: SignatureHash() : nOut=%d out of range\n", nOut);
  976. return 1;
  977. }
  978. txTmp.vout.resize(nOut+1);
  979. for (unsigned int i = 0; i < nOut; i++)
  980. txTmp.vout[i].SetNull();
  981. // Let the others update at will
  982. for (unsigned int i = 0; i < txTmp.vin.size(); i++)
  983. if (i != nIn)
  984. txTmp.vin[i].nSequence = 0;
  985. }
  986. // Blank out other inputs completely, not recommended for open transactions
  987. if (nHashType & SIGHASH_ANYONECANPAY)
  988. {
  989. txTmp.vin[0] = txTmp.vin[nIn];
  990. txTmp.vin.resize(1);
  991. }
  992. // Serialize and hash
  993. CDataStream ss(SER_GETHASH, 0);
  994. ss.reserve(10000);
  995. ss << txTmp << nHashType;
  996. return Hash(ss.begin(), ss.end());
  997. }
  998. // Valid signature cache, to avoid doing expensive ECDSA signature checking
  999. // twice for every transaction (once when accepted into memory pool, and
  1000. // again when accepted into the block chain)
  1001. class CSignatureCache
  1002. {
  1003. private:
  1004. // sigdata_type is (signature hash, signature, public key):
  1005. typedef boost::tuple<uint256, std::vector<unsigned char>, std::vector<unsigned char> > sigdata_type;
  1006. std::set< sigdata_type> setValid;
  1007. CCriticalSection cs_sigcache;
  1008. public:
  1009. bool
  1010. Get(uint256 hash, const std::vector<unsigned char>& vchSig, const std::vector<unsigned char>& pubKey)
  1011. {
  1012. LOCK(cs_sigcache);
  1013. sigdata_type k(hash, vchSig, pubKey);
  1014. std::set<sigdata_type>::iterator mi = setValid.find(k);
  1015. if (mi != setValid.end())
  1016. return true;
  1017. return false;
  1018. }
  1019. void Set(uint256 hash, const std::vector<unsigned char>& vchSig, const std::vector<unsigned char>& pubKey)
  1020. {
  1021. // DoS prevention: limit cache size to less than 10MB
  1022. // (~200 bytes per cache entry times 50,000 entries)
  1023. // Since there are a maximum of 20,000 signature operations per block
  1024. // 50,000 is a reasonable default.
  1025. int64 nMaxCacheSize = GetArg("-maxsigcachesize", 50000);
  1026. if (nMaxCacheSize <= 0) return;
  1027. LOCK(cs_sigcache);
  1028. while (static_cast<int64>(setValid.size()) > nMaxCacheSize)
  1029. {
  1030. // Evict a random entry. Random because that helps
  1031. // foil would-be DoS attackers who might try to pre-generate
  1032. // and re-use a set of valid signatures just-slightly-greater
  1033. // than our cache size.
  1034. uint256 randomHash = GetRandHash();
  1035. std::vector<unsigned char> unused;
  1036. std::set<sigdata_type>::iterator it =
  1037. setValid.lower_bound(sigdata_type(randomHash, unused, unused));
  1038. if (it == setValid.end())
  1039. it = setValid.begin();
  1040. setValid.erase(*it);
  1041. }
  1042. sigdata_type k(hash, vchSig, pubKey);
  1043. setValid.insert(k);
  1044. }
  1045. };
  1046. bool CheckSig(vector<unsigned char> vchSig, vector<unsigned char> vchPubKey, CScript scriptCode,
  1047. const CTransaction& txTo, unsigned int nIn, int nHashType)
  1048. {
  1049. static CSignatureCache signatureCache;
  1050. // Hash type is one byte tacked on to the end of the signature
  1051. if (vchSig.empty())
  1052. return false;
  1053. if (nHashType == 0)
  1054. nHashType = vchSig.back();
  1055. else if (nHashType != vchSig.back())
  1056. return false;
  1057. vchSig.pop_back();
  1058. uint256 sighash = SignatureHash(scriptCode, txTo, nIn, nHashType);
  1059. if (signatureCache.Get(sighash, vchSig, vchPubKey))
  1060. return true;
  1061. CKey key;
  1062. if (!key.SetPubKey(vchPubKey))
  1063. return false;
  1064. if (!key.Verify(sighash, vchSig))
  1065. return false;
  1066. signatureCache.Set(sighash, vchSig, vchPubKey);
  1067. return true;
  1068. }
  1069. //
  1070. // Return public keys or hashes from scriptPubKey, for 'standard' transaction types.
  1071. //
  1072. bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, vector<vector<unsigned char> >& vSolutionsRet)
  1073. {
  1074. // Templates
  1075. static map<txnouttype, CScript> mTemplates;
  1076. if (mTemplates.empty())
  1077. {
  1078. // Standard tx, sender provides pubkey, receiver adds signature
  1079. mTemplates.insert(make_pair(TX_PUBKEY, CScript() << OP_PUBKEY << OP_CHECKSIG));
  1080. // Ecoin address tx, sender provides hash of pubkey, receiver provides signature and pubkey
  1081. mTemplates.insert(make_pair(TX_PUBKEYHASH, CScript() << OP_DUP << OP_HASH160 << OP_PUBKEYHASH << OP_EQUALVERIFY << OP_CHECKSIG));
  1082. // Sender provides N pubkeys, receivers provides M signatures
  1083. mTemplates.insert(make_pair(TX_MULTISIG, CScript() << OP_SMALLINTEGER << OP_PUBKEYS << OP_SMALLINTEGER << OP_CHECKMULTISIG));
  1084. }
  1085. // Shortcut for pay-to-script-hash, which are more constrained than the other types:
  1086. // it is always OP_HASH160 20 [20 byte hash] OP_EQUAL
  1087. if (scriptPubKey.IsPayToScriptHash())
  1088. {
  1089. typeRet = TX_SCRIPTHASH;
  1090. vector<unsigned char> hashBytes(scriptPubKey.begin()+2, scriptPubKey.begin()+22);
  1091. vSolutionsRet.push_back(hashBytes);
  1092. return true;
  1093. }
  1094. // Scan templates
  1095. const CScript& script1 = scriptPubKey;
  1096. BOOST_FOREACH(const PAIRTYPE(txnouttype, CScript)& tplate, mTemplates)
  1097. {
  1098. const CScript& script2 = tplate.second;
  1099. vSolutionsRet.clear();
  1100. opcodetype opcode1, opcode2;
  1101. vector<unsigned char> vch1, vch2;
  1102. // Compare
  1103. CScript::const_iterator pc1 = script1.begin();
  1104. CScript::const_iterator pc2 = script2.begin();
  1105. while (true)
  1106. {
  1107. if (pc1 == script1.end() && pc2 == script2.end())
  1108. {
  1109. // Found a match
  1110. typeRet = tplate.first;
  1111. if (typeRet == TX_MULTISIG)
  1112. {
  1113. // Additional checks for TX_MULTISIG:
  1114. unsigned char m = vSolutionsRet.front()[0];
  1115. unsigned char n = vSolutionsRet.back()[0];
  1116. if (m < 1 || n < 1 || m > n || vSolutionsRet.size()-2 != n)
  1117. return false;
  1118. }
  1119. return true;
  1120. }
  1121. if (!script1.GetOp(pc1, opcode1, vch1))
  1122. break;
  1123. if (!script2.GetOp(pc2, opcode2, vch2))
  1124. break;
  1125. // Template matching opcodes:
  1126. if (opcode2 == OP_PUBKEYS)
  1127. {
  1128. while (vch1.size() >= 33 && vch1.size() <= 120)
  1129. {
  1130. vSolutionsRet.push_back(vch1);
  1131. if (!script1.GetOp(pc1, opcode1, vch1))
  1132. break;
  1133. }
  1134. if (!script2.GetOp(pc2, opcode2, vch2))
  1135. break;
  1136. // Normal situation is to fall through
  1137. // to other if/else statements
  1138. }
  1139. if (opcode2 == OP_PUBKEY)
  1140. {
  1141. if (vch1.size() < 33 || vch1.size() > 120)
  1142. break;
  1143. vSolutionsRet.push_back(vch1);
  1144. }
  1145. else if (opcode2 == OP_PUBKEYHASH)
  1146. {
  1147. if (vch1.size() != sizeof(uint160))
  1148. break;
  1149. vSolutionsRet.push_back(vch1);
  1150. }
  1151. else if (opcode2 == OP_SMALLINTEGER)
  1152. { // Single-byte small integer pushed onto vSolutions
  1153. if (opcode1 == OP_0 ||
  1154. (opcode1 >= OP_1 && opcode1 <= OP_16))
  1155. {
  1156. char n = (char)CScript::DecodeOP_N(opcode1);
  1157. vSolutionsRet.push_back(valtype(1, n));
  1158. }
  1159. else
  1160. break;
  1161. }
  1162. else if (opcode1 != opcode2 || vch1 != vch2)
  1163. {
  1164. // Others must match exactly
  1165. break;
  1166. }
  1167. }
  1168. }
  1169. vSolutionsRet.clear();
  1170. typeRet = TX_NONSTANDARD;
  1171. return false;
  1172. }
  1173. bool Sign1(const CKeyID& address, const CKeyStore& keystore, uint256 hash, int nHashType, CScript& scriptSigRet)
  1174. {
  1175. CKey key;
  1176. if (!keystore.GetKey(address, key))
  1177. return false;
  1178. vector<unsigned char> vchSig;
  1179. if (!key.Sign(hash, vchSig))
  1180. return false;
  1181. vchSig.push_back((unsigned char)nHashType);
  1182. scriptSigRet << vchSig;
  1183. return true;
  1184. }
  1185. bool SignN(const vector<valtype>& multisigdata, const CKeyStore& keystore, uint256 hash, int nHashType, CScript& scriptSigRet)
  1186. {
  1187. int nSigned = 0;
  1188. int nRequired = multisigdata.front()[0];
  1189. for (unsigned int i = 1; i < multisigdata.size()-1 && nSigned < nRequired; i++)
  1190. {
  1191. const valtype& pubkey = multisigdata[i];
  1192. CKeyID keyID = CPubKey(pubkey).GetID();
  1193. if (Sign1(keyID, keystore, hash, nHashType, scriptSigRet))
  1194. ++nSigned;
  1195. }
  1196. return nSigned==nRequired;
  1197. }
  1198. //
  1199. // Sign scriptPubKey with private keys stored in keystore, given transaction hash and hash type.
  1200. // Signatures are returned in scriptSigRet (or returns false if scriptPubKey can't be signed),
  1201. // unless whichTypeRet is TX_SCRIPTHASH, in which case scriptSigRet is the redemption script.
  1202. // Returns false if scriptPubKey could not be completely satisfied.
  1203. //
  1204. bool Solver(const CKeyStore& keystore, const CScript& scriptPubKey, uint256 hash, int nHashType,
  1205. CScript& scriptSigRet, txnouttype& whichTypeRet)
  1206. {
  1207. scriptSigRet.clear();
  1208. vector<valtype> vSolutions;
  1209. if (!Solver(scriptPubKey, whichTypeRet, vSolutions))
  1210. return false;
  1211. CKeyID keyID;
  1212. switch (whichTypeRet)
  1213. {
  1214. case TX_NONSTANDARD:
  1215. return false;
  1216. case TX_PUBKEY:
  1217. keyID = CPubKey(vSolutions[0]).GetID();
  1218. return Sign1(keyID, keystore, hash, nHashType, scriptSigRet);
  1219. case TX_PUBKEYHASH:
  1220. keyID = CKeyID(uint160(vSolutions[0]));
  1221. if (!Sign1(keyID, keystore, hash, nHashType, scriptSigRet))
  1222. return false;
  1223. else
  1224. {
  1225. CPubKey vch;
  1226. keystore.GetPubKey(keyID, vch);
  1227. scriptSigRet << vch;
  1228. }
  1229. return true;
  1230. case TX_SCRIPTHASH:
  1231. return keystore.GetCScript(uint160(vSolutions[0]), scriptSigRet);
  1232. case TX_MULTISIG:
  1233. scriptSigRet << OP_0; // workaround CHECKMULTISIG bug
  1234. return (SignN(vSolutions, keystore, hash, nHashType, scriptSigRet));
  1235. }
  1236. return false;
  1237. }
  1238. int ScriptSigArgsExpected(txnouttype t, const std::vector<std::vector<unsigned char> >& vSolutions)
  1239. {
  1240. switch (t)
  1241. {
  1242. case TX_NONSTANDARD:
  1243. return -1;
  1244. case TX_PUBKEY:
  1245. return 1;
  1246. case TX_PUBKEYHASH:
  1247. return 2;
  1248. case TX_MULTISIG:
  1249. if (vSolutions.size() < 1 || vSolutions[0].size() < 1)
  1250. return -1;
  1251. return vSolutions[0][0] + 1;
  1252. case TX_SCRIPTHASH:
  1253. return 1; // doesn't include args needed by the script
  1254. }
  1255. return -1;
  1256. }
  1257. bool IsStandard(const CScript& scriptPubKey)
  1258. {
  1259. vector<valtype> vSolutions;
  1260. txnouttype whichType;
  1261. if (!Solver(scriptPubKey, whichType, vSolutions))
  1262. return false;
  1263. if (whichType == TX_MULTISIG)
  1264. {
  1265. unsigned char m = vSolutions.front()[0];
  1266. unsigned char n = vSolutions.back()[0];
  1267. // Support up to x-of-3 multisig txns as standard
  1268. if (n < 1 || n > 3)
  1269. return false;
  1270. if (m < 1 || m > n)
  1271. return false;
  1272. }
  1273. return whichType != TX_NONSTANDARD;
  1274. }
  1275. unsigned int HaveKeys(const vector<valtype>& pubkeys, const CKeyStore& keystore)
  1276. {
  1277. unsigned int nResult = 0;
  1278. BOOST_FOREACH(const valtype& pubkey, pubkeys)
  1279. {
  1280. CKeyID keyID = CPubKey(pubkey).GetID();
  1281. if (keystore.HaveKey(keyID))
  1282. ++nResult;
  1283. }
  1284. return nResult;
  1285. }
  1286. class CKeyStoreIsMineVisitor : public boost::static_visitor<bool>
  1287. {
  1288. private:
  1289. const CKeyStore *keystore;
  1290. public:
  1291. CKeyStoreIsMineVisitor(const CKeyStore *keystoreIn) : keystore(keystoreIn) { }
  1292. bool operator()(const CNoDestination &dest) const { return false; }
  1293. bool operator()(const CKeyID &keyID) const { return keystore->HaveKey(keyID); }
  1294. bool operator()(const CScriptID &scriptID) const { return keystore->HaveCScript(scriptID); }
  1295. };
  1296. bool IsMine(const CKeyStore &keystore, const CTxDestination &dest)
  1297. {
  1298. return boost::apply_visitor(CKeyStoreIsMineVisitor(&keystore), dest);
  1299. }
  1300. bool IsMine(const CKeyStore &keystore, const CScript& scriptPubKey)
  1301. {
  1302. vector<valtype> vSolutions;
  1303. txnouttype whichType;
  1304. if (!Solver(scriptPubKey, whichType, vSolutions))
  1305. return false;
  1306. CKeyID keyID;
  1307. switch (whichType)
  1308. {
  1309. case TX_NONSTANDARD:
  1310. return false;
  1311. case TX_PUBKEY:
  1312. keyID = CPubKey(vSolutions[0]).GetID();
  1313. return keystore.HaveKey(keyID);
  1314. case TX_PUBKEYHASH:
  1315. keyID = CKeyID(uint160(vSolutions[0]));
  1316. return keystore.HaveKey(keyID);
  1317. case TX_SCRIPTHASH:
  1318. {
  1319. CScript subscript;
  1320. if (!keystore.GetCScript(CScriptID(uint160(vSolutions[0])), subscript))
  1321. return false;
  1322. return IsMine(keystore, subscript);
  1323. }
  1324. case TX_MULTISIG:
  1325. {
  1326. // Only consider transactions "mine" if we own ALL the
  1327. // keys involved. multi-signature transactions that are
  1328. // partially owned (somebody else has a key that can spend
  1329. // them) enable spend-out-from-under-you attacks, especially
  1330. // in shared-wallet situations.
  1331. vector<valtype> keys(vSolutions.begin()+1, vSolutions.begin()+vSolutions.size()-1);
  1332. return HaveKeys(keys, keystore) == keys.size();
  1333. }
  1334. }
  1335. return false;
  1336. }
  1337. bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet)
  1338. {
  1339. vector<valtype> vSolutions;
  1340. txnouttype whichType;
  1341. if (!Solver(scriptPubKey, whichType, vSolutions))
  1342. return false;
  1343. if (whichType == TX_PUBKEY)
  1344. {
  1345. addressRet = CPubKey(vSolutions[0]).GetID();
  1346. return true;
  1347. }
  1348. else if (whichType == TX_PUBKEYHASH)
  1349. {
  1350. addressRet = CKeyID(uint160(vSolutions[0]));
  1351. return true;
  1352. }
  1353. else if (whichType == TX_SCRIPTHASH)
  1354. {
  1355. addressRet = CScriptID(uint160(vSolutions[0]));
  1356. return true;
  1357. }
  1358. // Multisig txns have more than one address...
  1359. return false;
  1360. }
  1361. class CAffectedKeysVisitor : public boost::static_visitor<void> {
  1362. private:
  1363. const CKeyStore &keystore;
  1364. std::vector<CKeyID> &vKeys;
  1365. public:
  1366. CAffectedKeysVisitor(const CKeyStore &keystoreIn, std::vector<CKeyID> &vKeysIn) : keystore(keystoreIn), vKeys(vKeysIn) {}
  1367. void Process(const CScript &script) {
  1368. txnouttype type;
  1369. std::vector<CTxDestination> vDest;
  1370. int nRequired;
  1371. if (ExtractDestinations(script, type, vDest, nRequired)) {
  1372. BOOST_FOREACH(const CTxDestination &dest, vDest)
  1373. boost::apply_visitor(*this, dest);
  1374. }
  1375. }
  1376. void operator()(const CKeyID &keyId) {
  1377. if (keystore.HaveKey(keyId))
  1378. vKeys.push_back(keyId);
  1379. }
  1380. void operator()(const CScriptID &scriptId) {
  1381. CScript script;
  1382. if (keystore.GetCScript(scriptId, script))
  1383. Process(script);
  1384. }
  1385. void operator()(const CNoDestination &none) {}
  1386. };
  1387. void ExtractAffectedKeys(const CKeyStore &keystore, const CScript& scriptPubKey, std::vector<CKeyID> &vKeys) {
  1388. CAffectedKeysVisitor(keystore, vKeys).Process(scriptPubKey);
  1389. }
  1390. bool ExtractDestinations(const CScript& scriptPubKey, txnouttype& typeRet, vector<CTxDestination>& addressRet, int& nRequiredRet)
  1391. {
  1392. addressRet.clear();
  1393. typeRet = TX_NONSTANDARD;
  1394. vector<valtype> vSolutions;
  1395. if (!Solver(scriptPubKey, typeRet, vSolutions))
  1396. return false;
  1397. if (typeRet == TX_MULTISIG)
  1398. {
  1399. nRequiredRet = vSolutions.front()[0];
  1400. for (unsigned int i = 1; i < vSolutions.size()-1; i++)
  1401. {
  1402. CTxDestination address = CPubKey(vSolutions[i]).GetID();
  1403. addressRet.push_back(address);
  1404. }
  1405. }
  1406. else
  1407. {
  1408. nRequiredRet = 1;
  1409. CTxDestination address;
  1410. if (!ExtractDestination(scriptPubKey, address))
  1411. return false;
  1412. addressRet.push_back(address);
  1413. }
  1414. return true;
  1415. }
  1416. bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn,
  1417. bool fValidatePayToScriptHash, int nHashType)
  1418. {
  1419. vector<vector<unsigned char> > stack, stackCopy;
  1420. if (!EvalScript(stack, scriptSig, txTo, nIn, nHashType))
  1421. return false;
  1422. if (fValidatePayToScriptHash)
  1423. stackCopy = stack;
  1424. if (!EvalScript(stack, scriptPubKey, txTo, nIn, nHashType))
  1425. return false;
  1426. if (stack.empty())
  1427. return false;
  1428. if (CastToBool(stack.back()) == false)
  1429. return false;
  1430. // Additional validation for spend-to-script-hash transactions:
  1431. if (fValidatePayToScriptHash && scriptPubKey.IsPayToScriptHash())
  1432. {
  1433. if (!scriptSig.IsPushOnly()) // scriptSig must be literals-only
  1434. return false; // or validation fails
  1435. const valtype& pubKeySerialized = stackCopy.back();
  1436. CScript pubKey2(pubKeySerialized.begin(), pubKeySerialized.end());
  1437. popstack(stackCopy);
  1438. if (!EvalScript(stackCopy, pubKey2, txTo, nIn, nHashType))
  1439. return false;
  1440. if (stackCopy.empty())
  1441. return false;
  1442. return CastToBool(stackCopy.back());
  1443. }
  1444. return true;
  1445. }
  1446. bool SignSignature(const CKeyStore &keystore, const CScript& fromPubKey, CTransaction& txTo, unsigned int nIn, int nHashType)
  1447. {
  1448. assert(nIn < txTo.vin.size());
  1449. CTxIn& txin = txTo.vin[nIn];
  1450. // Leave out the signature from the hash, since a signature can't sign itself.
  1451. // The checksig op will also drop the signatures from its hash.
  1452. uint256 hash = SignatureHash(fromPubKey, txTo, nIn, nHashType);
  1453. txnouttype whichType;
  1454. if (!Solver(keystore, fromPubKey, hash, nHashType, txin.scriptSig, whichType))
  1455. return false;
  1456. if (whichType == TX_SCRIPTHASH)
  1457. {
  1458. // Solver returns the subscript that need to be evaluated;
  1459. // the final scriptSig is the signatures from that
  1460. // and then the serialized subscript:
  1461. CScript subscript = txin.scriptSig;
  1462. // Recompute txn hash using subscript in place of scriptPubKey:
  1463. uint256 hash2 = SignatureHash(subscript, txTo, nIn, nHashType);
  1464. txnouttype subType;
  1465. bool fSolved =
  1466. Solver(keystore, subscript, hash2, nHashType, txin.scriptSig, subType) && subType != TX_SCRIPTHASH;
  1467. // Append serialized subscript whether or not it is completely signed:
  1468. txin.scriptSig << static_cast<valtype>(subscript);
  1469. if (!fSolved) return false;
  1470. }
  1471. // Test solution
  1472. return VerifyScript(txin.scriptSig, fromPubKey, txTo, nIn, true, 0);
  1473. }
  1474. bool SignSignature(const CKeyStore &keystore, const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType)
  1475. {
  1476. assert(nIn < txTo.vin.size());
  1477. CTxIn& txin = txTo.vin[nIn];
  1478. assert(txin.prevout.n < txFrom.vout.size());
  1479. assert(txin.prevout.hash == txFrom.GetHash());
  1480. const CTxOut& txout = txFrom.vout[txin.prevout.n];
  1481. return SignSignature(keystore, txout.scriptPubKey, txTo, nIn, nHashType);
  1482. }
  1483. bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, bool fValidatePayToScriptHash, int nHashType)
  1484. {
  1485. assert(nIn < txTo.vin.size());
  1486. const CTxIn& txin = txTo.vin[nIn];
  1487. if (txin.prevout.n >= txFrom.vout.size())
  1488. return false;
  1489. const CTxOut& txout = txFrom.vout[txin.prevout.n];
  1490. if (txin.prevout.hash != txFrom.GetHash())
  1491. return false;
  1492. return VerifyScript(txin.scriptSig, txout.scriptPubKey, txTo, nIn, fValidatePayToScriptHash, nHashType);
  1493. }
  1494. static CScript PushAll(const vector<valtype>& values)
  1495. {
  1496. CScript result;
  1497. BOOST_FOREACH(const valtype& v, values)
  1498. result << v;
  1499. return result;
  1500. }
  1501. static CScript CombineMultisig(CScript scriptPubKey, const CTransaction& txTo, unsigned int nIn,
  1502. const vector<valtype>& vSolutions,
  1503. vector<valtype>& sigs1, vector<valtype>& sigs2)
  1504. {
  1505. // Combine all the signatures we've got:
  1506. set<valtype> allsigs;
  1507. BOOST_FOREACH(const valtype& v, sigs1)
  1508. {
  1509. if (!v.empty())
  1510. allsigs.insert(v);
  1511. }
  1512. BOOST_FOREACH(const valtype& v, sigs2)
  1513. {
  1514. if (!v.empty())
  1515. allsigs.insert(v);
  1516. }
  1517. // Build a map of pubkey -> signature by matching sigs to pubkeys:
  1518. assert(vSolutions.size() > 1);
  1519. unsigned int nSigsRequired = vSolutions.front()[0];
  1520. unsigned int nPubKeys = vSolutions.size()-2;
  1521. map<valtype, valtype> sigs;
  1522. BOOST_FOREACH(const valtype& sig, allsigs)
  1523. {
  1524. for (unsigned int i = 0; i < nPubKeys; i++)
  1525. {
  1526. const valtype& pubkey = vSolutions[i+1];
  1527. if (sigs.count(pubkey))
  1528. continue; // Already got a sig for this pubkey
  1529. if (CheckSig(sig, pubkey, scriptPubKey, txTo, nIn, 0))
  1530. {
  1531. sigs[pubkey] = sig;
  1532. break;
  1533. }
  1534. }
  1535. }
  1536. // Now build a merged CScript:
  1537. unsigned int nSigsHave = 0;
  1538. CScript result; result << OP_0; // pop-one-too-many workaround
  1539. for (unsigned int i = 0; i < nPubKeys && nSigsHave < nSigsRequired; i++)
  1540. {
  1541. if (sigs.count(vSolutions[i+1]))
  1542. {
  1543. result << sigs[vSolutions[i+1]];
  1544. ++nSigsHave;
  1545. }
  1546. }
  1547. // Fill any missing with OP_0:
  1548. for (unsigned int i = nSigsHave; i < nSigsRequired; i++)
  1549. result << OP_0;
  1550. return result;
  1551. }
  1552. static CScript CombineSignatures(CScript scriptPubKey, const CTransaction& txTo, unsigned int nIn,
  1553. const txnouttype txType, const vector<valtype>& vSolutions,
  1554. vector<valtype>& sigs1, vector<valtype>& sigs2)
  1555. {
  1556. switch (txType)
  1557. {
  1558. case TX_NONSTANDARD:
  1559. // Don't know anything about this, assume bigger one is correct:
  1560. if (sigs1.size() >= sigs2.size())
  1561. return PushAll(sigs1);
  1562. return PushAll(sigs2);
  1563. case TX_PUBKEY:
  1564. case TX_PUBKEYHASH:
  1565. // Signatures are bigger than placeholders or empty scripts:
  1566. if (sigs1.empty() || sigs1[0].empty())
  1567. return PushAll(sigs2);
  1568. return PushAll(sigs1);
  1569. case TX_SCRIPTHASH:
  1570. if (sigs1.empty() || sigs1.back().empty())
  1571. return PushAll(sigs2);
  1572. else if (sigs2.empty() || sigs2.back().empty())
  1573. return PushAll(sigs1);
  1574. else
  1575. {
  1576. // Recur to combine:
  1577. valtype spk = sigs1.back();
  1578. CScript pubKey2(spk.begin(), spk.end());
  1579. txnouttype txType2;
  1580. vector<vector<unsigned char> > vSolutions2;
  1581. Solver(pubKey2, txType2, vSolutions2);
  1582. sigs1.pop_back();
  1583. sigs2.pop_back();
  1584. CScript result = CombineSignatures(pubKey2, txTo, nIn, txType2, vSolutions2, sigs1, sigs2);
  1585. result << spk;
  1586. return result;
  1587. }
  1588. case TX_MULTISIG:
  1589. return CombineMultisig(scriptPubKey, txTo, nIn, vSolutions, sigs1, sigs2);
  1590. }
  1591. return CScript();
  1592. }
  1593. CScript CombineSignatures(CScript scriptPubKey, const CTransaction& txTo, unsigned int nIn,
  1594. const CScript& scriptSig1, const CScript& scriptSig2)
  1595. {
  1596. txnouttype txType;
  1597. vector<vector<unsigned char> > vSolutions;
  1598. Solver(scriptPubKey, txType, vSolutions);
  1599. vector<valtype> stack1;
  1600. EvalScript(stack1, scriptSig1, CTransaction(), 0, 0);
  1601. vector<valtype> stack2;
  1602. EvalScript(stack2, scriptSig2, CTransaction(), 0, 0);
  1603. return CombineSignatures(scriptPubKey, txTo, nIn, txType, vSolutions, stack1, stack2);
  1604. }
  1605. unsigned int CScript::GetSigOpCount(bool fAccurate) const
  1606. {
  1607. unsigned int n = 0;
  1608. const_iterator pc = begin();
  1609. opcodetype lastOpcode = OP_INVALIDOPCODE;
  1610. while (pc < end())
  1611. {
  1612. opcodetype opcode;
  1613. if (!GetOp(pc, opcode))
  1614. break;
  1615. if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
  1616. n++;
  1617. else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
  1618. {
  1619. if (fAccurate && lastOpcode >= OP_1 && lastOpcode <= OP_16)
  1620. n += DecodeOP_N(lastOpcode);
  1621. else
  1622. n += 20;
  1623. }
  1624. lastOpcode = opcode;
  1625. }
  1626. return n;
  1627. }
  1628. unsigned int CScript::GetSigOpCount(const CScript& scriptSig) const
  1629. {
  1630. if (!IsPayToScriptHash())
  1631. return GetSigOpCount(true);
  1632. // This is a pay-to-script-hash scriptPubKey;
  1633. // get the last item that the scriptSig
  1634. // pushes onto the stack:
  1635. const_iterator pc = scriptSig.begin();
  1636. vector<unsigned char> data;
  1637. while (pc < scriptSig.end())
  1638. {
  1639. opcodetype opcode;
  1640. if (!scriptSig.GetOp(pc, opcode, data))
  1641. return 0;
  1642. if (opcode > OP_16)
  1643. return 0;
  1644. }
  1645. /// ... and return its opcount:
  1646. CScript subscript(data.begin(), data.end());
  1647. return subscript.GetSigOpCount(true);
  1648. }
  1649. bool CScript::IsPayToScriptHash() const
  1650. {
  1651. // Extra-fast test for pay-to-script-hash CScripts:
  1652. return (this->size() == 23 &&
  1653. this->at(0) == OP_HASH160 &&
  1654. this->at(1) == 0x14 &&
  1655. this->at(22) == OP_EQUAL);
  1656. }
  1657. /**bool CScript::HasCanonicalPushes() const
  1658. {
  1659. const_iterator pc = begin();
  1660. while (pc < end())
  1661. {
  1662. opcodetype opcode;
  1663. std::vector<unsigned char> data;
  1664. if (!GetOp(pc, opcode, data))
  1665. return false;
  1666. if (opcode > OP_16)
  1667. continue;
  1668. if (opcode < OP_PUSHDATA1 && opcode > OP_0 && (data.size() == 1 && data[0] <= 16))
  1669. // Could have used an OP_n code, rather than a 1-byte push.
  1670. return false;
  1671. if (opcode == OP_PUSHDATA1 && data.size() < OP_PUSHDATA1)
  1672. // Could have used a normal n-byte push, rather than OP_PUSHDATA1.
  1673. return false;
  1674. if (opcode == OP_PUSHDATA2 && data.size() <= 0xFF)
  1675. // Could have used an OP_PUSHDATA1.
  1676. return false;
  1677. if (opcode == OP_PUSHDATA4 && data.size() <= 0xFFFF)
  1678. // Could have used an OP_PUSHDATA2.
  1679. return false;
  1680. }
  1681. return true;
  1682. }*/
  1683. class CScriptVisitor : public boost::static_visitor<bool>
  1684. {
  1685. private:
  1686. CScript *script;
  1687. public:
  1688. CScriptVisitor(CScript *scriptin) { script = scriptin; }
  1689. bool operator()(const CNoDestination &dest) const {
  1690. script->clear();
  1691. return false;
  1692. }
  1693. bool operator()(const CKeyID &keyID) const {
  1694. script->clear();
  1695. *script << OP_DUP << OP_HASH160 << keyID << OP_EQUALVERIFY << OP_CHECKSIG;
  1696. return true;
  1697. }
  1698. bool operator()(const CScriptID &scriptID) const {
  1699. script->clear();
  1700. *script << OP_HASH160 << scriptID << OP_EQUAL;
  1701. return true;
  1702. }
  1703. };
  1704. void CScript::SetDestination(const CTxDestination& dest)
  1705. {
  1706. boost::apply_visitor(CScriptVisitor(this), dest);
  1707. }
  1708. void CScript::SetMultisig(int nRequired, const std::vector<CKey>& keys)
  1709. {
  1710. this->clear();
  1711. *this << EncodeOP_N(nRequired);
  1712. BOOST_FOREACH(const CKey& key, keys)
  1713. *this << key.GetPubKey();
  1714. *this << EncodeOP_N(keys.size()) << OP_CHECKMULTISIG;
  1715. }