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
15 #include <boost/foreach.hpp>
25 SIGHASH_ANYONECANPAY = 0x80,
32 // 'standard' transaction types:
39 const char* GetTxnOutputType(txnouttype t);
145 OP_GREATERTHANOREQUAL,
161 OP_CHECKMULTISIGVERIFY,
164 OP_EVAL, // Was OP_NOP1
179 // template matching params
180 OP_SMALLINTEGER = 0xfa,
182 OP_SCRIPTHASH = 0xfc,
183 OP_PUBKEYHASH = 0xfd,
186 OP_INVALIDOPCODE = 0xff,
189 const char* GetOpName(opcodetype opcode);
193 inline std::string ValueString(const std::vector<unsigned char>& vch)
196 return strprintf("%d", CBigNum(vch).getint());
201 inline std::string StackString(const std::vector<std::vector<unsigned char> >& vStack)
204 BOOST_FOREACH(const std::vector<unsigned char>& vch, vStack)
208 str += ValueString(vch);
221 class CScript : public std::vector<unsigned char>
224 CScript& push_int64(int64_t n)
226 if (n == -1 || (n >= 1 && n <= 16))
228 push_back(n + (OP_1 - 1));
233 *this << bn.getvch();
238 CScript& push_uint64(uint64_t n)
240 if (n >= 1 && n <= 16)
242 push_back(n + (OP_1 - 1));
247 *this << bn.getvch();
254 CScript(const CScript& b) : std::vector<unsigned char>(b.begin(), b.end()) { }
255 CScript(const_iterator pbegin, const_iterator pend) : std::vector<unsigned char>(pbegin, pend) { }
257 CScript(const unsigned char* pbegin, const unsigned char* pend) : std::vector<unsigned char>(pbegin, pend) { }
260 CScript& operator+=(const CScript& b)
262 insert(end(), b.begin(), b.end());
266 friend CScript operator+(const CScript& a, const CScript& b)
274 explicit CScript(char b) { operator<<(b); }
275 explicit CScript(short b) { operator<<(b); }
276 explicit CScript(int b) { operator<<(b); }
277 explicit CScript(long b) { operator<<(b); }
278 explicit CScript(int64_t b) { operator<<(b); }
279 explicit CScript(unsigned char b) { operator<<(b); }
280 explicit CScript(unsigned int b) { operator<<(b); }
281 explicit CScript(unsigned short b) { operator<<(b); }
282 explicit CScript(unsigned long b) { operator<<(b); }
283 explicit CScript(uint64_t b) { operator<<(b); }
285 explicit CScript(opcodetype b) { operator<<(b); }
286 explicit CScript(const uint256& b) { operator<<(b); }
287 explicit CScript(const CBigNum& b) { operator<<(b); }
288 explicit CScript(const std::vector<unsigned char>& b) { operator<<(b); }
291 CScript& operator<<(char b) { return push_int64(b); }
292 CScript& operator<<(short b) { return push_int64(b); }
293 CScript& operator<<(int b) { return push_int64(b); }
294 CScript& operator<<(long b) { return push_int64(b); }
295 CScript& operator<<(int64_t b) { return push_int64(b); }
296 CScript& operator<<(unsigned char b) { return push_uint64(b); }
297 CScript& operator<<(unsigned int b) { return push_uint64(b); }
298 CScript& operator<<(unsigned short b) { return push_uint64(b); }
299 CScript& operator<<(unsigned long b) { return push_uint64(b); }
300 CScript& operator<<(uint64_t b) { return push_uint64(b); }
302 CScript& operator<<(opcodetype opcode)
304 if (opcode < 0 || opcode > 0xff)
305 throw std::runtime_error("CScript::operator<<() : invalid opcode");
306 insert(end(), (unsigned char)opcode);
310 CScript& operator<<(const uint160& b)
312 insert(end(), sizeof(b));
313 insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
317 CScript& operator<<(const uint256& b)
319 insert(end(), sizeof(b));
320 insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
324 CScript& operator<<(const CBigNum& b)
330 CScript& operator<<(const std::vector<unsigned char>& b)
332 if (b.size() < OP_PUSHDATA1)
334 insert(end(), (unsigned char)b.size());
336 else if (b.size() <= 0xff)
338 insert(end(), OP_PUSHDATA1);
339 insert(end(), (unsigned char)b.size());
341 else if (b.size() <= 0xffff)
343 insert(end(), OP_PUSHDATA2);
344 unsigned short nSize = b.size();
345 insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
349 insert(end(), OP_PUSHDATA4);
350 unsigned int nSize = b.size();
351 insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
353 insert(end(), b.begin(), b.end());
357 CScript& operator<<(const CScript& b)
359 // I'm not sure if this should push the script or concatenate scripts.
360 // If there's ever a use for pushing a script onto a script, delete this member fn
361 assert(!"warning: pushing a CScript onto a CScript with << is probably not intended, use + to concatenate");
366 bool GetOp(iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet)
368 // Wrapper so it can be called with either iterator or const_iterator
369 const_iterator pc2 = pc;
370 bool fRet = GetOp2(pc2, opcodeRet, &vchRet);
371 pc = begin() + (pc2 - begin());
375 bool GetOp(iterator& pc, opcodetype& opcodeRet)
377 const_iterator pc2 = pc;
378 bool fRet = GetOp2(pc2, opcodeRet, NULL);
379 pc = begin() + (pc2 - begin());
383 bool GetOp(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet) const
385 return GetOp2(pc, opcodeRet, &vchRet);
388 bool GetOp(const_iterator& pc, opcodetype& opcodeRet) const
390 return GetOp2(pc, opcodeRet, NULL);
393 bool GetOp2(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet) const
395 opcodeRet = OP_INVALIDOPCODE;
404 unsigned int opcode = *pc++;
407 if (opcode <= OP_PUSHDATA4)
410 if (opcode < OP_PUSHDATA1)
414 else if (opcode == OP_PUSHDATA1)
420 else if (opcode == OP_PUSHDATA2)
425 memcpy(&nSize, &pc[0], 2);
428 else if (opcode == OP_PUSHDATA4)
432 memcpy(&nSize, &pc[0], 4);
435 if (end() - pc < nSize)
438 pvchRet->assign(pc, pc + nSize);
442 opcodeRet = (opcodetype)opcode;
446 // Encode/decode small integers:
447 static int DecodeOP_N(opcodetype opcode)
451 assert(opcode >= OP_1 && opcode <= OP_16);
452 return (int)opcode - (int)(OP_1 - 1);
454 static opcodetype EncodeOP_N(int n)
456 assert(n >= 0 && n <= 16);
459 return (opcodetype)(OP_1+n-1);
462 int FindAndDelete(const CScript& b)
467 iterator pc = begin();
471 while (end() - pc >= b.size() && memcmp(&pc[0], &b[0], b.size()) == 0)
473 erase(pc, pc + b.size());
477 while (GetOp(pc, opcode));
480 int Find(opcodetype op) const
484 for (const_iterator pc = begin(); pc != end() && GetOp(pc, opcode);)
490 // This method should be removed when a compatibility-breaking block chain split has passed.
491 // Compatibility method for old clients that count sigops differently:
492 int GetSigOpCount() const
495 const_iterator pc = begin();
499 if (!GetOp(pc, opcode))
501 if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
503 else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
509 // Called by CTransaction::IsStandard
510 bool IsPushOnly() const
512 const_iterator pc = begin();
516 if (!GetOp(pc, opcode))
525 void SetBitcoinAddress(const CBitcoinAddress& address);
526 void SetBitcoinAddress(const std::vector<unsigned char>& vchPubKey)
528 SetBitcoinAddress(CBitcoinAddress(vchPubKey));
530 void SetMultisig(int nRequired, const std::vector<CKey>& keys);
531 void SetEval(const CScript& subscript);
534 void PrintHex() const
536 printf("CScript(%s)\n", HexStr(begin(), end(), true).c_str());
539 std::string ToString() const
543 std::vector<unsigned char> vch;
544 const_iterator pc = begin();
549 if (!GetOp(pc, opcode, vch))
554 if (0 <= opcode && opcode <= OP_PUSHDATA4)
555 str += ValueString(vch);
557 str += GetOpName(opcode);
564 printf("%s\n", ToString().c_str());
572 bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, int nHashType, bool fStrictOpEval, int& nSigOpCountRet);
574 bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::vector<unsigned char> >& vSolutionsRet);
575 bool IsStandard(const CScript& scriptPubKey);
576 bool IsMine(const CKeyStore& keystore, const CScript& scriptPubKey);
577 bool ExtractAddress(const CScript& scriptPubKey, const CKeyStore* pkeystore, CBitcoinAddress& addressRet);
578 bool ExtractAddresses(const CScript& scriptPubKey, const CKeyStore* pkeystore, txnouttype& typeRet, std::vector<CBitcoinAddress>& addressRet, int& nRequiredRet);
579 bool SignSignature(const CKeyStore& keystore, const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL, CScript scriptPrereq=CScript());
580 bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, int& nSigOpCountRet, int nHashType=0, bool fStrictOpEval=true);