1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Copyright (c) 2012 The PPCoin developers
4 // Distributed under the MIT/X11 software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #ifndef H_BITCOIN_SCRIPT
7 #define H_BITCOIN_SCRIPT
14 #include <boost/foreach.hpp>
16 typedef std::vector<unsigned char> valtype;
21 /** Signature hash types/flags */
27 SIGHASH_ANYONECANPAY = 0x80,
34 // 'standard' transaction types:
41 const char* GetTxnOutputType(txnouttype t);
86 OP_FROMALTSTACK = 0x6c,
118 OP_EQUALVERIFY = 0x88,
143 OP_NUMEQUALVERIFY = 0x9d,
144 OP_NUMNOTEQUAL = 0x9e,
146 OP_GREATERTHAN = 0xa0,
147 OP_LESSTHANOREQUAL = 0xa1,
148 OP_GREATERTHANOREQUAL = 0xa2,
160 OP_CODESEPARATOR = 0xab,
162 OP_CHECKSIGVERIFY = 0xad,
163 OP_CHECKMULTISIG = 0xae,
164 OP_CHECKMULTISIGVERIFY = 0xaf,
180 // template matching params
181 OP_SMALLINTEGER = 0xfa,
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);
220 /** Serialized script, used inside transaction inputs and outputs */
221 class CScript : public std::vector<unsigned char>
224 CScript& push_int64(int64 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 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) is not portable. Use 'signed char' or 'unsigned char'.
275 explicit CScript(signed char b) { operator<<(b); }
276 explicit CScript(short b) { operator<<(b); }
277 explicit CScript(int b) { operator<<(b); }
278 explicit CScript(long b) { operator<<(b); }
279 explicit CScript(int64 b) { operator<<(b); }
280 explicit CScript(unsigned char b) { operator<<(b); }
281 explicit CScript(unsigned int b) { operator<<(b); }
282 explicit CScript(unsigned short b) { operator<<(b); }
283 explicit CScript(unsigned long b) { operator<<(b); }
284 explicit CScript(uint64 b) { operator<<(b); }
286 explicit CScript(opcodetype b) { operator<<(b); }
287 explicit CScript(const uint256& b) { operator<<(b); }
288 explicit CScript(const CBigNum& b) { operator<<(b); }
289 explicit CScript(const std::vector<unsigned char>& b) { operator<<(b); }
292 //CScript& operator<<(char b) is not portable. Use 'signed char' or 'unsigned char'.
293 CScript& operator<<(signed char b) { return push_int64(b); }
294 CScript& operator<<(short b) { return push_int64(b); }
295 CScript& operator<<(int b) { return push_int64(b); }
296 CScript& operator<<(long b) { return push_int64(b); }
297 CScript& operator<<(int64 b) { return push_int64(b); }
298 CScript& operator<<(unsigned char b) { return push_uint64(b); }
299 CScript& operator<<(unsigned int b) { return push_uint64(b); }
300 CScript& operator<<(unsigned short b) { return push_uint64(b); }
301 CScript& operator<<(unsigned long b) { return push_uint64(b); }
302 CScript& operator<<(uint64 b) { return push_uint64(b); }
304 CScript& operator<<(opcodetype opcode)
306 if (opcode < 0 || opcode > 0xff)
307 throw std::runtime_error("CScript::operator<<() : invalid opcode");
308 insert(end(), (unsigned char)opcode);
312 CScript& operator<<(const uint160& b)
314 insert(end(), sizeof(b));
315 insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
319 CScript& operator<<(const uint256& b)
321 insert(end(), sizeof(b));
322 insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
326 CScript& operator<<(const CBigNum& b)
332 CScript& operator<<(const std::vector<unsigned char>& b)
334 if (b.size() < OP_PUSHDATA1)
336 insert(end(), (unsigned char)b.size());
338 else if (b.size() <= 0xff)
340 insert(end(), OP_PUSHDATA1);
341 insert(end(), (unsigned char)b.size());
343 else if (b.size() <= 0xffff)
345 insert(end(), OP_PUSHDATA2);
346 unsigned short nSize = b.size();
347 insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
351 insert(end(), OP_PUSHDATA4);
352 unsigned int nSize = b.size();
353 insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
355 insert(end(), b.begin(), b.end());
359 CScript& operator<<(const CScript& b)
361 // I'm not sure if this should push the script or concatenate scripts.
362 // If there's ever a use for pushing a script onto a script, delete this member fn
363 assert(!"warning: pushing a CScript onto a CScript with << is probably not intended, use + to concatenate");
368 bool GetOp(iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet)
370 // Wrapper so it can be called with either iterator or const_iterator
371 const_iterator pc2 = pc;
372 bool fRet = GetOp2(pc2, opcodeRet, &vchRet);
373 pc = begin() + (pc2 - begin());
377 bool GetOp(iterator& pc, opcodetype& opcodeRet)
379 const_iterator pc2 = pc;
380 bool fRet = GetOp2(pc2, opcodeRet, NULL);
381 pc = begin() + (pc2 - begin());
385 bool GetOp(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet) const
387 return GetOp2(pc, opcodeRet, &vchRet);
390 bool GetOp(const_iterator& pc, opcodetype& opcodeRet) const
392 return GetOp2(pc, opcodeRet, NULL);
395 bool GetOp2(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet) const
397 opcodeRet = OP_INVALIDOPCODE;
406 unsigned int opcode = *pc++;
409 if (opcode <= OP_PUSHDATA4)
412 if (opcode < OP_PUSHDATA1)
416 else if (opcode == OP_PUSHDATA1)
422 else if (opcode == OP_PUSHDATA2)
427 memcpy(&nSize, &pc[0], 2);
430 else if (opcode == OP_PUSHDATA4)
434 memcpy(&nSize, &pc[0], 4);
437 if (end() - pc < nSize)
440 pvchRet->assign(pc, pc + nSize);
444 opcodeRet = (opcodetype)opcode;
448 // Encode/decode small integers:
449 static int DecodeOP_N(opcodetype opcode)
453 assert(opcode >= OP_1 && opcode <= OP_16);
454 return (int)opcode - (int)(OP_1 - 1);
456 static opcodetype EncodeOP_N(int n)
458 assert(n >= 0 && n <= 16);
461 return (opcodetype)(OP_1+n-1);
464 int FindAndDelete(const CScript& b)
469 iterator pc = begin();
473 while (end() - pc >= (long)b.size() && memcmp(&pc[0], &b[0], b.size()) == 0)
475 erase(pc, pc + b.size());
479 while (GetOp(pc, opcode));
482 int Find(opcodetype op) const
486 for (const_iterator pc = begin(); pc != end() && GetOp(pc, opcode);)
492 // Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs
493 // as 20 sigops. With pay-to-script-hash, that changed:
494 // CHECKMULTISIGs serialized in scriptSigs are
495 // counted more accurately, assuming they are of the form
496 // ... OP_N CHECKMULTISIG ...
497 unsigned int GetSigOpCount(bool fAccurate) const;
499 // Accurately count sigOps, including sigOps in
500 // pay-to-script-hash transactions:
501 unsigned int GetSigOpCount(const CScript& scriptSig) const;
503 bool IsPayToScriptHash() const;
505 // Called by CTransaction::IsStandard
506 bool IsPushOnly() const
508 const_iterator pc = begin();
512 if (!GetOp(pc, opcode))
521 void SetBitcoinAddress(const CBitcoinAddress& address);
522 void SetBitcoinAddress(const std::vector<unsigned char>& vchPubKey)
524 SetBitcoinAddress(CBitcoinAddress(vchPubKey));
526 void SetMultisig(int nRequired, const std::vector<CKey>& keys);
527 void SetPayToScriptHash(const CScript& subscript);
530 void PrintHex() const
532 printf("CScript(%s)\n", HexStr(begin(), end(), true).c_str());
535 std::string ToString(bool fShort=false) const
539 std::vector<unsigned char> vch;
540 const_iterator pc = begin();
545 if (!GetOp(pc, opcode, vch))
550 if (0 <= opcode && opcode <= OP_PUSHDATA4)
551 str += fShort? ValueString(vch).substr(0, 10) : ValueString(vch);
553 str += GetOpName(opcode);
560 printf("%s\n", ToString().c_str());
568 bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, int nHashType);
569 bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::vector<unsigned char> >& vSolutionsRet);
570 int ScriptSigArgsExpected(txnouttype t, const std::vector<std::vector<unsigned char> >& vSolutions);
571 bool IsStandard(const CScript& scriptPubKey);
572 bool IsMine(const CKeyStore& keystore, const CScript& scriptPubKey);
573 bool ExtractAddress(const CScript& scriptPubKey, CBitcoinAddress& addressRet);
574 bool ExtractAddresses(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<CBitcoinAddress>& addressRet, int& nRequiredRet);
575 bool SignSignature(const CKeyStore& keystore, const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL);
576 bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, bool fValidatePayToScriptHash, int nHashType);