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 #ifndef H_BITCOIN_SCRIPT
6 #define H_BITCOIN_SCRIPT
11 #include <boost/foreach.hpp>
12 #include <boost/variant.hpp>
17 typedef std::vector<unsigned char> valtype;
21 /** Signature hash types/flags */
27 SIGHASH_ANYONECANPAY = 0x80,
34 // 'standard' transaction types:
41 class CNoDestination {
43 friend bool operator==(const CNoDestination &a, const CNoDestination &b) { return true; }
44 friend bool operator<(const CNoDestination &a, const CNoDestination &b) { return true; }
47 /** A txout script template with a specific destination. It is either:
48 * * CNoDestination: no destination set
49 * * CKeyID: TX_PUBKEYHASH destination
50 * * CScriptID: TX_SCRIPTHASH destination
51 * A CTxDestination is the internal data type encoded in a CBitcoinAddress
53 typedef boost::variant<CNoDestination, CKeyID, CScriptID> CTxDestination;
55 const char* GetTxnOutputType(txnouttype t);
100 OP_FROMALTSTACK = 0x6c,
132 OP_EQUALVERIFY = 0x88,
157 OP_NUMEQUALVERIFY = 0x9d,
158 OP_NUMNOTEQUAL = 0x9e,
160 OP_GREATERTHAN = 0xa0,
161 OP_LESSTHANOREQUAL = 0xa1,
162 OP_GREATERTHANOREQUAL = 0xa2,
174 OP_CODESEPARATOR = 0xab,
176 OP_CHECKSIGVERIFY = 0xad,
177 OP_CHECKMULTISIG = 0xae,
178 OP_CHECKMULTISIGVERIFY = 0xaf,
194 // template matching params
195 OP_SMALLINTEGER = 0xfa,
197 OP_PUBKEYHASH = 0xfd,
200 OP_INVALIDOPCODE = 0xff,
203 const char* GetOpName(opcodetype opcode);
207 inline std::string ValueString(const std::vector<unsigned char>& vch)
210 return strprintf("%d", CBigNum(vch).getint());
215 inline std::string StackString(const std::vector<std::vector<unsigned char> >& vStack)
218 BOOST_FOREACH(const std::vector<unsigned char>& vch, vStack)
222 str += ValueString(vch);
234 /** Serialized script, used inside transaction inputs and outputs */
235 class CScript : public std::vector<unsigned char>
238 CScript& push_int64(int64 n)
240 if (n == -1 || (n >= 1 && n <= 16))
242 push_back(n + (OP_1 - 1));
247 *this << bn.getvch();
252 CScript& push_uint64(uint64 n)
254 if (n >= 1 && n <= 16)
256 push_back(n + (OP_1 - 1));
261 *this << bn.getvch();
268 CScript(const CScript& b) : std::vector<unsigned char>(b.begin(), b.end()) { }
269 CScript(const_iterator pbegin, const_iterator pend) : std::vector<unsigned char>(pbegin, pend) { }
271 CScript(const unsigned char* pbegin, const unsigned char* pend) : std::vector<unsigned char>(pbegin, pend) { }
274 CScript& operator+=(const CScript& b)
276 insert(end(), b.begin(), b.end());
280 friend CScript operator+(const CScript& a, const CScript& b)
288 //explicit CScript(char b) is not portable. Use 'signed char' or 'unsigned char'.
289 explicit CScript(signed char b) { operator<<(b); }
290 explicit CScript(short b) { operator<<(b); }
291 explicit CScript(int b) { operator<<(b); }
292 explicit CScript(long b) { operator<<(b); }
293 explicit CScript(int64 b) { operator<<(b); }
294 explicit CScript(unsigned char b) { operator<<(b); }
295 explicit CScript(unsigned int b) { operator<<(b); }
296 explicit CScript(unsigned short b) { operator<<(b); }
297 explicit CScript(unsigned long b) { operator<<(b); }
298 explicit CScript(uint64 b) { operator<<(b); }
300 explicit CScript(opcodetype b) { operator<<(b); }
301 explicit CScript(const uint256& b) { operator<<(b); }
302 explicit CScript(const CBigNum& b) { operator<<(b); }
303 explicit CScript(const std::vector<unsigned char>& b) { operator<<(b); }
306 //CScript& operator<<(char b) is not portable. Use 'signed char' or 'unsigned char'.
307 CScript& operator<<(signed char b) { return push_int64(b); }
308 CScript& operator<<(short b) { return push_int64(b); }
309 CScript& operator<<(int b) { return push_int64(b); }
310 CScript& operator<<(long b) { return push_int64(b); }
311 CScript& operator<<(int64 b) { return push_int64(b); }
312 CScript& operator<<(unsigned char b) { return push_uint64(b); }
313 CScript& operator<<(unsigned int b) { return push_uint64(b); }
314 CScript& operator<<(unsigned short b) { return push_uint64(b); }
315 CScript& operator<<(unsigned long b) { return push_uint64(b); }
316 CScript& operator<<(uint64 b) { return push_uint64(b); }
318 CScript& operator<<(opcodetype opcode)
320 if (opcode < 0 || opcode > 0xff)
321 throw std::runtime_error("CScript::operator<<() : invalid opcode");
322 insert(end(), (unsigned char)opcode);
326 CScript& operator<<(const uint160& b)
328 insert(end(), sizeof(b));
329 insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
333 CScript& operator<<(const uint256& b)
335 insert(end(), sizeof(b));
336 insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
340 CScript& operator<<(const CPubKey& key)
342 std::vector<unsigned char> vchKey = key.Raw();
343 return (*this) << vchKey;
346 CScript& operator<<(const CBigNum& b)
352 CScript& operator<<(const std::vector<unsigned char>& b)
354 if (b.size() < OP_PUSHDATA1)
356 insert(end(), (unsigned char)b.size());
358 else if (b.size() <= 0xff)
360 insert(end(), OP_PUSHDATA1);
361 insert(end(), (unsigned char)b.size());
363 else if (b.size() <= 0xffff)
365 insert(end(), OP_PUSHDATA2);
366 unsigned short nSize = b.size();
367 insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
371 insert(end(), OP_PUSHDATA4);
372 unsigned int nSize = b.size();
373 insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
375 insert(end(), b.begin(), b.end());
379 CScript& operator<<(const CScript& b)
381 // I'm not sure if this should push the script or concatenate scripts.
382 // If there's ever a use for pushing a script onto a script, delete this member fn
383 assert(!"Warning: Pushing a CScript onto a CScript with << is probably not intended, use + to concatenate!");
388 bool GetOp(iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet)
390 // Wrapper so it can be called with either iterator or const_iterator
391 const_iterator pc2 = pc;
392 bool fRet = GetOp2(pc2, opcodeRet, &vchRet);
393 pc = begin() + (pc2 - begin());
397 bool GetOp(iterator& pc, opcodetype& opcodeRet)
399 const_iterator pc2 = pc;
400 bool fRet = GetOp2(pc2, opcodeRet, NULL);
401 pc = begin() + (pc2 - begin());
405 bool GetOp(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet) const
407 return GetOp2(pc, opcodeRet, &vchRet);
410 bool GetOp(const_iterator& pc, opcodetype& opcodeRet) const
412 return GetOp2(pc, opcodeRet, NULL);
415 bool GetOp2(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet) const
417 opcodeRet = OP_INVALIDOPCODE;
426 unsigned int opcode = *pc++;
429 if (opcode <= OP_PUSHDATA4)
432 if (opcode < OP_PUSHDATA1)
436 else if (opcode == OP_PUSHDATA1)
442 else if (opcode == OP_PUSHDATA2)
447 memcpy(&nSize, &pc[0], 2);
450 else if (opcode == OP_PUSHDATA4)
454 memcpy(&nSize, &pc[0], 4);
457 if (end() - pc < 0 || (unsigned int)(end() - pc) < nSize)
460 pvchRet->assign(pc, pc + nSize);
464 opcodeRet = (opcodetype)opcode;
468 // Encode/decode small integers:
469 static int DecodeOP_N(opcodetype opcode)
473 assert(opcode >= OP_1 && opcode <= OP_16);
474 return (int)opcode - (int)(OP_1 - 1);
476 static opcodetype EncodeOP_N(int n)
478 assert(n >= 0 && n <= 16);
481 return (opcodetype)(OP_1+n-1);
484 int FindAndDelete(const CScript& b)
489 iterator pc = begin();
493 while (end() - pc >= (long)b.size() && memcmp(&pc[0], &b[0], b.size()) == 0)
495 erase(pc, pc + b.size());
499 while (GetOp(pc, opcode));
502 int Find(opcodetype op) const
506 for (const_iterator pc = begin(); pc != end() && GetOp(pc, opcode);)
512 // Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs
513 // as 20 sigops. With pay-to-script-hash, that changed:
514 // CHECKMULTISIGs serialized in scriptSigs are
515 // counted more accurately, assuming they are of the form
516 // ... OP_N CHECKMULTISIG ...
517 unsigned int GetSigOpCount(bool fAccurate) const;
519 // Accurately count sigOps, including sigOps in
520 // pay-to-script-hash transactions:
521 unsigned int GetSigOpCount(const CScript& scriptSig) const;
523 bool IsPayToScriptHash() const;
525 // Called by CTransaction::IsStandard and P2SH VerifyScript (which makes it consensus-critical).
526 bool IsPushOnly() const
528 const_iterator pc = begin();
532 if (!GetOp(pc, opcode))
540 // Called by CTransaction::IsStandard.
541 bool HasCanonicalPushes() const;
543 void SetDestination(const CTxDestination& address);
544 void SetMultisig(int nRequired, const std::vector<CKey>& keys);
547 void PrintHex() const
549 printf("CScript(%s)\n", HexStr(begin(), end(), true).c_str());
552 std::string ToString(bool fShort=false) const
556 std::vector<unsigned char> vch;
557 const_iterator pc = begin();
562 if (!GetOp(pc, opcode, vch))
567 if (0 <= opcode && opcode <= OP_PUSHDATA4)
568 str += fShort? ValueString(vch).substr(0, 10) : ValueString(vch);
570 str += GetOpName(opcode);
577 printf("%s\n", ToString().c_str());
580 CScriptID GetID() const
582 return CScriptID(Hash160(*this));
590 bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, int nHashType);
591 bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::vector<unsigned char> >& vSolutionsRet);
592 int ScriptSigArgsExpected(txnouttype t, const std::vector<std::vector<unsigned char> >& vSolutions);
593 bool IsStandard(const CScript& scriptPubKey);
594 bool IsMine(const CKeyStore& keystore, const CScript& scriptPubKey);
595 bool IsMine(const CKeyStore& keystore, const CTxDestination &dest);
596 void ExtractAffectedKeys(const CKeyStore &keystore, const CScript& scriptPubKey, std::vector<CKeyID> &vKeys);
597 bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet);
598 bool ExtractDestinations(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<CTxDestination>& addressRet, int& nRequiredRet);
599 bool SignSignature(const CKeyStore& keystore, const CScript& fromPubKey, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL);
600 bool SignSignature(const CKeyStore& keystore, const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL);
601 bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn,
602 bool fValidatePayToScriptHash, int nHashType);
603 bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, bool fValidatePayToScriptHash, int nHashType);
605 // Given two sets of signatures for scriptPubKey, possibly with OP_0 placeholders,
606 // combine them intelligently and return the result.
607 CScript CombineSignatures(CScript scriptPubKey, const CTransaction& txTo, unsigned int nIn, const CScript& scriptSig1, const CScript& scriptSig2);