5 #ifndef H_BITCOIN_SCRIPT
6 #define H_BITCOIN_SCRIPT
11 #include <boost/foreach.hpp>
12 #include <boost/variant.hpp>
20 static const unsigned int MAX_SCRIPT_ELEMENT_SIZE = 520;
216 inline std::string
ValueString(
const std::vector<unsigned char>& vch)
224 inline std::string
StackString(
const std::vector<std::vector<unsigned char> >& vStack)
227 BOOST_FOREACH(
const std::vector<unsigned char>& vch, vStack)
244 class CScript :
public std::vector<unsigned char>
249 if (n == -1 || (n >= 1 && n <= 16))
251 push_back(n + (
OP_1 - 1));
263 if (n >= 1 && n <= 16)
265 push_back(n + (
OP_1 - 1));
278 CScript(const_iterator pbegin, const_iterator pend) :
std::vector<unsigned char>(pbegin, pend) { }
280 CScript(
const unsigned char* pbegin,
const unsigned char* pend) :
std::vector<unsigned char>(pbegin, pend) { }
285 insert(end(), b.begin(), b.end());
329 if (opcode < 0 || opcode > 0xff)
330 throw std::runtime_error(
"CScript::operator<<() : invalid opcode");
331 insert(end(), (
unsigned char)opcode);
337 insert(end(),
sizeof(b));
338 insert(end(), (
unsigned char*)&b, (
unsigned char*)&b +
sizeof(b));
344 insert(end(),
sizeof(b));
345 insert(end(), (
unsigned char*)&b, (
unsigned char*)&b +
sizeof(b));
352 insert(end(), (
unsigned char)key.
size());
353 insert(end(), key.
begin(), key.
end());
363 CScript& operator<<(const std::vector<unsigned char>& b)
367 insert(end(), (
unsigned char)b.size());
369 else if (b.size() <= 0xff)
372 insert(end(), (
unsigned char)b.size());
374 else if (b.size() <= 0xffff)
377 unsigned short nSize = b.size();
378 insert(end(), (
unsigned char*)&nSize, (
unsigned char*)&nSize +
sizeof(nSize));
383 unsigned int nSize = b.size();
384 insert(end(), (
unsigned char*)&nSize, (
unsigned char*)&nSize +
sizeof(nSize));
386 insert(end(), b.begin(), b.end());
394 assert(!
"Warning: Pushing a CScript onto a CScript with << is probably not intended, use + to concatenate!");
402 const_iterator pc2 = pc;
403 bool fRet =
GetOp2(pc2, opcodeRet, &vchRet);
404 pc = begin() + (pc2 - begin());
410 const_iterator pc2 = pc;
411 bool fRet =
GetOp2(pc2, opcodeRet, NULL);
412 pc = begin() + (pc2 - begin());
416 bool GetOp(const_iterator& pc,
opcodetype& opcodeRet, std::vector<unsigned char>& vchRet)
const
418 return GetOp2(pc, opcodeRet, &vchRet);
423 return GetOp2(pc, opcodeRet, NULL);
426 bool GetOp2(const_iterator& pc,
opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet)
const
437 unsigned int opcode = *pc++;
442 unsigned int nSize = 0;
458 memcpy(&nSize, &pc[0], 2);
465 memcpy(&nSize, &pc[0], 4);
468 if (end() - pc < 0 || (
unsigned int)(end() - pc) < nSize)
471 pvchRet->assign(pc, pc + nSize);
484 assert(opcode >=
OP_1 && opcode <=
OP_16);
485 return (
int)opcode - (int)(
OP_1 - 1);
489 assert(n >= 0 && n <= 16);
500 iterator pc = begin();
504 while (end() - pc >= (
long)b.size() && memcmp(&pc[0], &b[0], b.size()) == 0)
506 erase(pc, pc + b.size());
510 while (
GetOp(pc, opcode));
517 for (const_iterator pc = begin(); pc != end() &&
GetOp(pc, opcode);)
539 const_iterator pc = begin();
543 if (!
GetOp(pc, opcode))
553 void SetMultisig(
int nRequired,
const std::vector<CPubKey>& keys);
558 printf(
"CScript(%s)\n",
HexStr(begin(), end(),
true).c_str());
565 std::vector<unsigned char> vch;
566 const_iterator pc = begin();
571 if (!
GetOp(pc, opcode, vch))
626 bool Compress(std::vector<unsigned char> &out)
const;
628 bool Decompress(
unsigned int nSize,
const std::vector<unsigned char> &out);
633 std::vector<unsigned char> compr;
637 return script.size() +
VARINT(nSize).GetSerializeSize(nType, nVersion);
640 template<
typename Stream>
641 void Serialize(Stream &s,
int nType,
int nVersion)
const {
642 std::vector<unsigned char> compr;
644 s <<
CFlatData(&compr[0], &compr[compr.size()]);
649 s <<
CFlatData(&script[0], &script[script.size()]);
652 template<
typename Stream>
654 unsigned int nSize = 0;
656 if (nSize < nSpecialScripts) {
663 script.resize(nSize);
664 s >>
REF(
CFlatData(&script[0], &script[script.size()]));
671 bool EvalScript(std::vector<std::vector<unsigned char> >& stack,
const CScript& script,
const CTransaction& txTo,
unsigned int nIn,
unsigned int flags,
int nHashType);
672 bool Solver(
const CScript& scriptPubKey,
txnouttype& typeRet, std::vector<std::vector<unsigned char> >& vSolutionsRet);
bool IsToKeyID(CKeyID &hash) const
#define strprintf(format,...)
static int DecodeOP_N(opcodetype opcode)
bool GetOp2(const_iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > *pvchRet) const
CScript & operator<<(int64 b)
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CTransaction &txTo, unsigned int nIn, unsigned int flags, int nHashType)
CScript & operator<<(unsigned short b)
CScript & operator<<(const uint160 &b)
CScript(const CScript &b)
bool Compress(std::vector< unsigned char > &out) const
bool IsPayToScriptHash() const
bool EvalScript(std::vector< std::vector< unsigned char > > &stack, const CScript &script, const CTransaction &txTo, unsigned int nIn, unsigned int flags, int nHashType)
const char * GetTxnOutputType(txnouttype t)
unsigned int GetSpecialSize(unsigned int nSize) const
CScript & push_int64(int64 n)
unsigned int size() const
bool IsToPubKey(CPubKey &pubkey) const
Compact serializer for scripts.
friend bool operator==(const CNoDestination &a, const CNoDestination &b)
unsigned long long uint64
pruned version of CTransaction: only retains metadata and unspent transaction outputs ...
uint160 Hash160(const T1 pbegin, const T1 pend)
bool IsStandard(const CScript &scriptPubKey)
friend CScript operator+(const CScript &a, const CScript &b)
int ScriptSigArgsExpected(txnouttype t, const std::vector< std::vector< unsigned char > > &vSolutions)
CScript(const std::vector< unsigned char > &b)
bool GetOp(const_iterator &pc, opcodetype &opcodeRet) const
CScript & operator<<(unsigned long b)
bool IsMine(const CKeyStore &keystore, const CScript &scriptPubKey)
std::string ToString() const
const char * GetOpName(opcodetype opcode)
void SetDestination(const CTxDestination &address)
std::string ValueString(const std::vector< unsigned char > &vch)
void Serialize(Stream &s, int nType, int nVersion) const
CScript & operator<<(opcodetype opcode)
friend bool operator<(const CNoDestination &a, const CNoDestination &b)
opcodetype
Script opcodes.
CScript & operator<<(const CScript &b)
CScript & operator<<(unsigned char b)
unsigned int GetSerializeSize(int nType, int nVersion) const
CScriptCompressor(CScript &scriptIn)
unsigned int GetSigOpCount(bool fAccurate) const
bool IsToScriptID(CScriptID &hash) const
An encapsulated public key.
CScript & operator<<(signed char b)
CScript(const unsigned char *pbegin, const unsigned char *pend)
C++ wrapper for BIGNUM (OpenSSL bignum)
std::vector< unsigned char > getvch() const
bool GetOp(iterator &pc, opcodetype &opcodeRet)
CScript & operator<<(unsigned int b)
bool Solver(const CScript &scriptPubKey, txnouttype &typeRet, std::vector< std::vector< unsigned char > > &vSolutionsRet)
CScript & operator<<(const CBigNum &b)
CScript & operator<<(short b)
const unsigned char * begin() const
bool IsCanonicalSignature(const std::vector< unsigned char > &vchSig)
CScript & operator<<(int b)
bool SignSignature(const CKeyStore &keystore, const CScript &fromPubKey, CTransaction &txTo, unsigned int nIn, int nHashType=SIGHASH_ALL)
std::string StackString(const std::vector< std::vector< unsigned char > > &vStack)
CScript & operator<<(long b)
static const unsigned int nSpecialScripts
CScript(const uint256 &b)
CScript(const_iterator pbegin, const_iterator pend)
static opcodetype EncodeOP_N(int n)
Serialized script, used inside transaction inputs and outputs.
bool Decompress(unsigned int nSize, const std::vector< unsigned char > &out)
CScript & operator+=(const CScript &b)
CScript & push_uint64(uint64 n)
A virtual base class for key stores.
void SetMultisig(int nRequired, const std::vector< CPubKey > &keys)
A reference to a CKey: the Hash160 of its serialized public key.
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
int FindAndDelete(const CScript &b)
CScript CombineSignatures(CScript scriptPubKey, const CTransaction &txTo, unsigned int nIn, const CScript &scriptSig1, const CScript &scriptSig2)
CScript(const CBigNum &b)
bool ExtractDestinations(const CScript &scriptPubKey, txnouttype &typeRet, std::vector< CTxDestination > &addressRet, int &nRequiredRet)
A reference to a CScript: the Hash160 of its serialization (see script.h)
bool GetOp(const_iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet) const
CScript & operator<<(const uint256 &b)
boost::variant< CNoDestination, CKeyID, CScriptID > CTxDestination
A txout script template with a specific destination.
bool GetOp(iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet)
The basic transaction that is broadcasted on the network and contained in blocks. ...
CScript(unsigned short b)
bool IsCanonicalPubKey(const std::vector< unsigned char > &vchPubKey)
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
CScript & operator<<(uint64 b)
CScript & operator<<(const CPubKey &key)
const unsigned char * end() const
Wrapper for serializing arrays and POD.
int Find(opcodetype op) const
void Unserialize(Stream &s, int nType, int nVersion)