1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
4 #ifndef H_BITCOIN_SCRIPT
5 #define H_BITCOIN_SCRIPT
19 SIGHASH_ANYONECANPAY = 0x80,
128 OP_GREATERTHANOREQUAL,
144 OP_CHECKMULTISIGVERIFY,
160 // template matching params
161 OP_PUBKEYHASH = 0xfd,
164 OP_INVALIDOPCODE = 0xff,
174 inline const char* GetOpName(opcodetype opcode)
179 case OP_0 : return "0";
180 case OP_PUSHDATA1 : return "OP_PUSHDATA1";
181 case OP_PUSHDATA2 : return "OP_PUSHDATA2";
182 case OP_PUSHDATA4 : return "OP_PUSHDATA4";
183 case OP_1NEGATE : return "-1";
184 case OP_RESERVED : return "OP_RESERVED";
185 case OP_1 : return "1";
186 case OP_2 : return "2";
187 case OP_3 : return "3";
188 case OP_4 : return "4";
189 case OP_5 : return "5";
190 case OP_6 : return "6";
191 case OP_7 : return "7";
192 case OP_8 : return "8";
193 case OP_9 : return "9";
194 case OP_10 : return "10";
195 case OP_11 : return "11";
196 case OP_12 : return "12";
197 case OP_13 : return "13";
198 case OP_14 : return "14";
199 case OP_15 : return "15";
200 case OP_16 : return "16";
203 case OP_NOP : return "OP_NOP";
204 case OP_VER : return "OP_VER";
205 case OP_IF : return "OP_IF";
206 case OP_NOTIF : return "OP_NOTIF";
207 case OP_VERIF : return "OP_VERIF";
208 case OP_VERNOTIF : return "OP_VERNOTIF";
209 case OP_ELSE : return "OP_ELSE";
210 case OP_ENDIF : return "OP_ENDIF";
211 case OP_VERIFY : return "OP_VERIFY";
212 case OP_RETURN : return "OP_RETURN";
215 case OP_TOALTSTACK : return "OP_TOALTSTACK";
216 case OP_FROMALTSTACK : return "OP_FROMALTSTACK";
217 case OP_2DROP : return "OP_2DROP";
218 case OP_2DUP : return "OP_2DUP";
219 case OP_3DUP : return "OP_3DUP";
220 case OP_2OVER : return "OP_2OVER";
221 case OP_2ROT : return "OP_2ROT";
222 case OP_2SWAP : return "OP_2SWAP";
223 case OP_IFDUP : return "OP_IFDUP";
224 case OP_DEPTH : return "OP_DEPTH";
225 case OP_DROP : return "OP_DROP";
226 case OP_DUP : return "OP_DUP";
227 case OP_NIP : return "OP_NIP";
228 case OP_OVER : return "OP_OVER";
229 case OP_PICK : return "OP_PICK";
230 case OP_ROLL : return "OP_ROLL";
231 case OP_ROT : return "OP_ROT";
232 case OP_SWAP : return "OP_SWAP";
233 case OP_TUCK : return "OP_TUCK";
236 case OP_CAT : return "OP_CAT";
237 case OP_SUBSTR : return "OP_SUBSTR";
238 case OP_LEFT : return "OP_LEFT";
239 case OP_RIGHT : return "OP_RIGHT";
240 case OP_SIZE : return "OP_SIZE";
243 case OP_INVERT : return "OP_INVERT";
244 case OP_AND : return "OP_AND";
245 case OP_OR : return "OP_OR";
246 case OP_XOR : return "OP_XOR";
247 case OP_EQUAL : return "OP_EQUAL";
248 case OP_EQUALVERIFY : return "OP_EQUALVERIFY";
249 case OP_RESERVED1 : return "OP_RESERVED1";
250 case OP_RESERVED2 : return "OP_RESERVED2";
253 case OP_1ADD : return "OP_1ADD";
254 case OP_1SUB : return "OP_1SUB";
255 case OP_2MUL : return "OP_2MUL";
256 case OP_2DIV : return "OP_2DIV";
257 case OP_NEGATE : return "OP_NEGATE";
258 case OP_ABS : return "OP_ABS";
259 case OP_NOT : return "OP_NOT";
260 case OP_0NOTEQUAL : return "OP_0NOTEQUAL";
261 case OP_ADD : return "OP_ADD";
262 case OP_SUB : return "OP_SUB";
263 case OP_MUL : return "OP_MUL";
264 case OP_DIV : return "OP_DIV";
265 case OP_MOD : return "OP_MOD";
266 case OP_LSHIFT : return "OP_LSHIFT";
267 case OP_RSHIFT : return "OP_RSHIFT";
268 case OP_BOOLAND : return "OP_BOOLAND";
269 case OP_BOOLOR : return "OP_BOOLOR";
270 case OP_NUMEQUAL : return "OP_NUMEQUAL";
271 case OP_NUMEQUALVERIFY : return "OP_NUMEQUALVERIFY";
272 case OP_NUMNOTEQUAL : return "OP_NUMNOTEQUAL";
273 case OP_LESSTHAN : return "OP_LESSTHAN";
274 case OP_GREATERTHAN : return "OP_GREATERTHAN";
275 case OP_LESSTHANOREQUAL : return "OP_LESSTHANOREQUAL";
276 case OP_GREATERTHANOREQUAL : return "OP_GREATERTHANOREQUAL";
277 case OP_MIN : return "OP_MIN";
278 case OP_MAX : return "OP_MAX";
279 case OP_WITHIN : return "OP_WITHIN";
282 case OP_RIPEMD160 : return "OP_RIPEMD160";
283 case OP_SHA1 : return "OP_SHA1";
284 case OP_SHA256 : return "OP_SHA256";
285 case OP_HASH160 : return "OP_HASH160";
286 case OP_HASH256 : return "OP_HASH256";
287 case OP_CODESEPARATOR : return "OP_CODESEPARATOR";
288 case OP_CHECKSIG : return "OP_CHECKSIG";
289 case OP_CHECKSIGVERIFY : return "OP_CHECKSIGVERIFY";
290 case OP_CHECKMULTISIG : return "OP_CHECKMULTISIG";
291 case OP_CHECKMULTISIGVERIFY : return "OP_CHECKMULTISIGVERIFY";
294 case OP_NOP1 : return "OP_NOP1";
295 case OP_NOP2 : return "OP_NOP2";
296 case OP_NOP3 : return "OP_NOP3";
297 case OP_NOP4 : return "OP_NOP4";
298 case OP_NOP5 : return "OP_NOP5";
299 case OP_NOP6 : return "OP_NOP6";
300 case OP_NOP7 : return "OP_NOP7";
301 case OP_NOP8 : return "OP_NOP8";
302 case OP_NOP9 : return "OP_NOP9";
303 case OP_NOP10 : return "OP_NOP10";
307 // template matching params
308 case OP_PUBKEYHASH : return "OP_PUBKEYHASH";
309 case OP_PUBKEY : return "OP_PUBKEY";
311 case OP_INVALIDOPCODE : return "OP_INVALIDOPCODE";
320 inline std::string ValueString(const std::vector<unsigned char>& vch)
323 return strprintf("%d", CBigNum(vch).getint());
328 inline std::string StackString(const std::vector<std::vector<unsigned char> >& vStack)
331 BOOST_FOREACH(const std::vector<unsigned char>& vch, vStack)
335 str += ValueString(vch);
348 class CScript : public std::vector<unsigned char>
351 CScript& push_int64(int64 n)
353 if (n == -1 || (n >= 1 && n <= 16))
355 push_back(n + (OP_1 - 1));
360 *this << bn.getvch();
365 CScript& push_uint64(uint64 n)
367 if (n >= 1 && n <= 16)
369 push_back(n + (OP_1 - 1));
374 *this << bn.getvch();
381 CScript(const CScript& b) : std::vector<unsigned char>(b.begin(), b.end()) { }
382 CScript(const_iterator pbegin, const_iterator pend) : std::vector<unsigned char>(pbegin, pend) { }
384 CScript(const unsigned char* pbegin, const unsigned char* pend) : std::vector<unsigned char>(pbegin, pend) { }
387 CScript& operator+=(const CScript& b)
389 insert(end(), b.begin(), b.end());
393 friend CScript operator+(const CScript& a, const CScript& b)
401 explicit CScript(char b) { operator<<(b); }
402 explicit CScript(short b) { operator<<(b); }
403 explicit CScript(int b) { operator<<(b); }
404 explicit CScript(long b) { operator<<(b); }
405 explicit CScript(int64 b) { operator<<(b); }
406 explicit CScript(unsigned char b) { operator<<(b); }
407 explicit CScript(unsigned int b) { operator<<(b); }
408 explicit CScript(unsigned short b) { operator<<(b); }
409 explicit CScript(unsigned long b) { operator<<(b); }
410 explicit CScript(uint64 b) { operator<<(b); }
412 explicit CScript(opcodetype b) { operator<<(b); }
413 explicit CScript(const uint256& b) { operator<<(b); }
414 explicit CScript(const CBigNum& b) { operator<<(b); }
415 explicit CScript(const std::vector<unsigned char>& b) { operator<<(b); }
418 CScript& operator<<(char b) { return push_int64(b); }
419 CScript& operator<<(short b) { return push_int64(b); }
420 CScript& operator<<(int b) { return push_int64(b); }
421 CScript& operator<<(long b) { return push_int64(b); }
422 CScript& operator<<(int64 b) { return push_int64(b); }
423 CScript& operator<<(unsigned char b) { return push_uint64(b); }
424 CScript& operator<<(unsigned int b) { return push_uint64(b); }
425 CScript& operator<<(unsigned short b) { return push_uint64(b); }
426 CScript& operator<<(unsigned long b) { return push_uint64(b); }
427 CScript& operator<<(uint64 b) { return push_uint64(b); }
429 CScript& operator<<(opcodetype opcode)
431 if (opcode < 0 || opcode > 0xff)
432 throw std::runtime_error("CScript::operator<<() : invalid opcode");
433 insert(end(), (unsigned char)opcode);
437 CScript& operator<<(const uint160& b)
439 insert(end(), sizeof(b));
440 insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
444 CScript& operator<<(const uint256& b)
446 insert(end(), sizeof(b));
447 insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
451 CScript& operator<<(const CBigNum& b)
457 CScript& operator<<(const std::vector<unsigned char>& b)
459 if (b.size() < OP_PUSHDATA1)
461 insert(end(), (unsigned char)b.size());
463 else if (b.size() <= 0xff)
465 insert(end(), OP_PUSHDATA1);
466 insert(end(), (unsigned char)b.size());
468 else if (b.size() <= 0xffff)
470 insert(end(), OP_PUSHDATA2);
471 unsigned short nSize = b.size();
472 insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
476 insert(end(), OP_PUSHDATA4);
477 unsigned int nSize = b.size();
478 insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
480 insert(end(), b.begin(), b.end());
484 CScript& operator<<(const CScript& b)
486 // I'm not sure if this should push the script or concatenate scripts.
487 // If there's ever a use for pushing a script onto a script, delete this member fn
488 assert(("warning: pushing a CScript onto a CScript with << is probably not intended, use + to concatenate", false));
493 bool GetOp(iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet)
495 // Wrapper so it can be called with either iterator or const_iterator
496 const_iterator pc2 = pc;
497 bool fRet = GetOp2(pc2, opcodeRet, &vchRet);
498 pc = begin() + (pc2 - begin());
502 bool GetOp(iterator& pc, opcodetype& opcodeRet)
504 const_iterator pc2 = pc;
505 bool fRet = GetOp2(pc2, opcodeRet, NULL);
506 pc = begin() + (pc2 - begin());
510 bool GetOp(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet) const
512 return GetOp2(pc, opcodeRet, &vchRet);
515 bool GetOp(const_iterator& pc, opcodetype& opcodeRet) const
517 return GetOp2(pc, opcodeRet, NULL);
520 bool GetOp2(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet) const
522 opcodeRet = OP_INVALIDOPCODE;
531 unsigned int opcode = *pc++;
534 if (opcode <= OP_PUSHDATA4)
537 if (opcode < OP_PUSHDATA1)
541 else if (opcode == OP_PUSHDATA1)
547 else if (opcode == OP_PUSHDATA2)
552 memcpy(&nSize, &pc[0], 2);
555 else if (opcode == OP_PUSHDATA4)
559 memcpy(&nSize, &pc[0], 4);
562 if (end() - pc < nSize)
565 pvchRet->assign(pc, pc + nSize);
569 opcodeRet = (opcodetype)opcode;
574 void FindAndDelete(const CScript& b)
578 iterator pc = begin();
582 while (end() - pc >= b.size() && memcmp(&pc[0], &b[0], b.size()) == 0)
583 erase(pc, pc + b.size());
585 while (GetOp(pc, opcode));
589 int GetSigOpCount() const
592 const_iterator pc = begin();
596 if (!GetOp(pc, opcode))
598 if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
600 else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
607 bool IsPushOnly() const
611 const_iterator pc = begin();
615 if (!GetOp(pc, opcode))
624 uint160 GetBitcoinAddressHash160() const
627 std::vector<unsigned char> vch;
628 CScript::const_iterator pc = begin();
629 if (!GetOp(pc, opcode, vch) || opcode != OP_DUP) return 0;
630 if (!GetOp(pc, opcode, vch) || opcode != OP_HASH160) return 0;
631 if (!GetOp(pc, opcode, vch) || vch.size() != sizeof(uint160)) return 0;
632 uint160 hash160 = uint160(vch);
633 if (!GetOp(pc, opcode, vch) || opcode != OP_EQUALVERIFY) return 0;
634 if (!GetOp(pc, opcode, vch) || opcode != OP_CHECKSIG) return 0;
635 if (pc != end()) return 0;
639 std::string GetBitcoinAddress() const
641 uint160 hash160 = GetBitcoinAddressHash160();
644 return Hash160ToAddress(hash160);
647 void SetBitcoinAddress(const uint160& hash160)
650 *this << OP_DUP << OP_HASH160 << hash160 << OP_EQUALVERIFY << OP_CHECKSIG;
653 void SetBitcoinAddress(const std::vector<unsigned char>& vchPubKey)
655 SetBitcoinAddress(Hash160(vchPubKey));
658 bool SetBitcoinAddress(const std::string& strAddress)
662 if (!AddressToHash160(strAddress, hash160))
664 SetBitcoinAddress(hash160);
669 void PrintHex() const
671 printf("CScript(%s)\n", HexStr(begin(), end(), true).c_str());
674 std::string ToString() const
678 std::vector<unsigned char> vch;
679 const_iterator pc = begin();
684 if (!GetOp(pc, opcode, vch))
689 if (0 <= opcode && opcode <= OP_PUSHDATA4)
690 str += ValueString(vch);
692 str += GetOpName(opcode);
699 printf("%s\n", ToString().c_str());
710 uint256 SignatureHash(CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType);
711 bool IsStandard(const CScript& scriptPubKey);
712 bool IsMine(const CScript& scriptPubKey);
713 bool ExtractPubKey(const CScript& scriptPubKey, bool fMineOnly, std::vector<unsigned char>& vchPubKeyRet);
714 bool ExtractHash160(const CScript& scriptPubKey, uint160& hash160Ret);
715 bool SignSignature(const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL, CScript scriptPrereq=CScript());
716 bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, int nHashType=0);