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:
42 class CNoDestination {
44 friend bool operator==(const CNoDestination &a, const CNoDestination &b) { return true; }
45 friend bool operator<(const CNoDestination &a, const CNoDestination &b) { return true; }
48 /** A txout script template with a specific destination. It is either:
49 * * CNoDestination: no destination set
50 * * CKeyID: TX_PUBKEYHASH destination
51 * * CScriptID: TX_SCRIPTHASH destination
52 * A CTxDestination is the internal data type encoded in a CBitcoinAddress
54 typedef boost::variant<CNoDestination, CKeyID, CScriptID> CTxDestination;
56 const char* GetTxnOutputType(txnouttype t);
100 OP_TOALTSTACK = 0x6b,
101 OP_FROMALTSTACK = 0x6c,
133 OP_EQUALVERIFY = 0x88,
158 OP_NUMEQUALVERIFY = 0x9d,
159 OP_NUMNOTEQUAL = 0x9e,
161 OP_GREATERTHAN = 0xa0,
162 OP_LESSTHANOREQUAL = 0xa1,
163 OP_GREATERTHANOREQUAL = 0xa2,
175 OP_CODESEPARATOR = 0xab,
177 OP_CHECKSIGVERIFY = 0xad,
178 OP_CHECKMULTISIG = 0xae,
179 OP_CHECKMULTISIGVERIFY = 0xaf,
195 // template matching params
197 OP_SMALLINTEGER = 0xfa,
199 OP_PUBKEYHASH = 0xfd,
202 OP_INVALIDOPCODE = 0xff,
205 const char* GetOpName(opcodetype opcode);
209 inline std::string ValueString(const std::vector<unsigned char>& vch)
212 return strprintf("%d", CBigNum(vch).getint());
217 inline std::string StackString(const std::vector<std::vector<unsigned char> >& vStack)
220 BOOST_FOREACH(const std::vector<unsigned char>& vch, vStack)
224 str += ValueString(vch);
236 /** Serialized script, used inside transaction inputs and outputs */
237 class CScript : public std::vector<unsigned char>
240 CScript& push_int64(int64 n)
242 if (n == -1 || (n >= 1 && n <= 16))
244 push_back(n + (OP_1 - 1));
249 *this << bn.getvch();
254 CScript& push_uint64(uint64 n)
256 if (n >= 1 && n <= 16)
258 push_back(n + (OP_1 - 1));
263 *this << bn.getvch();
270 CScript(const CScript& b) : std::vector<unsigned char>(b.begin(), b.end()) { }
271 CScript(const_iterator pbegin, const_iterator pend) : std::vector<unsigned char>(pbegin, pend) { }
273 CScript(const unsigned char* pbegin, const unsigned char* pend) : std::vector<unsigned char>(pbegin, pend) { }
276 CScript& operator+=(const CScript& b)
278 insert(end(), b.begin(), b.end());
282 friend CScript operator+(const CScript& a, const CScript& b)
290 //explicit CScript(char b) is not portable. Use 'signed char' or 'unsigned char'.
291 explicit CScript(signed char b) { operator<<(b); }
292 explicit CScript(short b) { operator<<(b); }
293 explicit CScript(int b) { operator<<(b); }
294 explicit CScript(long b) { operator<<(b); }
295 explicit CScript(int64 b) { operator<<(b); }
296 explicit CScript(unsigned char b) { operator<<(b); }
297 explicit CScript(unsigned int b) { operator<<(b); }
298 explicit CScript(unsigned short b) { operator<<(b); }
299 explicit CScript(unsigned long b) { operator<<(b); }
300 explicit CScript(uint64 b) { operator<<(b); }
302 explicit CScript(opcodetype b) { operator<<(b); }
303 explicit CScript(const uint256& b) { operator<<(b); }
304 explicit CScript(const CBigNum& b) { operator<<(b); }
305 explicit CScript(const std::vector<unsigned char>& b) { operator<<(b); }
308 //CScript& operator<<(char b) is not portable. Use 'signed char' or 'unsigned char'.
309 CScript& operator<<(signed char b) { return push_int64(b); }
310 CScript& operator<<(short b) { return push_int64(b); }
311 CScript& operator<<(int b) { return push_int64(b); }
312 CScript& operator<<(long b) { return push_int64(b); }
313 CScript& operator<<(int64 b) { return push_int64(b); }
314 CScript& operator<<(unsigned char b) { return push_uint64(b); }
315 CScript& operator<<(unsigned int b) { return push_uint64(b); }
316 CScript& operator<<(unsigned short b) { return push_uint64(b); }
317 CScript& operator<<(unsigned long b) { return push_uint64(b); }
318 CScript& operator<<(uint64 b) { return push_uint64(b); }
320 CScript& operator<<(opcodetype opcode)
322 if (opcode < 0 || opcode > 0xff)
323 throw std::runtime_error("CScript::operator<<() : invalid opcode");
324 insert(end(), (unsigned char)opcode);
328 CScript& operator<<(const uint160& b)
330 insert(end(), sizeof(b));
331 insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
335 CScript& operator<<(const uint256& b)
337 insert(end(), sizeof(b));
338 insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
342 CScript& operator<<(const CPubKey& key)
344 std::vector<unsigned char> vchKey = key.Raw();
345 return (*this) << vchKey;
348 CScript& operator<<(const CBigNum& b)
354 CScript& operator<<(const std::vector<unsigned char>& b)
356 if (b.size() < OP_PUSHDATA1)
358 insert(end(), (unsigned char)b.size());
360 else if (b.size() <= 0xff)
362 insert(end(), OP_PUSHDATA1);
363 insert(end(), (unsigned char)b.size());
365 else if (b.size() <= 0xffff)
367 insert(end(), OP_PUSHDATA2);
368 unsigned short nSize = b.size();
369 insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
373 insert(end(), OP_PUSHDATA4);
374 unsigned int nSize = b.size();
375 insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
377 insert(end(), b.begin(), b.end());
381 CScript& operator<<(const CScript& b)
383 // I'm not sure if this should push the script or concatenate scripts.
384 // If there's ever a use for pushing a script onto a script, delete this member fn
385 assert(!"Warning: Pushing a CScript onto a CScript with << is probably not intended, use + to concatenate!");
390 bool GetOp(iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet)
392 // Wrapper so it can be called with either iterator or const_iterator
393 const_iterator pc2 = pc;
394 bool fRet = GetOp2(pc2, opcodeRet, &vchRet);
395 pc = begin() + (pc2 - begin());
399 bool GetOp(iterator& pc, opcodetype& opcodeRet)
401 const_iterator pc2 = pc;
402 bool fRet = GetOp2(pc2, opcodeRet, NULL);
403 pc = begin() + (pc2 - begin());
407 bool GetOp(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet) const
409 return GetOp2(pc, opcodeRet, &vchRet);
412 bool GetOp(const_iterator& pc, opcodetype& opcodeRet) const
414 return GetOp2(pc, opcodeRet, NULL);
417 bool GetOp2(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet) const
419 opcodeRet = OP_INVALIDOPCODE;
428 unsigned int opcode = *pc++;
431 if (opcode <= OP_PUSHDATA4)
434 if (opcode < OP_PUSHDATA1)
438 else if (opcode == OP_PUSHDATA1)
444 else if (opcode == OP_PUSHDATA2)
449 memcpy(&nSize, &pc[0], 2);
452 else if (opcode == OP_PUSHDATA4)
456 memcpy(&nSize, &pc[0], 4);
459 if (end() - pc < 0 || (unsigned int)(end() - pc) < nSize)
462 pvchRet->assign(pc, pc + nSize);
466 opcodeRet = (opcodetype)opcode;
470 // Encode/decode small integers:
471 static int DecodeOP_N(opcodetype opcode)
475 assert(opcode >= OP_1 && opcode <= OP_16);
476 return (int)opcode - (int)(OP_1 - 1);
478 static opcodetype EncodeOP_N(int n)
480 assert(n >= 0 && n <= 16);
483 return (opcodetype)(OP_1+n-1);
486 int FindAndDelete(const CScript& b)
491 iterator pc = begin();
495 while (end() - pc >= (long)b.size() && memcmp(&pc[0], &b[0], b.size()) == 0)
497 erase(pc, pc + b.size());
501 while (GetOp(pc, opcode));
504 int Find(opcodetype op) const
508 for (const_iterator pc = begin(); pc != end() && GetOp(pc, opcode);)
514 // Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs
515 // as 20 sigops. With pay-to-script-hash, that changed:
516 // CHECKMULTISIGs serialized in scriptSigs are
517 // counted more accurately, assuming they are of the form
518 // ... OP_N CHECKMULTISIG ...
519 unsigned int GetSigOpCount(bool fAccurate) const;
521 // Accurately count sigOps, including sigOps in
522 // pay-to-script-hash transactions:
523 unsigned int GetSigOpCount(const CScript& scriptSig) const;
525 bool IsPayToScriptHash() const;
527 // Called by CTransaction::IsStandard and P2SH VerifyScript (which makes it consensus-critical).
528 bool IsPushOnly() const
530 const_iterator pc = begin();
534 if (!GetOp(pc, opcode))
542 // Called by CTransaction::IsStandard.
543 bool HasCanonicalPushes() const;
545 void SetDestination(const CTxDestination& address);
546 void SetMultisig(int nRequired, const std::vector<CKey>& keys);
549 void PrintHex() const
551 printf("CScript(%s)\n", HexStr(begin(), end(), true).c_str());
554 std::string ToString(bool fShort=false) const
558 std::vector<unsigned char> vch;
559 const_iterator pc = begin();
564 if (!GetOp(pc, opcode, vch))
569 if (0 <= opcode && opcode <= OP_PUSHDATA4)
570 str += fShort? ValueString(vch).substr(0, 10) : ValueString(vch);
572 str += GetOpName(opcode);
579 printf("%s\n", ToString().c_str());
582 CScriptID GetID() const
584 return CScriptID(Hash160(*this));
592 bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, int nHashType);
593 bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::vector<unsigned char> >& vSolutionsRet);
594 int ScriptSigArgsExpected(txnouttype t, const std::vector<std::vector<unsigned char> >& vSolutions);
595 bool IsStandard(const CScript& scriptPubKey, txnouttype& whichType);
596 bool IsMine(const CKeyStore& keystore, const CScript& scriptPubKey);
597 bool IsMine(const CKeyStore& keystore, const CTxDestination &dest);
598 void ExtractAffectedKeys(const CKeyStore &keystore, const CScript& scriptPubKey, std::vector<CKeyID> &vKeys);
599 bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet);
600 bool ExtractDestinations(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<CTxDestination>& addressRet, int& nRequiredRet);
601 bool SignSignature(const CKeyStore& keystore, const CScript& fromPubKey, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL);
602 bool SignSignature(const CKeyStore& keystore, const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL);
603 bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn,
604 bool fValidatePayToScriptHash, int nHashType);
605 bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, bool fValidatePayToScriptHash, int nHashType);
607 // Given two sets of signatures for scriptPubKey, possibly with OP_0 placeholders,
608 // combine them intelligently and return the result.
609 CScript CombineSignatures(CScript scriptPubKey, const CTransaction& txTo, unsigned int nIn, const CScript& scriptSig1, const CScript& scriptSig2);