1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #include <boost/foreach.hpp>
6 #include <boost/tuple/tuple.hpp>
19 bool CheckSig(vector<unsigned char> vchSig, vector<unsigned char> vchPubKey, CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType);
21 static const valtype vchFalse(0);
22 static const valtype vchZero(0);
23 static const valtype vchTrue(1, 1);
24 static const CBigNum bnZero(0);
25 static const CBigNum bnOne(1);
26 static const CBigNum bnFalse(0);
27 static const CBigNum bnTrue(1);
28 static const size_t nMaxNumSize = 4;
31 CBigNum CastToBigNum(const valtype& vch)
33 if (vch.size() > nMaxNumSize)
34 throw runtime_error("CastToBigNum() : overflow");
35 // Get rid of extra leading zeros
36 return CBigNum(CBigNum(vch).getvch());
39 bool CastToBool(const valtype& vch)
41 for (unsigned int i = 0; i < vch.size(); i++)
45 // Can be negative zero
46 if (i == vch.size()-1 && vch[i] == 0x80)
55 // WARNING: This does not work as expected for signed integers; the sign-bit
56 // is left in place as the integer is zero-extended. The correct behavior
57 // would be to move the most significant bit of the last byte during the
58 // resize process. MakeSameSize() is currently only used by the disabled
59 // opcodes OP_AND, OP_OR, and OP_XOR.
61 void MakeSameSize(valtype& vch1, valtype& vch2)
63 // Lengthen the shorter one
64 if (vch1.size() < vch2.size())
66 // +unsigned char msb = vch1[vch1.size()-1];
67 // +vch1[vch1.size()-1] &= 0x7f;
68 // vch1.resize(vch2.size(), 0);
69 // +vch1[vch1.size()-1] = msb;
70 vch1.resize(vch2.size(), 0);
71 if (vch2.size() < vch1.size())
73 // +unsigned char msb = vch2[vch2.size()-1];
74 // +vch2[vch2.size()-1] &= 0x7f;
75 // vch2.resize(vch1.size(), 0);
76 // +vch2[vch2.size()-1] = msb;
77 vch2.resize(vch1.size(), 0);
83 // Script is a stack machine (like Forth) that evaluates a predicate
84 // returning a bool indicating valid or not. There are no loops.
86 #define stacktop(i) (stack.at(stack.size()+(i)))
87 #define altstacktop(i) (altstack.at(altstack.size()+(i)))
88 static inline void popstack(vector<valtype>& stack)
91 throw runtime_error("popstack() : stack empty");
96 const char* GetTxnOutputType(txnouttype t)
100 case TX_NONSTANDARD: return "nonstandard";
101 case TX_PUBKEY: return "pubkey";
102 case TX_PUBKEYHASH: return "pubkeyhash";
103 case TX_SCRIPTHASH: return "scripthash";
104 case TX_MULTISIG: return "multisig";
105 case TX_NULL_DATA: return "nulldata";
111 const char* GetOpName(opcodetype opcode)
116 case OP_0 : return "0";
117 case OP_PUSHDATA1 : return "OP_PUSHDATA1";
118 case OP_PUSHDATA2 : return "OP_PUSHDATA2";
119 case OP_PUSHDATA4 : return "OP_PUSHDATA4";
120 case OP_1NEGATE : return "-1";
121 case OP_RESERVED : return "OP_RESERVED";
122 case OP_1 : return "1";
123 case OP_2 : return "2";
124 case OP_3 : return "3";
125 case OP_4 : return "4";
126 case OP_5 : return "5";
127 case OP_6 : return "6";
128 case OP_7 : return "7";
129 case OP_8 : return "8";
130 case OP_9 : return "9";
131 case OP_10 : return "10";
132 case OP_11 : return "11";
133 case OP_12 : return "12";
134 case OP_13 : return "13";
135 case OP_14 : return "14";
136 case OP_15 : return "15";
137 case OP_16 : return "16";
140 case OP_NOP : return "OP_NOP";
141 case OP_VER : return "OP_VER";
142 case OP_IF : return "OP_IF";
143 case OP_NOTIF : return "OP_NOTIF";
144 case OP_VERIF : return "OP_VERIF";
145 case OP_VERNOTIF : return "OP_VERNOTIF";
146 case OP_ELSE : return "OP_ELSE";
147 case OP_ENDIF : return "OP_ENDIF";
148 case OP_VERIFY : return "OP_VERIFY";
149 case OP_RETURN : return "OP_RETURN";
152 case OP_TOALTSTACK : return "OP_TOALTSTACK";
153 case OP_FROMALTSTACK : return "OP_FROMALTSTACK";
154 case OP_2DROP : return "OP_2DROP";
155 case OP_2DUP : return "OP_2DUP";
156 case OP_3DUP : return "OP_3DUP";
157 case OP_2OVER : return "OP_2OVER";
158 case OP_2ROT : return "OP_2ROT";
159 case OP_2SWAP : return "OP_2SWAP";
160 case OP_IFDUP : return "OP_IFDUP";
161 case OP_DEPTH : return "OP_DEPTH";
162 case OP_DROP : return "OP_DROP";
163 case OP_DUP : return "OP_DUP";
164 case OP_NIP : return "OP_NIP";
165 case OP_OVER : return "OP_OVER";
166 case OP_PICK : return "OP_PICK";
167 case OP_ROLL : return "OP_ROLL";
168 case OP_ROT : return "OP_ROT";
169 case OP_SWAP : return "OP_SWAP";
170 case OP_TUCK : return "OP_TUCK";
173 case OP_CAT : return "OP_CAT";
174 case OP_SUBSTR : return "OP_SUBSTR";
175 case OP_LEFT : return "OP_LEFT";
176 case OP_RIGHT : return "OP_RIGHT";
177 case OP_SIZE : return "OP_SIZE";
180 case OP_INVERT : return "OP_INVERT";
181 case OP_AND : return "OP_AND";
182 case OP_OR : return "OP_OR";
183 case OP_XOR : return "OP_XOR";
184 case OP_EQUAL : return "OP_EQUAL";
185 case OP_EQUALVERIFY : return "OP_EQUALVERIFY";
186 case OP_RESERVED1 : return "OP_RESERVED1";
187 case OP_RESERVED2 : return "OP_RESERVED2";
190 case OP_1ADD : return "OP_1ADD";
191 case OP_1SUB : return "OP_1SUB";
192 case OP_2MUL : return "OP_2MUL";
193 case OP_2DIV : return "OP_2DIV";
194 case OP_NEGATE : return "OP_NEGATE";
195 case OP_ABS : return "OP_ABS";
196 case OP_NOT : return "OP_NOT";
197 case OP_0NOTEQUAL : return "OP_0NOTEQUAL";
198 case OP_ADD : return "OP_ADD";
199 case OP_SUB : return "OP_SUB";
200 case OP_MUL : return "OP_MUL";
201 case OP_DIV : return "OP_DIV";
202 case OP_MOD : return "OP_MOD";
203 case OP_LSHIFT : return "OP_LSHIFT";
204 case OP_RSHIFT : return "OP_RSHIFT";
205 case OP_BOOLAND : return "OP_BOOLAND";
206 case OP_BOOLOR : return "OP_BOOLOR";
207 case OP_NUMEQUAL : return "OP_NUMEQUAL";
208 case OP_NUMEQUALVERIFY : return "OP_NUMEQUALVERIFY";
209 case OP_NUMNOTEQUAL : return "OP_NUMNOTEQUAL";
210 case OP_LESSTHAN : return "OP_LESSTHAN";
211 case OP_GREATERTHAN : return "OP_GREATERTHAN";
212 case OP_LESSTHANOREQUAL : return "OP_LESSTHANOREQUAL";
213 case OP_GREATERTHANOREQUAL : return "OP_GREATERTHANOREQUAL";
214 case OP_MIN : return "OP_MIN";
215 case OP_MAX : return "OP_MAX";
216 case OP_WITHIN : return "OP_WITHIN";
219 case OP_RIPEMD160 : return "OP_RIPEMD160";
220 case OP_SHA1 : return "OP_SHA1";
221 case OP_SHA256 : return "OP_SHA256";
222 case OP_HASH160 : return "OP_HASH160";
223 case OP_HASH256 : return "OP_HASH256";
224 case OP_CODESEPARATOR : return "OP_CODESEPARATOR";
225 case OP_CHECKSIG : return "OP_CHECKSIG";
226 case OP_CHECKSIGVERIFY : return "OP_CHECKSIGVERIFY";
227 case OP_CHECKMULTISIG : return "OP_CHECKMULTISIG";
228 case OP_CHECKMULTISIGVERIFY : return "OP_CHECKMULTISIGVERIFY";
231 case OP_NOP1 : return "OP_NOP1";
232 case OP_NOP2 : return "OP_NOP2";
233 case OP_NOP3 : return "OP_NOP3";
234 case OP_NOP4 : return "OP_NOP4";
235 case OP_NOP5 : return "OP_NOP5";
236 case OP_NOP6 : return "OP_NOP6";
237 case OP_NOP7 : return "OP_NOP7";
238 case OP_NOP8 : return "OP_NOP8";
239 case OP_NOP9 : return "OP_NOP9";
240 case OP_NOP10 : return "OP_NOP10";
244 // template matching params
245 case OP_PUBKEYHASH : return "OP_PUBKEYHASH";
246 case OP_PUBKEY : return "OP_PUBKEY";
248 case OP_INVALIDOPCODE : return "OP_INVALIDOPCODE";
254 bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, int nHashType)
257 CScript::const_iterator pc = script.begin();
258 CScript::const_iterator pend = script.end();
259 CScript::const_iterator pbegincodehash = script.begin();
261 valtype vchPushValue;
263 vector<valtype> altstack;
264 if (script.size() > 10000)
273 bool fExec = !count(vfExec.begin(), vfExec.end(), false);
278 if (!script.GetOp(pc, opcode, vchPushValue))
280 if (vchPushValue.size() > 520)
282 if (opcode > OP_16 && ++nOpCount > 201)
285 if (opcode == OP_CAT ||
286 opcode == OP_SUBSTR ||
288 opcode == OP_RIGHT ||
289 opcode == OP_INVERT ||
298 opcode == OP_LSHIFT ||
302 if (fExec && 0 <= opcode && opcode <= OP_PUSHDATA4)
303 stack.push_back(vchPushValue);
304 else if (fExec || (OP_IF <= opcode && opcode <= OP_ENDIF))
329 CBigNum bn((int)opcode - (int)(OP_1 - 1));
330 stack.push_back(bn.getvch());
339 case OP_NOP1: case OP_NOP2: case OP_NOP3: case OP_NOP4: case OP_NOP5:
340 case OP_NOP6: case OP_NOP7: case OP_NOP8: case OP_NOP9: case OP_NOP10:
346 // <expression> if [statements] [else [statements]] endif
350 if (stack.size() < 1)
352 valtype& vch = stacktop(-1);
353 fValue = CastToBool(vch);
354 if (opcode == OP_NOTIF)
358 vfExec.push_back(fValue);
366 vfExec.back() = !vfExec.back();
381 // (false -- false) and return
382 if (stack.size() < 1)
384 bool fValue = CastToBool(stacktop(-1));
404 if (stack.size() < 1)
406 altstack.push_back(stacktop(-1));
411 case OP_FROMALTSTACK:
413 if (altstack.size() < 1)
415 stack.push_back(altstacktop(-1));
423 if (stack.size() < 2)
432 // (x1 x2 -- x1 x2 x1 x2)
433 if (stack.size() < 2)
435 valtype vch1 = stacktop(-2);
436 valtype vch2 = stacktop(-1);
437 stack.push_back(vch1);
438 stack.push_back(vch2);
444 // (x1 x2 x3 -- x1 x2 x3 x1 x2 x3)
445 if (stack.size() < 3)
447 valtype vch1 = stacktop(-3);
448 valtype vch2 = stacktop(-2);
449 valtype vch3 = stacktop(-1);
450 stack.push_back(vch1);
451 stack.push_back(vch2);
452 stack.push_back(vch3);
458 // (x1 x2 x3 x4 -- x1 x2 x3 x4 x1 x2)
459 if (stack.size() < 4)
461 valtype vch1 = stacktop(-4);
462 valtype vch2 = stacktop(-3);
463 stack.push_back(vch1);
464 stack.push_back(vch2);
470 // (x1 x2 x3 x4 x5 x6 -- x3 x4 x5 x6 x1 x2)
471 if (stack.size() < 6)
473 valtype vch1 = stacktop(-6);
474 valtype vch2 = stacktop(-5);
475 stack.erase(stack.end()-6, stack.end()-4);
476 stack.push_back(vch1);
477 stack.push_back(vch2);
483 // (x1 x2 x3 x4 -- x3 x4 x1 x2)
484 if (stack.size() < 4)
486 swap(stacktop(-4), stacktop(-2));
487 swap(stacktop(-3), stacktop(-1));
494 if (stack.size() < 1)
496 valtype vch = stacktop(-1);
498 stack.push_back(vch);
505 CBigNum bn(stack.size());
506 stack.push_back(bn.getvch());
513 if (stack.size() < 1)
522 if (stack.size() < 1)
524 valtype vch = stacktop(-1);
525 stack.push_back(vch);
532 if (stack.size() < 2)
534 stack.erase(stack.end() - 2);
540 // (x1 x2 -- x1 x2 x1)
541 if (stack.size() < 2)
543 valtype vch = stacktop(-2);
544 stack.push_back(vch);
551 // (xn ... x2 x1 x0 n - xn ... x2 x1 x0 xn)
552 // (xn ... x2 x1 x0 n - ... x2 x1 x0 xn)
553 if (stack.size() < 2)
555 int n = CastToBigNum(stacktop(-1)).getint();
557 if (n < 0 || n >= (int)stack.size())
559 valtype vch = stacktop(-n-1);
560 if (opcode == OP_ROLL)
561 stack.erase(stack.end()-n-1);
562 stack.push_back(vch);
568 // (x1 x2 x3 -- x2 x3 x1)
569 // x2 x1 x3 after first swap
570 // x2 x3 x1 after second swap
571 if (stack.size() < 3)
573 swap(stacktop(-3), stacktop(-2));
574 swap(stacktop(-2), stacktop(-1));
581 if (stack.size() < 2)
583 swap(stacktop(-2), stacktop(-1));
589 // (x1 x2 -- x2 x1 x2)
590 if (stack.size() < 2)
592 valtype vch = stacktop(-1);
593 stack.insert(stack.end()-2, vch);
604 if (stack.size() < 2)
606 valtype& vch1 = stacktop(-2);
607 valtype& vch2 = stacktop(-1);
608 vch1.insert(vch1.end(), vch2.begin(), vch2.end());
610 if (stacktop(-1).size() > 520)
617 // (in begin size -- out)
618 if (stack.size() < 3)
620 valtype& vch = stacktop(-3);
621 int nBegin = CastToBigNum(stacktop(-2)).getint();
622 int nEnd = nBegin + CastToBigNum(stacktop(-1)).getint();
623 if (nBegin < 0 || nEnd < nBegin)
625 if (nBegin > (int)vch.size())
627 if (nEnd > (int)vch.size())
629 vch.erase(vch.begin() + nEnd, vch.end());
630 vch.erase(vch.begin(), vch.begin() + nBegin);
640 if (stack.size() < 2)
642 valtype& vch = stacktop(-2);
643 int nSize = CastToBigNum(stacktop(-1)).getint();
646 if (nSize > (int)vch.size())
648 if (opcode == OP_LEFT)
649 vch.erase(vch.begin() + nSize, vch.end());
651 vch.erase(vch.begin(), vch.end() - nSize);
659 if (stack.size() < 1)
661 CBigNum bn(stacktop(-1).size());
662 stack.push_back(bn.getvch());
673 if (stack.size() < 1)
675 valtype& vch = stacktop(-1);
676 for (unsigned int i = 0; i < vch.size(); i++)
682 // WARNING: These disabled opcodes exhibit unexpected behavior
683 // when used on signed integers due to a bug in MakeSameSize()
684 // [see definition of MakeSameSize() above].
691 if (stack.size() < 2)
693 valtype& vch1 = stacktop(-2);
694 valtype& vch2 = stacktop(-1);
695 MakeSameSize(vch1, vch2); // <-- NOT SAFE FOR SIGNED VALUES
696 if (opcode == OP_AND)
698 for (unsigned int i = 0; i < vch1.size(); i++)
701 else if (opcode == OP_OR)
703 for (unsigned int i = 0; i < vch1.size(); i++)
706 else if (opcode == OP_XOR)
708 for (unsigned int i = 0; i < vch1.size(); i++)
717 //case OP_NOTEQUAL: // use OP_NUMNOTEQUAL
720 if (stack.size() < 2)
722 valtype& vch1 = stacktop(-2);
723 valtype& vch2 = stacktop(-1);
724 bool fEqual = (vch1 == vch2);
725 // OP_NOTEQUAL is disabled because it would be too easy to say
726 // something like n != 1 and have some wiseguy pass in 1 with extra
727 // zero bytes after it (numerically, 0x01 == 0x0001 == 0x000001)
728 //if (opcode == OP_NOTEQUAL)
732 stack.push_back(fEqual ? vchTrue : vchFalse);
733 if (opcode == OP_EQUALVERIFY)
757 if (stack.size() < 1)
759 CBigNum bn = CastToBigNum(stacktop(-1));
762 case OP_1ADD: bn += bnOne; break;
763 case OP_1SUB: bn -= bnOne; break;
764 case OP_2MUL: bn <<= 1; break;
765 case OP_2DIV: bn >>= 1; break;
766 case OP_NEGATE: bn = -bn; break;
767 case OP_ABS: if (bn < bnZero) bn = -bn; break;
768 case OP_NOT: bn = (bn == bnZero); break;
769 case OP_0NOTEQUAL: bn = (bn != bnZero); break;
770 default: assert(!"invalid opcode"); break;
773 stack.push_back(bn.getvch());
787 case OP_NUMEQUALVERIFY:
791 case OP_LESSTHANOREQUAL:
792 case OP_GREATERTHANOREQUAL:
797 if (stack.size() < 2)
799 CBigNum bn1 = CastToBigNum(stacktop(-2));
800 CBigNum bn2 = CastToBigNum(stacktop(-1));
813 if (!BN_mul(&bn, &bn1, &bn2, pctx))
818 if (!BN_div(&bn, NULL, &bn1, &bn2, pctx))
823 if (!BN_mod(&bn, &bn1, &bn2, pctx))
828 if (bn2 < bnZero || bn2 > CBigNum(2048))
830 bn = bn1 << bn2.getulong();
834 if (bn2 < bnZero || bn2 > CBigNum(2048))
836 bn = bn1 >> bn2.getulong();
839 case OP_BOOLAND: bn = (bn1 != bnZero && bn2 != bnZero); break;
840 case OP_BOOLOR: bn = (bn1 != bnZero || bn2 != bnZero); break;
841 case OP_NUMEQUAL: bn = (bn1 == bn2); break;
842 case OP_NUMEQUALVERIFY: bn = (bn1 == bn2); break;
843 case OP_NUMNOTEQUAL: bn = (bn1 != bn2); break;
844 case OP_LESSTHAN: bn = (bn1 < bn2); break;
845 case OP_GREATERTHAN: bn = (bn1 > bn2); break;
846 case OP_LESSTHANOREQUAL: bn = (bn1 <= bn2); break;
847 case OP_GREATERTHANOREQUAL: bn = (bn1 >= bn2); break;
848 case OP_MIN: bn = (bn1 < bn2 ? bn1 : bn2); break;
849 case OP_MAX: bn = (bn1 > bn2 ? bn1 : bn2); break;
850 default: assert(!"invalid opcode"); break;
854 stack.push_back(bn.getvch());
856 if (opcode == OP_NUMEQUALVERIFY)
858 if (CastToBool(stacktop(-1)))
868 // (x min max -- out)
869 if (stack.size() < 3)
871 CBigNum bn1 = CastToBigNum(stacktop(-3));
872 CBigNum bn2 = CastToBigNum(stacktop(-2));
873 CBigNum bn3 = CastToBigNum(stacktop(-1));
874 bool fValue = (bn2 <= bn1 && bn1 < bn3);
878 stack.push_back(fValue ? vchTrue : vchFalse);
893 if (stack.size() < 1)
895 valtype& vch = stacktop(-1);
896 valtype vchHash((opcode == OP_RIPEMD160 || opcode == OP_SHA1 || opcode == OP_HASH160) ? 20 : 32);
897 if (opcode == OP_RIPEMD160)
898 RIPEMD160(&vch[0], vch.size(), &vchHash[0]);
899 else if (opcode == OP_SHA1)
900 SHA1(&vch[0], vch.size(), &vchHash[0]);
901 else if (opcode == OP_SHA256)
902 SHA256(&vch[0], vch.size(), &vchHash[0]);
903 else if (opcode == OP_HASH160)
905 uint160 hash160 = Hash160(vch);
906 memcpy(&vchHash[0], &hash160, sizeof(hash160));
908 else if (opcode == OP_HASH256)
910 uint256 hash = Hash(vch.begin(), vch.end());
911 memcpy(&vchHash[0], &hash, sizeof(hash));
914 stack.push_back(vchHash);
918 case OP_CODESEPARATOR:
920 // Hash starts after the code separator
926 case OP_CHECKSIGVERIFY:
928 // (sig pubkey -- bool)
929 if (stack.size() < 2)
932 valtype& vchSig = stacktop(-2);
933 valtype& vchPubKey = stacktop(-1);
936 //PrintHex(vchSig.begin(), vchSig.end(), "sig: %s\n");
937 //PrintHex(vchPubKey.begin(), vchPubKey.end(), "pubkey: %s\n");
939 // Subset of script starting at the most recent codeseparator
940 CScript scriptCode(pbegincodehash, pend);
942 // Drop the signature, since there's no way for a signature to sign itself
943 scriptCode.FindAndDelete(CScript(vchSig));
945 bool fSuccess = CheckSig(vchSig, vchPubKey, scriptCode, txTo, nIn, nHashType);
949 stack.push_back(fSuccess ? vchTrue : vchFalse);
950 if (opcode == OP_CHECKSIGVERIFY)
960 case OP_CHECKMULTISIG:
961 case OP_CHECKMULTISIGVERIFY:
963 // ([sig ...] num_of_signatures [pubkey ...] num_of_pubkeys -- bool)
966 if ((int)stack.size() < i)
969 int nKeysCount = CastToBigNum(stacktop(-i)).getint();
970 if (nKeysCount < 0 || nKeysCount > 20)
972 nOpCount += nKeysCount;
977 if ((int)stack.size() < i)
980 int nSigsCount = CastToBigNum(stacktop(-i)).getint();
981 if (nSigsCount < 0 || nSigsCount > nKeysCount)
985 if ((int)stack.size() < i)
988 // Subset of script starting at the most recent codeseparator
989 CScript scriptCode(pbegincodehash, pend);
991 // Drop the signatures, since there's no way for a signature to sign itself
992 for (int k = 0; k < nSigsCount; k++)
994 valtype& vchSig = stacktop(-isig-k);
995 scriptCode.FindAndDelete(CScript(vchSig));
998 bool fSuccess = true;
999 while (fSuccess && nSigsCount > 0)
1001 valtype& vchSig = stacktop(-isig);
1002 valtype& vchPubKey = stacktop(-ikey);
1005 if (CheckSig(vchSig, vchPubKey, scriptCode, txTo, nIn, nHashType))
1013 // If there are more signatures left than keys left,
1014 // then too many signatures have failed
1015 if (nSigsCount > nKeysCount)
1021 stack.push_back(fSuccess ? vchTrue : vchFalse);
1023 if (opcode == OP_CHECKMULTISIGVERIFY)
1038 if (stack.size() + altstack.size() > 1000)
1048 if (!vfExec.empty())
1062 uint256 SignatureHash(CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType)
1064 if (nIn >= txTo.vin.size())
1066 printf("ERROR: SignatureHash() : nIn=%d out of range\n", nIn);
1069 CTransaction txTmp(txTo);
1071 // In case concatenating two scripts ends up with two codeseparators,
1072 // or an extra one at the end, this prevents all those possible incompatibilities.
1073 scriptCode.FindAndDelete(CScript(OP_CODESEPARATOR));
1075 // Blank out other inputs' signatures
1076 for (unsigned int i = 0; i < txTmp.vin.size(); i++)
1077 txTmp.vin[i].scriptSig = CScript();
1078 txTmp.vin[nIn].scriptSig = scriptCode;
1080 // Blank out some of the outputs
1081 if ((nHashType & 0x1f) == SIGHASH_NONE)
1086 // Let the others update at will
1087 for (unsigned int i = 0; i < txTmp.vin.size(); i++)
1089 txTmp.vin[i].nSequence = 0;
1091 else if ((nHashType & 0x1f) == SIGHASH_SINGLE)
1093 // Only lock-in the txout payee at same index as txin
1094 unsigned int nOut = nIn;
1095 if (nOut >= txTmp.vout.size())
1097 printf("ERROR: SignatureHash() : nOut=%d out of range\n", nOut);
1100 txTmp.vout.resize(nOut+1);
1101 for (unsigned int i = 0; i < nOut; i++)
1102 txTmp.vout[i].SetNull();
1104 // Let the others update at will
1105 for (unsigned int i = 0; i < txTmp.vin.size(); i++)
1107 txTmp.vin[i].nSequence = 0;
1110 // Blank out other inputs completely, not recommended for open transactions
1111 if (nHashType & SIGHASH_ANYONECANPAY)
1113 txTmp.vin[0] = txTmp.vin[nIn];
1114 txTmp.vin.resize(1);
1117 // Serialize and hash
1118 CDataStream ss(SER_GETHASH, 0);
1120 ss << txTmp << nHashType;
1121 return Hash(ss.begin(), ss.end());
1125 // Valid signature cache, to avoid doing expensive ECDSA signature checking
1126 // twice for every transaction (once when accepted into memory pool, and
1127 // again when accepted into the block chain)
1129 class CSignatureCache
1132 // sigdata_type is (signature hash, signature, public key):
1133 typedef boost::tuple<uint256, std::vector<unsigned char>, std::vector<unsigned char> > sigdata_type;
1134 std::set< sigdata_type> setValid;
1135 CCriticalSection cs_sigcache;
1139 Get(uint256 hash, const std::vector<unsigned char>& vchSig, const std::vector<unsigned char>& pubKey)
1143 sigdata_type k(hash, vchSig, pubKey);
1144 std::set<sigdata_type>::iterator mi = setValid.find(k);
1145 if (mi != setValid.end())
1150 void Set(uint256 hash, const std::vector<unsigned char>& vchSig, const std::vector<unsigned char>& pubKey)
1152 // DoS prevention: limit cache size to less than 10MB
1153 // (~200 bytes per cache entry times 50,000 entries)
1154 // Since there are a maximum of 20,000 signature operations per block
1155 // 50,000 is a reasonable default.
1156 int64 nMaxCacheSize = GetArg("-maxsigcachesize", 50000);
1157 if (nMaxCacheSize <= 0) return;
1161 while (static_cast<int64>(setValid.size()) > nMaxCacheSize)
1163 // Evict a random entry. Random because that helps
1164 // foil would-be DoS attackers who might try to pre-generate
1165 // and re-use a set of valid signatures just-slightly-greater
1166 // than our cache size.
1167 uint256 randomHash = GetRandHash();
1168 std::vector<unsigned char> unused;
1169 std::set<sigdata_type>::iterator it =
1170 setValid.lower_bound(sigdata_type(randomHash, unused, unused));
1171 if (it == setValid.end())
1172 it = setValid.begin();
1173 setValid.erase(*it);
1176 sigdata_type k(hash, vchSig, pubKey);
1181 bool CheckSig(vector<unsigned char> vchSig, vector<unsigned char> vchPubKey, CScript scriptCode,
1182 const CTransaction& txTo, unsigned int nIn, int nHashType)
1184 static CSignatureCache signatureCache;
1186 // Hash type is one byte tacked on to the end of the signature
1190 nHashType = vchSig.back();
1191 else if (nHashType != vchSig.back())
1195 uint256 sighash = SignatureHash(scriptCode, txTo, nIn, nHashType);
1197 if (signatureCache.Get(sighash, vchSig, vchPubKey))
1201 if (!key.SetPubKey(vchPubKey))
1204 if (!key.Verify(sighash, vchSig))
1207 signatureCache.Set(sighash, vchSig, vchPubKey);
1220 // Return public keys or hashes from scriptPubKey, for 'standard' transaction types.
1222 bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, vector<vector<unsigned char> >& vSolutionsRet)
1225 static map<txnouttype, CScript> mTemplates;
1226 if (mTemplates.empty())
1228 // Standard tx, sender provides pubkey, receiver adds signature
1229 mTemplates.insert(make_pair(TX_PUBKEY, CScript() << OP_PUBKEY << OP_CHECKSIG));
1231 // Bitcoin address tx, sender provides hash of pubkey, receiver provides signature and pubkey
1232 mTemplates.insert(make_pair(TX_PUBKEYHASH, CScript() << OP_DUP << OP_HASH160 << OP_PUBKEYHASH << OP_EQUALVERIFY << OP_CHECKSIG));
1234 // Sender provides N pubkeys, receivers provides M signatures
1235 mTemplates.insert(make_pair(TX_MULTISIG, CScript() << OP_SMALLINTEGER << OP_PUBKEYS << OP_SMALLINTEGER << OP_CHECKMULTISIG));
1237 // Empty, provably prunable, data-carrying output
1238 mTemplates.insert(make_pair(TX_NULL_DATA, CScript() << OP_RETURN << OP_SMALLDATA));
1241 // Shortcut for pay-to-script-hash, which are more constrained than the other types:
1242 // it is always OP_HASH160 20 [20 byte hash] OP_EQUAL
1243 if (scriptPubKey.IsPayToScriptHash())
1245 typeRet = TX_SCRIPTHASH;
1246 vector<unsigned char> hashBytes(scriptPubKey.begin()+2, scriptPubKey.begin()+22);
1247 vSolutionsRet.push_back(hashBytes);
1252 const CScript& script1 = scriptPubKey;
1253 BOOST_FOREACH(const PAIRTYPE(txnouttype, CScript)& tplate, mTemplates)
1255 const CScript& script2 = tplate.second;
1256 vSolutionsRet.clear();
1258 opcodetype opcode1, opcode2;
1259 vector<unsigned char> vch1, vch2;
1262 CScript::const_iterator pc1 = script1.begin();
1263 CScript::const_iterator pc2 = script2.begin();
1266 if (pc1 == script1.end() && pc2 == script2.end())
1269 typeRet = tplate.first;
1270 if (typeRet == TX_MULTISIG)
1272 // Additional checks for TX_MULTISIG:
1273 unsigned char m = vSolutionsRet.front()[0];
1274 unsigned char n = vSolutionsRet.back()[0];
1275 if (m < 1 || n < 1 || m > n || vSolutionsRet.size()-2 != n)
1280 if (!script1.GetOp(pc1, opcode1, vch1))
1282 if (!script2.GetOp(pc2, opcode2, vch2))
1285 // Template matching opcodes:
1286 if (opcode2 == OP_PUBKEYS)
1288 while (vch1.size() >= 33 && vch1.size() <= 120)
1290 vSolutionsRet.push_back(vch1);
1291 if (!script1.GetOp(pc1, opcode1, vch1))
1294 if (!script2.GetOp(pc2, opcode2, vch2))
1296 // Normal situation is to fall through
1297 // to other if/else statements
1300 if (opcode2 == OP_PUBKEY)
1302 if (vch1.size() < 33 || vch1.size() > 120)
1304 vSolutionsRet.push_back(vch1);
1306 else if (opcode2 == OP_PUBKEYHASH)
1308 if (vch1.size() != sizeof(uint160))
1310 vSolutionsRet.push_back(vch1);
1312 else if (opcode2 == OP_SMALLINTEGER)
1313 { // Single-byte small integer pushed onto vSolutions
1314 if (opcode1 == OP_0 ||
1315 (opcode1 >= OP_1 && opcode1 <= OP_16))
1317 char n = (char)CScript::DecodeOP_N(opcode1);
1318 vSolutionsRet.push_back(valtype(1, n));
1323 else if (opcode2 == OP_SMALLDATA)
1325 // small pushdata, <= 80 bytes
1326 if (vch1.size() > 80)
1329 else if (opcode1 != opcode2 || vch1 != vch2)
1331 // Others must match exactly
1337 vSolutionsRet.clear();
1338 typeRet = TX_NONSTANDARD;
1343 bool Sign1(const CKeyID& address, const CKeyStore& keystore, uint256 hash, int nHashType, CScript& scriptSigRet)
1346 if (!keystore.GetKey(address, key))
1349 vector<unsigned char> vchSig;
1350 if (!key.Sign(hash, vchSig))
1352 vchSig.push_back((unsigned char)nHashType);
1353 scriptSigRet << vchSig;
1358 bool SignN(const vector<valtype>& multisigdata, const CKeyStore& keystore, uint256 hash, int nHashType, CScript& scriptSigRet)
1361 int nRequired = multisigdata.front()[0];
1362 for (unsigned int i = 1; i < multisigdata.size()-1 && nSigned < nRequired; i++)
1364 const valtype& pubkey = multisigdata[i];
1365 CKeyID keyID = CPubKey(pubkey).GetID();
1366 if (Sign1(keyID, keystore, hash, nHashType, scriptSigRet))
1369 return nSigned==nRequired;
1373 // Sign scriptPubKey with private keys stored in keystore, given transaction hash and hash type.
1374 // Signatures are returned in scriptSigRet (or returns false if scriptPubKey can't be signed),
1375 // unless whichTypeRet is TX_SCRIPTHASH, in which case scriptSigRet is the redemption script.
1376 // Returns false if scriptPubKey could not be completely satisfied.
1378 bool Solver(const CKeyStore& keystore, const CScript& scriptPubKey, uint256 hash, int nHashType,
1379 CScript& scriptSigRet, txnouttype& whichTypeRet)
1381 scriptSigRet.clear();
1383 vector<valtype> vSolutions;
1384 if (!Solver(scriptPubKey, whichTypeRet, vSolutions))
1388 switch (whichTypeRet)
1390 case TX_NONSTANDARD:
1394 keyID = CPubKey(vSolutions[0]).GetID();
1395 return Sign1(keyID, keystore, hash, nHashType, scriptSigRet);
1397 keyID = CKeyID(uint160(vSolutions[0]));
1398 if (!Sign1(keyID, keystore, hash, nHashType, scriptSigRet))
1403 keystore.GetPubKey(keyID, vch);
1404 scriptSigRet << vch;
1408 return keystore.GetCScript(uint160(vSolutions[0]), scriptSigRet);
1411 scriptSigRet << OP_0; // workaround CHECKMULTISIG bug
1412 return (SignN(vSolutions, keystore, hash, nHashType, scriptSigRet));
1417 int ScriptSigArgsExpected(txnouttype t, const std::vector<std::vector<unsigned char> >& vSolutions)
1421 case TX_NONSTANDARD:
1430 if (vSolutions.size() < 1 || vSolutions[0].size() < 1)
1432 return vSolutions[0][0] + 1;
1434 return 1; // doesn't include args needed by the script
1439 bool IsStandard(const CScript& scriptPubKey, txnouttype& whichType)
1441 vector<valtype> vSolutions;
1442 if (!Solver(scriptPubKey, whichType, vSolutions))
1445 if (whichType == TX_MULTISIG)
1447 unsigned char m = vSolutions.front()[0];
1448 unsigned char n = vSolutions.back()[0];
1449 // Support up to x-of-3 multisig txns as standard
1456 return whichType != TX_NONSTANDARD;
1460 unsigned int HaveKeys(const vector<valtype>& pubkeys, const CKeyStore& keystore)
1462 unsigned int nResult = 0;
1463 BOOST_FOREACH(const valtype& pubkey, pubkeys)
1465 CKeyID keyID = CPubKey(pubkey).GetID();
1466 if (keystore.HaveKey(keyID))
1473 class CKeyStoreIsMineVisitor : public boost::static_visitor<bool>
1476 const CKeyStore *keystore;
1478 CKeyStoreIsMineVisitor(const CKeyStore *keystoreIn) : keystore(keystoreIn) { }
1479 bool operator()(const CNoDestination &dest) const { return false; }
1480 bool operator()(const CKeyID &keyID) const { return keystore->HaveKey(keyID); }
1481 bool operator()(const CScriptID &scriptID) const { return keystore->HaveCScript(scriptID); }
1484 bool IsMine(const CKeyStore &keystore, const CTxDestination &dest)
1486 return boost::apply_visitor(CKeyStoreIsMineVisitor(&keystore), dest);
1489 bool IsMine(const CKeyStore &keystore, const CScript& scriptPubKey)
1491 vector<valtype> vSolutions;
1492 txnouttype whichType;
1493 if (!Solver(scriptPubKey, whichType, vSolutions))
1499 case TX_NONSTANDARD:
1503 keyID = CPubKey(vSolutions[0]).GetID();
1504 return keystore.HaveKey(keyID);
1506 keyID = CKeyID(uint160(vSolutions[0]));
1507 return keystore.HaveKey(keyID);
1511 if (!keystore.GetCScript(CScriptID(uint160(vSolutions[0])), subscript))
1513 return IsMine(keystore, subscript);
1517 // Only consider transactions "mine" if we own ALL the
1518 // keys involved. multi-signature transactions that are
1519 // partially owned (somebody else has a key that can spend
1520 // them) enable spend-out-from-under-you attacks, especially
1521 // in shared-wallet situations.
1522 vector<valtype> keys(vSolutions.begin()+1, vSolutions.begin()+vSolutions.size()-1);
1523 return HaveKeys(keys, keystore) == keys.size();
1529 bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet)
1531 vector<valtype> vSolutions;
1532 txnouttype whichType;
1533 if (!Solver(scriptPubKey, whichType, vSolutions))
1536 if (whichType == TX_PUBKEY)
1538 addressRet = CPubKey(vSolutions[0]).GetID();
1541 else if (whichType == TX_PUBKEYHASH)
1543 addressRet = CKeyID(uint160(vSolutions[0]));
1546 else if (whichType == TX_SCRIPTHASH)
1548 addressRet = CScriptID(uint160(vSolutions[0]));
1551 // Multisig txns have more than one address...
1555 class CAffectedKeysVisitor : public boost::static_visitor<void> {
1557 const CKeyStore &keystore;
1558 std::vector<CKeyID> &vKeys;
1561 CAffectedKeysVisitor(const CKeyStore &keystoreIn, std::vector<CKeyID> &vKeysIn) : keystore(keystoreIn), vKeys(vKeysIn) {}
1563 void Process(const CScript &script) {
1565 std::vector<CTxDestination> vDest;
1567 if (ExtractDestinations(script, type, vDest, nRequired)) {
1568 BOOST_FOREACH(const CTxDestination &dest, vDest)
1569 boost::apply_visitor(*this, dest);
1573 void operator()(const CKeyID &keyId) {
1574 if (keystore.HaveKey(keyId))
1575 vKeys.push_back(keyId);
1578 void operator()(const CScriptID &scriptId) {
1580 if (keystore.GetCScript(scriptId, script))
1584 void operator()(const CNoDestination &none) {}
1588 void ExtractAffectedKeys(const CKeyStore &keystore, const CScript& scriptPubKey, std::vector<CKeyID> &vKeys) {
1589 CAffectedKeysVisitor(keystore, vKeys).Process(scriptPubKey);
1592 bool ExtractDestinations(const CScript& scriptPubKey, txnouttype& typeRet, vector<CTxDestination>& addressRet, int& nRequiredRet)
1595 typeRet = TX_NONSTANDARD;
1596 vector<valtype> vSolutions;
1597 if (!Solver(scriptPubKey, typeRet, vSolutions))
1599 if (typeRet == TX_NULL_DATA)
1602 if (typeRet == TX_MULTISIG)
1604 nRequiredRet = vSolutions.front()[0];
1605 for (unsigned int i = 1; i < vSolutions.size()-1; i++)
1607 CTxDestination address = CPubKey(vSolutions[i]).GetID();
1608 addressRet.push_back(address);
1614 CTxDestination address;
1615 if (!ExtractDestination(scriptPubKey, address))
1617 addressRet.push_back(address);
1623 bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn,
1624 bool fValidatePayToScriptHash, int nHashType)
1626 vector<vector<unsigned char> > stack, stackCopy;
1627 if (!EvalScript(stack, scriptSig, txTo, nIn, nHashType))
1629 if (fValidatePayToScriptHash)
1631 if (!EvalScript(stack, scriptPubKey, txTo, nIn, nHashType))
1636 if (CastToBool(stack.back()) == false)
1639 // Additional validation for spend-to-script-hash transactions:
1640 if (fValidatePayToScriptHash && scriptPubKey.IsPayToScriptHash())
1642 if (!scriptSig.IsPushOnly()) // scriptSig must be literals-only
1643 return false; // or validation fails
1645 const valtype& pubKeySerialized = stackCopy.back();
1646 CScript pubKey2(pubKeySerialized.begin(), pubKeySerialized.end());
1647 popstack(stackCopy);
1649 if (!EvalScript(stackCopy, pubKey2, txTo, nIn, nHashType))
1651 if (stackCopy.empty())
1653 return CastToBool(stackCopy.back());
1660 bool SignSignature(const CKeyStore &keystore, const CScript& fromPubKey, CTransaction& txTo, unsigned int nIn, int nHashType)
1662 assert(nIn < txTo.vin.size());
1663 CTxIn& txin = txTo.vin[nIn];
1665 // Leave out the signature from the hash, since a signature can't sign itself.
1666 // The checksig op will also drop the signatures from its hash.
1667 uint256 hash = SignatureHash(fromPubKey, txTo, nIn, nHashType);
1669 txnouttype whichType;
1670 if (!Solver(keystore, fromPubKey, hash, nHashType, txin.scriptSig, whichType))
1673 if (whichType == TX_SCRIPTHASH)
1675 // Solver returns the subscript that need to be evaluated;
1676 // the final scriptSig is the signatures from that
1677 // and then the serialized subscript:
1678 CScript subscript = txin.scriptSig;
1680 // Recompute txn hash using subscript in place of scriptPubKey:
1681 uint256 hash2 = SignatureHash(subscript, txTo, nIn, nHashType);
1685 Solver(keystore, subscript, hash2, nHashType, txin.scriptSig, subType) && subType != TX_SCRIPTHASH;
1686 // Append serialized subscript whether or not it is completely signed:
1687 txin.scriptSig << static_cast<valtype>(subscript);
1688 if (!fSolved) return false;
1692 return VerifyScript(txin.scriptSig, fromPubKey, txTo, nIn, true, 0);
1695 bool SignSignature(const CKeyStore &keystore, const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType)
1697 assert(nIn < txTo.vin.size());
1698 CTxIn& txin = txTo.vin[nIn];
1699 assert(txin.prevout.n < txFrom.vout.size());
1700 assert(txin.prevout.hash == txFrom.GetHash());
1701 const CTxOut& txout = txFrom.vout[txin.prevout.n];
1703 return SignSignature(keystore, txout.scriptPubKey, txTo, nIn, nHashType);
1706 bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, bool fValidatePayToScriptHash, int nHashType)
1708 assert(nIn < txTo.vin.size());
1709 const CTxIn& txin = txTo.vin[nIn];
1710 if (txin.prevout.n >= txFrom.vout.size())
1712 const CTxOut& txout = txFrom.vout[txin.prevout.n];
1714 if (txin.prevout.hash != txFrom.GetHash())
1717 return VerifyScript(txin.scriptSig, txout.scriptPubKey, txTo, nIn, fValidatePayToScriptHash, nHashType);
1720 static CScript PushAll(const vector<valtype>& values)
1723 BOOST_FOREACH(const valtype& v, values)
1728 static CScript CombineMultisig(CScript scriptPubKey, const CTransaction& txTo, unsigned int nIn,
1729 const vector<valtype>& vSolutions,
1730 vector<valtype>& sigs1, vector<valtype>& sigs2)
1732 // Combine all the signatures we've got:
1733 set<valtype> allsigs;
1734 BOOST_FOREACH(const valtype& v, sigs1)
1739 BOOST_FOREACH(const valtype& v, sigs2)
1745 // Build a map of pubkey -> signature by matching sigs to pubkeys:
1746 assert(vSolutions.size() > 1);
1747 unsigned int nSigsRequired = vSolutions.front()[0];
1748 unsigned int nPubKeys = vSolutions.size()-2;
1749 map<valtype, valtype> sigs;
1750 BOOST_FOREACH(const valtype& sig, allsigs)
1752 for (unsigned int i = 0; i < nPubKeys; i++)
1754 const valtype& pubkey = vSolutions[i+1];
1755 if (sigs.count(pubkey))
1756 continue; // Already got a sig for this pubkey
1758 if (CheckSig(sig, pubkey, scriptPubKey, txTo, nIn, 0))
1765 // Now build a merged CScript:
1766 unsigned int nSigsHave = 0;
1767 CScript result; result << OP_0; // pop-one-too-many workaround
1768 for (unsigned int i = 0; i < nPubKeys && nSigsHave < nSigsRequired; i++)
1770 if (sigs.count(vSolutions[i+1]))
1772 result << sigs[vSolutions[i+1]];
1776 // Fill any missing with OP_0:
1777 for (unsigned int i = nSigsHave; i < nSigsRequired; i++)
1783 static CScript CombineSignatures(CScript scriptPubKey, const CTransaction& txTo, unsigned int nIn,
1784 const txnouttype txType, const vector<valtype>& vSolutions,
1785 vector<valtype>& sigs1, vector<valtype>& sigs2)
1789 case TX_NONSTANDARD:
1791 // Don't know anything about this, assume bigger one is correct:
1792 if (sigs1.size() >= sigs2.size())
1793 return PushAll(sigs1);
1794 return PushAll(sigs2);
1797 // Signatures are bigger than placeholders or empty scripts:
1798 if (sigs1.empty() || sigs1[0].empty())
1799 return PushAll(sigs2);
1800 return PushAll(sigs1);
1802 if (sigs1.empty() || sigs1.back().empty())
1803 return PushAll(sigs2);
1804 else if (sigs2.empty() || sigs2.back().empty())
1805 return PushAll(sigs1);
1808 // Recur to combine:
1809 valtype spk = sigs1.back();
1810 CScript pubKey2(spk.begin(), spk.end());
1813 vector<vector<unsigned char> > vSolutions2;
1814 Solver(pubKey2, txType2, vSolutions2);
1817 CScript result = CombineSignatures(pubKey2, txTo, nIn, txType2, vSolutions2, sigs1, sigs2);
1822 return CombineMultisig(scriptPubKey, txTo, nIn, vSolutions, sigs1, sigs2);
1828 CScript CombineSignatures(CScript scriptPubKey, const CTransaction& txTo, unsigned int nIn,
1829 const CScript& scriptSig1, const CScript& scriptSig2)
1832 vector<vector<unsigned char> > vSolutions;
1833 Solver(scriptPubKey, txType, vSolutions);
1835 vector<valtype> stack1;
1836 EvalScript(stack1, scriptSig1, CTransaction(), 0, 0);
1837 vector<valtype> stack2;
1838 EvalScript(stack2, scriptSig2, CTransaction(), 0, 0);
1840 return CombineSignatures(scriptPubKey, txTo, nIn, txType, vSolutions, stack1, stack2);
1843 unsigned int CScript::GetSigOpCount(bool fAccurate) const
1846 const_iterator pc = begin();
1847 opcodetype lastOpcode = OP_INVALIDOPCODE;
1851 if (!GetOp(pc, opcode))
1853 if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
1855 else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
1857 if (fAccurate && lastOpcode >= OP_1 && lastOpcode <= OP_16)
1858 n += DecodeOP_N(lastOpcode);
1862 lastOpcode = opcode;
1867 unsigned int CScript::GetSigOpCount(const CScript& scriptSig) const
1869 if (!IsPayToScriptHash())
1870 return GetSigOpCount(true);
1872 // This is a pay-to-script-hash scriptPubKey;
1873 // get the last item that the scriptSig
1874 // pushes onto the stack:
1875 const_iterator pc = scriptSig.begin();
1876 vector<unsigned char> data;
1877 while (pc < scriptSig.end())
1880 if (!scriptSig.GetOp(pc, opcode, data))
1886 /// ... and return its opcount:
1887 CScript subscript(data.begin(), data.end());
1888 return subscript.GetSigOpCount(true);
1891 bool CScript::IsPayToScriptHash() const
1893 // Extra-fast test for pay-to-script-hash CScripts:
1894 return (this->size() == 23 &&
1895 this->at(0) == OP_HASH160 &&
1896 this->at(1) == 0x14 &&
1897 this->at(22) == OP_EQUAL);
1900 bool CScript::HasCanonicalPushes() const
1902 const_iterator pc = begin();
1906 std::vector<unsigned char> data;
1907 if (!GetOp(pc, opcode, data))
1911 if (opcode < OP_PUSHDATA1 && opcode > OP_0 && (data.size() == 1 && data[0] <= 16))
1912 // Could have used an OP_n code, rather than a 1-byte push.
1914 if (opcode == OP_PUSHDATA1 && data.size() < OP_PUSHDATA1)
1915 // Could have used a normal n-byte push, rather than OP_PUSHDATA1.
1917 if (opcode == OP_PUSHDATA2 && data.size() <= 0xFF)
1918 // Could have used an OP_PUSHDATA1.
1920 if (opcode == OP_PUSHDATA4 && data.size() <= 0xFFFF)
1921 // Could have used an OP_PUSHDATA2.
1927 class CScriptVisitor : public boost::static_visitor<bool>
1932 CScriptVisitor(CScript *scriptin) { script = scriptin; }
1934 bool operator()(const CNoDestination &dest) const {
1939 bool operator()(const CKeyID &keyID) const {
1941 *script << OP_DUP << OP_HASH160 << keyID << OP_EQUALVERIFY << OP_CHECKSIG;
1945 bool operator()(const CScriptID &scriptID) const {
1947 *script << OP_HASH160 << scriptID << OP_EQUAL;
1952 void CScript::SetDestination(const CTxDestination& dest)
1954 boost::apply_visitor(CScriptVisitor(this), dest);
1957 void CScript::SetMultisig(int nRequired, const std::vector<CKey>& keys)
1961 *this << EncodeOP_N(nRequired);
1962 BOOST_FOREACH(const CKey& key, keys)
1963 *this << key.GetPubKey();
1964 *this << EncodeOP_N(keys.size()) << OP_CHECKMULTISIG;