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";
110 const char* GetOpName(opcodetype opcode)
115 case OP_0 : return "0";
116 case OP_PUSHDATA1 : return "OP_PUSHDATA1";
117 case OP_PUSHDATA2 : return "OP_PUSHDATA2";
118 case OP_PUSHDATA4 : return "OP_PUSHDATA4";
119 case OP_1NEGATE : return "-1";
120 case OP_RESERVED : return "OP_RESERVED";
121 case OP_1 : return "1";
122 case OP_2 : return "2";
123 case OP_3 : return "3";
124 case OP_4 : return "4";
125 case OP_5 : return "5";
126 case OP_6 : return "6";
127 case OP_7 : return "7";
128 case OP_8 : return "8";
129 case OP_9 : return "9";
130 case OP_10 : return "10";
131 case OP_11 : return "11";
132 case OP_12 : return "12";
133 case OP_13 : return "13";
134 case OP_14 : return "14";
135 case OP_15 : return "15";
136 case OP_16 : return "16";
139 case OP_NOP : return "OP_NOP";
140 case OP_VER : return "OP_VER";
141 case OP_IF : return "OP_IF";
142 case OP_NOTIF : return "OP_NOTIF";
143 case OP_VERIF : return "OP_VERIF";
144 case OP_VERNOTIF : return "OP_VERNOTIF";
145 case OP_ELSE : return "OP_ELSE";
146 case OP_ENDIF : return "OP_ENDIF";
147 case OP_VERIFY : return "OP_VERIFY";
148 case OP_RETURN : return "OP_RETURN";
151 case OP_TOALTSTACK : return "OP_TOALTSTACK";
152 case OP_FROMALTSTACK : return "OP_FROMALTSTACK";
153 case OP_2DROP : return "OP_2DROP";
154 case OP_2DUP : return "OP_2DUP";
155 case OP_3DUP : return "OP_3DUP";
156 case OP_2OVER : return "OP_2OVER";
157 case OP_2ROT : return "OP_2ROT";
158 case OP_2SWAP : return "OP_2SWAP";
159 case OP_IFDUP : return "OP_IFDUP";
160 case OP_DEPTH : return "OP_DEPTH";
161 case OP_DROP : return "OP_DROP";
162 case OP_DUP : return "OP_DUP";
163 case OP_NIP : return "OP_NIP";
164 case OP_OVER : return "OP_OVER";
165 case OP_PICK : return "OP_PICK";
166 case OP_ROLL : return "OP_ROLL";
167 case OP_ROT : return "OP_ROT";
168 case OP_SWAP : return "OP_SWAP";
169 case OP_TUCK : return "OP_TUCK";
172 case OP_CAT : return "OP_CAT";
173 case OP_SUBSTR : return "OP_SUBSTR";
174 case OP_LEFT : return "OP_LEFT";
175 case OP_RIGHT : return "OP_RIGHT";
176 case OP_SIZE : return "OP_SIZE";
179 case OP_INVERT : return "OP_INVERT";
180 case OP_AND : return "OP_AND";
181 case OP_OR : return "OP_OR";
182 case OP_XOR : return "OP_XOR";
183 case OP_EQUAL : return "OP_EQUAL";
184 case OP_EQUALVERIFY : return "OP_EQUALVERIFY";
185 case OP_RESERVED1 : return "OP_RESERVED1";
186 case OP_RESERVED2 : return "OP_RESERVED2";
189 case OP_1ADD : return "OP_1ADD";
190 case OP_1SUB : return "OP_1SUB";
191 case OP_2MUL : return "OP_2MUL";
192 case OP_2DIV : return "OP_2DIV";
193 case OP_NEGATE : return "OP_NEGATE";
194 case OP_ABS : return "OP_ABS";
195 case OP_NOT : return "OP_NOT";
196 case OP_0NOTEQUAL : return "OP_0NOTEQUAL";
197 case OP_ADD : return "OP_ADD";
198 case OP_SUB : return "OP_SUB";
199 case OP_MUL : return "OP_MUL";
200 case OP_DIV : return "OP_DIV";
201 case OP_MOD : return "OP_MOD";
202 case OP_LSHIFT : return "OP_LSHIFT";
203 case OP_RSHIFT : return "OP_RSHIFT";
204 case OP_BOOLAND : return "OP_BOOLAND";
205 case OP_BOOLOR : return "OP_BOOLOR";
206 case OP_NUMEQUAL : return "OP_NUMEQUAL";
207 case OP_NUMEQUALVERIFY : return "OP_NUMEQUALVERIFY";
208 case OP_NUMNOTEQUAL : return "OP_NUMNOTEQUAL";
209 case OP_LESSTHAN : return "OP_LESSTHAN";
210 case OP_GREATERTHAN : return "OP_GREATERTHAN";
211 case OP_LESSTHANOREQUAL : return "OP_LESSTHANOREQUAL";
212 case OP_GREATERTHANOREQUAL : return "OP_GREATERTHANOREQUAL";
213 case OP_MIN : return "OP_MIN";
214 case OP_MAX : return "OP_MAX";
215 case OP_WITHIN : return "OP_WITHIN";
218 case OP_RIPEMD160 : return "OP_RIPEMD160";
219 case OP_SHA1 : return "OP_SHA1";
220 case OP_SHA256 : return "OP_SHA256";
221 case OP_HASH160 : return "OP_HASH160";
222 case OP_HASH256 : return "OP_HASH256";
223 case OP_CODESEPARATOR : return "OP_CODESEPARATOR";
224 case OP_CHECKSIG : return "OP_CHECKSIG";
225 case OP_CHECKSIGVERIFY : return "OP_CHECKSIGVERIFY";
226 case OP_CHECKMULTISIG : return "OP_CHECKMULTISIG";
227 case OP_CHECKMULTISIGVERIFY : return "OP_CHECKMULTISIGVERIFY";
230 case OP_NOP1 : return "OP_NOP1";
231 case OP_NOP2 : return "OP_NOP2";
232 case OP_NOP3 : return "OP_NOP3";
233 case OP_NOP4 : return "OP_NOP4";
234 case OP_NOP5 : return "OP_NOP5";
235 case OP_NOP6 : return "OP_NOP6";
236 case OP_NOP7 : return "OP_NOP7";
237 case OP_NOP8 : return "OP_NOP8";
238 case OP_NOP9 : return "OP_NOP9";
239 case OP_NOP10 : return "OP_NOP10";
243 // template matching params
244 case OP_PUBKEYHASH : return "OP_PUBKEYHASH";
245 case OP_PUBKEY : return "OP_PUBKEY";
247 case OP_INVALIDOPCODE : return "OP_INVALIDOPCODE";
253 bool EvalScript(vector<vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, int nHashType)
256 CScript::const_iterator pc = script.begin();
257 CScript::const_iterator pend = script.end();
258 CScript::const_iterator pbegincodehash = script.begin();
260 valtype vchPushValue;
262 vector<valtype> altstack;
263 if (script.size() > 10000)
272 bool fExec = !count(vfExec.begin(), vfExec.end(), false);
277 if (!script.GetOp(pc, opcode, vchPushValue))
279 if (vchPushValue.size() > 520)
281 if (opcode > OP_16 && ++nOpCount > 201)
284 if (opcode == OP_CAT ||
285 opcode == OP_SUBSTR ||
287 opcode == OP_RIGHT ||
288 opcode == OP_INVERT ||
297 opcode == OP_LSHIFT ||
301 if (fExec && 0 <= opcode && opcode <= OP_PUSHDATA4)
302 stack.push_back(vchPushValue);
303 else if (fExec || (OP_IF <= opcode && opcode <= OP_ENDIF))
328 CBigNum bn((int)opcode - (int)(OP_1 - 1));
329 stack.push_back(bn.getvch());
338 case OP_NOP1: case OP_NOP2: case OP_NOP3: case OP_NOP4: case OP_NOP5:
339 case OP_NOP6: case OP_NOP7: case OP_NOP8: case OP_NOP9: case OP_NOP10:
345 // <expression> if [statements] [else [statements]] endif
349 if (stack.size() < 1)
351 valtype& vch = stacktop(-1);
352 fValue = CastToBool(vch);
353 if (opcode == OP_NOTIF)
357 vfExec.push_back(fValue);
365 vfExec.back() = !vfExec.back();
380 // (false -- false) and return
381 if (stack.size() < 1)
383 bool fValue = CastToBool(stacktop(-1));
403 if (stack.size() < 1)
405 altstack.push_back(stacktop(-1));
410 case OP_FROMALTSTACK:
412 if (altstack.size() < 1)
414 stack.push_back(altstacktop(-1));
422 if (stack.size() < 2)
431 // (x1 x2 -- x1 x2 x1 x2)
432 if (stack.size() < 2)
434 valtype vch1 = stacktop(-2);
435 valtype vch2 = stacktop(-1);
436 stack.push_back(vch1);
437 stack.push_back(vch2);
443 // (x1 x2 x3 -- x1 x2 x3 x1 x2 x3)
444 if (stack.size() < 3)
446 valtype vch1 = stacktop(-3);
447 valtype vch2 = stacktop(-2);
448 valtype vch3 = stacktop(-1);
449 stack.push_back(vch1);
450 stack.push_back(vch2);
451 stack.push_back(vch3);
457 // (x1 x2 x3 x4 -- x1 x2 x3 x4 x1 x2)
458 if (stack.size() < 4)
460 valtype vch1 = stacktop(-4);
461 valtype vch2 = stacktop(-3);
462 stack.push_back(vch1);
463 stack.push_back(vch2);
469 // (x1 x2 x3 x4 x5 x6 -- x3 x4 x5 x6 x1 x2)
470 if (stack.size() < 6)
472 valtype vch1 = stacktop(-6);
473 valtype vch2 = stacktop(-5);
474 stack.erase(stack.end()-6, stack.end()-4);
475 stack.push_back(vch1);
476 stack.push_back(vch2);
482 // (x1 x2 x3 x4 -- x3 x4 x1 x2)
483 if (stack.size() < 4)
485 swap(stacktop(-4), stacktop(-2));
486 swap(stacktop(-3), stacktop(-1));
493 if (stack.size() < 1)
495 valtype vch = stacktop(-1);
497 stack.push_back(vch);
504 CBigNum bn(stack.size());
505 stack.push_back(bn.getvch());
512 if (stack.size() < 1)
521 if (stack.size() < 1)
523 valtype vch = stacktop(-1);
524 stack.push_back(vch);
531 if (stack.size() < 2)
533 stack.erase(stack.end() - 2);
539 // (x1 x2 -- x1 x2 x1)
540 if (stack.size() < 2)
542 valtype vch = stacktop(-2);
543 stack.push_back(vch);
550 // (xn ... x2 x1 x0 n - xn ... x2 x1 x0 xn)
551 // (xn ... x2 x1 x0 n - ... x2 x1 x0 xn)
552 if (stack.size() < 2)
554 int n = CastToBigNum(stacktop(-1)).getint();
556 if (n < 0 || n >= (int)stack.size())
558 valtype vch = stacktop(-n-1);
559 if (opcode == OP_ROLL)
560 stack.erase(stack.end()-n-1);
561 stack.push_back(vch);
567 // (x1 x2 x3 -- x2 x3 x1)
568 // x2 x1 x3 after first swap
569 // x2 x3 x1 after second swap
570 if (stack.size() < 3)
572 swap(stacktop(-3), stacktop(-2));
573 swap(stacktop(-2), stacktop(-1));
580 if (stack.size() < 2)
582 swap(stacktop(-2), stacktop(-1));
588 // (x1 x2 -- x2 x1 x2)
589 if (stack.size() < 2)
591 valtype vch = stacktop(-1);
592 stack.insert(stack.end()-2, vch);
603 if (stack.size() < 2)
605 valtype& vch1 = stacktop(-2);
606 valtype& vch2 = stacktop(-1);
607 vch1.insert(vch1.end(), vch2.begin(), vch2.end());
609 if (stacktop(-1).size() > 520)
616 // (in begin size -- out)
617 if (stack.size() < 3)
619 valtype& vch = stacktop(-3);
620 int nBegin = CastToBigNum(stacktop(-2)).getint();
621 int nEnd = nBegin + CastToBigNum(stacktop(-1)).getint();
622 if (nBegin < 0 || nEnd < nBegin)
624 if (nBegin > (int)vch.size())
626 if (nEnd > (int)vch.size())
628 vch.erase(vch.begin() + nEnd, vch.end());
629 vch.erase(vch.begin(), vch.begin() + nBegin);
639 if (stack.size() < 2)
641 valtype& vch = stacktop(-2);
642 int nSize = CastToBigNum(stacktop(-1)).getint();
645 if (nSize > (int)vch.size())
647 if (opcode == OP_LEFT)
648 vch.erase(vch.begin() + nSize, vch.end());
650 vch.erase(vch.begin(), vch.end() - nSize);
658 if (stack.size() < 1)
660 CBigNum bn(stacktop(-1).size());
661 stack.push_back(bn.getvch());
672 if (stack.size() < 1)
674 valtype& vch = stacktop(-1);
675 for (unsigned int i = 0; i < vch.size(); i++)
681 // WARNING: These disabled opcodes exhibit unexpected behavior
682 // when used on signed integers due to a bug in MakeSameSize()
683 // [see definition of MakeSameSize() above].
690 if (stack.size() < 2)
692 valtype& vch1 = stacktop(-2);
693 valtype& vch2 = stacktop(-1);
694 MakeSameSize(vch1, vch2); // <-- NOT SAFE FOR SIGNED VALUES
695 if (opcode == OP_AND)
697 for (unsigned int i = 0; i < vch1.size(); i++)
700 else if (opcode == OP_OR)
702 for (unsigned int i = 0; i < vch1.size(); i++)
705 else if (opcode == OP_XOR)
707 for (unsigned int i = 0; i < vch1.size(); i++)
716 //case OP_NOTEQUAL: // use OP_NUMNOTEQUAL
719 if (stack.size() < 2)
721 valtype& vch1 = stacktop(-2);
722 valtype& vch2 = stacktop(-1);
723 bool fEqual = (vch1 == vch2);
724 // OP_NOTEQUAL is disabled because it would be too easy to say
725 // something like n != 1 and have some wiseguy pass in 1 with extra
726 // zero bytes after it (numerically, 0x01 == 0x0001 == 0x000001)
727 //if (opcode == OP_NOTEQUAL)
731 stack.push_back(fEqual ? vchTrue : vchFalse);
732 if (opcode == OP_EQUALVERIFY)
756 if (stack.size() < 1)
758 CBigNum bn = CastToBigNum(stacktop(-1));
761 case OP_1ADD: bn += bnOne; break;
762 case OP_1SUB: bn -= bnOne; break;
763 case OP_2MUL: bn <<= 1; break;
764 case OP_2DIV: bn >>= 1; break;
765 case OP_NEGATE: bn = -bn; break;
766 case OP_ABS: if (bn < bnZero) bn = -bn; break;
767 case OP_NOT: bn = (bn == bnZero); break;
768 case OP_0NOTEQUAL: bn = (bn != bnZero); break;
769 default: assert(!"invalid opcode"); break;
772 stack.push_back(bn.getvch());
786 case OP_NUMEQUALVERIFY:
790 case OP_LESSTHANOREQUAL:
791 case OP_GREATERTHANOREQUAL:
796 if (stack.size() < 2)
798 CBigNum bn1 = CastToBigNum(stacktop(-2));
799 CBigNum bn2 = CastToBigNum(stacktop(-1));
812 if (!BN_mul(&bn, &bn1, &bn2, pctx))
817 if (!BN_div(&bn, NULL, &bn1, &bn2, pctx))
822 if (!BN_mod(&bn, &bn1, &bn2, pctx))
827 if (bn2 < bnZero || bn2 > CBigNum(2048))
829 bn = bn1 << bn2.getulong();
833 if (bn2 < bnZero || bn2 > CBigNum(2048))
835 bn = bn1 >> bn2.getulong();
838 case OP_BOOLAND: bn = (bn1 != bnZero && bn2 != bnZero); break;
839 case OP_BOOLOR: bn = (bn1 != bnZero || bn2 != bnZero); break;
840 case OP_NUMEQUAL: bn = (bn1 == bn2); break;
841 case OP_NUMEQUALVERIFY: bn = (bn1 == bn2); break;
842 case OP_NUMNOTEQUAL: bn = (bn1 != bn2); break;
843 case OP_LESSTHAN: bn = (bn1 < bn2); break;
844 case OP_GREATERTHAN: bn = (bn1 > bn2); break;
845 case OP_LESSTHANOREQUAL: bn = (bn1 <= bn2); break;
846 case OP_GREATERTHANOREQUAL: bn = (bn1 >= bn2); break;
847 case OP_MIN: bn = (bn1 < bn2 ? bn1 : bn2); break;
848 case OP_MAX: bn = (bn1 > bn2 ? bn1 : bn2); break;
849 default: assert(!"invalid opcode"); break;
853 stack.push_back(bn.getvch());
855 if (opcode == OP_NUMEQUALVERIFY)
857 if (CastToBool(stacktop(-1)))
867 // (x min max -- out)
868 if (stack.size() < 3)
870 CBigNum bn1 = CastToBigNum(stacktop(-3));
871 CBigNum bn2 = CastToBigNum(stacktop(-2));
872 CBigNum bn3 = CastToBigNum(stacktop(-1));
873 bool fValue = (bn2 <= bn1 && bn1 < bn3);
877 stack.push_back(fValue ? vchTrue : vchFalse);
892 if (stack.size() < 1)
894 valtype& vch = stacktop(-1);
895 valtype vchHash((opcode == OP_RIPEMD160 || opcode == OP_SHA1 || opcode == OP_HASH160) ? 20 : 32);
896 if (opcode == OP_RIPEMD160)
897 RIPEMD160(&vch[0], vch.size(), &vchHash[0]);
898 else if (opcode == OP_SHA1)
899 SHA1(&vch[0], vch.size(), &vchHash[0]);
900 else if (opcode == OP_SHA256)
901 SHA256(&vch[0], vch.size(), &vchHash[0]);
902 else if (opcode == OP_HASH160)
904 uint160 hash160 = Hash160(vch);
905 memcpy(&vchHash[0], &hash160, sizeof(hash160));
907 else if (opcode == OP_HASH256)
909 uint256 hash = Hash(vch.begin(), vch.end());
910 memcpy(&vchHash[0], &hash, sizeof(hash));
913 stack.push_back(vchHash);
917 case OP_CODESEPARATOR:
919 // Hash starts after the code separator
925 case OP_CHECKSIGVERIFY:
927 // (sig pubkey -- bool)
928 if (stack.size() < 2)
931 valtype& vchSig = stacktop(-2);
932 valtype& vchPubKey = stacktop(-1);
935 //PrintHex(vchSig.begin(), vchSig.end(), "sig: %s\n");
936 //PrintHex(vchPubKey.begin(), vchPubKey.end(), "pubkey: %s\n");
938 // Subset of script starting at the most recent codeseparator
939 CScript scriptCode(pbegincodehash, pend);
941 // Drop the signature, since there's no way for a signature to sign itself
942 scriptCode.FindAndDelete(CScript(vchSig));
944 bool fSuccess = CheckSig(vchSig, vchPubKey, scriptCode, txTo, nIn, nHashType);
948 stack.push_back(fSuccess ? vchTrue : vchFalse);
949 if (opcode == OP_CHECKSIGVERIFY)
959 case OP_CHECKMULTISIG:
960 case OP_CHECKMULTISIGVERIFY:
962 // ([sig ...] num_of_signatures [pubkey ...] num_of_pubkeys -- bool)
965 if ((int)stack.size() < i)
968 int nKeysCount = CastToBigNum(stacktop(-i)).getint();
969 if (nKeysCount < 0 || nKeysCount > 20)
971 nOpCount += nKeysCount;
976 if ((int)stack.size() < i)
979 int nSigsCount = CastToBigNum(stacktop(-i)).getint();
980 if (nSigsCount < 0 || nSigsCount > nKeysCount)
984 if ((int)stack.size() < i)
987 // Subset of script starting at the most recent codeseparator
988 CScript scriptCode(pbegincodehash, pend);
990 // Drop the signatures, since there's no way for a signature to sign itself
991 for (int k = 0; k < nSigsCount; k++)
993 valtype& vchSig = stacktop(-isig-k);
994 scriptCode.FindAndDelete(CScript(vchSig));
997 bool fSuccess = true;
998 while (fSuccess && nSigsCount > 0)
1000 valtype& vchSig = stacktop(-isig);
1001 valtype& vchPubKey = stacktop(-ikey);
1004 if (CheckSig(vchSig, vchPubKey, scriptCode, txTo, nIn, nHashType))
1012 // If there are more signatures left than keys left,
1013 // then too many signatures have failed
1014 if (nSigsCount > nKeysCount)
1020 stack.push_back(fSuccess ? vchTrue : vchFalse);
1022 if (opcode == OP_CHECKMULTISIGVERIFY)
1037 if (stack.size() + altstack.size() > 1000)
1047 if (!vfExec.empty())
1061 uint256 SignatureHash(CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType)
1063 if (nIn >= txTo.vin.size())
1065 printf("ERROR: SignatureHash() : nIn=%d out of range\n", nIn);
1068 CTransaction txTmp(txTo);
1070 // In case concatenating two scripts ends up with two codeseparators,
1071 // or an extra one at the end, this prevents all those possible incompatibilities.
1072 scriptCode.FindAndDelete(CScript(OP_CODESEPARATOR));
1074 // Blank out other inputs' signatures
1075 for (unsigned int i = 0; i < txTmp.vin.size(); i++)
1076 txTmp.vin[i].scriptSig = CScript();
1077 txTmp.vin[nIn].scriptSig = scriptCode;
1079 // Blank out some of the outputs
1080 if ((nHashType & 0x1f) == SIGHASH_NONE)
1085 // Let the others update at will
1086 for (unsigned int i = 0; i < txTmp.vin.size(); i++)
1088 txTmp.vin[i].nSequence = 0;
1090 else if ((nHashType & 0x1f) == SIGHASH_SINGLE)
1092 // Only lock-in the txout payee at same index as txin
1093 unsigned int nOut = nIn;
1094 if (nOut >= txTmp.vout.size())
1096 printf("ERROR: SignatureHash() : nOut=%d out of range\n", nOut);
1099 txTmp.vout.resize(nOut+1);
1100 for (unsigned int i = 0; i < nOut; i++)
1101 txTmp.vout[i].SetNull();
1103 // Let the others update at will
1104 for (unsigned int i = 0; i < txTmp.vin.size(); i++)
1106 txTmp.vin[i].nSequence = 0;
1109 // Blank out other inputs completely, not recommended for open transactions
1110 if (nHashType & SIGHASH_ANYONECANPAY)
1112 txTmp.vin[0] = txTmp.vin[nIn];
1113 txTmp.vin.resize(1);
1116 // Serialize and hash
1117 CDataStream ss(SER_GETHASH, 0);
1119 ss << txTmp << nHashType;
1120 return Hash(ss.begin(), ss.end());
1124 // Valid signature cache, to avoid doing expensive ECDSA signature checking
1125 // twice for every transaction (once when accepted into memory pool, and
1126 // again when accepted into the block chain)
1128 class CSignatureCache
1131 // sigdata_type is (signature hash, signature, public key):
1132 typedef boost::tuple<uint256, std::vector<unsigned char>, std::vector<unsigned char> > sigdata_type;
1133 std::set< sigdata_type> setValid;
1134 CCriticalSection cs_sigcache;
1138 Get(uint256 hash, const std::vector<unsigned char>& vchSig, const std::vector<unsigned char>& pubKey)
1142 sigdata_type k(hash, vchSig, pubKey);
1143 std::set<sigdata_type>::iterator mi = setValid.find(k);
1144 if (mi != setValid.end())
1149 void Set(uint256 hash, const std::vector<unsigned char>& vchSig, const std::vector<unsigned char>& pubKey)
1151 // DoS prevention: limit cache size to less than 10MB
1152 // (~200 bytes per cache entry times 50,000 entries)
1153 // Since there are a maximum of 20,000 signature operations per block
1154 // 50,000 is a reasonable default.
1155 int64 nMaxCacheSize = GetArg("-maxsigcachesize", 50000);
1156 if (nMaxCacheSize <= 0) return;
1160 while (static_cast<int64>(setValid.size()) > nMaxCacheSize)
1162 // Evict a random entry. Random because that helps
1163 // foil would-be DoS attackers who might try to pre-generate
1164 // and re-use a set of valid signatures just-slightly-greater
1165 // than our cache size.
1166 uint256 randomHash = GetRandHash();
1167 std::vector<unsigned char> unused;
1168 std::set<sigdata_type>::iterator it =
1169 setValid.lower_bound(sigdata_type(randomHash, unused, unused));
1170 if (it == setValid.end())
1171 it = setValid.begin();
1172 setValid.erase(*it);
1175 sigdata_type k(hash, vchSig, pubKey);
1180 bool CheckSig(vector<unsigned char> vchSig, vector<unsigned char> vchPubKey, CScript scriptCode,
1181 const CTransaction& txTo, unsigned int nIn, int nHashType)
1183 static CSignatureCache signatureCache;
1185 // Hash type is one byte tacked on to the end of the signature
1189 nHashType = vchSig.back();
1190 else if (nHashType != vchSig.back())
1194 uint256 sighash = SignatureHash(scriptCode, txTo, nIn, nHashType);
1196 if (signatureCache.Get(sighash, vchSig, vchPubKey))
1200 if (!key.SetPubKey(vchPubKey))
1203 if (!key.Verify(sighash, vchSig))
1206 signatureCache.Set(sighash, vchSig, vchPubKey);
1219 // Return public keys or hashes from scriptPubKey, for 'standard' transaction types.
1221 bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, vector<vector<unsigned char> >& vSolutionsRet)
1224 static map<txnouttype, CScript> mTemplates;
1225 if (mTemplates.empty())
1227 // Standard tx, sender provides pubkey, receiver adds signature
1228 mTemplates.insert(make_pair(TX_PUBKEY, CScript() << OP_PUBKEY << OP_CHECKSIG));
1230 // Bitcoin address tx, sender provides hash of pubkey, receiver provides signature and pubkey
1231 mTemplates.insert(make_pair(TX_PUBKEYHASH, CScript() << OP_DUP << OP_HASH160 << OP_PUBKEYHASH << OP_EQUALVERIFY << OP_CHECKSIG));
1233 // Sender provides N pubkeys, receivers provides M signatures
1234 mTemplates.insert(make_pair(TX_MULTISIG, CScript() << OP_SMALLINTEGER << OP_PUBKEYS << OP_SMALLINTEGER << OP_CHECKMULTISIG));
1237 // Shortcut for pay-to-script-hash, which are more constrained than the other types:
1238 // it is always OP_HASH160 20 [20 byte hash] OP_EQUAL
1239 if (scriptPubKey.IsPayToScriptHash())
1241 typeRet = TX_SCRIPTHASH;
1242 vector<unsigned char> hashBytes(scriptPubKey.begin()+2, scriptPubKey.begin()+22);
1243 vSolutionsRet.push_back(hashBytes);
1248 const CScript& script1 = scriptPubKey;
1249 BOOST_FOREACH(const PAIRTYPE(txnouttype, CScript)& tplate, mTemplates)
1251 const CScript& script2 = tplate.second;
1252 vSolutionsRet.clear();
1254 opcodetype opcode1, opcode2;
1255 vector<unsigned char> vch1, vch2;
1258 CScript::const_iterator pc1 = script1.begin();
1259 CScript::const_iterator pc2 = script2.begin();
1262 if (pc1 == script1.end() && pc2 == script2.end())
1265 typeRet = tplate.first;
1266 if (typeRet == TX_MULTISIG)
1268 // Additional checks for TX_MULTISIG:
1269 unsigned char m = vSolutionsRet.front()[0];
1270 unsigned char n = vSolutionsRet.back()[0];
1271 if (m < 1 || n < 1 || m > n || vSolutionsRet.size()-2 != n)
1276 if (!script1.GetOp(pc1, opcode1, vch1))
1278 if (!script2.GetOp(pc2, opcode2, vch2))
1281 // Template matching opcodes:
1282 if (opcode2 == OP_PUBKEYS)
1284 while (vch1.size() >= 33 && vch1.size() <= 120)
1286 vSolutionsRet.push_back(vch1);
1287 if (!script1.GetOp(pc1, opcode1, vch1))
1290 if (!script2.GetOp(pc2, opcode2, vch2))
1292 // Normal situation is to fall through
1293 // to other if/else statements
1296 if (opcode2 == OP_PUBKEY)
1298 if (vch1.size() < 33 || vch1.size() > 120)
1300 vSolutionsRet.push_back(vch1);
1302 else if (opcode2 == OP_PUBKEYHASH)
1304 if (vch1.size() != sizeof(uint160))
1306 vSolutionsRet.push_back(vch1);
1308 else if (opcode2 == OP_SMALLINTEGER)
1309 { // Single-byte small integer pushed onto vSolutions
1310 if (opcode1 == OP_0 ||
1311 (opcode1 >= OP_1 && opcode1 <= OP_16))
1313 char n = (char)CScript::DecodeOP_N(opcode1);
1314 vSolutionsRet.push_back(valtype(1, n));
1319 else if (opcode1 != opcode2 || vch1 != vch2)
1321 // Others must match exactly
1327 vSolutionsRet.clear();
1328 typeRet = TX_NONSTANDARD;
1333 bool Sign1(const CKeyID& address, const CKeyStore& keystore, uint256 hash, int nHashType, CScript& scriptSigRet)
1336 if (!keystore.GetKey(address, key))
1339 vector<unsigned char> vchSig;
1340 if (!key.Sign(hash, vchSig))
1342 vchSig.push_back((unsigned char)nHashType);
1343 scriptSigRet << vchSig;
1348 bool SignN(const vector<valtype>& multisigdata, const CKeyStore& keystore, uint256 hash, int nHashType, CScript& scriptSigRet)
1351 int nRequired = multisigdata.front()[0];
1352 for (unsigned int i = 1; i < multisigdata.size()-1 && nSigned < nRequired; i++)
1354 const valtype& pubkey = multisigdata[i];
1355 CKeyID keyID = CPubKey(pubkey).GetID();
1356 if (Sign1(keyID, keystore, hash, nHashType, scriptSigRet))
1359 return nSigned==nRequired;
1363 // Sign scriptPubKey with private keys stored in keystore, given transaction hash and hash type.
1364 // Signatures are returned in scriptSigRet (or returns false if scriptPubKey can't be signed),
1365 // unless whichTypeRet is TX_SCRIPTHASH, in which case scriptSigRet is the redemption script.
1366 // Returns false if scriptPubKey could not be completely satisfied.
1368 bool Solver(const CKeyStore& keystore, const CScript& scriptPubKey, uint256 hash, int nHashType,
1369 CScript& scriptSigRet, txnouttype& whichTypeRet)
1371 scriptSigRet.clear();
1373 vector<valtype> vSolutions;
1374 if (!Solver(scriptPubKey, whichTypeRet, vSolutions))
1378 switch (whichTypeRet)
1380 case TX_NONSTANDARD:
1383 keyID = CPubKey(vSolutions[0]).GetID();
1384 return Sign1(keyID, keystore, hash, nHashType, scriptSigRet);
1386 keyID = CKeyID(uint160(vSolutions[0]));
1387 if (!Sign1(keyID, keystore, hash, nHashType, scriptSigRet))
1392 keystore.GetPubKey(keyID, vch);
1393 scriptSigRet << vch;
1397 return keystore.GetCScript(uint160(vSolutions[0]), scriptSigRet);
1400 scriptSigRet << OP_0; // workaround CHECKMULTISIG bug
1401 return (SignN(vSolutions, keystore, hash, nHashType, scriptSigRet));
1406 int ScriptSigArgsExpected(txnouttype t, const std::vector<std::vector<unsigned char> >& vSolutions)
1410 case TX_NONSTANDARD:
1417 if (vSolutions.size() < 1 || vSolutions[0].size() < 1)
1419 return vSolutions[0][0] + 1;
1421 return 1; // doesn't include args needed by the script
1426 bool IsStandard(const CScript& scriptPubKey)
1428 vector<valtype> vSolutions;
1429 txnouttype whichType;
1430 if (!Solver(scriptPubKey, whichType, vSolutions))
1433 if (whichType == TX_MULTISIG)
1435 unsigned char m = vSolutions.front()[0];
1436 unsigned char n = vSolutions.back()[0];
1437 // Support up to x-of-3 multisig txns as standard
1444 return whichType != TX_NONSTANDARD;
1448 unsigned int HaveKeys(const vector<valtype>& pubkeys, const CKeyStore& keystore)
1450 unsigned int nResult = 0;
1451 BOOST_FOREACH(const valtype& pubkey, pubkeys)
1453 CKeyID keyID = CPubKey(pubkey).GetID();
1454 if (keystore.HaveKey(keyID))
1461 class CKeyStoreIsMineVisitor : public boost::static_visitor<bool>
1464 const CKeyStore *keystore;
1466 CKeyStoreIsMineVisitor(const CKeyStore *keystoreIn) : keystore(keystoreIn) { }
1467 bool operator()(const CNoDestination &dest) const { return false; }
1468 bool operator()(const CKeyID &keyID) const { return keystore->HaveKey(keyID); }
1469 bool operator()(const CScriptID &scriptID) const { return keystore->HaveCScript(scriptID); }
1472 bool IsMine(const CKeyStore &keystore, const CTxDestination &dest)
1474 return boost::apply_visitor(CKeyStoreIsMineVisitor(&keystore), dest);
1477 bool IsMine(const CKeyStore &keystore, const CScript& scriptPubKey)
1479 vector<valtype> vSolutions;
1480 txnouttype whichType;
1481 if (!Solver(scriptPubKey, whichType, vSolutions))
1487 case TX_NONSTANDARD:
1490 keyID = CPubKey(vSolutions[0]).GetID();
1491 return keystore.HaveKey(keyID);
1493 keyID = CKeyID(uint160(vSolutions[0]));
1494 return keystore.HaveKey(keyID);
1498 if (!keystore.GetCScript(CScriptID(uint160(vSolutions[0])), subscript))
1500 return IsMine(keystore, subscript);
1504 // Only consider transactions "mine" if we own ALL the
1505 // keys involved. multi-signature transactions that are
1506 // partially owned (somebody else has a key that can spend
1507 // them) enable spend-out-from-under-you attacks, especially
1508 // in shared-wallet situations.
1509 vector<valtype> keys(vSolutions.begin()+1, vSolutions.begin()+vSolutions.size()-1);
1510 return HaveKeys(keys, keystore) == keys.size();
1516 bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet)
1518 vector<valtype> vSolutions;
1519 txnouttype whichType;
1520 if (!Solver(scriptPubKey, whichType, vSolutions))
1523 if (whichType == TX_PUBKEY)
1525 addressRet = CPubKey(vSolutions[0]).GetID();
1528 else if (whichType == TX_PUBKEYHASH)
1530 addressRet = CKeyID(uint160(vSolutions[0]));
1533 else if (whichType == TX_SCRIPTHASH)
1535 addressRet = CScriptID(uint160(vSolutions[0]));
1538 // Multisig txns have more than one address...
1542 class CAffectedKeysVisitor : public boost::static_visitor<void> {
1544 const CKeyStore &keystore;
1545 std::vector<CKeyID> &vKeys;
1548 CAffectedKeysVisitor(const CKeyStore &keystoreIn, std::vector<CKeyID> &vKeysIn) : keystore(keystoreIn), vKeys(vKeysIn) {}
1550 void Process(const CScript &script) {
1552 std::vector<CTxDestination> vDest;
1554 if (ExtractDestinations(script, type, vDest, nRequired)) {
1555 BOOST_FOREACH(const CTxDestination &dest, vDest)
1556 boost::apply_visitor(*this, dest);
1560 void operator()(const CKeyID &keyId) {
1561 if (keystore.HaveKey(keyId))
1562 vKeys.push_back(keyId);
1565 void operator()(const CScriptID &scriptId) {
1567 if (keystore.GetCScript(scriptId, script))
1571 void operator()(const CNoDestination &none) {}
1575 void ExtractAffectedKeys(const CKeyStore &keystore, const CScript& scriptPubKey, std::vector<CKeyID> &vKeys) {
1576 CAffectedKeysVisitor(keystore, vKeys).Process(scriptPubKey);
1579 bool ExtractDestinations(const CScript& scriptPubKey, txnouttype& typeRet, vector<CTxDestination>& addressRet, int& nRequiredRet)
1582 typeRet = TX_NONSTANDARD;
1583 vector<valtype> vSolutions;
1584 if (!Solver(scriptPubKey, typeRet, vSolutions))
1587 if (typeRet == TX_MULTISIG)
1589 nRequiredRet = vSolutions.front()[0];
1590 for (unsigned int i = 1; i < vSolutions.size()-1; i++)
1592 CTxDestination address = CPubKey(vSolutions[i]).GetID();
1593 addressRet.push_back(address);
1599 CTxDestination address;
1600 if (!ExtractDestination(scriptPubKey, address))
1602 addressRet.push_back(address);
1608 bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn,
1609 bool fValidatePayToScriptHash, int nHashType)
1611 vector<vector<unsigned char> > stack, stackCopy;
1612 if (!EvalScript(stack, scriptSig, txTo, nIn, nHashType))
1614 if (fValidatePayToScriptHash)
1616 if (!EvalScript(stack, scriptPubKey, txTo, nIn, nHashType))
1621 if (CastToBool(stack.back()) == false)
1624 // Additional validation for spend-to-script-hash transactions:
1625 if (fValidatePayToScriptHash && scriptPubKey.IsPayToScriptHash())
1627 if (!scriptSig.IsPushOnly()) // scriptSig must be literals-only
1628 return false; // or validation fails
1630 const valtype& pubKeySerialized = stackCopy.back();
1631 CScript pubKey2(pubKeySerialized.begin(), pubKeySerialized.end());
1632 popstack(stackCopy);
1634 if (!EvalScript(stackCopy, pubKey2, txTo, nIn, nHashType))
1636 if (stackCopy.empty())
1638 return CastToBool(stackCopy.back());
1645 bool SignSignature(const CKeyStore &keystore, const CScript& fromPubKey, CTransaction& txTo, unsigned int nIn, int nHashType)
1647 assert(nIn < txTo.vin.size());
1648 CTxIn& txin = txTo.vin[nIn];
1650 // Leave out the signature from the hash, since a signature can't sign itself.
1651 // The checksig op will also drop the signatures from its hash.
1652 uint256 hash = SignatureHash(fromPubKey, txTo, nIn, nHashType);
1654 txnouttype whichType;
1655 if (!Solver(keystore, fromPubKey, hash, nHashType, txin.scriptSig, whichType))
1658 if (whichType == TX_SCRIPTHASH)
1660 // Solver returns the subscript that need to be evaluated;
1661 // the final scriptSig is the signatures from that
1662 // and then the serialized subscript:
1663 CScript subscript = txin.scriptSig;
1665 // Recompute txn hash using subscript in place of scriptPubKey:
1666 uint256 hash2 = SignatureHash(subscript, txTo, nIn, nHashType);
1670 Solver(keystore, subscript, hash2, nHashType, txin.scriptSig, subType) && subType != TX_SCRIPTHASH;
1671 // Append serialized subscript whether or not it is completely signed:
1672 txin.scriptSig << static_cast<valtype>(subscript);
1673 if (!fSolved) return false;
1677 return VerifyScript(txin.scriptSig, fromPubKey, txTo, nIn, true, 0);
1680 bool SignSignature(const CKeyStore &keystore, const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType)
1682 assert(nIn < txTo.vin.size());
1683 CTxIn& txin = txTo.vin[nIn];
1684 assert(txin.prevout.n < txFrom.vout.size());
1685 assert(txin.prevout.hash == txFrom.GetHash());
1686 const CTxOut& txout = txFrom.vout[txin.prevout.n];
1688 return SignSignature(keystore, txout.scriptPubKey, txTo, nIn, nHashType);
1691 bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, bool fValidatePayToScriptHash, int nHashType)
1693 assert(nIn < txTo.vin.size());
1694 const CTxIn& txin = txTo.vin[nIn];
1695 if (txin.prevout.n >= txFrom.vout.size())
1697 const CTxOut& txout = txFrom.vout[txin.prevout.n];
1699 if (txin.prevout.hash != txFrom.GetHash())
1702 return VerifyScript(txin.scriptSig, txout.scriptPubKey, txTo, nIn, fValidatePayToScriptHash, nHashType);
1705 static CScript PushAll(const vector<valtype>& values)
1708 BOOST_FOREACH(const valtype& v, values)
1713 static CScript CombineMultisig(CScript scriptPubKey, const CTransaction& txTo, unsigned int nIn,
1714 const vector<valtype>& vSolutions,
1715 vector<valtype>& sigs1, vector<valtype>& sigs2)
1717 // Combine all the signatures we've got:
1718 set<valtype> allsigs;
1719 BOOST_FOREACH(const valtype& v, sigs1)
1724 BOOST_FOREACH(const valtype& v, sigs2)
1730 // Build a map of pubkey -> signature by matching sigs to pubkeys:
1731 assert(vSolutions.size() > 1);
1732 unsigned int nSigsRequired = vSolutions.front()[0];
1733 unsigned int nPubKeys = vSolutions.size()-2;
1734 map<valtype, valtype> sigs;
1735 BOOST_FOREACH(const valtype& sig, allsigs)
1737 for (unsigned int i = 0; i < nPubKeys; i++)
1739 const valtype& pubkey = vSolutions[i+1];
1740 if (sigs.count(pubkey))
1741 continue; // Already got a sig for this pubkey
1743 if (CheckSig(sig, pubkey, scriptPubKey, txTo, nIn, 0))
1750 // Now build a merged CScript:
1751 unsigned int nSigsHave = 0;
1752 CScript result; result << OP_0; // pop-one-too-many workaround
1753 for (unsigned int i = 0; i < nPubKeys && nSigsHave < nSigsRequired; i++)
1755 if (sigs.count(vSolutions[i+1]))
1757 result << sigs[vSolutions[i+1]];
1761 // Fill any missing with OP_0:
1762 for (unsigned int i = nSigsHave; i < nSigsRequired; i++)
1768 static CScript CombineSignatures(CScript scriptPubKey, const CTransaction& txTo, unsigned int nIn,
1769 const txnouttype txType, const vector<valtype>& vSolutions,
1770 vector<valtype>& sigs1, vector<valtype>& sigs2)
1774 case TX_NONSTANDARD:
1775 // Don't know anything about this, assume bigger one is correct:
1776 if (sigs1.size() >= sigs2.size())
1777 return PushAll(sigs1);
1778 return PushAll(sigs2);
1781 // Signatures are bigger than placeholders or empty scripts:
1782 if (sigs1.empty() || sigs1[0].empty())
1783 return PushAll(sigs2);
1784 return PushAll(sigs1);
1786 if (sigs1.empty() || sigs1.back().empty())
1787 return PushAll(sigs2);
1788 else if (sigs2.empty() || sigs2.back().empty())
1789 return PushAll(sigs1);
1792 // Recur to combine:
1793 valtype spk = sigs1.back();
1794 CScript pubKey2(spk.begin(), spk.end());
1797 vector<vector<unsigned char> > vSolutions2;
1798 Solver(pubKey2, txType2, vSolutions2);
1801 CScript result = CombineSignatures(pubKey2, txTo, nIn, txType2, vSolutions2, sigs1, sigs2);
1806 return CombineMultisig(scriptPubKey, txTo, nIn, vSolutions, sigs1, sigs2);
1812 CScript CombineSignatures(CScript scriptPubKey, const CTransaction& txTo, unsigned int nIn,
1813 const CScript& scriptSig1, const CScript& scriptSig2)
1816 vector<vector<unsigned char> > vSolutions;
1817 Solver(scriptPubKey, txType, vSolutions);
1819 vector<valtype> stack1;
1820 EvalScript(stack1, scriptSig1, CTransaction(), 0, 0);
1821 vector<valtype> stack2;
1822 EvalScript(stack2, scriptSig2, CTransaction(), 0, 0);
1824 return CombineSignatures(scriptPubKey, txTo, nIn, txType, vSolutions, stack1, stack2);
1827 unsigned int CScript::GetSigOpCount(bool fAccurate) const
1830 const_iterator pc = begin();
1831 opcodetype lastOpcode = OP_INVALIDOPCODE;
1835 if (!GetOp(pc, opcode))
1837 if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
1839 else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
1841 if (fAccurate && lastOpcode >= OP_1 && lastOpcode <= OP_16)
1842 n += DecodeOP_N(lastOpcode);
1846 lastOpcode = opcode;
1851 unsigned int CScript::GetSigOpCount(const CScript& scriptSig) const
1853 if (!IsPayToScriptHash())
1854 return GetSigOpCount(true);
1856 // This is a pay-to-script-hash scriptPubKey;
1857 // get the last item that the scriptSig
1858 // pushes onto the stack:
1859 const_iterator pc = scriptSig.begin();
1860 vector<unsigned char> data;
1861 while (pc < scriptSig.end())
1864 if (!scriptSig.GetOp(pc, opcode, data))
1870 /// ... and return its opcount:
1871 CScript subscript(data.begin(), data.end());
1872 return subscript.GetSigOpCount(true);
1875 bool CScript::IsPayToScriptHash() const
1877 // Extra-fast test for pay-to-script-hash CScripts:
1878 return (this->size() == 23 &&
1879 this->at(0) == OP_HASH160 &&
1880 this->at(1) == 0x14 &&
1881 this->at(22) == OP_EQUAL);
1884 class CScriptVisitor : public boost::static_visitor<bool>
1889 CScriptVisitor(CScript *scriptin) { script = scriptin; }
1891 bool operator()(const CNoDestination &dest) const {
1896 bool operator()(const CKeyID &keyID) const {
1898 *script << OP_DUP << OP_HASH160 << keyID << OP_EQUALVERIFY << OP_CHECKSIG;
1902 bool operator()(const CScriptID &scriptID) const {
1904 *script << OP_HASH160 << scriptID << OP_EQUAL;
1909 void CScript::SetDestination(const CTxDestination& dest)
1911 boost::apply_visitor(CScriptVisitor(this), dest);
1914 void CScript::SetMultisig(int nRequired, const std::vector<CKey>& keys)
1918 *this << EncodeOP_N(nRequired);
1919 BOOST_FOREACH(const CKey& key, keys)
1920 *this << key.GetPubKey();
1921 *this << EncodeOP_N(keys.size()) << OP_CHECKMULTISIG;