1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2011 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
10 bool CheckSig(vector<unsigned char> vchSig, vector<unsigned char> vchPubKey, CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType);
14 typedef vector<unsigned char> valtype;
15 static const valtype vchFalse(0);
16 static const valtype vchZero(0);
17 static const valtype vchTrue(1, 1);
18 static const CBigNum bnZero(0);
19 static const CBigNum bnOne(1);
20 static const CBigNum bnFalse(0);
21 static const CBigNum bnTrue(1);
22 static const size_t nMaxNumSize = 4;
25 CBigNum CastToBigNum(const valtype& vch)
27 if (vch.size() > nMaxNumSize)
28 throw runtime_error("CastToBigNum() : overflow");
29 // Get rid of extra leading zeros
30 return CBigNum(CBigNum(vch).getvch());
33 bool CastToBool(const valtype& vch)
35 for (int i = 0; i < vch.size(); i++)
39 // Can be negative zero
40 if (i == vch.size()-1 && vch[i] == 0x80)
48 void MakeSameSize(valtype& vch1, valtype& vch2)
50 // Lengthen the shorter one
51 if (vch1.size() < vch2.size())
52 vch1.resize(vch2.size(), 0);
53 if (vch2.size() < vch1.size())
54 vch2.resize(vch1.size(), 0);
60 // Script is a stack machine (like Forth) that evaluates a predicate
61 // returning a bool indicating valid or not. There are no loops.
63 #define stacktop(i) (stack.at(stack.size()+(i)))
64 #define altstacktop(i) (altstack.at(altstack.size()+(i)))
65 static inline void popstack(vector<valtype>& stack)
68 throw runtime_error("popstack() : stack empty");
73 bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, int nHashType)
76 CScript::const_iterator pc = script.begin();
77 CScript::const_iterator pend = script.end();
78 CScript::const_iterator pbegincodehash = script.begin();
82 vector<valtype> altstack;
83 if (script.size() > 10000)
92 bool fExec = !count(vfExec.begin(), vfExec.end(), false);
97 if (!script.GetOp(pc, opcode, vchPushValue))
99 if (vchPushValue.size() > 520)
101 if (opcode > OP_16 && ++nOpCount > 201)
104 if (opcode == OP_CAT ||
105 opcode == OP_SUBSTR ||
107 opcode == OP_RIGHT ||
108 opcode == OP_INVERT ||
117 opcode == OP_LSHIFT ||
121 if (fExec && 0 <= opcode && opcode <= OP_PUSHDATA4)
122 stack.push_back(vchPushValue);
123 else if (fExec || (OP_IF <= opcode && opcode <= OP_ENDIF))
148 CBigNum bn((int)opcode - (int)(OP_1 - 1));
149 stack.push_back(bn.getvch());
158 case OP_NOP1: case OP_NOP2: case OP_NOP3: case OP_NOP4: case OP_NOP5:
159 case OP_NOP6: case OP_NOP7: case OP_NOP8: case OP_NOP9: case OP_NOP10:
165 // <expression> if [statements] [else [statements]] endif
169 if (stack.size() < 1)
171 valtype& vch = stacktop(-1);
172 fValue = CastToBool(vch);
173 if (opcode == OP_NOTIF)
177 vfExec.push_back(fValue);
185 vfExec.back() = !vfExec.back();
200 // (false -- false) and return
201 if (stack.size() < 1)
203 bool fValue = CastToBool(stacktop(-1));
223 if (stack.size() < 1)
225 altstack.push_back(stacktop(-1));
230 case OP_FROMALTSTACK:
232 if (altstack.size() < 1)
234 stack.push_back(altstacktop(-1));
242 if (stack.size() < 2)
251 // (x1 x2 -- x1 x2 x1 x2)
252 if (stack.size() < 2)
254 valtype vch1 = stacktop(-2);
255 valtype vch2 = stacktop(-1);
256 stack.push_back(vch1);
257 stack.push_back(vch2);
263 // (x1 x2 x3 -- x1 x2 x3 x1 x2 x3)
264 if (stack.size() < 3)
266 valtype vch1 = stacktop(-3);
267 valtype vch2 = stacktop(-2);
268 valtype vch3 = stacktop(-1);
269 stack.push_back(vch1);
270 stack.push_back(vch2);
271 stack.push_back(vch3);
277 // (x1 x2 x3 x4 -- x1 x2 x3 x4 x1 x2)
278 if (stack.size() < 4)
280 valtype vch1 = stacktop(-4);
281 valtype vch2 = stacktop(-3);
282 stack.push_back(vch1);
283 stack.push_back(vch2);
289 // (x1 x2 x3 x4 x5 x6 -- x3 x4 x5 x6 x1 x2)
290 if (stack.size() < 6)
292 valtype vch1 = stacktop(-6);
293 valtype vch2 = stacktop(-5);
294 stack.erase(stack.end()-6, stack.end()-4);
295 stack.push_back(vch1);
296 stack.push_back(vch2);
302 // (x1 x2 x3 x4 -- x3 x4 x1 x2)
303 if (stack.size() < 4)
305 swap(stacktop(-4), stacktop(-2));
306 swap(stacktop(-3), stacktop(-1));
313 if (stack.size() < 1)
315 valtype vch = stacktop(-1);
317 stack.push_back(vch);
324 CBigNum bn(stack.size());
325 stack.push_back(bn.getvch());
332 if (stack.size() < 1)
341 if (stack.size() < 1)
343 valtype vch = stacktop(-1);
344 stack.push_back(vch);
351 if (stack.size() < 2)
353 stack.erase(stack.end() - 2);
359 // (x1 x2 -- x1 x2 x1)
360 if (stack.size() < 2)
362 valtype vch = stacktop(-2);
363 stack.push_back(vch);
370 // (xn ... x2 x1 x0 n - xn ... x2 x1 x0 xn)
371 // (xn ... x2 x1 x0 n - ... x2 x1 x0 xn)
372 if (stack.size() < 2)
374 int n = CastToBigNum(stacktop(-1)).getint();
376 if (n < 0 || n >= stack.size())
378 valtype vch = stacktop(-n-1);
379 if (opcode == OP_ROLL)
380 stack.erase(stack.end()-n-1);
381 stack.push_back(vch);
387 // (x1 x2 x3 -- x2 x3 x1)
388 // x2 x1 x3 after first swap
389 // x2 x3 x1 after second swap
390 if (stack.size() < 3)
392 swap(stacktop(-3), stacktop(-2));
393 swap(stacktop(-2), stacktop(-1));
400 if (stack.size() < 2)
402 swap(stacktop(-2), stacktop(-1));
408 // (x1 x2 -- x2 x1 x2)
409 if (stack.size() < 2)
411 valtype vch = stacktop(-1);
412 stack.insert(stack.end()-2, vch);
423 if (stack.size() < 2)
425 valtype& vch1 = stacktop(-2);
426 valtype& vch2 = stacktop(-1);
427 vch1.insert(vch1.end(), vch2.begin(), vch2.end());
429 if (stacktop(-1).size() > 520)
436 // (in begin size -- out)
437 if (stack.size() < 3)
439 valtype& vch = stacktop(-3);
440 int nBegin = CastToBigNum(stacktop(-2)).getint();
441 int nEnd = nBegin + CastToBigNum(stacktop(-1)).getint();
442 if (nBegin < 0 || nEnd < nBegin)
444 if (nBegin > vch.size())
446 if (nEnd > vch.size())
448 vch.erase(vch.begin() + nEnd, vch.end());
449 vch.erase(vch.begin(), vch.begin() + nBegin);
459 if (stack.size() < 2)
461 valtype& vch = stacktop(-2);
462 int nSize = CastToBigNum(stacktop(-1)).getint();
465 if (nSize > vch.size())
467 if (opcode == OP_LEFT)
468 vch.erase(vch.begin() + nSize, vch.end());
470 vch.erase(vch.begin(), vch.end() - nSize);
478 if (stack.size() < 1)
480 CBigNum bn(stacktop(-1).size());
481 stack.push_back(bn.getvch());
492 if (stack.size() < 1)
494 valtype& vch = stacktop(-1);
495 for (int i = 0; i < vch.size(); i++)
505 if (stack.size() < 2)
507 valtype& vch1 = stacktop(-2);
508 valtype& vch2 = stacktop(-1);
509 MakeSameSize(vch1, vch2);
510 if (opcode == OP_AND)
512 for (int i = 0; i < vch1.size(); i++)
515 else if (opcode == OP_OR)
517 for (int i = 0; i < vch1.size(); i++)
520 else if (opcode == OP_XOR)
522 for (int i = 0; i < vch1.size(); i++)
531 //case OP_NOTEQUAL: // use OP_NUMNOTEQUAL
534 if (stack.size() < 2)
536 valtype& vch1 = stacktop(-2);
537 valtype& vch2 = stacktop(-1);
538 bool fEqual = (vch1 == vch2);
539 // OP_NOTEQUAL is disabled because it would be too easy to say
540 // something like n != 1 and have some wiseguy pass in 1 with extra
541 // zero bytes after it (numerically, 0x01 == 0x0001 == 0x000001)
542 //if (opcode == OP_NOTEQUAL)
546 stack.push_back(fEqual ? vchTrue : vchFalse);
547 if (opcode == OP_EQUALVERIFY)
571 if (stack.size() < 1)
573 CBigNum bn = CastToBigNum(stacktop(-1));
576 case OP_1ADD: bn += bnOne; break;
577 case OP_1SUB: bn -= bnOne; break;
578 case OP_2MUL: bn <<= 1; break;
579 case OP_2DIV: bn >>= 1; break;
580 case OP_NEGATE: bn = -bn; break;
581 case OP_ABS: if (bn < bnZero) bn = -bn; break;
582 case OP_NOT: bn = (bn == bnZero); break;
583 case OP_0NOTEQUAL: bn = (bn != bnZero); break;
584 default: assert(!"invalid opcode"); break;
587 stack.push_back(bn.getvch());
601 case OP_NUMEQUALVERIFY:
605 case OP_LESSTHANOREQUAL:
606 case OP_GREATERTHANOREQUAL:
611 if (stack.size() < 2)
613 CBigNum bn1 = CastToBigNum(stacktop(-2));
614 CBigNum bn2 = CastToBigNum(stacktop(-1));
627 if (!BN_mul(&bn, &bn1, &bn2, pctx))
632 if (!BN_div(&bn, NULL, &bn1, &bn2, pctx))
637 if (!BN_mod(&bn, &bn1, &bn2, pctx))
642 if (bn2 < bnZero || bn2 > CBigNum(2048))
644 bn = bn1 << bn2.getulong();
648 if (bn2 < bnZero || bn2 > CBigNum(2048))
650 bn = bn1 >> bn2.getulong();
653 case OP_BOOLAND: bn = (bn1 != bnZero && bn2 != bnZero); break;
654 case OP_BOOLOR: bn = (bn1 != bnZero || bn2 != bnZero); break;
655 case OP_NUMEQUAL: bn = (bn1 == bn2); break;
656 case OP_NUMEQUALVERIFY: bn = (bn1 == bn2); break;
657 case OP_NUMNOTEQUAL: bn = (bn1 != bn2); break;
658 case OP_LESSTHAN: bn = (bn1 < bn2); break;
659 case OP_GREATERTHAN: bn = (bn1 > bn2); break;
660 case OP_LESSTHANOREQUAL: bn = (bn1 <= bn2); break;
661 case OP_GREATERTHANOREQUAL: bn = (bn1 >= bn2); break;
662 case OP_MIN: bn = (bn1 < bn2 ? bn1 : bn2); break;
663 case OP_MAX: bn = (bn1 > bn2 ? bn1 : bn2); break;
664 default: assert(!"invalid opcode"); break;
668 stack.push_back(bn.getvch());
670 if (opcode == OP_NUMEQUALVERIFY)
672 if (CastToBool(stacktop(-1)))
682 // (x min max -- out)
683 if (stack.size() < 3)
685 CBigNum bn1 = CastToBigNum(stacktop(-3));
686 CBigNum bn2 = CastToBigNum(stacktop(-2));
687 CBigNum bn3 = CastToBigNum(stacktop(-1));
688 bool fValue = (bn2 <= bn1 && bn1 < bn3);
692 stack.push_back(fValue ? vchTrue : vchFalse);
707 if (stack.size() < 1)
709 valtype& vch = stacktop(-1);
710 valtype vchHash((opcode == OP_RIPEMD160 || opcode == OP_SHA1 || opcode == OP_HASH160) ? 20 : 32);
711 if (opcode == OP_RIPEMD160)
712 RIPEMD160(&vch[0], vch.size(), &vchHash[0]);
713 else if (opcode == OP_SHA1)
714 SHA1(&vch[0], vch.size(), &vchHash[0]);
715 else if (opcode == OP_SHA256)
716 SHA256(&vch[0], vch.size(), &vchHash[0]);
717 else if (opcode == OP_HASH160)
719 uint160 hash160 = Hash160(vch);
720 memcpy(&vchHash[0], &hash160, sizeof(hash160));
722 else if (opcode == OP_HASH256)
724 uint256 hash = Hash(vch.begin(), vch.end());
725 memcpy(&vchHash[0], &hash, sizeof(hash));
728 stack.push_back(vchHash);
732 case OP_CODESEPARATOR:
734 // Hash starts after the code separator
740 case OP_CHECKSIGVERIFY:
742 // (sig pubkey -- bool)
743 if (stack.size() < 2)
746 valtype& vchSig = stacktop(-2);
747 valtype& vchPubKey = stacktop(-1);
750 //PrintHex(vchSig.begin(), vchSig.end(), "sig: %s\n");
751 //PrintHex(vchPubKey.begin(), vchPubKey.end(), "pubkey: %s\n");
753 // Subset of script starting at the most recent codeseparator
754 CScript scriptCode(pbegincodehash, pend);
756 // Drop the signature, since there's no way for a signature to sign itself
757 scriptCode.FindAndDelete(CScript(vchSig));
759 bool fSuccess = CheckSig(vchSig, vchPubKey, scriptCode, txTo, nIn, nHashType);
763 stack.push_back(fSuccess ? vchTrue : vchFalse);
764 if (opcode == OP_CHECKSIGVERIFY)
774 case OP_CHECKMULTISIG:
775 case OP_CHECKMULTISIGVERIFY:
777 // ([sig ...] num_of_signatures [pubkey ...] num_of_pubkeys -- bool)
780 if (stack.size() < i)
783 int nKeysCount = CastToBigNum(stacktop(-i)).getint();
784 if (nKeysCount < 0 || nKeysCount > 20)
786 nOpCount += nKeysCount;
791 if (stack.size() < i)
794 int nSigsCount = CastToBigNum(stacktop(-i)).getint();
795 if (nSigsCount < 0 || nSigsCount > nKeysCount)
799 if (stack.size() < i)
802 // Subset of script starting at the most recent codeseparator
803 CScript scriptCode(pbegincodehash, pend);
805 // Drop the signatures, since there's no way for a signature to sign itself
806 for (int k = 0; k < nSigsCount; k++)
808 valtype& vchSig = stacktop(-isig-k);
809 scriptCode.FindAndDelete(CScript(vchSig));
812 bool fSuccess = true;
813 while (fSuccess && nSigsCount > 0)
815 valtype& vchSig = stacktop(-isig);
816 valtype& vchPubKey = stacktop(-ikey);
819 if (CheckSig(vchSig, vchPubKey, scriptCode, txTo, nIn, nHashType))
827 // If there are more signatures left than keys left,
828 // then too many signatures have failed
829 if (nSigsCount > nKeysCount)
835 stack.push_back(fSuccess ? vchTrue : vchFalse);
837 if (opcode == OP_CHECKMULTISIGVERIFY)
852 if (stack.size() + altstack.size() > 1000)
876 uint256 SignatureHash(CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType)
878 if (nIn >= txTo.vin.size())
880 printf("ERROR: SignatureHash() : nIn=%d out of range\n", nIn);
883 CTransaction txTmp(txTo);
885 // In case concatenating two scripts ends up with two codeseparators,
886 // or an extra one at the end, this prevents all those possible incompatibilities.
887 scriptCode.FindAndDelete(CScript(OP_CODESEPARATOR));
889 // Blank out other inputs' signatures
890 for (int i = 0; i < txTmp.vin.size(); i++)
891 txTmp.vin[i].scriptSig = CScript();
892 txTmp.vin[nIn].scriptSig = scriptCode;
894 // Blank out some of the outputs
895 if ((nHashType & 0x1f) == SIGHASH_NONE)
900 // Let the others update at will
901 for (int i = 0; i < txTmp.vin.size(); i++)
903 txTmp.vin[i].nSequence = 0;
905 else if ((nHashType & 0x1f) == SIGHASH_SINGLE)
907 // Only lockin the txout payee at same index as txin
908 unsigned int nOut = nIn;
909 if (nOut >= txTmp.vout.size())
911 printf("ERROR: SignatureHash() : nOut=%d out of range\n", nOut);
914 txTmp.vout.resize(nOut+1);
915 for (int i = 0; i < nOut; i++)
916 txTmp.vout[i].SetNull();
918 // Let the others update at will
919 for (int i = 0; i < txTmp.vin.size(); i++)
921 txTmp.vin[i].nSequence = 0;
924 // Blank out other inputs completely, not recommended for open transactions
925 if (nHashType & SIGHASH_ANYONECANPAY)
927 txTmp.vin[0] = txTmp.vin[nIn];
931 // Serialize and hash
932 CDataStream ss(SER_GETHASH);
934 ss << txTmp << nHashType;
935 return Hash(ss.begin(), ss.end());
939 bool CheckSig(vector<unsigned char> vchSig, vector<unsigned char> vchPubKey, CScript scriptCode,
940 const CTransaction& txTo, unsigned int nIn, int nHashType)
943 if (!key.SetPubKey(vchPubKey))
946 // Hash type is one byte tacked on to the end of the signature
950 nHashType = vchSig.back();
951 else if (nHashType != vchSig.back())
955 return key.Verify(SignatureHash(scriptCode, txTo, nIn, nHashType), vchSig);
967 bool Solver(const CScript& scriptPubKey, vector<pair<opcodetype, valtype> >& vSolutionRet)
970 static vector<CScript> vTemplates;
971 if (vTemplates.empty())
973 // Standard tx, sender provides pubkey, receiver adds signature
974 vTemplates.push_back(CScript() << OP_PUBKEY << OP_CHECKSIG);
976 // Bitcoin address tx, sender provides hash of pubkey, receiver provides signature and pubkey
977 vTemplates.push_back(CScript() << OP_DUP << OP_HASH160 << OP_PUBKEYHASH << OP_EQUALVERIFY << OP_CHECKSIG);
981 const CScript& script1 = scriptPubKey;
982 BOOST_FOREACH(const CScript& script2, vTemplates)
984 vSolutionRet.clear();
985 opcodetype opcode1, opcode2;
986 vector<unsigned char> vch1, vch2;
989 CScript::const_iterator pc1 = script1.begin();
990 CScript::const_iterator pc2 = script2.begin();
993 if (pc1 == script1.end() && pc2 == script2.end())
996 reverse(vSolutionRet.begin(), vSolutionRet.end());
999 if (!script1.GetOp(pc1, opcode1, vch1))
1001 if (!script2.GetOp(pc2, opcode2, vch2))
1003 if (opcode2 == OP_PUBKEY)
1005 if (vch1.size() < 33 || vch1.size() > 120)
1007 vSolutionRet.push_back(make_pair(opcode2, vch1));
1009 else if (opcode2 == OP_PUBKEYHASH)
1011 if (vch1.size() != sizeof(uint160))
1013 vSolutionRet.push_back(make_pair(opcode2, vch1));
1015 else if (opcode1 != opcode2 || vch1 != vch2)
1022 vSolutionRet.clear();
1027 bool Solver(const CKeyStore& keystore, const CScript& scriptPubKey, uint256 hash, int nHashType, CScript& scriptSigRet)
1029 scriptSigRet.clear();
1031 vector<pair<opcodetype, valtype> > vSolution;
1032 if (!Solver(scriptPubKey, vSolution))
1036 BOOST_FOREACH(PAIRTYPE(opcodetype, valtype)& item, vSolution)
1038 if (item.first == OP_PUBKEY)
1041 const valtype& vchPubKey = item.second;
1043 if (!keystore.GetKey(Hash160(vchPubKey), key))
1045 if (key.GetPubKey() != vchPubKey)
1049 vector<unsigned char> vchSig;
1050 if (!key.Sign(hash, vchSig))
1052 vchSig.push_back((unsigned char)nHashType);
1053 scriptSigRet << vchSig;
1056 else if (item.first == OP_PUBKEYHASH)
1058 // Sign and give pubkey
1060 if (!keystore.GetKey(uint160(item.second), key))
1064 vector<unsigned char> vchSig;
1065 if (!key.Sign(hash, vchSig))
1067 vchSig.push_back((unsigned char)nHashType);
1068 scriptSigRet << vchSig << key.GetPubKey();
1081 bool IsStandard(const CScript& scriptPubKey)
1083 vector<pair<opcodetype, valtype> > vSolution;
1084 return Solver(scriptPubKey, vSolution);
1088 bool IsMine(const CKeyStore &keystore, const CScript& scriptPubKey)
1090 vector<pair<opcodetype, valtype> > vSolution;
1091 if (!Solver(scriptPubKey, vSolution))
1095 BOOST_FOREACH(PAIRTYPE(opcodetype, valtype)& item, vSolution)
1097 if (item.first == OP_PUBKEY)
1099 const valtype& vchPubKey = item.second;
1100 vector<unsigned char> vchPubKeyFound;
1101 if (!keystore.GetPubKey(Hash160(vchPubKey), vchPubKeyFound))
1103 if (vchPubKeyFound != vchPubKey)
1106 else if (item.first == OP_PUBKEYHASH)
1108 if (!keystore.HaveKey(uint160(item.second)))
1120 bool static ExtractAddressInner(const CScript& scriptPubKey, const CKeyStore* keystore, CBitcoinAddress& addressRet)
1122 vector<pair<opcodetype, valtype> > vSolution;
1123 if (!Solver(scriptPubKey, vSolution))
1126 BOOST_FOREACH(PAIRTYPE(opcodetype, valtype)& item, vSolution)
1128 if (item.first == OP_PUBKEY)
1129 addressRet.SetPubKey(item.second);
1130 else if (item.first == OP_PUBKEYHASH)
1131 addressRet.SetHash160((uint160)item.second);
1132 if (keystore == NULL || keystore->HaveKey(addressRet))
1139 bool ExtractAddress(const CScript& scriptPubKey, const CKeyStore* keystore, CBitcoinAddress& addressRet)
1142 return ExtractAddressInner(scriptPubKey, keystore, addressRet);
1144 return ExtractAddressInner(scriptPubKey, NULL, addressRet);
1149 bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn,
1150 bool fValidatePayToScriptHash, int nHashType)
1152 vector<vector<unsigned char> > stack, stackCopy;
1153 if (!EvalScript(stack, scriptSig, txTo, nIn, nHashType))
1155 if (fValidatePayToScriptHash)
1157 if (!EvalScript(stack, scriptPubKey, txTo, nIn, nHashType))
1162 if (CastToBool(stack.back()) == false)
1165 // Additional validation for spend-to-script-hash transactions:
1166 if (fValidatePayToScriptHash && scriptPubKey.IsPayToScriptHash())
1168 if (!scriptSig.IsPushOnly()) // scriptSig must be literals-only
1169 return false; // or validation fails
1171 const valtype& pubKeySerialized = stackCopy.back();
1172 CScript pubKey2(pubKeySerialized.begin(), pubKeySerialized.end());
1173 popstack(stackCopy);
1175 if (!EvalScript(stackCopy, pubKey2, txTo, nIn, nHashType))
1177 if (stackCopy.empty())
1179 return CastToBool(stackCopy.back());
1186 bool SignSignature(const CKeyStore &keystore, const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType, CScript scriptPrereq)
1188 assert(nIn < txTo.vin.size());
1189 CTxIn& txin = txTo.vin[nIn];
1190 assert(txin.prevout.n < txFrom.vout.size());
1191 const CTxOut& txout = txFrom.vout[txin.prevout.n];
1193 // Leave out the signature from the hash, since a signature can't sign itself.
1194 // The checksig op will also drop the signatures from its hash.
1195 uint256 hash = SignatureHash(scriptPrereq + txout.scriptPubKey, txTo, nIn, nHashType);
1197 if (!Solver(keystore, txout.scriptPubKey, hash, nHashType, txin.scriptSig))
1200 txin.scriptSig = scriptPrereq + txin.scriptSig;
1203 if (scriptPrereq.empty())
1204 if (!VerifyScript(txin.scriptSig, txout.scriptPubKey, txTo, nIn, true, 0))
1211 bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, bool fValidatePayToScriptHash, int nHashType)
1213 assert(nIn < txTo.vin.size());
1214 const CTxIn& txin = txTo.vin[nIn];
1215 if (txin.prevout.n >= txFrom.vout.size())
1217 const CTxOut& txout = txFrom.vout[txin.prevout.n];
1219 if (txin.prevout.hash != txFrom.GetHash())
1222 if (!VerifyScript(txin.scriptSig, txout.scriptPubKey, txTo, nIn, fValidatePayToScriptHash, nHashType))
1228 int CScript::GetSigOpCount(bool fAccurate) const
1231 const_iterator pc = begin();
1232 opcodetype lastOpcode = OP_INVALIDOPCODE;
1236 if (!GetOp(pc, opcode))
1238 if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
1240 else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
1242 if (fAccurate && lastOpcode >= OP_1 && lastOpcode <= OP_16)
1243 n += DecodeOP_N(lastOpcode);
1247 lastOpcode = opcode;
1252 int CScript::GetSigOpCount(const CScript& scriptSig) const
1254 if (!IsPayToScriptHash())
1255 return GetSigOpCount(true);
1257 // This is a pay-to-script-hash scriptPubKey;
1258 // get the last item that the scriptSig
1259 // pushes onto the stack:
1260 const_iterator pc = scriptSig.begin();
1261 vector<unsigned char> data;
1262 while (pc < scriptSig.end())
1265 if (!scriptSig.GetOp(pc, opcode, data))
1271 /// ... and return it's opcount:
1272 CScript subscript(data.begin(), data.end());
1273 return subscript.GetSigOpCount(true);
1276 bool CScript::IsPayToScriptHash() const
1278 // Extra-fast test for pay-to-script-hash CScripts:
1279 return (this->size() == 23 &&
1280 this->at(0) == OP_HASH160 &&
1281 this->at(1) == 0x14 &&
1282 this->at(22) == OP_EQUAL);