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.
5 #ifndef H_BITCOIN_SCRIPT
6 #define H_BITCOIN_SCRIPT
14 #include <boost/foreach.hpp>
23 SIGHASH_ANYONECANPAY = 0x80,
132 OP_GREATERTHANOREQUAL,
148 OP_CHECKMULTISIGVERIFY,
164 // template matching params
165 OP_PUBKEYHASH = 0xfd,
168 OP_INVALIDOPCODE = 0xff,
178 inline const char* GetOpName(opcodetype opcode)
183 case OP_0 : return "0";
184 case OP_PUSHDATA1 : return "OP_PUSHDATA1";
185 case OP_PUSHDATA2 : return "OP_PUSHDATA2";
186 case OP_PUSHDATA4 : return "OP_PUSHDATA4";
187 case OP_1NEGATE : return "-1";
188 case OP_RESERVED : return "OP_RESERVED";
189 case OP_1 : return "1";
190 case OP_2 : return "2";
191 case OP_3 : return "3";
192 case OP_4 : return "4";
193 case OP_5 : return "5";
194 case OP_6 : return "6";
195 case OP_7 : return "7";
196 case OP_8 : return "8";
197 case OP_9 : return "9";
198 case OP_10 : return "10";
199 case OP_11 : return "11";
200 case OP_12 : return "12";
201 case OP_13 : return "13";
202 case OP_14 : return "14";
203 case OP_15 : return "15";
204 case OP_16 : return "16";
207 case OP_NOP : return "OP_NOP";
208 case OP_VER : return "OP_VER";
209 case OP_IF : return "OP_IF";
210 case OP_NOTIF : return "OP_NOTIF";
211 case OP_VERIF : return "OP_VERIF";
212 case OP_VERNOTIF : return "OP_VERNOTIF";
213 case OP_ELSE : return "OP_ELSE";
214 case OP_ENDIF : return "OP_ENDIF";
215 case OP_VERIFY : return "OP_VERIFY";
216 case OP_RETURN : return "OP_RETURN";
219 case OP_TOALTSTACK : return "OP_TOALTSTACK";
220 case OP_FROMALTSTACK : return "OP_FROMALTSTACK";
221 case OP_2DROP : return "OP_2DROP";
222 case OP_2DUP : return "OP_2DUP";
223 case OP_3DUP : return "OP_3DUP";
224 case OP_2OVER : return "OP_2OVER";
225 case OP_2ROT : return "OP_2ROT";
226 case OP_2SWAP : return "OP_2SWAP";
227 case OP_IFDUP : return "OP_IFDUP";
228 case OP_DEPTH : return "OP_DEPTH";
229 case OP_DROP : return "OP_DROP";
230 case OP_DUP : return "OP_DUP";
231 case OP_NIP : return "OP_NIP";
232 case OP_OVER : return "OP_OVER";
233 case OP_PICK : return "OP_PICK";
234 case OP_ROLL : return "OP_ROLL";
235 case OP_ROT : return "OP_ROT";
236 case OP_SWAP : return "OP_SWAP";
237 case OP_TUCK : return "OP_TUCK";
240 case OP_CAT : return "OP_CAT";
241 case OP_SUBSTR : return "OP_SUBSTR";
242 case OP_LEFT : return "OP_LEFT";
243 case OP_RIGHT : return "OP_RIGHT";
244 case OP_SIZE : return "OP_SIZE";
247 case OP_INVERT : return "OP_INVERT";
248 case OP_AND : return "OP_AND";
249 case OP_OR : return "OP_OR";
250 case OP_XOR : return "OP_XOR";
251 case OP_EQUAL : return "OP_EQUAL";
252 case OP_EQUALVERIFY : return "OP_EQUALVERIFY";
253 case OP_RESERVED1 : return "OP_RESERVED1";
254 case OP_RESERVED2 : return "OP_RESERVED2";
257 case OP_1ADD : return "OP_1ADD";
258 case OP_1SUB : return "OP_1SUB";
259 case OP_2MUL : return "OP_2MUL";
260 case OP_2DIV : return "OP_2DIV";
261 case OP_NEGATE : return "OP_NEGATE";
262 case OP_ABS : return "OP_ABS";
263 case OP_NOT : return "OP_NOT";
264 case OP_0NOTEQUAL : return "OP_0NOTEQUAL";
265 case OP_ADD : return "OP_ADD";
266 case OP_SUB : return "OP_SUB";
267 case OP_MUL : return "OP_MUL";
268 case OP_DIV : return "OP_DIV";
269 case OP_MOD : return "OP_MOD";
270 case OP_LSHIFT : return "OP_LSHIFT";
271 case OP_RSHIFT : return "OP_RSHIFT";
272 case OP_BOOLAND : return "OP_BOOLAND";
273 case OP_BOOLOR : return "OP_BOOLOR";
274 case OP_NUMEQUAL : return "OP_NUMEQUAL";
275 case OP_NUMEQUALVERIFY : return "OP_NUMEQUALVERIFY";
276 case OP_NUMNOTEQUAL : return "OP_NUMNOTEQUAL";
277 case OP_LESSTHAN : return "OP_LESSTHAN";
278 case OP_GREATERTHAN : return "OP_GREATERTHAN";
279 case OP_LESSTHANOREQUAL : return "OP_LESSTHANOREQUAL";
280 case OP_GREATERTHANOREQUAL : return "OP_GREATERTHANOREQUAL";
281 case OP_MIN : return "OP_MIN";
282 case OP_MAX : return "OP_MAX";
283 case OP_WITHIN : return "OP_WITHIN";
286 case OP_RIPEMD160 : return "OP_RIPEMD160";
287 case OP_SHA1 : return "OP_SHA1";
288 case OP_SHA256 : return "OP_SHA256";
289 case OP_HASH160 : return "OP_HASH160";
290 case OP_HASH256 : return "OP_HASH256";
291 case OP_CODESEPARATOR : return "OP_CODESEPARATOR";
292 case OP_CHECKSIG : return "OP_CHECKSIG";
293 case OP_CHECKSIGVERIFY : return "OP_CHECKSIGVERIFY";
294 case OP_CHECKMULTISIG : return "OP_CHECKMULTISIG";
295 case OP_CHECKMULTISIGVERIFY : return "OP_CHECKMULTISIGVERIFY";
298 case OP_NOP1 : return "OP_NOP1";
299 case OP_NOP2 : return "OP_NOP2";
300 case OP_NOP3 : return "OP_NOP3";
301 case OP_NOP4 : return "OP_NOP4";
302 case OP_NOP5 : return "OP_NOP5";
303 case OP_NOP6 : return "OP_NOP6";
304 case OP_NOP7 : return "OP_NOP7";
305 case OP_NOP8 : return "OP_NOP8";
306 case OP_NOP9 : return "OP_NOP9";
307 case OP_NOP10 : return "OP_NOP10";
311 // template matching params
312 case OP_PUBKEYHASH : return "OP_PUBKEYHASH";
313 case OP_PUBKEY : return "OP_PUBKEY";
315 case OP_INVALIDOPCODE : return "OP_INVALIDOPCODE";
324 inline std::string ValueString(const std::vector<unsigned char>& vch)
327 return strprintf("%d", CBigNum(vch).getint());
332 inline std::string StackString(const std::vector<std::vector<unsigned char> >& vStack)
335 BOOST_FOREACH(const std::vector<unsigned char>& vch, vStack)
339 str += ValueString(vch);
352 class CScript : public std::vector<unsigned char>
355 CScript& push_int64(int64 n)
357 if (n == -1 || (n >= 1 && n <= 16))
359 push_back(n + (OP_1 - 1));
364 *this << bn.getvch();
369 CScript& push_uint64(uint64 n)
371 if (n >= 1 && n <= 16)
373 push_back(n + (OP_1 - 1));
378 *this << bn.getvch();
385 CScript(const CScript& b) : std::vector<unsigned char>(b.begin(), b.end()) { }
386 CScript(const_iterator pbegin, const_iterator pend) : std::vector<unsigned char>(pbegin, pend) { }
388 CScript(const unsigned char* pbegin, const unsigned char* pend) : std::vector<unsigned char>(pbegin, pend) { }
391 CScript& operator+=(const CScript& b)
393 insert(end(), b.begin(), b.end());
397 friend CScript operator+(const CScript& a, const CScript& b)
405 explicit CScript(char b) { operator<<(b); }
406 explicit CScript(short b) { operator<<(b); }
407 explicit CScript(int b) { operator<<(b); }
408 explicit CScript(long b) { operator<<(b); }
409 explicit CScript(int64 b) { operator<<(b); }
410 explicit CScript(unsigned char b) { operator<<(b); }
411 explicit CScript(unsigned int b) { operator<<(b); }
412 explicit CScript(unsigned short b) { operator<<(b); }
413 explicit CScript(unsigned long b) { operator<<(b); }
414 explicit CScript(uint64 b) { operator<<(b); }
416 explicit CScript(opcodetype b) { operator<<(b); }
417 explicit CScript(const uint256& b) { operator<<(b); }
418 explicit CScript(const CBigNum& b) { operator<<(b); }
419 explicit CScript(const std::vector<unsigned char>& b) { operator<<(b); }
422 CScript& operator<<(char b) { return push_int64(b); }
423 CScript& operator<<(short b) { return push_int64(b); }
424 CScript& operator<<(int b) { return push_int64(b); }
425 CScript& operator<<(long b) { return push_int64(b); }
426 CScript& operator<<(int64 b) { return push_int64(b); }
427 CScript& operator<<(unsigned char b) { return push_uint64(b); }
428 CScript& operator<<(unsigned int b) { return push_uint64(b); }
429 CScript& operator<<(unsigned short b) { return push_uint64(b); }
430 CScript& operator<<(unsigned long b) { return push_uint64(b); }
431 CScript& operator<<(uint64 b) { return push_uint64(b); }
433 CScript& operator<<(opcodetype opcode)
435 if (opcode < 0 || opcode > 0xff)
436 throw std::runtime_error("CScript::operator<<() : invalid opcode");
437 insert(end(), (unsigned char)opcode);
441 CScript& operator<<(const uint160& b)
443 insert(end(), sizeof(b));
444 insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
448 CScript& operator<<(const uint256& b)
450 insert(end(), sizeof(b));
451 insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
455 CScript& operator<<(const CBigNum& b)
461 CScript& operator<<(const std::vector<unsigned char>& b)
463 if (b.size() < OP_PUSHDATA1)
465 insert(end(), (unsigned char)b.size());
467 else if (b.size() <= 0xff)
469 insert(end(), OP_PUSHDATA1);
470 insert(end(), (unsigned char)b.size());
472 else if (b.size() <= 0xffff)
474 insert(end(), OP_PUSHDATA2);
475 unsigned short nSize = b.size();
476 insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
480 insert(end(), OP_PUSHDATA4);
481 unsigned int nSize = b.size();
482 insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
484 insert(end(), b.begin(), b.end());
488 CScript& operator<<(const CScript& b)
490 // I'm not sure if this should push the script or concatenate scripts.
491 // If there's ever a use for pushing a script onto a script, delete this member fn
492 assert(!"warning: pushing a CScript onto a CScript with << is probably not intended, use + to concatenate");
497 bool GetOp(iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet)
499 // Wrapper so it can be called with either iterator or const_iterator
500 const_iterator pc2 = pc;
501 bool fRet = GetOp2(pc2, opcodeRet, &vchRet);
502 pc = begin() + (pc2 - begin());
506 bool GetOp(iterator& pc, opcodetype& opcodeRet)
508 const_iterator pc2 = pc;
509 bool fRet = GetOp2(pc2, opcodeRet, NULL);
510 pc = begin() + (pc2 - begin());
514 bool GetOp(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet) const
516 return GetOp2(pc, opcodeRet, &vchRet);
519 bool GetOp(const_iterator& pc, opcodetype& opcodeRet) const
521 return GetOp2(pc, opcodeRet, NULL);
524 bool GetOp2(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet) const
526 opcodeRet = OP_INVALIDOPCODE;
535 unsigned int opcode = *pc++;
538 if (opcode <= OP_PUSHDATA4)
541 if (opcode < OP_PUSHDATA1)
545 else if (opcode == OP_PUSHDATA1)
551 else if (opcode == OP_PUSHDATA2)
556 memcpy(&nSize, &pc[0], 2);
559 else if (opcode == OP_PUSHDATA4)
563 memcpy(&nSize, &pc[0], 4);
566 if (end() - pc < nSize)
569 pvchRet->assign(pc, pc + nSize);
573 opcodeRet = (opcodetype)opcode;
578 void FindAndDelete(const CScript& b)
582 iterator pc = begin();
586 while (end() - pc >= b.size() && memcmp(&pc[0], &b[0], b.size()) == 0)
587 erase(pc, pc + b.size());
589 while (GetOp(pc, opcode));
593 int GetSigOpCount() const
596 const_iterator pc = begin();
600 if (!GetOp(pc, opcode))
602 if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
604 else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
611 bool IsPushOnly() const
615 const_iterator pc = begin();
619 if (!GetOp(pc, opcode))
628 CBitcoinAddress GetBitcoinAddress() const
631 std::vector<unsigned char> vch;
632 CScript::const_iterator pc = begin();
633 if (!GetOp(pc, opcode, vch) || opcode != OP_DUP) return 0;
634 if (!GetOp(pc, opcode, vch) || opcode != OP_HASH160) return 0;
635 if (!GetOp(pc, opcode, vch) || vch.size() != sizeof(uint160)) return 0;
636 uint160 hash160 = uint160(vch);
637 if (!GetOp(pc, opcode, vch) || opcode != OP_EQUALVERIFY) return 0;
638 if (!GetOp(pc, opcode, vch) || opcode != OP_CHECKSIG) return 0;
639 if (pc != end()) return 0;
640 return CBitcoinAddress(hash160);
643 void SetBitcoinAddress(const CBitcoinAddress& address)
646 *this << OP_DUP << OP_HASH160 << address.GetHash160() << OP_EQUALVERIFY << OP_CHECKSIG;
649 void SetBitcoinAddress(const std::vector<unsigned char>& vchPubKey)
651 SetBitcoinAddress(CBitcoinAddress(vchPubKey));
655 void PrintHex() const
657 printf("CScript(%s)\n", HexStr(begin(), end(), true).c_str());
660 std::string ToString() const
664 std::vector<unsigned char> vch;
665 const_iterator pc = begin();
670 if (!GetOp(pc, opcode, vch))
675 if (0 <= opcode && opcode <= OP_PUSHDATA4)
676 str += ValueString(vch);
678 str += GetOpName(opcode);
685 printf("%s\n", ToString().c_str());
695 bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, int nHashType);
697 bool IsStandard(const CScript& scriptPubKey);
698 bool IsMine(const CKeyStore& keystore, const CScript& scriptPubKey);
699 bool ExtractAddress(const CScript& scriptPubKey, const CKeyStore* pkeystore, CBitcoinAddress& addressRet);
700 bool SignSignature(const CKeyStore& keystore, const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL, CScript scriptPrereq=CScript());
701 bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, int nHashType=0);