Bitcoin v0.1.5 · 2009-02-04
script.cpp
1// Copyright (c) 2009 Satoshi Nakamoto2// Distributed under the MIT/X11 software license, see the accompanying3// file license.txt or http://www.opensource.org/licenses/mit-license.php.45#include "headers.h"67bool CheckSig(vector<unsigned char> vchSig, vector<unsigned char> vchPubKey, CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType);891011typedef vector<unsigned char> valtype;12static const valtype vchFalse(0);13static const valtype vchZero(0);14static const valtype vchTrue(1, 1);15static const CBigNum bnZero(0);16static const CBigNum bnOne(1);17static const CBigNum bnFalse(0);18static const CBigNum bnTrue(1);192021bool CastToBool(const valtype& vch)22{23 return (CBigNum(vch) != bnZero);24}2526void MakeSameSize(valtype& vch1, valtype& vch2)27{28 // Lengthen the shorter one29 if (vch1.size() < vch2.size())30 vch1.resize(vch2.size(), 0);31 if (vch2.size() < vch1.size())32 vch2.resize(vch1.size(), 0);33}34353637//38// Script is a stack machine (like Forth) that evaluates a predicate39// returning a bool indicating valid or not. There are no loops.40//41#define stacktop(i) (stack.at(stack.size()+(i)))42#define altstacktop(i) (altstack.at(altstack.size()+(i)))4344bool EvalScript(const CScript& script, const CTransaction& txTo, unsigned int nIn, int nHashType,45 vector<vector<unsigned char> >* pvStackRet)46{47 CAutoBN_CTX pctx;48 CScript::const_iterator pc = script.begin();49 CScript::const_iterator pend = script.end();50 CScript::const_iterator pbegincodehash = script.begin();51 vector<bool> vfExec;52 vector<valtype> stack;53 vector<valtype> altstack;54 if (pvStackRet)55 pvStackRet->clear();565758 while (pc < pend)59 {60 bool fExec = !count(vfExec.begin(), vfExec.end(), false);6162 //63 // Read instruction64 //65 opcodetype opcode;66 valtype vchPushValue;67 if (!script.GetOp(pc, opcode, vchPushValue))68 return false;6970 if (fExec && opcode <= OP_PUSHDATA4)71 stack.push_back(vchPushValue);72 else if (fExec || (OP_IF <= opcode && opcode <= OP_ENDIF))73 switch (opcode)74 {75 //76 // Push value77 //78 case OP_1NEGATE:79 case OP_1:80 case OP_2:81 case OP_3:82 case OP_4:83 case OP_5:84 case OP_6:85 case OP_7:86 case OP_8:87 case OP_9:88 case OP_10:89 case OP_11:90 case OP_12:91 case OP_13:92 case OP_14:93 case OP_15:94 case OP_16:95 {96 // ( -- value)97 CBigNum bn((int)opcode - (int)(OP_1 - 1));98 stack.push_back(bn.getvch());99 }100 break;101102103 //104 // Control105 //106 case OP_NOP:107 break;108109 case OP_VER:110 {111 CBigNum bn(VERSION);112 stack.push_back(bn.getvch());113 }114 break;115116 case OP_IF:117 case OP_NOTIF:118 case OP_VERIF:119 case OP_VERNOTIF:120 {121 // <expression> if [statements] [else [statements]] endif122 bool fValue = false;123 if (fExec)124 {125 if (stack.size() < 1)126 return false;127 valtype& vch = stacktop(-1);128 if (opcode == OP_VERIF || opcode == OP_VERNOTIF)129 fValue = (CBigNum(VERSION) >= CBigNum(vch));130 else131 fValue = CastToBool(vch);132 if (opcode == OP_NOTIF || opcode == OP_VERNOTIF)133 fValue = !fValue;134 stack.pop_back();135 }136 vfExec.push_back(fValue);137 }138 break;139140 case OP_ELSE:141 {142 if (vfExec.empty())143 return false;144 vfExec.back() = !vfExec.back();145 }146 break;147148 case OP_ENDIF:149 {150 if (vfExec.empty())151 return false;152 vfExec.pop_back();153 }154 break;155156 case OP_VERIFY:157 {158 // (true -- ) or159 // (false -- false) and return160 if (stack.size() < 1)161 return false;162 bool fValue = CastToBool(stacktop(-1));163 if (fValue)164 stack.pop_back();165 else166 pc = pend;167 }168 break;169170 case OP_RETURN:171 {172 pc = pend;173 }174 break;175176177 //178 // Stack ops179 //180 case OP_TOALTSTACK:181 {182 if (stack.size() < 1)183 return false;184 altstack.push_back(stacktop(-1));185 stack.pop_back();186 }187 break;188189 case OP_FROMALTSTACK:190 {191 if (altstack.size() < 1)192 return false;193 stack.push_back(altstacktop(-1));194 altstack.pop_back();195 }196 break;197198 case OP_2DROP:199 {200 // (x1 x2 -- )201 stack.pop_back();202 stack.pop_back();203 }204 break;205206 case OP_2DUP:207 {208 // (x1 x2 -- x1 x2 x1 x2)209 if (stack.size() < 2)210 return false;211 valtype vch1 = stacktop(-2);212 valtype vch2 = stacktop(-1);213 stack.push_back(vch1);214 stack.push_back(vch2);215 }216 break;217218 case OP_3DUP:219 {220 // (x1 x2 x3 -- x1 x2 x3 x1 x2 x3)221 if (stack.size() < 3)222 return false;223 valtype vch1 = stacktop(-3);224 valtype vch2 = stacktop(-2);225 valtype vch3 = stacktop(-1);226 stack.push_back(vch1);227 stack.push_back(vch2);228 stack.push_back(vch3);229 }230 break;231232 case OP_2OVER:233 {234 // (x1 x2 x3 x4 -- x1 x2 x3 x4 x1 x2)235 if (stack.size() < 4)236 return false;237 valtype vch1 = stacktop(-4);238 valtype vch2 = stacktop(-3);239 stack.push_back(vch1);240 stack.push_back(vch2);241 }242 break;243244 case OP_2ROT:245 {246 // (x1 x2 x3 x4 x5 x6 -- x3 x4 x5 x6 x1 x2)247 if (stack.size() < 6)248 return false;249 valtype vch1 = stacktop(-6);250 valtype vch2 = stacktop(-5);251 stack.erase(stack.end()-6, stack.end()-4);252 stack.push_back(vch1);253 stack.push_back(vch2);254 }255 break;256257 case OP_2SWAP:258 {259 // (x1 x2 x3 x4 -- x3 x4 x1 x2)260 if (stack.size() < 4)261 return false;262 swap(stacktop(-4), stacktop(-2));263 swap(stacktop(-3), stacktop(-1));264 }265 break;266267 case OP_IFDUP:268 {269 // (x - 0 | x x)270 if (stack.size() < 1)271 return false;272 valtype vch = stacktop(-1);273 if (CastToBool(vch))274 stack.push_back(vch);275 }276 break;277278 case OP_DEPTH:279 {280 // -- stacksize281 CBigNum bn(stack.size());282 stack.push_back(bn.getvch());283 }284 break;285286 case OP_DROP:287 {288 // (x -- )289 if (stack.size() < 1)290 return false;291 stack.pop_back();292 }293 break;294295 case OP_DUP:296 {297 // (x -- x x)298 if (stack.size() < 1)299 return false;300 valtype vch = stacktop(-1);301 stack.push_back(vch);302 }303 break;304305 case OP_NIP:306 {307 // (x1 x2 -- x2)308 if (stack.size() < 2)309 return false;310 stack.erase(stack.end() - 2);311 }312 break;313314 case OP_OVER:315 {316 // (x1 x2 -- x1 x2 x1)317 if (stack.size() < 2)318 return false;319 valtype vch = stacktop(-2);320 stack.push_back(vch);321 }322 break;323324 case OP_PICK:325 case OP_ROLL:326 {327 // (xn ... x2 x1 x0 n - xn ... x2 x1 x0 xn)328 // (xn ... x2 x1 x0 n - ... x2 x1 x0 xn)329 if (stack.size() < 2)330 return false;331 int n = CBigNum(stacktop(-1)).getint();332 stack.pop_back();333 if (n < 0 || n >= stack.size())334 return false;335 valtype vch = stacktop(-n-1);336 if (opcode == OP_ROLL)337 stack.erase(stack.end()-n-1);338 stack.push_back(vch);339 }340 break;341342 case OP_ROT:343 {344 // (x1 x2 x3 -- x2 x3 x1)345 // x2 x1 x3 after first swap346 // x2 x3 x1 after second swap347 if (stack.size() < 3)348 return false;349 swap(stacktop(-3), stacktop(-2));350 swap(stacktop(-2), stacktop(-1));351 }352 break;353354 case OP_SWAP:355 {356 // (x1 x2 -- x2 x1)357 if (stack.size() < 2)358 return false;359 swap(stacktop(-2), stacktop(-1));360 }361 break;362363 case OP_TUCK:364 {365 // (x1 x2 -- x2 x1 x2)366 if (stack.size() < 2)367 return false;368 valtype vch = stacktop(-1);369 stack.insert(stack.end()-2, vch);370 }371 break;372373374 //375 // Splice ops376 //377 case OP_CAT:378 {379 // (x1 x2 -- out)380 if (stack.size() < 2)381 return false;382 valtype& vch1 = stacktop(-2);383 valtype& vch2 = stacktop(-1);384 vch1.insert(vch1.end(), vch2.begin(), vch2.end());385 stack.pop_back();386 }387 break;388389 case OP_SUBSTR:390 {391 // (in begin size -- out)392 if (stack.size() < 3)393 return false;394 valtype& vch = stacktop(-3);395 int nBegin = CBigNum(stacktop(-2)).getint();396 int nEnd = nBegin + CBigNum(stacktop(-1)).getint();397 if (nBegin < 0 || nEnd < nBegin)398 return false;399 if (nBegin > vch.size())400 nBegin = vch.size();401 if (nEnd > vch.size())402 nEnd = vch.size();403 vch.erase(vch.begin() + nEnd, vch.end());404 vch.erase(vch.begin(), vch.begin() + nBegin);405 stack.pop_back();406 stack.pop_back();407 }408 break;409410 case OP_LEFT:411 case OP_RIGHT:412 {413 // (in size -- out)414 if (stack.size() < 2)415 return false;416 valtype& vch = stacktop(-2);417 int nSize = CBigNum(stacktop(-1)).getint();418 if (nSize < 0)419 return false;420 if (nSize > vch.size())421 nSize = vch.size();422 if (opcode == OP_LEFT)423 vch.erase(vch.begin() + nSize, vch.end());424 else425 vch.erase(vch.begin(), vch.end() - nSize);426 stack.pop_back();427 }428 break;429430 case OP_SIZE:431 {432 // (in -- in size)433 if (stack.size() < 1)434 return false;435 CBigNum bn(stacktop(-1).size());436 stack.push_back(bn.getvch());437 }438 break;439440441 //442 // Bitwise logic443 //444 case OP_INVERT:445 {446 // (in - out)447 if (stack.size() < 1)448 return false;449 valtype& vch = stacktop(-1);450 for (int i = 0; i < vch.size(); i++)451 vch[i] = ~vch[i];452 }453 break;454455 case OP_AND:456 case OP_OR:457 case OP_XOR:458 {459 // (x1 x2 - out)460 if (stack.size() < 2)461 return false;462 valtype& vch1 = stacktop(-2);463 valtype& vch2 = stacktop(-1);464 MakeSameSize(vch1, vch2);465 if (opcode == OP_AND)466 {467 for (int i = 0; i < vch1.size(); i++)468 vch1[i] &= vch2[i];469 }470 else if (opcode == OP_OR)471 {472 for (int i = 0; i < vch1.size(); i++)473 vch1[i] |= vch2[i];474 }475 else if (opcode == OP_XOR)476 {477 for (int i = 0; i < vch1.size(); i++)478 vch1[i] ^= vch2[i];479 }480 stack.pop_back();481 }482 break;483484 case OP_EQUAL:485 case OP_EQUALVERIFY:486 //case OP_NOTEQUAL: // use OP_NUMNOTEQUAL487 {488 // (x1 x2 - bool)489 if (stack.size() < 2)490 return false;491 valtype& vch1 = stacktop(-2);492 valtype& vch2 = stacktop(-1);493 bool fEqual = (vch1 == vch2);494 // OP_NOTEQUAL is disabled because it would be too easy to say495 // something like n != 1 and have some wiseguy pass in 1 with extra496 // zero bytes after it (numerically, 0x01 == 0x0001 == 0x000001)497 //if (opcode == OP_NOTEQUAL)498 // fEqual = !fEqual;499 stack.pop_back();500 stack.pop_back();501 stack.push_back(fEqual ? vchTrue : vchFalse);502 if (opcode == OP_EQUALVERIFY)503 {504 if (fEqual)505 stack.pop_back();506 else507 pc = pend;508 }509 }510 break;511512513 //514 // Numeric515 //516 case OP_1ADD:517 case OP_1SUB:518 case OP_2MUL:519 case OP_2DIV:520 case OP_NEGATE:521 case OP_ABS:522 case OP_NOT:523 case OP_0NOTEQUAL:524 {525 // (in -- out)526 if (stack.size() < 1)527 return false;528 CBigNum bn(stacktop(-1));529 switch (opcode)530 {531 case OP_1ADD: bn += bnOne; break;532 case OP_1SUB: bn -= bnOne; break;533 case OP_2MUL: bn <<= 1; break;534 case OP_2DIV: bn >>= 1; break;535 case OP_NEGATE: bn = -bn; break;536 case OP_ABS: if (bn < bnZero) bn = -bn; break;537 case OP_NOT: bn = (bn == bnZero); break;538 case OP_0NOTEQUAL: bn = (bn != bnZero); break;539 }540 stack.pop_back();541 stack.push_back(bn.getvch());542 }543 break;544545 case OP_ADD:546 case OP_SUB:547 case OP_MUL:548 case OP_DIV:549 case OP_MOD:550 case OP_LSHIFT:551 case OP_RSHIFT:552 case OP_BOOLAND:553 case OP_BOOLOR:554 case OP_NUMEQUAL:555 case OP_NUMEQUALVERIFY:556 case OP_NUMNOTEQUAL:557 case OP_LESSTHAN:558 case OP_GREATERTHAN:559 case OP_LESSTHANOREQUAL:560 case OP_GREATERTHANOREQUAL:561 case OP_MIN:562 case OP_MAX:563 {564 // (x1 x2 -- out)565 if (stack.size() < 2)566 return false;567 CBigNum bn1(stacktop(-2));568 CBigNum bn2(stacktop(-1));569 CBigNum bn;570 switch (opcode)571 {572 case OP_ADD:573 bn = bn1 + bn2;574 break;575576 case OP_SUB:577 bn = bn1 - bn2;578 break;579580 case OP_MUL:581 if (!BN_mul(&bn, &bn1, &bn2, pctx))582 return false;583 break;584585 case OP_DIV:586 if (!BN_div(&bn, NULL, &bn1, &bn2, pctx))587 return false;588 break;589590 case OP_MOD:591 if (!BN_mod(&bn, &bn1, &bn2, pctx))592 return false;593 break;594595 case OP_LSHIFT:596 if (bn2 < bnZero)597 return false;598 bn = bn1 << bn2.getulong();599 break;600601 case OP_RSHIFT:602 if (bn2 < bnZero)603 return false;604 bn = bn1 >> bn2.getulong();605 break;606607 case OP_BOOLAND: bn = (bn1 != bnZero && bn2 != bnZero); break;608 case OP_BOOLOR: bn = (bn1 != bnZero || bn2 != bnZero); break;609 case OP_NUMEQUAL: bn = (bn1 == bn2); break;610 case OP_NUMEQUALVERIFY: bn = (bn1 == bn2); break;611 case OP_NUMNOTEQUAL: bn = (bn1 != bn2); break;612 case OP_LESSTHAN: bn = (bn1 < bn2); break;613 case OP_GREATERTHAN: bn = (bn1 > bn2); break;614 case OP_LESSTHANOREQUAL: bn = (bn1 <= bn2); break;615 case OP_GREATERTHANOREQUAL: bn = (bn1 >= bn2); break;616 case OP_MIN: bn = (bn1 < bn2 ? bn1 : bn2); break;617 case OP_MAX: bn = (bn1 > bn2 ? bn1 : bn2); break;618 }619 stack.pop_back();620 stack.pop_back();621 stack.push_back(bn.getvch());622623 if (opcode == OP_NUMEQUALVERIFY)624 {625 if (CastToBool(stacktop(-1)))626 stack.pop_back();627 else628 pc = pend;629 }630 }631 break;632633 case OP_WITHIN:634 {635 // (x min max -- out)636 if (stack.size() < 3)637 return false;638 CBigNum bn1(stacktop(-3));639 CBigNum bn2(stacktop(-2));640 CBigNum bn3(stacktop(-1));641 bool fValue = (bn2 <= bn1 && bn1 < bn3);642 stack.pop_back();643 stack.pop_back();644 stack.pop_back();645 stack.push_back(fValue ? vchTrue : vchFalse);646 }647 break;648649650 //651 // Crypto652 //653 case OP_RIPEMD160:654 case OP_SHA1:655 case OP_SHA256:656 case OP_HASH160:657 case OP_HASH256:658 {659 // (in -- hash)660 if (stack.size() < 1)661 return false;662 valtype& vch = stacktop(-1);663 valtype vchHash(opcode == OP_RIPEMD160 || opcode == OP_SHA1 || opcode == OP_HASH160 ? 20 : 32);664 if (opcode == OP_RIPEMD160)665 RIPEMD160(&vch[0], vch.size(), &vchHash[0]);666 else if (opcode == OP_SHA1)667 SHA1(&vch[0], vch.size(), &vchHash[0]);668 else if (opcode == OP_SHA256)669 SHA256(&vch[0], vch.size(), &vchHash[0]);670 else if (opcode == OP_HASH160)671 {672 uint160 hash160 = Hash160(vch);673 memcpy(&vchHash[0], &hash160, sizeof(hash160));674 }675 else if (opcode == OP_HASH256)676 {677 uint256 hash = Hash(vch.begin(), vch.end());678 memcpy(&vchHash[0], &hash, sizeof(hash));679 }680 stack.pop_back();681 stack.push_back(vchHash);682 }683 break;684685 case OP_CODESEPARATOR:686 {687 // Hash starts after the code separator688 pbegincodehash = pc;689 }690 break;691692 case OP_CHECKSIG:693 case OP_CHECKSIGVERIFY:694 {695 // (sig pubkey -- bool)696 if (stack.size() < 2)697 return false;698699 valtype& vchSig = stacktop(-2);700 valtype& vchPubKey = stacktop(-1);701702 ////// debug print703 //PrintHex(vchSig.begin(), vchSig.end(), "sig: %s\n");704 //PrintHex(vchPubKey.begin(), vchPubKey.end(), "pubkey: %s\n");705706 // Subset of script starting at the most recent codeseparator707 CScript scriptCode(pbegincodehash, pend);708709 // Drop the signature, since there's no way for a signature to sign itself710 scriptCode.FindAndDelete(CScript(vchSig));711712 bool fSuccess = CheckSig(vchSig, vchPubKey, scriptCode, txTo, nIn, nHashType);713714 stack.pop_back();715 stack.pop_back();716 stack.push_back(fSuccess ? vchTrue : vchFalse);717 if (opcode == OP_CHECKSIGVERIFY)718 {719 if (fSuccess)720 stack.pop_back();721 else722 pc = pend;723 }724 }725 break;726727 case OP_CHECKMULTISIG:728 case OP_CHECKMULTISIGVERIFY:729 {730 // ([sig ...] num_of_signatures [pubkey ...] num_of_pubkeys -- bool)731732 int i = 1;733 if (stack.size() < i)734 return false;735736 int nKeysCount = CBigNum(stacktop(-i)).getint();737 if (nKeysCount < 0)738 return false;739 int ikey = ++i;740 i += nKeysCount;741 if (stack.size() < i)742 return false;743744 int nSigsCount = CBigNum(stacktop(-i)).getint();745 if (nSigsCount < 0 || nSigsCount > nKeysCount)746 return false;747 int isig = ++i;748 i += nSigsCount;749 if (stack.size() < i)750 return false;751752 // Subset of script starting at the most recent codeseparator753 CScript scriptCode(pbegincodehash, pend);754755 // Drop the signatures, since there's no way for a signature to sign itself756 for (int i = 0; i < nSigsCount; i++)757 {758 valtype& vchSig = stacktop(-isig-i);759 scriptCode.FindAndDelete(CScript(vchSig));760 }761762 bool fSuccess = true;763 while (fSuccess && nSigsCount > 0)764 {765 valtype& vchSig = stacktop(-isig);766 valtype& vchPubKey = stacktop(-ikey);767768 // Check signature769 if (CheckSig(vchSig, vchPubKey, scriptCode, txTo, nIn, nHashType))770 {771 isig++;772 nSigsCount--;773 }774 ikey++;775 nKeysCount--;776777 // If there are more signatures left than keys left,778 // then too many signatures have failed779 if (nSigsCount > nKeysCount)780 fSuccess = false;781 }782783 while (i-- > 0)784 stack.pop_back();785 stack.push_back(fSuccess ? vchTrue : vchFalse);786787 if (opcode == OP_CHECKMULTISIGVERIFY)788 {789 if (fSuccess)790 stack.pop_back();791 else792 pc = pend;793 }794 }795 break;796797 default:798 return false;799 }800 }801802803 if (pvStackRet)804 *pvStackRet = stack;805 return (stack.empty() ? false : CastToBool(stack.back()));806}807808#undef top809810811812813814815816817818uint256 SignatureHash(CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType)819{820 if (nIn >= txTo.vin.size())821 {822 printf("ERROR: SignatureHash() : nIn=%d out of range\n", nIn);823 return 1;824 }825 CTransaction txTmp(txTo);826827 // In case concatenating two scripts ends up with two codeseparators,828 // or an extra one at the end, this prevents all those possible incompatibilities.829 scriptCode.FindAndDelete(CScript(OP_CODESEPARATOR));830831 // Blank out other inputs' signatures832 for (int i = 0; i < txTmp.vin.size(); i++)833 txTmp.vin[i].scriptSig = CScript();834 txTmp.vin[nIn].scriptSig = scriptCode;835836 // Blank out some of the outputs837 if ((nHashType & 0x1f) == SIGHASH_NONE)838 {839 // Wildcard payee840 txTmp.vout.clear();841842 // Let the others update at will843 for (int i = 0; i < txTmp.vin.size(); i++)844 if (i != nIn)845 txTmp.vin[i].nSequence = 0;846 }847 else if ((nHashType & 0x1f) == SIGHASH_SINGLE)848 {849 // Only lockin the txout payee at same index as txin850 unsigned int nOut = nIn;851 if (nOut >= txTmp.vout.size())852 {853 printf("ERROR: SignatureHash() : nOut=%d out of range\n", nOut);854 return 1;855 }856 txTmp.vout.resize(nOut+1);857 for (int i = 0; i < nOut; i++)858 txTmp.vout[i].SetNull();859860 // Let the others update at will861 for (int i = 0; i < txTmp.vin.size(); i++)862 if (i != nIn)863 txTmp.vin[i].nSequence = 0;864 }865866 // Blank out other inputs completely, not recommended for open transactions867 if (nHashType & SIGHASH_ANYONECANPAY)868 {869 txTmp.vin[0] = txTmp.vin[nIn];870 txTmp.vin.resize(1);871 }872873 // Serialize and hash874 CDataStream ss(SER_GETHASH);875 ss.reserve(10000);876 ss << txTmp << nHashType;877 return Hash(ss.begin(), ss.end());878}879880881bool CheckSig(vector<unsigned char> vchSig, vector<unsigned char> vchPubKey, CScript scriptCode,882 const CTransaction& txTo, unsigned int nIn, int nHashType)883{884 CKey key;885 if (!key.SetPubKey(vchPubKey))886 return false;887888 // Hash type is one byte tacked on to the end of the signature889 if (vchSig.empty())890 return false;891 if (nHashType == 0)892 nHashType = vchSig.back();893 else if (nHashType != vchSig.back())894 return false;895 vchSig.pop_back();896897 if (key.Verify(SignatureHash(scriptCode, txTo, nIn, nHashType), vchSig))898 return true;899900 return false;901}902903904905906907908909910911912913bool Solver(const CScript& scriptPubKey, vector<pair<opcodetype, valtype> >& vSolutionRet)914{915 // Templates916 static vector<CScript> vTemplates;917 if (vTemplates.empty())918 {919 // Standard tx, sender provides pubkey, receiver adds signature920 vTemplates.push_back(CScript() << OP_PUBKEY << OP_CHECKSIG);921922 // Short account number tx, sender provides hash of pubkey, receiver provides signature and pubkey923 vTemplates.push_back(CScript() << OP_DUP << OP_HASH160 << OP_PUBKEYHASH << OP_EQUALVERIFY << OP_CHECKSIG);924 }925926 // Scan templates927 const CScript& script1 = scriptPubKey;928 foreach(const CScript& script2, vTemplates)929 {930 vSolutionRet.clear();931 opcodetype opcode1, opcode2;932 vector<unsigned char> vch1, vch2;933934 // Compare935 CScript::const_iterator pc1 = script1.begin();936 CScript::const_iterator pc2 = script2.begin();937 loop938 {939 bool f1 = script1.GetOp(pc1, opcode1, vch1);940 bool f2 = script2.GetOp(pc2, opcode2, vch2);941 if (!f1 && !f2)942 {943 // Success944 reverse(vSolutionRet.begin(), vSolutionRet.end());945 return true;946 }947 else if (f1 != f2)948 {949 break;950 }951 else if (opcode2 == OP_PUBKEY)952 {953 if (vch1.size() <= sizeof(uint256))954 break;955 vSolutionRet.push_back(make_pair(opcode2, vch1));956 }957 else if (opcode2 == OP_PUBKEYHASH)958 {959 if (vch1.size() != sizeof(uint160))960 break;961 vSolutionRet.push_back(make_pair(opcode2, vch1));962 }963 else if (opcode1 != opcode2)964 {965 break;966 }967 }968 }969970 vSolutionRet.clear();971 return false;972}973974975bool Solver(const CScript& scriptPubKey, uint256 hash, int nHashType, CScript& scriptSigRet)976{977 scriptSigRet.clear();978979 vector<pair<opcodetype, valtype> > vSolution;980 if (!Solver(scriptPubKey, vSolution))981 return false;982983 // Compile solution984 CRITICAL_BLOCK(cs_mapKeys)985 {986 foreach(PAIRTYPE(opcodetype, valtype)& item, vSolution)987 {988 if (item.first == OP_PUBKEY)989 {990 // Sign991 const valtype& vchPubKey = item.second;992 if (!mapKeys.count(vchPubKey))993 return false;994 if (hash != 0)995 {996 vector<unsigned char> vchSig;997 if (!CKey::Sign(mapKeys[vchPubKey], hash, vchSig))998 return false;999 vchSig.push_back((unsigned char)nHashType);1000 scriptSigRet << vchSig;1001 }1002 }1003 else if (item.first == OP_PUBKEYHASH)1004 {1005 // Sign and give pubkey1006 map<uint160, valtype>::iterator mi = mapPubKeys.find(uint160(item.second));1007 if (mi == mapPubKeys.end())1008 return false;1009 const vector<unsigned char>& vchPubKey = (*mi).second;1010 if (!mapKeys.count(vchPubKey))1011 return false;1012 if (hash != 0)1013 {1014 vector<unsigned char> vchSig;1015 if (!CKey::Sign(mapKeys[vchPubKey], hash, vchSig))1016 return false;1017 vchSig.push_back((unsigned char)nHashType);1018 scriptSigRet << vchSig << vchPubKey;1019 }1020 }1021 }1022 }10231024 return true;1025}102610271028bool IsMine(const CScript& scriptPubKey)1029{1030 CScript scriptSig;1031 return Solver(scriptPubKey, 0, 0, scriptSig);1032}103310341035bool ExtractPubKey(const CScript& scriptPubKey, bool fMineOnly, vector<unsigned char>& vchPubKeyRet)1036{1037 vchPubKeyRet.clear();10381039 vector<pair<opcodetype, valtype> > vSolution;1040 if (!Solver(scriptPubKey, vSolution))1041 return false;10421043 CRITICAL_BLOCK(cs_mapKeys)1044 {1045 foreach(PAIRTYPE(opcodetype, valtype)& item, vSolution)1046 {1047 valtype vchPubKey;1048 if (item.first == OP_PUBKEY)1049 {1050 vchPubKey = item.second;1051 }1052 else if (item.first == OP_PUBKEYHASH)1053 {1054 map<uint160, valtype>::iterator mi = mapPubKeys.find(uint160(item.second));1055 if (mi == mapPubKeys.end())1056 continue;1057 vchPubKey = (*mi).second;1058 }1059 if (!fMineOnly || mapKeys.count(vchPubKey))1060 {1061 vchPubKeyRet = vchPubKey;1062 return true;1063 }1064 }1065 }1066 return false;1067}106810691070bool ExtractHash160(const CScript& scriptPubKey, uint160& hash160Ret)1071{1072 hash160Ret = 0;10731074 vector<pair<opcodetype, valtype> > vSolution;1075 if (!Solver(scriptPubKey, vSolution))1076 return false;10771078 foreach(PAIRTYPE(opcodetype, valtype)& item, vSolution)1079 {1080 if (item.first == OP_PUBKEYHASH)1081 {1082 hash160Ret = uint160(item.second);1083 return true;1084 }1085 }1086 return false;1087}108810891090bool SignSignature(const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType, CScript scriptPrereq)1091{1092 assert(nIn < txTo.vin.size());1093 CTxIn& txin = txTo.vin[nIn];1094 assert(txin.prevout.n < txFrom.vout.size());1095 const CTxOut& txout = txFrom.vout[txin.prevout.n];10961097 // Leave out the signature from the hash, since a signature can't sign itself.1098 // The checksig op will also drop the signatures from its hash.1099 uint256 hash = SignatureHash(scriptPrereq + txout.scriptPubKey, txTo, nIn, nHashType);11001101 if (!Solver(txout.scriptPubKey, hash, nHashType, txin.scriptSig))1102 return false;11031104 txin.scriptSig = scriptPrereq + txin.scriptSig;11051106 // Test solution1107 if (scriptPrereq.empty())1108 if (!EvalScript(txin.scriptSig + CScript(OP_CODESEPARATOR) + txout.scriptPubKey, txTo, nIn))1109 return false;11101111 return true;1112}111311141115bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, int nHashType)1116{1117 assert(nIn < txTo.vin.size());1118 const CTxIn& txin = txTo.vin[nIn];1119 if (txin.prevout.n >= txFrom.vout.size())1120 return false;1121 const CTxOut& txout = txFrom.vout[txin.prevout.n];11221123 if (txin.prevout.hash != txFrom.GetHash())1124 return false;11251126 return EvalScript(txin.scriptSig + CScript(OP_CODESEPARATOR) + txout.scriptPubKey, txTo, nIn, nHashType);1127}
Click a line number to link to it; use #L10-L20 for a range. "=" next to a version means the file is identical there.
