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 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>
18 /** Signature hash types/flags */
24 SIGHASH_ANYONECANPAY = 0x80,
31 // 'standard' transaction types:
38 const char* GetTxnOutputType(txnouttype t);
145 OP_GREATERTHANOREQUAL,
161 OP_CHECKMULTISIGVERIFY,
177 // template matching params
178 OP_SMALLINTEGER = 0xfa,
180 OP_PUBKEYHASH = 0xfd,
183 OP_INVALIDOPCODE = 0xff,
186 const char* GetOpName(opcodetype opcode);
190 inline std::string ValueString(const std::vector<unsigned char>& vch)
193 return strprintf("%d", CBigNum(vch).getint());
198 inline std::string StackString(const std::vector<std::vector<unsigned char> >& vStack)
201 BOOST_FOREACH(const std::vector<unsigned char>& vch, vStack)
205 str += ValueString(vch);
217 /** Serialized script, used inside transaction inputs and outputs */
218 class CScript : public std::vector<unsigned char>
221 CScript& push_int64(int64 n)
223 if (n == -1 || (n >= 1 && n <= 16))
225 push_back(n + (OP_1 - 1));
230 *this << bn.getvch();
235 CScript& push_uint64(uint64 n)
237 if (n >= 1 && n <= 16)
239 push_back(n + (OP_1 - 1));
244 *this << bn.getvch();
251 CScript(const CScript& b) : std::vector<unsigned char>(b.begin(), b.end()) { }
252 CScript(const_iterator pbegin, const_iterator pend) : std::vector<unsigned char>(pbegin, pend) { }
254 CScript(const unsigned char* pbegin, const unsigned char* pend) : std::vector<unsigned char>(pbegin, pend) { }
257 CScript& operator+=(const CScript& b)
259 insert(end(), b.begin(), b.end());
263 friend CScript operator+(const CScript& a, const CScript& b)
271 //explicit CScript(char b) is not portable. Use 'signed char' or 'unsigned char'.
272 explicit CScript(signed 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) is not portable. Use 'signed char' or 'unsigned char'.
290 CScript& operator<<(signed char b) { return push_int64(b); }
291 CScript& operator<<(short b) { return push_int64(b); }
292 CScript& operator<<(int b) { return push_int64(b); }
293 CScript& operator<<(long b) { return push_int64(b); }
294 CScript& operator<<(int64 b) { return push_int64(b); }
295 CScript& operator<<(unsigned char b) { return push_uint64(b); }
296 CScript& operator<<(unsigned int b) { return push_uint64(b); }
297 CScript& operator<<(unsigned short b) { return push_uint64(b); }
298 CScript& operator<<(unsigned long b) { return push_uint64(b); }
299 CScript& operator<<(uint64 b) { return push_uint64(b); }
301 CScript& operator<<(opcodetype opcode)
303 if (opcode < 0 || opcode > 0xff)
304 throw std::runtime_error("CScript::operator<<() : invalid opcode");
305 insert(end(), (unsigned char)opcode);
309 CScript& operator<<(const uint160& b)
311 insert(end(), sizeof(b));
312 insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
316 CScript& operator<<(const uint256& b)
318 insert(end(), sizeof(b));
319 insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
323 CScript& operator<<(const CBigNum& b)
329 CScript& operator<<(const std::vector<unsigned char>& b)
331 if (b.size() < OP_PUSHDATA1)
333 insert(end(), (unsigned char)b.size());
335 else if (b.size() <= 0xff)
337 insert(end(), OP_PUSHDATA1);
338 insert(end(), (unsigned char)b.size());
340 else if (b.size() <= 0xffff)
342 insert(end(), OP_PUSHDATA2);
343 unsigned short nSize = b.size();
344 insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
348 insert(end(), OP_PUSHDATA4);
349 unsigned int nSize = b.size();
350 insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
352 insert(end(), b.begin(), b.end());
356 CScript& operator<<(const CScript& b)
358 // I'm not sure if this should push the script or concatenate scripts.
359 // If there's ever a use for pushing a script onto a script, delete this member fn
360 assert(!"warning: pushing a CScript onto a CScript with << is probably not intended, use + to concatenate");
365 bool GetOp(iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet)
367 // Wrapper so it can be called with either iterator or const_iterator
368 const_iterator pc2 = pc;
369 bool fRet = GetOp2(pc2, opcodeRet, &vchRet);
370 pc = begin() + (pc2 - begin());
374 bool GetOp(iterator& pc, opcodetype& opcodeRet)
376 const_iterator pc2 = pc;
377 bool fRet = GetOp2(pc2, opcodeRet, NULL);
378 pc = begin() + (pc2 - begin());
382 bool GetOp(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet) const
384 return GetOp2(pc, opcodeRet, &vchRet);
387 bool GetOp(const_iterator& pc, opcodetype& opcodeRet) const
389 return GetOp2(pc, opcodeRet, NULL);
392 bool GetOp2(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet) const
394 opcodeRet = OP_INVALIDOPCODE;
403 unsigned int opcode = *pc++;
406 if (opcode <= OP_PUSHDATA4)
409 if (opcode < OP_PUSHDATA1)
413 else if (opcode == OP_PUSHDATA1)
419 else if (opcode == OP_PUSHDATA2)
424 memcpy(&nSize, &pc[0], 2);
427 else if (opcode == OP_PUSHDATA4)
431 memcpy(&nSize, &pc[0], 4);
434 if (end() - pc < nSize)
437 pvchRet->assign(pc, pc + nSize);
441 opcodeRet = (opcodetype)opcode;
445 // Encode/decode small integers:
446 static int DecodeOP_N(opcodetype opcode)
450 assert(opcode >= OP_1 && opcode <= OP_16);
451 return (int)opcode - (int)(OP_1 - 1);
453 static opcodetype EncodeOP_N(int n)
455 assert(n >= 0 && n <= 16);
458 return (opcodetype)(OP_1+n-1);
461 int FindAndDelete(const CScript& b)
466 iterator pc = begin();
470 while (end() - pc >= b.size() && memcmp(&pc[0], &b[0], b.size()) == 0)
472 erase(pc, pc + b.size());
476 while (GetOp(pc, opcode));
479 int Find(opcodetype op) const
483 for (const_iterator pc = begin(); pc != end() && GetOp(pc, opcode);)
489 // Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs
490 // as 20 sigops. With pay-to-script-hash, that changed:
491 // CHECKMULTISIGs serialized in scriptSigs are
492 // counted more accurately, assuming they are of the form
493 // ... OP_N CHECKMULTISIG ...
494 int GetSigOpCount(bool fAccurate) const;
496 // Accurately count sigOps, including sigOps in
497 // pay-to-script-hash transactions:
498 int GetSigOpCount(const CScript& scriptSig) const;
500 bool IsPayToScriptHash() const;
502 // Called by CTransaction::IsStandard
503 bool IsPushOnly() const
505 const_iterator pc = begin();
509 if (!GetOp(pc, opcode))
518 void SetBitcoinAddress(const CBitcoinAddress& address);
519 void SetBitcoinAddress(const std::vector<unsigned char>& vchPubKey)
521 SetBitcoinAddress(CBitcoinAddress(vchPubKey));
523 void SetMultisig(int nRequired, const std::vector<CKey>& keys);
524 void SetPayToScriptHash(const CScript& subscript);
527 void PrintHex() const
529 printf("CScript(%s)\n", HexStr(begin(), end(), true).c_str());
532 std::string ToString() const
536 std::vector<unsigned char> vch;
537 const_iterator pc = begin();
542 if (!GetOp(pc, opcode, vch))
547 if (0 <= opcode && opcode <= OP_PUSHDATA4)
548 str += ValueString(vch);
550 str += GetOpName(opcode);
557 printf("%s\n", ToString().c_str());
565 bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, int nHashType);
566 bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::vector<unsigned char> >& vSolutionsRet);
567 int ScriptSigArgsExpected(txnouttype t, const std::vector<std::vector<unsigned char> >& vSolutions);
568 bool IsStandard(const CScript& scriptPubKey);
569 bool IsMine(const CKeyStore& keystore, const CScript& scriptPubKey);
570 bool ExtractAddress(const CScript& scriptPubKey, CBitcoinAddress& addressRet);
571 bool ExtractAddresses(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<CBitcoinAddress>& addressRet, int& nRequiredRet);
572 bool SignSignature(const CKeyStore& keystore, const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL);
573 bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, bool fValidatePayToScriptHash, int nHashType);