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
13 #include <boost/foreach.hpp>
23 SIGHASH_ANYONECANPAY = 0x80,
30 // 'standard' transaction types:
37 const char* GetTxnOutputType(txnouttype t);
143 OP_GREATERTHANOREQUAL,
159 OP_CHECKMULTISIGVERIFY,
162 OP_EVAL, // Was OP_NOP1
177 // template matching params
178 OP_SMALLINTEGER = 0xfa,
180 OP_SCRIPTHASH = 0xfc,
181 OP_PUBKEYHASH = 0xfd,
184 OP_INVALIDOPCODE = 0xff,
187 const char* GetOpName(opcodetype opcode);
191 inline std::string ValueString(const std::vector<unsigned char>& vch)
194 return strprintf("%d", CBigNum(vch).getint());
199 inline std::string StackString(const std::vector<std::vector<unsigned char> >& vStack)
202 BOOST_FOREACH(const std::vector<unsigned char>& vch, vStack)
206 str += ValueString(vch);
219 class CScript : public std::vector<unsigned char>
222 CScript& push_int64(int64 n)
224 if (n == -1 || (n >= 1 && n <= 16))
226 push_back(n + (OP_1 - 1));
231 *this << bn.getvch();
236 CScript& push_uint64(uint64 n)
238 if (n >= 1 && n <= 16)
240 push_back(n + (OP_1 - 1));
245 *this << bn.getvch();
252 CScript(const CScript& b) : std::vector<unsigned char>(b.begin(), b.end()) { }
253 CScript(const_iterator pbegin, const_iterator pend) : std::vector<unsigned char>(pbegin, pend) { }
255 CScript(const unsigned char* pbegin, const unsigned char* pend) : std::vector<unsigned char>(pbegin, pend) { }
258 CScript& operator+=(const CScript& b)
260 insert(end(), b.begin(), b.end());
264 friend CScript operator+(const CScript& a, const CScript& b)
272 explicit CScript(char b) { operator<<(b); }
273 explicit CScript(short b) { operator<<(b); }
274 explicit CScript(int b) { operator<<(b); }
275 explicit CScript(long b) { operator<<(b); }
276 explicit CScript(int64 b) { operator<<(b); }
277 explicit CScript(unsigned char b) { operator<<(b); }
278 explicit CScript(unsigned int b) { operator<<(b); }
279 explicit CScript(unsigned short b) { operator<<(b); }
280 explicit CScript(unsigned long b) { operator<<(b); }
281 explicit CScript(uint64 b) { operator<<(b); }
283 explicit CScript(opcodetype b) { operator<<(b); }
284 explicit CScript(const uint256& b) { operator<<(b); }
285 explicit CScript(const CBigNum& b) { operator<<(b); }
286 explicit CScript(const std::vector<unsigned char>& b) { operator<<(b); }
289 CScript& operator<<(char b) { return push_int64(b); }
290 CScript& operator<<(short b) { return push_int64(b); }
291 CScript& operator<<(int b) { return push_int64(b); }
292 CScript& operator<<(long b) { return push_int64(b); }
293 CScript& operator<<(int64 b) { return push_int64(b); }
294 CScript& operator<<(unsigned char b) { return push_uint64(b); }
295 CScript& operator<<(unsigned int b) { return push_uint64(b); }
296 CScript& operator<<(unsigned short b) { return push_uint64(b); }
297 CScript& operator<<(unsigned long b) { return push_uint64(b); }
298 CScript& operator<<(uint64 b) { return push_uint64(b); }
300 CScript& operator<<(opcodetype opcode)
302 if (opcode < 0 || opcode > 0xff)
303 throw std::runtime_error("CScript::operator<<() : invalid opcode");
304 insert(end(), (unsigned char)opcode);
308 CScript& operator<<(const uint160& b)
310 insert(end(), sizeof(b));
311 insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
315 CScript& operator<<(const uint256& b)
317 insert(end(), sizeof(b));
318 insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
322 CScript& operator<<(const CBigNum& b)
328 CScript& operator<<(const std::vector<unsigned char>& b)
330 if (b.size() < OP_PUSHDATA1)
332 insert(end(), (unsigned char)b.size());
334 else if (b.size() <= 0xff)
336 insert(end(), OP_PUSHDATA1);
337 insert(end(), (unsigned char)b.size());
339 else if (b.size() <= 0xffff)
341 insert(end(), OP_PUSHDATA2);
342 unsigned short nSize = b.size();
343 insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
347 insert(end(), OP_PUSHDATA4);
348 unsigned int nSize = b.size();
349 insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
351 insert(end(), b.begin(), b.end());
355 CScript& operator<<(const CScript& b)
357 // I'm not sure if this should push the script or concatenate scripts.
358 // If there's ever a use for pushing a script onto a script, delete this member fn
359 assert(!"warning: pushing a CScript onto a CScript with << is probably not intended, use + to concatenate");
364 bool GetOp(iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet)
366 // Wrapper so it can be called with either iterator or const_iterator
367 const_iterator pc2 = pc;
368 bool fRet = GetOp2(pc2, opcodeRet, &vchRet);
369 pc = begin() + (pc2 - begin());
373 bool GetOp(iterator& pc, opcodetype& opcodeRet)
375 const_iterator pc2 = pc;
376 bool fRet = GetOp2(pc2, opcodeRet, NULL);
377 pc = begin() + (pc2 - begin());
381 bool GetOp(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet) const
383 return GetOp2(pc, opcodeRet, &vchRet);
386 bool GetOp(const_iterator& pc, opcodetype& opcodeRet) const
388 return GetOp2(pc, opcodeRet, NULL);
391 bool GetOp2(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet) const
393 opcodeRet = OP_INVALIDOPCODE;
402 unsigned int opcode = *pc++;
405 if (opcode <= OP_PUSHDATA4)
408 if (opcode < OP_PUSHDATA1)
412 else if (opcode == OP_PUSHDATA1)
418 else if (opcode == OP_PUSHDATA2)
423 memcpy(&nSize, &pc[0], 2);
426 else if (opcode == OP_PUSHDATA4)
430 memcpy(&nSize, &pc[0], 4);
433 if (end() - pc < nSize)
436 pvchRet->assign(pc, pc + nSize);
440 opcodeRet = (opcodetype)opcode;
444 // Encode/decode small integers:
445 static int DecodeOP_N(opcodetype opcode)
449 assert(opcode >= OP_1 && opcode <= OP_16);
450 return (int)opcode - (int)(OP_1 - 1);
452 static opcodetype EncodeOP_N(int n)
454 assert(n >= 0 && n <= 16);
457 return (opcodetype)(OP_1+n-1);
460 int FindAndDelete(const CScript& b)
465 iterator pc = begin();
469 while (end() - pc >= b.size() && memcmp(&pc[0], &b[0], b.size()) == 0)
471 erase(pc, pc + b.size());
475 while (GetOp(pc, opcode));
478 int Find(opcodetype op) const
482 for (const_iterator pc = begin(); pc != end() && GetOp(pc, opcode);)
488 // This method should be removed when a compatibility-breaking block chain split has passed.
489 // Compatibility method for old clients that count sigops differently:
490 int GetSigOpCount() const
493 const_iterator pc = begin();
497 if (!GetOp(pc, opcode))
499 if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
501 else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
507 // Called by CTransaction::IsStandard
508 bool IsPushOnly() const
510 const_iterator pc = begin();
514 if (!GetOp(pc, opcode))
523 void SetBitcoinAddress(const CBitcoinAddress& address);
524 void SetBitcoinAddress(const std::vector<unsigned char>& vchPubKey)
526 SetBitcoinAddress(CBitcoinAddress(vchPubKey));
528 void SetMultisig(int nRequired, const std::vector<CKey>& keys);
529 void SetEval(const CScript& subscript);
532 void PrintHex() const
534 printf("CScript(%s)\n", HexStr(begin(), end(), true).c_str());
537 std::string ToString() const
541 std::vector<unsigned char> vch;
542 const_iterator pc = begin();
547 if (!GetOp(pc, opcode, vch))
552 if (0 <= opcode && opcode <= OP_PUSHDATA4)
553 str += ValueString(vch);
555 str += GetOpName(opcode);
562 printf("%s\n", ToString().c_str());
570 bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, int nHashType, bool fStrictOpEval, int& nSigOpCountRet);
572 bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::vector<unsigned char> >& vSolutionsRet);
573 bool IsStandard(const CScript& scriptPubKey);
574 bool IsMine(const CKeyStore& keystore, const CScript& scriptPubKey);
575 bool ExtractAddress(const CScript& scriptPubKey, const CKeyStore* pkeystore, CBitcoinAddress& addressRet);
576 bool ExtractAddresses(const CScript& scriptPubKey, const CKeyStore* pkeystore, txnouttype& typeRet, std::vector<CBitcoinAddress>& addressRet, int& nRequiredRet);
577 bool SignSignature(const CKeyStore& keystore, const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL, CScript scriptPrereq=CScript());
578 bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, int& nSigOpCountRet, int nHashType=0, bool fStrictOpEval=true);